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