2 * Copyright (c) 2010-2023 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.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;
39 * The {@link WolfSmartsetAccountDiscoveryService} is responsible for discovering the WolfSmartset
40 * systems that are associated with the WolfSmartset Account
42 * @author Bo Biene - Initial contribution
45 public class WolfSmartsetAccountDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
47 private final Logger logger = LoggerFactory.getLogger(WolfSmartsetAccountDiscoveryService.class);
49 private @NonNullByDefault({}) WolfSmartsetAccountBridgeHandler bridgeHandler;
51 private @Nullable Future<?> discoveryJob;
53 public WolfSmartsetAccountDiscoveryService() {
54 super(SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS, 8, true);
58 public void setThingHandler(@Nullable ThingHandler handler) {
59 if (handler instanceof WolfSmartsetAccountBridgeHandler) {
60 this.bridgeHandler = (WolfSmartsetAccountBridgeHandler) handler;
65 public @Nullable ThingHandler getThingHandler() {
70 public void activate() {
75 public void deactivate() {
80 public Set<ThingTypeUID> getSupportedThingTypes() {
81 return SUPPORTED_SYSTEM_AND_UNIT_THING_TYPES_UIDS;
85 protected void startBackgroundDiscovery() {
86 logger.debug("WolfSmartsetDiscovery: Starting background discovery job");
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);
96 protected void stopBackgroundDiscovery() {
97 logger.debug("WolfSmartsetDiscovery: Stopping background discovery job");
98 Future<?> localDiscoveryJob = discoveryJob;
99 if (localDiscoveryJob != null) {
100 localDiscoveryJob.cancel(true);
106 public void startScan() {
107 logger.debug("WolfSmartsetDiscovery: Starting discovery scan");
111 private void backgroundDiscover() {
112 if (!bridgeHandler.isBackgroundDiscoveryEnabled()) {
118 private void discover() {
119 if (bridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) {
120 logger.debug("WolfSmartsetDiscovery: Skipping discovery because Account Bridge thing is not ONLINE");
123 logger.debug("WolfSmartsetDiscovery: Discovering WolfSmartset devices");
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();
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,
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();