]> git.basschouten.com Git - openhab-addons.git/blob
d62b4a36ef871a370a4e595f70e781947e4db767
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.wolfsmartset.internal.discovery;
14
15 import static org.openhab.binding.wolfsmartset.internal.WolfSmartsetBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22
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;
38
39 /**
40  * The {@link WolfSmartsetAccountDiscoveryService} is responsible for discovering the WolfSmartset Units
41  * that are associated with the WolfSmartset System
42  *
43  * @author Bo Biene - Initial contribution
44  */
45 @NonNullByDefault
46 public class WolfSmartsetSystemDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
47
48     private final Logger logger = LoggerFactory.getLogger(WolfSmartsetSystemDiscoveryService.class);
49
50     private @NonNullByDefault({}) WolfSmartsetSystemBridgeHandler bridgeHandler;
51
52     private @Nullable Future<?> discoveryJob;
53
54     public WolfSmartsetSystemDiscoveryService() {
55         super(SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS, 8, true);
56     }
57
58     @Override
59     public void setThingHandler(@Nullable ThingHandler handler) {
60         if (handler instanceof WolfSmartsetSystemBridgeHandler systemBridgeHandler) {
61             this.bridgeHandler = systemBridgeHandler;
62         }
63     }
64
65     @Override
66     public @Nullable ThingHandler getThingHandler() {
67         return bridgeHandler;
68     }
69
70     @Override
71     public void activate() {
72         super.activate(null);
73     }
74
75     @Override
76     public void deactivate() {
77         super.deactivate();
78     }
79
80     @Override
81     public Set<ThingTypeUID> getSupportedThingTypes() {
82         return SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS;
83     }
84
85     @Override
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);
92         }
93     }
94
95     @Override
96     protected void stopBackgroundDiscovery() {
97         logger.debug("WolfSmartsetSystemDiscovery: Stopping background discovery job");
98         Future<?> localDiscoveryJob = discoveryJob;
99         if (localDiscoveryJob != null) {
100             localDiscoveryJob.cancel(true);
101             discoveryJob = null;
102         }
103     }
104
105     @Override
106     public void startScan() {
107         logger.debug("WolfSmartsetSystemDiscovery: Starting discovery scan");
108         discover();
109     }
110
111     private void backgroundDiscover() {
112         var accountBridgeHandler = bridgeHandler.getAccountBridgeHandler();
113         if (accountBridgeHandler == null || !accountBridgeHandler.isBackgroundDiscoveryEnabled()) {
114             return;
115         }
116         discover();
117     }
118
119     private void discover() {
120         if (bridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) {
121             logger.debug("WolfSmartsetSystemDiscovery: Skipping discovery because Account Bridge thing is not ONLINE");
122             return;
123         }
124         logger.debug("WolfSmartsetSystemDiscovery: Discovering WolfSmartset devices");
125         discoverUnits();
126     }
127
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));
140                     logger.debug(
141                             "WolfSmartsetSystemDiscovery: Unit for '{}' with id '{}' and name '{}' added with UID '{}'",
142                             systemId, unit.menuItemTabViewDTO.bundleId, unit.menuItemTabViewDTO.tabName, unitUID);
143                 }
144             }
145         }
146     }
147
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) ? ""
155                 : "-" + tabName;
156
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();
160     }
161 }