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.wolfsmartset.internal.discovery;
15 import static org.openhab.binding.wolfsmartset.internal.WolfSmartsetBindingConstants.*;
17 import java.util.HashMap;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.wolfsmartset.internal.dto.GetSystemListDTO;
26 import org.openhab.binding.wolfsmartset.internal.dto.SubMenuEntryWithMenuItemTabView;
27 import org.openhab.binding.wolfsmartset.internal.handler.WolfSmartsetSystemBridgeHandler;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link WolfSmartsetAccountDiscoveryService} is responsible for discovering the WolfSmartset Units
41 * that are associated with the WolfSmartset System
43 * @author Bo Biene - Initial contribution
46 public class WolfSmartsetSystemDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
48 private final Logger logger = LoggerFactory.getLogger(WolfSmartsetSystemDiscoveryService.class);
50 private @NonNullByDefault({}) WolfSmartsetSystemBridgeHandler bridgeHandler;
52 private @Nullable Future<?> discoveryJob;
54 public WolfSmartsetSystemDiscoveryService() {
55 super(SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS, 8, true);
59 public void setThingHandler(@Nullable ThingHandler handler) {
60 if (handler instanceof WolfSmartsetSystemBridgeHandler systemBridgeHandler) {
61 this.bridgeHandler = systemBridgeHandler;
66 public @Nullable ThingHandler getThingHandler() {
71 public void activate() {
76 public void deactivate() {
81 public Set<ThingTypeUID> getSupportedThingTypes() {
82 return SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS;
86 protected void startBackgroundDiscovery() {
87 logger.debug("WolfSmartsetSystemDiscovery: Starting background discovery job");
88 Future<?> localDiscoveryJob = discoveryJob;
89 if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
90 discoveryJob = scheduler.scheduleWithFixedDelay(() -> this.backgroundDiscover(),
91 DISCOVERY_INITIAL_DELAY_SECONDS, DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
96 protected void stopBackgroundDiscovery() {
97 logger.debug("WolfSmartsetSystemDiscovery: Stopping background discovery job");
98 Future<?> localDiscoveryJob = discoveryJob;
99 if (localDiscoveryJob != null) {
100 localDiscoveryJob.cancel(true);
106 public void startScan() {
107 logger.debug("WolfSmartsetSystemDiscovery: Starting discovery scan");
111 private void backgroundDiscover() {
112 var accountBridgeHandler = bridgeHandler.getAccountBridgeHandler();
113 if (accountBridgeHandler == null || !accountBridgeHandler.isBackgroundDiscoveryEnabled()) {
119 private void discover() {
120 if (bridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) {
121 logger.debug("WolfSmartsetSystemDiscovery: Skipping discovery because Account Bridge thing is not ONLINE");
124 logger.debug("WolfSmartsetSystemDiscovery: Discovering WolfSmartset devices");
128 private synchronized void discoverUnits() {
129 if (this.bridgeHandler != null) {
130 String systemId = this.bridgeHandler.getSystemId();
131 var systemConfig = this.bridgeHandler.getSystemConfig();
132 if (systemConfig != null) {
133 logger.debug("WolfSmartsetSystemDiscovery: Discovering units for system '{}' (id {})",
134 systemConfig.getName(), systemId);
135 for (var unit : this.bridgeHandler.getUnits()) {
136 ThingUID bridgeUID = this.bridgeHandler.getThing().getUID();
137 ThingUID unitUID = new ThingUID(UID_UNIT_THING, bridgeUID,
138 unit.menuItemTabViewDTO.bundleId.toString());
139 thingDiscovered(createUnitDiscoveryResult(unitUID, bridgeUID, systemConfig, unit));
141 "WolfSmartsetSystemDiscovery: Unit for '{}' with id '{}' and name '{}' added with UID '{}'",
142 systemId, unit.menuItemTabViewDTO.bundleId, unit.menuItemTabViewDTO.tabName, unitUID);
148 private DiscoveryResult createUnitDiscoveryResult(ThingUID unitUID, ThingUID bridgeUID,
149 GetSystemListDTO systemConfig, SubMenuEntryWithMenuItemTabView unit) {
150 Map<String, Object> properties = new HashMap<>();
151 properties.put(CONFIG_UNIT_ID, unit.menuItemTabViewDTO.bundleId.toString());
152 var tabName = unit.menuItemTabViewDTO.tabName;
153 var menuName = unit.subMenuEntryDTO.getName();
154 tabName = tabName.isEmpty() || "NULL".equalsIgnoreCase(tabName) || menuName.equalsIgnoreCase(tabName) ? ""
157 return DiscoveryResultBuilder.create(unitUID).withProperties(properties)
158 .withRepresentationProperty(CONFIG_UNIT_ID).withBridge(bridgeUID)
159 .withLabel(String.format("%s %s%s", systemConfig.getName(), menuName, tabName)).build();