2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bticinosmarther.internal.discovery;
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
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;
39 * The {@code SmartherModuleDiscoveryService} queries the Smarther API gateway to discover available Chronothermostat
40 * modules inside existing plants registered under the configured Bridges.
42 * @author Fabio Possieri - Initial contribution
45 public class SmartherModuleDiscoveryService extends AbstractDiscoveryService
46 implements DiscoveryService, ThingHandlerService {
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);
51 private static final int DISCOVERY_TIME_SECONDS = 30;
53 private static final String ID_SEPARATOR = "-";
55 private final Logger logger = LoggerFactory.getLogger(SmartherModuleDiscoveryService.class);
57 private @Nullable SmartherAccountHandler bridgeHandler;
58 private @Nullable ThingUID bridgeUID;
61 * Constructs a {@code SmartherModuleDiscoveryService}.
63 public SmartherModuleDiscoveryService() {
64 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
68 public Set<ThingTypeUID> getSupportedThingTypes() {
69 return SUPPORTED_THING_TYPES_UIDS;
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);
81 public void deactivate() {
82 logger.debug("Bridge[{}] Deactivating chronothermostat discovery service", this.bridgeUID);
83 removeOlderResults(new Date().getTime());
87 public void setThingHandler(@Nullable ThingHandler handler) {
88 if (handler instanceof SmartherAccountHandler localBridgeHandler) {
89 this.bridgeHandler = localBridgeHandler;
90 this.bridgeUID = localBridgeHandler.getUID();
95 public @Nullable ThingHandler getThingHandler() {
96 return this.bridgeHandler;
100 protected void startBackgroundDiscovery() {
101 logger.debug("Bridge[{}] Performing background discovery scan for chronothermostats", this.bridgeUID);
102 discoverChronothermostats();
106 protected void startScan() {
107 logger.debug("Bridge[{}] Starting discovery scan for chronothermostats", this.bridgeUID);
108 discoverChronothermostats();
112 public synchronized void abortScan() {
117 protected synchronized void stopScan() {
119 removeOlderResults(getTimestampOfLastScan());
123 * Discovers Chronothermostat devices for the given bridge handler.
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)));
137 * Creates a Chronothermostat module Thing based on the remotely discovered location and module.
140 * the location containing the discovered module
142 * the discovered module
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());
153 ThingUID thingUID = new ThingUID(THING_TYPE_MODULE, localBridgeUID, getThingIdFromModule(module));
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);
165 * Generates the Thing identifier based on the Chronothermostat module identifier.
168 * the Chronothermostat module to use
170 * @return a string containing the generated Thing identifier
172 private String getThingIdFromModule(Module module) {
173 final String moduleId = module.getId();
174 return moduleId.substring(0, moduleId.indexOf(ID_SEPARATOR));