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.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;
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
44 @Component(scope = ServiceScope.PROTOTYPE, service = SmartherModuleDiscoveryService.class)
46 public class SmartherModuleDiscoveryService extends AbstractThingHandlerDiscoveryService<SmartherAccountHandler> {
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 ThingUID bridgeUID;
60 * Constructs a {@code SmartherModuleDiscoveryService}.
62 public SmartherModuleDiscoveryService() {
63 super(SmartherAccountHandler.class, SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
67 public Set<ThingTypeUID> getSupportedThingTypes() {
68 return SUPPORTED_THING_TYPES_UIDS;
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);
79 public void initialize() {
80 logger.debug("Bridge[{}] Activating chronothermostat discovery service", this.bridgeUID);
81 this.bridgeUID = thingHandler.getThing().getUID();
86 public void dispose() {
88 logger.debug("Bridge[{}] Deactivating chronothermostat discovery service", this.bridgeUID);
89 removeOlderResults(new Date().getTime());
93 protected void startBackgroundDiscovery() {
94 logger.debug("Bridge[{}] Performing background discovery scan for chronothermostats", this.bridgeUID);
95 discoverChronothermostats();
99 protected void startScan() {
100 logger.debug("Bridge[{}] Starting discovery scan for chronothermostats", this.bridgeUID);
101 discoverChronothermostats();
105 public synchronized void abortScan() {
110 protected synchronized void stopScan() {
112 removeOlderResults(getTimestampOfLastScan());
116 * Discovers Chronothermostat devices for the given bridge handler.
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)));
127 * Creates a Chronothermostat module Thing based on the remotely discovered location and module.
130 * the location containing the discovered module
132 * the discovered module
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());
143 ThingUID thingUID = new ThingUID(THING_TYPE_MODULE, localBridgeUID, getThingIdFromModule(module));
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);
155 * Generates the Thing identifier based on the Chronothermostat module identifier.
158 * the Chronothermostat module to use
160 * @return a string containing the generated Thing identifier
162 private String getThingIdFromModule(Module module) {
163 final String moduleId = module.getId();
164 return moduleId.substring(0, moduleId.indexOf(ID_SEPARATOR));