]> git.basschouten.com Git - openhab-addons.git/blob
dce929fbdf53415dc400758556795c68ab8fc4dc
[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.handler.WolfSmartsetAccountBridgeHandler;
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.thing.ThingStatus;
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;
37
38 /**
39  * The {@link WolfSmartsetAccountDiscoveryService} is responsible for discovering the WolfSmartset
40  * systems that are associated with the WolfSmartset Account
41  *
42  * @author Bo Biene - Initial contribution
43  */
44 @NonNullByDefault
45 public class WolfSmartsetAccountDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
46
47     private final Logger logger = LoggerFactory.getLogger(WolfSmartsetAccountDiscoveryService.class);
48
49     private @NonNullByDefault({}) WolfSmartsetAccountBridgeHandler bridgeHandler;
50
51     private @Nullable Future<?> discoveryJob;
52
53     public WolfSmartsetAccountDiscoveryService() {
54         super(SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS, 8, true);
55     }
56
57     @Override
58     public void setThingHandler(@Nullable ThingHandler handler) {
59         if (handler instanceof WolfSmartsetAccountBridgeHandler accountBridgeHandler) {
60             this.bridgeHandler = accountBridgeHandler;
61         }
62     }
63
64     @Override
65     public @Nullable ThingHandler getThingHandler() {
66         return bridgeHandler;
67     }
68
69     @Override
70     public void activate() {
71         super.activate(null);
72     }
73
74     @Override
75     public void deactivate() {
76         super.deactivate();
77     }
78
79     @Override
80     public Set<ThingTypeUID> getSupportedThingTypes() {
81         return SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS;
82     }
83
84     @Override
85     protected void startBackgroundDiscovery() {
86         logger.debug("WolfSmartsetDiscovery: Starting background discovery job");
87
88         Future<?> localDiscoveryJob = discoveryJob;
89         if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
90             discoveryJob = scheduler.scheduleWithFixedDelay(this::backgroundDiscover, DISCOVERY_INITIAL_DELAY_SECONDS,
91                     DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
92         }
93     }
94
95     @Override
96     protected void stopBackgroundDiscovery() {
97         logger.debug("WolfSmartsetDiscovery: 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("WolfSmartsetDiscovery: Starting discovery scan");
108         discover();
109     }
110
111     private void backgroundDiscover() {
112         if (!bridgeHandler.isBackgroundDiscoveryEnabled()) {
113             return;
114         }
115         discover();
116     }
117
118     private void discover() {
119         if (bridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) {
120             logger.debug("WolfSmartsetDiscovery: Skipping discovery because Account Bridge thing is not ONLINE");
121             return;
122         }
123         logger.debug("WolfSmartsetDiscovery: Discovering WolfSmartset devices");
124         discoverSystems();
125     }
126
127     private synchronized void discoverSystems() {
128         logger.debug("WolfSmartsetDiscovery: Discovering systems");
129         var registeredSytems = bridgeHandler.getRegisteredSystems();
130         if (registeredSytems != null) {
131             for (GetSystemListDTO system : registeredSytems) {
132                 String name = system.getName();
133                 String identifier = null;
134                 if (system.getId() != null) {
135                     identifier = system.getId().toString();
136                 }
137                 if (identifier != null && name != null) {
138                     ThingUID thingUID = new ThingUID(UID_SYSTEM_BRIDGE, bridgeHandler.getThing().getUID(), identifier);
139                     thingDiscovered(createSystemDiscoveryResult(thingUID, identifier, name));
140                     logger.debug("WolfSmartsetDiscovery: System '{}' and name '{}' added with UID '{}'", identifier,
141                             name, thingUID);
142                 }
143             }
144         }
145     }
146
147     private DiscoveryResult createSystemDiscoveryResult(ThingUID systemUID, String identifier, String name) {
148         Map<String, Object> properties = new HashMap<>();
149         properties.put(CONFIG_SYSTEM_ID, identifier);
150         return DiscoveryResultBuilder.create(systemUID).withProperties(properties)
151                 .withRepresentationProperty(CONFIG_SYSTEM_ID).withBridge(bridgeHandler.getThing().getUID())
152                 .withLabel(String.format("WolfSmartset System %s", name)).build();
153     }
154 }