]> git.basschouten.com Git - openhab-addons.git/blob
51cff7b9312964e5e33be3536e6f7414350e3bfd
[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.unifi.internal.handler;
14
15 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_CID;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_MAC_ADDRESS;
17 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_PORT_NUMBER;
18 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_SID;
19 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_SITE;
20 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_WID;
21 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.PARAMETER_WIFI_NAME;
22
23 import java.util.Map;
24 import java.util.concurrent.TimeUnit;
25 import java.util.regex.Pattern;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.unifi.internal.UniFiBindingConstants;
30 import org.openhab.binding.unifi.internal.api.UniFiController;
31 import org.openhab.binding.unifi.internal.api.UniFiException;
32 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
33 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
34 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTuple;
35 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
36 import org.openhab.binding.unifi.internal.api.dto.UniFiSwitchPorts;
37 import org.openhab.binding.unifi.internal.api.dto.UniFiWlan;
38 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
39 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
40 import org.openhab.core.thing.ThingUID;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.ServiceScope;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Discovery service for detecting things connected to a UniFi controller.
48  *
49  * @author Hilbrand Bouwkamp - Initial contribution
50  */
51 @Component(scope = ServiceScope.PROTOTYPE, service = UniFiThingDiscoveryService.class)
52 @NonNullByDefault
53 public class UniFiThingDiscoveryService extends AbstractThingHandlerDiscoveryService<UniFiControllerThingHandler> {
54
55     /**
56      * Timeout for discovery time.
57      */
58     private static final int UNIFI_DISCOVERY_TIMEOUT_SECONDS = 30;
59     private static final long TTL_SECONDS = TimeUnit.MINUTES.toSeconds(5);
60     private static final int THING_ID_LENGTH = 8;
61     private static final Pattern DEFAULT_PORTNAME = Pattern.compile("Port \\d+");
62
63     private final Logger logger = LoggerFactory.getLogger(UniFiThingDiscoveryService.class);
64
65     public UniFiThingDiscoveryService() {
66         super(UniFiControllerThingHandler.class, UniFiBindingConstants.THING_TYPE_SUPPORTED,
67                 UNIFI_DISCOVERY_TIMEOUT_SECONDS, false);
68     }
69
70     @Override
71     protected void startScan() {
72         removeOlderResults(getTimestampOfLastScan());
73         final UniFiController controller = thingHandler.getController();
74         if (controller == null) {
75             return;
76         }
77         try {
78             controller.refresh();
79             final UniFiControllerCache cache = controller.getCache();
80             final ThingUID bridgeUID = thingHandler.getThing().getUID();
81
82             discoverSites(cache, bridgeUID);
83             discoverWlans(cache, bridgeUID);
84             discoverClients(cache, bridgeUID);
85             discoverPoePorts(cache, bridgeUID);
86         } catch (final UniFiException e) {
87             logger.debug("Exception during discovery of UniFi Things", e);
88         }
89     }
90
91     private void discoverSites(final UniFiControllerCache cache, final ThingUID bridgeUID) {
92         for (final UniFiSite site : cache.getSites()) {
93             final ThingUID thingUID = new ThingUID(UniFiBindingConstants.THING_TYPE_SITE, bridgeUID,
94                     stripIdShort(site.getId()));
95             final Map<String, Object> properties = Map.of(PARAMETER_SID, site.getId());
96
97             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(UniFiBindingConstants.THING_TYPE_SITE)
98                     .withBridge(bridgeUID).withRepresentationProperty(PARAMETER_SID).withTTL(TTL_SECONDS)
99                     .withProperties(properties).withLabel(site.getName()).build());
100         }
101     }
102
103     private void discoverWlans(final UniFiControllerCache cache, final ThingUID bridgeUID) {
104         for (final UniFiWlan wlan : cache.getWlans()) {
105             final ThingUID thingUID = new ThingUID(UniFiBindingConstants.THING_TYPE_WLAN, bridgeUID,
106                     stripIdShort(wlan.getId()));
107             final String siteName = wlan.getSite() == null ? "" : wlan.getSite().getName();
108             final Map<String, Object> properties = Map.of(PARAMETER_WID, wlan.getId(), PARAMETER_SITE, siteName,
109                     PARAMETER_WIFI_NAME, wlan.getName());
110
111             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(UniFiBindingConstants.THING_TYPE_WLAN)
112                     .withBridge(bridgeUID).withRepresentationProperty(PARAMETER_WID).withTTL(TTL_SECONDS)
113                     .withProperties(properties).withLabel(wlan.getName()).build());
114         }
115     }
116
117     private void discoverClients(final UniFiControllerCache cache, final ThingUID bridgeUID) {
118         for (final UniFiClient uc : cache.getClients()) {
119             final var thingTypeUID = uc.isWireless() ? UniFiBindingConstants.THING_TYPE_WIRELESS_CLIENT
120                     : UniFiBindingConstants.THING_TYPE_WIRED_CLIENT;
121             final ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, stripIdShort(uc.getId()));
122             final Map<String, Object> properties = Map.of(PARAMETER_CID, uc.getMac(), PARAMETER_SITE,
123                     uc.getSite().getName());
124
125             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withBridge(bridgeUID)
126                     .withRepresentationProperty(PARAMETER_CID).withTTL(TTL_SECONDS).withProperties(properties)
127                     .withLabel(uc.getName()).build());
128         }
129     }
130
131     /**
132      * Shorten the id to make it a bit more comprehensible.
133      *
134      * @param id id to shorten.
135      * @return shortened id or if to short the original id
136      */
137     private static String stripIdShort(final String id) {
138         return id != null && id.length() > THING_ID_LENGTH ? id.substring(id.length() - THING_ID_LENGTH) : id;
139     }
140
141     private void discoverPoePorts(final UniFiControllerCache cache, final ThingUID bridgeUID) {
142         for (final UniFiSwitchPorts uc : cache.getSwitchPorts()) {
143             for (final UniFiPortTuple pt : uc.getPoePorts()) {
144                 final String deviceMac = pt.getDevice().getMac();
145                 final String id = deviceMac.replace(":", "") + "_" + pt.getPortIdx();
146                 final ThingUID thingUID = new ThingUID(UniFiBindingConstants.THING_TYPE_POE_PORT, bridgeUID, id);
147                 final Map<String, Object> properties = Map.of(PARAMETER_PORT_NUMBER, pt.getPortIdx(),
148                         PARAMETER_MAC_ADDRESS, deviceMac);
149
150                 thingDiscovered(DiscoveryResultBuilder.create(thingUID)
151                         .withThingType(UniFiBindingConstants.THING_TYPE_POE_PORT).withBridge(bridgeUID)
152                         .withTTL(TTL_SECONDS).withProperties(properties).withLabel(portName(pt)).build());
153             }
154         }
155     }
156
157     /**
158      * If the PoE port hasn't it's own name, but is named Port with a number the name is prefixed with the device name.
159      *
160      * @param pt port object
161      * @return label for the discovered PoE port
162      */
163     private @Nullable String portName(final UniFiPortTuple pt) {
164         final String portName = pt.getTable().getName();
165
166         return DEFAULT_PORTNAME.matcher(portName).find() ? pt.getDevice().getName() + " " + portName : portName;
167     }
168 }