]> git.basschouten.com Git - openhab-addons.git/blob
e56fd652b824562459c376123627a612e2836cf6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.asuswrt.internal;
14
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.*;
16 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingSettings.*;
17 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.*;
18
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.asuswrt.internal.structures.AsuswrtClientInfo;
25 import org.openhab.binding.asuswrt.internal.structures.AsuswrtClientList;
26 import org.openhab.binding.asuswrt.internal.structures.AsuswrtInterfaceList;
27 import org.openhab.binding.asuswrt.internal.structures.AsuswrtIpInfo;
28 import org.openhab.binding.asuswrt.internal.things.AsuswrtRouter;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingUID;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.ServiceScope;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link AsuswrtDiscoveryService} is responsible for discovering clients.
42  *
43  * @author Christian Wild - Initial contribution
44  */
45 @Component(scope = ServiceScope.PROTOTYPE, service = AbstractDiscoveryService.class)
46 @NonNullByDefault
47 public class AsuswrtDiscoveryService extends AbstractThingHandlerDiscoveryService<AsuswrtRouter> {
48     private final Logger logger = LoggerFactory.getLogger(AsuswrtDiscoveryService.class);
49     private String uid = "";
50
51     public AsuswrtDiscoveryService() {
52         super(AsuswrtRouter.class, SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_S, false);
53     }
54
55     @Override
56     public void dispose() {
57         super.dispose();
58         removeAllResults();
59     }
60
61     @Override
62     public void initialize() {
63         thingHandler.setDiscoveryService(this);
64         uid = thingHandler.getThing().getUID().getAsString();
65         super.initialize();
66     }
67
68     /*
69      * Scan handling
70      */
71
72     /**
73      * Starts a manual scan.
74      */
75     @Override
76     public void startScan() {
77         logger.trace("{} starting scan", uid);
78         /* query Data */
79         thingHandler.queryDeviceData(false);
80         /* discover interfaces */
81         AsuswrtInterfaceList ifList = thingHandler.getInterfaces();
82         handleInterfaceScan(ifList);
83         /* discover clients */
84         AsuswrtClientList clientList = thingHandler.getClients();
85         handleClientScan(clientList);
86     }
87
88     @Override
89     public void stopScan() {
90         super.stopScan();
91         removeOlderResults(getTimestampOfLastScan());
92     }
93
94     /**
95      * Removes all scan results.
96      */
97     private void removeAllResults() {
98         removeOlderResults(new Date().getTime());
99     }
100
101     /**
102      * Creates {@link DiscoveryResult}s from the provided {@link AsuswrtInterfaceList}.
103      */
104     private void handleInterfaceScan(AsuswrtInterfaceList ifList) {
105         try {
106             for (AsuswrtIpInfo ifInfo : ifList) {
107                 DiscoveryResult discoveryResult = createInterfaceResult(ifInfo);
108                 thingDiscovered(discoveryResult);
109             }
110         } catch (Exception e) {
111             logger.debug("Error while handling interface scan reults", e);
112         }
113     }
114
115     /**
116      * Creates {@link DiscoveryResult}s from the provided {@link AsuswrtClientList}.
117      */
118     public void handleClientScan(AsuswrtClientList clientList) {
119         try {
120             for (AsuswrtClientInfo client : clientList) {
121                 DiscoveryResult discoveryResult = createClientResult(client);
122                 thingDiscovered(discoveryResult);
123             }
124         } catch (Exception e) {
125             logger.debug("Error while handling client scan results", e);
126         }
127     }
128
129     /*
130      * Discovery result creation
131      */
132
133     /**
134      * Creates a {@link DiscoveryResult} from the provided {@link AsuswrtIpInfo}.
135      */
136     private DiscoveryResult createInterfaceResult(AsuswrtIpInfo interfaceInfo) {
137         String ifName = interfaceInfo.getName();
138         String macAddress = interfaceInfo.getMAC();
139         String label = "AwrtInterface_" + ifName;
140
141         Map<String, Object> properties = new HashMap<>();
142         properties.put(NETWORK_REPRESENTATION_PROPERTY, ifName);
143         properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
144
145         logger.debug("{} thing discovered: '{}", uid, label);
146
147         ThingUID bridgeUID = thingHandler.getUID();
148         ThingUID thingUID = new ThingUID(THING_TYPE_INTERFACE, bridgeUID, ifName);
149         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
150                 .withRepresentationProperty(NETWORK_REPRESENTATION_PROPERTY).withBridge(bridgeUID).withLabel(label)
151                 .build();
152     }
153
154     /**
155      * Creates a {@link DiscoveryResult} from the provided {@link AsuswrtClientInfo}.
156      */
157     private DiscoveryResult createClientResult(AsuswrtClientInfo clientInfo) {
158         String macAddress = clientInfo.getMac();
159         String unformatedMac = unformatMac(macAddress);
160         String clientName;
161         String nickName;
162         String label = "AwrtClient_";
163
164         // Create label and thing names
165         clientName = stringOrDefault(clientInfo.getName(), "client_" + unformatedMac);
166         nickName = stringOrDefault(clientInfo.getNickName(), clientName);
167         if (nickName.equals(clientName)) {
168             label += nickName;
169         } else {
170             label += nickName + " (" + clientName + ")";
171         }
172
173         // Create properties
174         Map<String, Object> properties = new HashMap<>();
175         properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
176         properties.put(Thing.PROPERTY_VENDOR, clientInfo.getVendor());
177         properties.put(PROPERTY_CLIENT_NAME, clientName);
178         properties.put(CHANNEL_CLIENT_NICKNAME, nickName);
179
180         logger.debug("{} thing discovered: '{}", uid, label);
181         ThingUID bridgeUID = thingHandler.getUID();
182         ThingUID thingUID = new ThingUID(THING_TYPE_CLIENT, bridgeUID, unformatedMac);
183         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
184                 .withRepresentationProperty(CLIENT_REPRESENTATION_PROPERTY).withTTL(DISCOVERY_AUTOREMOVE_S)
185                 .withBridge(bridgeUID).withLabel(label).build();
186     }
187 }