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.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;
41 * The {@link AsuswrtDiscoveryService} is responsible for discovering clients.
43 * @author Christian Wild - Initial contribution
45 @Component(scope = ServiceScope.PROTOTYPE, service = AbstractDiscoveryService.class)
47 public class AsuswrtDiscoveryService extends AbstractThingHandlerDiscoveryService<AsuswrtRouter> {
48 private final Logger logger = LoggerFactory.getLogger(AsuswrtDiscoveryService.class);
49 private String uid = "";
51 public AsuswrtDiscoveryService() {
52 super(AsuswrtRouter.class, SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_S, false);
56 public void dispose() {
62 public void initialize() {
63 thingHandler.setDiscoveryService(this);
64 uid = thingHandler.getThing().getUID().getAsString();
73 * Starts a manual scan.
76 public void startScan() {
77 logger.trace("{} starting scan", uid);
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);
89 public void stopScan() {
91 removeOlderResults(getTimestampOfLastScan());
95 * Removes all scan results.
97 private void removeAllResults() {
98 removeOlderResults(new Date().getTime());
102 * Creates {@link DiscoveryResult}s from the provided {@link AsuswrtInterfaceList}.
104 private void handleInterfaceScan(AsuswrtInterfaceList ifList) {
106 for (AsuswrtIpInfo ifInfo : ifList) {
107 DiscoveryResult discoveryResult = createInterfaceResult(ifInfo);
108 thingDiscovered(discoveryResult);
110 } catch (Exception e) {
111 logger.debug("Error while handling interface scan reults", e);
116 * Creates {@link DiscoveryResult}s from the provided {@link AsuswrtClientList}.
118 public void handleClientScan(AsuswrtClientList clientList) {
120 for (AsuswrtClientInfo client : clientList) {
121 DiscoveryResult discoveryResult = createClientResult(client);
122 thingDiscovered(discoveryResult);
124 } catch (Exception e) {
125 logger.debug("Error while handling client scan results", e);
130 * Discovery result creation
134 * Creates a {@link DiscoveryResult} from the provided {@link AsuswrtIpInfo}.
136 private DiscoveryResult createInterfaceResult(AsuswrtIpInfo interfaceInfo) {
137 String ifName = interfaceInfo.getName();
138 String macAddress = interfaceInfo.getMAC();
139 String label = "AwrtInterface_" + ifName;
141 Map<String, Object> properties = new HashMap<>();
142 properties.put(NETWORK_REPRESENTATION_PROPERTY, ifName);
143 properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
145 logger.debug("{} thing discovered: '{}", uid, label);
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)
155 * Creates a {@link DiscoveryResult} from the provided {@link AsuswrtClientInfo}.
157 private DiscoveryResult createClientResult(AsuswrtClientInfo clientInfo) {
158 String macAddress = clientInfo.getMac();
159 String unformatedMac = unformatMac(macAddress);
162 String label = "AwrtClient_";
164 // Create label and thing names
165 clientName = stringOrDefault(clientInfo.getName(), "client_" + unformatedMac);
166 nickName = stringOrDefault(clientInfo.getNickName(), clientName);
167 if (nickName.equals(clientName)) {
170 label += nickName + " (" + clientName + ")";
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);
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();