2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.asuswrt.internal;
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.*;
19 import java.util.Date;
20 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.asuswrt.internal.structures.AsuswrtClientInfo;
26 import org.openhab.binding.asuswrt.internal.structures.AsuswrtClientList;
27 import org.openhab.binding.asuswrt.internal.structures.AsuswrtInterfaceList;
28 import org.openhab.binding.asuswrt.internal.structures.AsuswrtIpInfo;
29 import org.openhab.binding.asuswrt.internal.things.AsuswrtRouter;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
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.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link AsuswrtDiscoveryService} is responsible for discovering clients.
43 * @author Christian Wild - Initial contribution
46 public class AsuswrtDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
47 private final Logger logger = LoggerFactory.getLogger(AsuswrtDiscoveryService.class);
48 private String uid = "";
49 protected @NonNullByDefault({}) AsuswrtRouter router;
51 public AsuswrtDiscoveryService() {
52 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_S, false);
56 public void activate() {
60 public void deactivate() {
66 public void setThingHandler(@Nullable ThingHandler handler) {
67 if (handler instanceof AsuswrtRouter router) {
68 router.setDiscoveryService(this);
70 this.uid = router.getUID().getAsString();
75 public @Nullable ThingHandler getThingHandler() {
84 * Starts a manual scan.
87 public void startScan() {
88 logger.trace("{} starting scan", uid);
91 router.queryDeviceData(false);
92 /* discover interfaces */
93 AsuswrtInterfaceList ifList = router.getInterfaces();
94 handleInterfaceScan(ifList);
95 /* discover clients */
96 AsuswrtClientList clientList = router.getClients();
97 handleClientScan(clientList);
102 public void stopScan() {
104 removeOlderResults(getTimestampOfLastScan());
108 * Removes all scan results.
110 private void removeAllResults() {
111 removeOlderResults(new Date().getTime());
115 * Creates {@link DiscoveryResult}s from the provided {@link AsuswrtInterfaceList}.
117 private void handleInterfaceScan(AsuswrtInterfaceList ifList) {
119 for (AsuswrtIpInfo ifInfo : ifList) {
120 DiscoveryResult discoveryResult = createInterfaceResult(ifInfo);
121 thingDiscovered(discoveryResult);
123 } catch (Exception e) {
124 logger.debug("Error while handling interface scan reults", e);
129 * Creates {@link DiscoveryResult}s from the provided {@link AsuswrtClientList}.
131 public void handleClientScan(AsuswrtClientList clientList) {
133 for (AsuswrtClientInfo client : clientList) {
134 DiscoveryResult discoveryResult = createClientResult(client);
135 thingDiscovered(discoveryResult);
137 } catch (Exception e) {
138 logger.debug("Error while handling client scan results", e);
143 * Discovery result creation
147 * Creates a {@link DiscoveryResult} from the provided {@link AsuswrtIpInfo}.
149 private DiscoveryResult createInterfaceResult(AsuswrtIpInfo interfaceInfo) {
150 String ifName = interfaceInfo.getName();
151 String macAddress = interfaceInfo.getMAC();
152 String label = "AwrtInterface_" + ifName;
154 Map<String, Object> properties = new HashMap<>();
155 properties.put(NETWORK_REPRESENTATION_PROPERTY, ifName);
156 properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
158 logger.debug("{} thing discovered: '{}", uid, label);
159 if (this.router != null) {
160 ThingUID bridgeUID = router.getUID();
161 ThingUID thingUID = new ThingUID(THING_TYPE_INTERFACE, bridgeUID, ifName);
162 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
163 .withRepresentationProperty(NETWORK_REPRESENTATION_PROPERTY).withBridge(bridgeUID).withLabel(label)
166 ThingUID thingUID = new ThingUID(BINDING_ID, ifName);
167 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
168 .withRepresentationProperty(NETWORK_REPRESENTATION_PROPERTY).withLabel(label).build();
173 * Creates a {@link DiscoveryResult} from the provided {@link AsuswrtClientInfo}.
175 private DiscoveryResult createClientResult(AsuswrtClientInfo clientInfo) {
176 String macAddress = clientInfo.getMac();
177 String unformatedMac = unformatMac(macAddress);
180 String label = "AwrtClient_";
182 // Create label and thing names
183 clientName = stringOrDefault(clientInfo.getName(), "client_" + unformatedMac);
184 nickName = stringOrDefault(clientInfo.getNickName(), clientName);
185 if (nickName.equals(clientName)) {
188 label += nickName + " (" + clientName + ")";
192 Map<String, Object> properties = new HashMap<>();
193 properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
194 properties.put(Thing.PROPERTY_VENDOR, clientInfo.getVendor());
195 properties.put(PROPERTY_CLIENT_NAME, clientName);
196 properties.put(CHANNEL_CLIENT_NICKNAME, nickName);
198 logger.debug("{} thing discovered: '{}", uid, label);
199 if (this.router != null) {
200 ThingUID bridgeUID = router.getUID();
201 ThingUID thingUID = new ThingUID(THING_TYPE_CLIENT, bridgeUID, unformatedMac);
202 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
203 .withRepresentationProperty(CLIENT_REPRESENTATION_PROPERTY).withTTL(DISCOVERY_AUTOREMOVE_S)
204 .withBridge(bridgeUID).withLabel(label).build();
206 ThingUID thingUID = new ThingUID(BINDING_ID, unformatedMac);
207 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
208 .withRepresentationProperty(CLIENT_REPRESENTATION_PROPERTY).withTTL(DISCOVERY_AUTOREMOVE_S)
209 .withLabel(label).build();