]> git.basschouten.com Git - openhab-addons.git/blob
bb58bc316eb3b33cd82fa71d6d17c0779e28ee3b
[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.bticinosmarther.internal.discovery;
14
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.*;
16
17 import java.util.Date;
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.bticinosmarther.internal.account.SmartherAccountHandler;
25 import org.openhab.binding.bticinosmarther.internal.api.dto.Location;
26 import org.openhab.binding.bticinosmarther.internal.api.dto.Module;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@code SmartherModuleDiscoveryService} queries the Smarther API gateway to discover available Chronothermostat
40  * modules inside existing plants registered under the configured Bridges.
41  *
42  * @author Fabio Possieri - Initial contribution
43  */
44 @NonNullByDefault
45 public class SmartherModuleDiscoveryService extends AbstractDiscoveryService
46         implements DiscoveryService, ThingHandlerService {
47
48     // Only modules can be discovered. A bridge must be manually added.
49     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_MODULE);
50
51     private static final int DISCOVERY_TIME_SECONDS = 30;
52
53     private static final String ID_SEPARATOR = "-";
54
55     private final Logger logger = LoggerFactory.getLogger(SmartherModuleDiscoveryService.class);
56
57     private @Nullable SmartherAccountHandler bridgeHandler;
58     private @Nullable ThingUID bridgeUID;
59
60     /**
61      * Constructs a {@code SmartherModuleDiscoveryService}.
62      */
63     public SmartherModuleDiscoveryService() {
64         super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
65     }
66
67     @Override
68     public Set<ThingTypeUID> getSupportedThingTypes() {
69         return SUPPORTED_THING_TYPES_UIDS;
70     }
71
72     @Override
73     public void activate() {
74         logger.debug("Bridge[{}] Activating chronothermostat discovery service", this.bridgeUID);
75         Map<String, Object> properties = new HashMap<>();
76         properties.put(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, Boolean.TRUE);
77         super.activate(properties);
78     }
79
80     @Override
81     public void deactivate() {
82         logger.debug("Bridge[{}] Deactivating chronothermostat discovery service", this.bridgeUID);
83         removeOlderResults(new Date().getTime());
84     }
85
86     @Override
87     public void setThingHandler(@Nullable ThingHandler handler) {
88         if (handler instanceof SmartherAccountHandler localBridgeHandler) {
89             this.bridgeHandler = localBridgeHandler;
90             this.bridgeUID = localBridgeHandler.getUID();
91         }
92     }
93
94     @Override
95     public @Nullable ThingHandler getThingHandler() {
96         return this.bridgeHandler;
97     }
98
99     @Override
100     protected void startBackgroundDiscovery() {
101         logger.debug("Bridge[{}] Performing background discovery scan for chronothermostats", this.bridgeUID);
102         discoverChronothermostats();
103     }
104
105     @Override
106     protected void startScan() {
107         logger.debug("Bridge[{}] Starting discovery scan for chronothermostats", this.bridgeUID);
108         discoverChronothermostats();
109     }
110
111     @Override
112     public synchronized void abortScan() {
113         super.abortScan();
114     }
115
116     @Override
117     protected synchronized void stopScan() {
118         super.stopScan();
119         removeOlderResults(getTimestampOfLastScan());
120     }
121
122     /**
123      * Discovers Chronothermostat devices for the given bridge handler.
124      */
125     private synchronized void discoverChronothermostats() {
126         final SmartherAccountHandler localBridgeHandler = this.bridgeHandler;
127         if (localBridgeHandler != null) {
128             // If the bridge is not online no other thing devices can be found, so no reason to scan at this moment
129             if (localBridgeHandler.isOnline()) {
130                 localBridgeHandler.getLocations()
131                         .forEach(l -> localBridgeHandler.getLocationModules(l).forEach(m -> addDiscoveredDevice(l, m)));
132             }
133         }
134     }
135
136     /**
137      * Creates a Chronothermostat module Thing based on the remotely discovered location and module.
138      *
139      * @param location
140      *            the location containing the discovered module
141      * @param module
142      *            the discovered module
143      */
144     private void addDiscoveredDevice(Location location, Module module) {
145         ThingUID localBridgeUID = this.bridgeUID;
146         if (localBridgeUID != null) {
147             Map<String, Object> properties = new HashMap<>();
148             properties.put(PROPERTY_PLANT_ID, location.getPlantId());
149             properties.put(PROPERTY_MODULE_ID, module.getId());
150             properties.put(PROPERTY_MODULE_NAME, module.getName());
151             properties.put(PROPERTY_DEVICE_TYPE, module.getDeviceType());
152
153             ThingUID thingUID = new ThingUID(THING_TYPE_MODULE, localBridgeUID, getThingIdFromModule(module));
154
155             final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(localBridgeUID)
156                     .withProperties(properties).withRepresentationProperty(PROPERTY_MODULE_ID)
157                     .withLabel(module.getName()).build();
158             thingDiscovered(discoveryResult);
159             logger.debug("Bridge[{}] Chronothermostat with id '{}' and name '{}' added to Inbox with UID '{}'",
160                     localBridgeUID, module.getId(), module.getName(), thingUID);
161         }
162     }
163
164     /**
165      * Generates the Thing identifier based on the Chronothermostat module identifier.
166      *
167      * @param module
168      *            the Chronothermostat module to use
169      *
170      * @return a string containing the generated Thing identifier
171      */
172     private String getThingIdFromModule(Module module) {
173         final String moduleId = module.getId();
174         return moduleId.substring(0, moduleId.indexOf(ID_SEPARATOR));
175     }
176 }