]> git.basschouten.com Git - openhab-addons.git/blob
ec0e0c92ef85817a2e88f1f16c9abedf9231bd63
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.vesync.internal.discovery;
14
15 import static org.openhab.binding.vesync.internal.VeSyncConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.vesync.internal.handlers.VeSyncBaseDeviceHandler;
25 import org.openhab.binding.vesync.internal.handlers.VeSyncBridgeHandler;
26 import org.openhab.binding.vesync.internal.handlers.VeSyncDeviceAirHumidifierHandler;
27 import org.openhab.binding.vesync.internal.handlers.VeSyncDeviceAirPurifierHandler;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.osgi.service.component.annotations.Component;
37
38 /**
39  * The {@link VeSyncDiscoveryService} is an implementation of a discovery service for VeSync devices. The meta-data is
40  * read by the bridge, and the discovery data updated via a callback implemented by the DeviceMetaDataUpdatedHandler.
41  *
42  * @author David Godyear - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(service = DiscoveryService.class, immediate = true, configurationPid = "discovery.vesync")
46 public class VeSyncDiscoveryService extends AbstractDiscoveryService
47         implements DiscoveryService, ThingHandlerService, DeviceMetaDataUpdatedHandler {
48
49     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
50
51     private static final int DISCOVER_TIMEOUT_SECONDS = 5;
52
53     private @NonNullByDefault({}) VeSyncBridgeHandler bridgeHandler;
54     private @NonNullByDefault({}) ThingUID bridgeUID;
55
56     /**
57      * Creates a VeSyncDiscoveryService with enabled autostart.
58      */
59     public VeSyncDiscoveryService() {
60         super(SUPPORTED_THING_TYPES, DISCOVER_TIMEOUT_SECONDS);
61     }
62
63     @Override
64     public Set<ThingTypeUID> getSupportedThingTypes() {
65         return SUPPORTED_THING_TYPES;
66     }
67
68     @Override
69     public void activate() {
70         final Map<String, Object> properties = new HashMap<>();
71         properties.put(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, Boolean.TRUE);
72         super.activate(properties);
73     }
74
75     @Override
76     public void deactivate() {
77         super.deactivate();
78     }
79
80     @Override
81     public void setThingHandler(@Nullable ThingHandler handler) {
82         if (handler instanceof VeSyncBridgeHandler) {
83             bridgeHandler = (VeSyncBridgeHandler) handler;
84             bridgeUID = bridgeHandler.getUID();
85         }
86     }
87
88     @Override
89     public @Nullable ThingHandler getThingHandler() {
90         return bridgeHandler;
91     }
92
93     @Override
94     protected void startBackgroundDiscovery() {
95         if (bridgeHandler != null) {
96             bridgeHandler.registerMetaDataUpdatedHandler(this);
97         }
98     }
99
100     @Override
101     protected void stopBackgroundDiscovery() {
102         if (bridgeHandler != null) {
103             bridgeHandler.unregisterMetaDataUpdatedHandler(this);
104         }
105     }
106
107     @Override
108     protected void startScan() {
109         // If the bridge is not online no other thing devices can be found, so no reason to scan at this moment.
110         removeOlderResults(getTimestampOfLastScan());
111         if (ThingStatus.ONLINE.equals(bridgeHandler.getThing().getStatus())) {
112             bridgeHandler.runDeviceScanSequenceNoAuthErrors();
113         }
114     }
115
116     @Override
117     public void handleMetadataRetrieved(VeSyncBridgeHandler handler) {
118         bridgeHandler.getAirPurifiersMetadata().map(apMeta -> {
119             final Map<String, Object> properties = new HashMap<>(6);
120             final String deviceUUID = apMeta.getUuid();
121             properties.put(DEVICE_PROP_DEVICE_NAME, apMeta.getDeviceName());
122             properties.put(DEVICE_PROP_DEVICE_TYPE, apMeta.getDeviceType());
123             properties.put(DEVICE_PROP_DEVICE_FAMILY,
124                     VeSyncBaseDeviceHandler.getDeviceFamilyMetadata(apMeta.getDeviceType(),
125                             VeSyncDeviceAirHumidifierHandler.DEV_TYPE_FAMILY_AIR_HUMIDIFIER,
126                             VeSyncDeviceAirHumidifierHandler.SUPPORTED_MODEL_FAMILIES));
127             properties.put(DEVICE_PROP_DEVICE_MAC_ID, apMeta.getMacId());
128             properties.put(DEVICE_PROP_DEVICE_UUID, deviceUUID);
129             properties.put(DEVICE_PROP_CONFIG_DEVICE_MAC, apMeta.getMacId());
130             properties.put(DEVICE_PROP_CONFIG_DEVICE_NAME, apMeta.getDeviceName());
131             return DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_AIR_PURIFIER, bridgeUID, deviceUUID))
132                     .withLabel(apMeta.getDeviceName()).withBridge(bridgeUID).withProperties(properties)
133                     .withRepresentationProperty(DEVICE_PROP_DEVICE_MAC_ID).build();
134         }).forEach(this::thingDiscovered);
135
136         bridgeHandler.getAirHumidifiersMetadata().map(apMeta -> {
137             final Map<String, Object> properties = new HashMap<>(6);
138             final String deviceUUID = apMeta.getUuid();
139             properties.put(DEVICE_PROP_DEVICE_NAME, apMeta.getDeviceName());
140             properties.put(DEVICE_PROP_DEVICE_TYPE, apMeta.getDeviceType());
141             properties.put(DEVICE_PROP_DEVICE_FAMILY,
142                     VeSyncBaseDeviceHandler.getDeviceFamilyMetadata(apMeta.getDeviceType(),
143                             VeSyncDeviceAirPurifierHandler.DEV_TYPE_FAMILY_AIR_PURIFIER,
144                             VeSyncDeviceAirPurifierHandler.SUPPORTED_MODEL_FAMILIES));
145             properties.put(DEVICE_PROP_DEVICE_MAC_ID, apMeta.getMacId());
146             properties.put(DEVICE_PROP_DEVICE_UUID, deviceUUID);
147             properties.put(DEVICE_PROP_CONFIG_DEVICE_MAC, apMeta.getMacId());
148             properties.put(DEVICE_PROP_CONFIG_DEVICE_NAME, apMeta.getDeviceName());
149             return DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_AIR_HUMIDIFIER, bridgeUID, deviceUUID))
150                     .withLabel(apMeta.getDeviceName()).withBridge(bridgeUID).withProperties(properties)
151                     .withRepresentationProperty(DEVICE_PROP_DEVICE_MAC_ID).build();
152         }).forEach(this::thingDiscovered);
153     }
154 }