]> git.basschouten.com Git - openhab-addons.git/blob
6cff33630d98dc2985eaa380173d907156d3e197
[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.AbstractThingHandlerDiscoveryService;
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.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.ServiceScope;
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 @Component(scope = ServiceScope.PROTOTYPE, service = SmartherModuleDiscoveryService.class)
45 @NonNullByDefault
46 public class SmartherModuleDiscoveryService extends AbstractThingHandlerDiscoveryService<SmartherAccountHandler> {
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 ThingUID bridgeUID;
58
59     /**
60      * Constructs a {@code SmartherModuleDiscoveryService}.
61      */
62     public SmartherModuleDiscoveryService() {
63         super(SmartherAccountHandler.class, SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
64     }
65
66     @Override
67     public Set<ThingTypeUID> getSupportedThingTypes() {
68         return SUPPORTED_THING_TYPES_UIDS;
69     }
70
71     @Override
72     public void activate() {
73         Map<String, Object> properties = new HashMap<>();
74         properties.put(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, Boolean.TRUE);
75         super.activate(properties);
76     }
77
78     @Override
79     public void initialize() {
80         logger.debug("Bridge[{}] Activating chronothermostat discovery service", this.bridgeUID);
81         this.bridgeUID = thingHandler.getThing().getUID();
82         super.initialize();
83     }
84
85     @Override
86     public void dispose() {
87         super.dispose();
88         logger.debug("Bridge[{}] Deactivating chronothermostat discovery service", this.bridgeUID);
89         removeOlderResults(new Date().getTime());
90     }
91
92     @Override
93     protected void startBackgroundDiscovery() {
94         logger.debug("Bridge[{}] Performing background discovery scan for chronothermostats", this.bridgeUID);
95         discoverChronothermostats();
96     }
97
98     @Override
99     protected void startScan() {
100         logger.debug("Bridge[{}] Starting discovery scan for chronothermostats", this.bridgeUID);
101         discoverChronothermostats();
102     }
103
104     @Override
105     public synchronized void abortScan() {
106         super.abortScan();
107     }
108
109     @Override
110     protected synchronized void stopScan() {
111         super.stopScan();
112         removeOlderResults(getTimestampOfLastScan());
113     }
114
115     /**
116      * Discovers Chronothermostat devices for the given bridge handler.
117      */
118     private synchronized void discoverChronothermostats() {
119         // If the bridge is not online no other thing devices can be found, so no reason to scan at this moment
120         if (thingHandler.isOnline()) {
121             thingHandler.getLocations()
122                     .forEach(l -> thingHandler.getLocationModules(l).forEach(m -> addDiscoveredDevice(l, m)));
123         }
124     }
125
126     /**
127      * Creates a Chronothermostat module Thing based on the remotely discovered location and module.
128      *
129      * @param location
130      *            the location containing the discovered module
131      * @param module
132      *            the discovered module
133      */
134     private void addDiscoveredDevice(Location location, Module module) {
135         ThingUID localBridgeUID = this.bridgeUID;
136         if (localBridgeUID != null) {
137             Map<String, Object> properties = new HashMap<>();
138             properties.put(PROPERTY_PLANT_ID, location.getPlantId());
139             properties.put(PROPERTY_MODULE_ID, module.getId());
140             properties.put(PROPERTY_MODULE_NAME, module.getName());
141             properties.put(PROPERTY_DEVICE_TYPE, module.getDeviceType());
142
143             ThingUID thingUID = new ThingUID(THING_TYPE_MODULE, localBridgeUID, getThingIdFromModule(module));
144
145             final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(localBridgeUID)
146                     .withProperties(properties).withRepresentationProperty(PROPERTY_MODULE_ID)
147                     .withLabel(module.getName()).build();
148             thingDiscovered(discoveryResult);
149             logger.debug("Bridge[{}] Chronothermostat with id '{}' and name '{}' added to Inbox with UID '{}'",
150                     localBridgeUID, module.getId(), module.getName(), thingUID);
151         }
152     }
153
154     /**
155      * Generates the Thing identifier based on the Chronothermostat module identifier.
156      *
157      * @param module
158      *            the Chronothermostat module to use
159      *
160      * @return a string containing the generated Thing identifier
161      */
162     private String getThingIdFromModule(Module module) {
163         final String moduleId = module.getId();
164         return moduleId.substring(0, moduleId.indexOf(ID_SEPARATOR));
165     }
166 }