]> git.basschouten.com Git - openhab-addons.git/blob
a1eb4f2d08bb5eac68f7c27099c1c19b9fbf19e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.elroconnects.internal.discovery;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants;
24 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceType;
25 import org.openhab.binding.elroconnects.internal.devices.ElroConnectsDevice;
26 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The {@link ElroConnectsDiscoveryService} discovers devices connected to the ELRO Connects K1 Controller.
38  *
39  * @author Mark Herwege - Initial contribution
40  */
41 @NonNullByDefault
42 public class ElroConnectsDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
43
44     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDiscoveryService.class);
45
46     private @Nullable ElroConnectsBridgeHandler bridgeHandler;
47
48     private static final int TIMEOUT_SECONDS = 5;
49     private static final int REFRESH_INTERVAL_SECONDS = 60;
50
51     private @Nullable ScheduledFuture<?> discoveryJob;
52
53     public ElroConnectsDiscoveryService() {
54         super(ElroConnectsBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT_SECONDS);
55         logger.debug("Bridge discovery service started");
56     }
57
58     @Override
59     protected void startScan() {
60         discoverDevices();
61     }
62
63     private void discoverDevices() {
64         logger.debug("Starting device discovery scan");
65         ElroConnectsBridgeHandler bridge = bridgeHandler;
66         if (bridge != null) {
67             Map<Integer, ElroConnectsDevice> devices = bridge.getDevices();
68             ThingUID bridgeUID = bridge.getThing().getUID();
69             devices.entrySet().forEach(e -> {
70                 String deviceId = e.getKey().toString();
71                 String deviceName = e.getValue().getDeviceName();
72                 String deviceType = e.getValue().getDeviceType();
73                 if (!deviceType.isEmpty()) {
74                     ElroDeviceType type = TYPE_MAP.get(deviceType);
75                     if (type != null) {
76                         ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(type);
77                         if (thingTypeUID != null) {
78                             thingDiscovered(DiscoveryResultBuilder
79                                     .create(new ThingUID(thingTypeUID, bridgeUID, deviceId)).withLabel(deviceName)
80                                     .withBridge(bridgeUID).withProperty(CONFIG_DEVICE_ID, deviceId)
81                                     .withRepresentationProperty(CONFIG_DEVICE_ID).build());
82                         }
83                     }
84                 }
85             });
86         }
87     }
88
89     @Override
90     protected synchronized void stopScan() {
91         super.stopScan();
92         removeOlderResults(getTimestampOfLastScan());
93     }
94
95     @Override
96     protected void startBackgroundDiscovery() {
97         logger.debug("Start device background discovery");
98         ScheduledFuture<?> job = discoveryJob;
99         if (job == null || job.isCancelled()) {
100             discoveryJob = scheduler.scheduleWithFixedDelay(this::discoverDevices, 0, REFRESH_INTERVAL_SECONDS,
101                     TimeUnit.SECONDS);
102         }
103     }
104
105     @Override
106     protected void stopBackgroundDiscovery() {
107         logger.debug("Stop device background discovery");
108         ScheduledFuture<?> job = discoveryJob;
109         if (job != null) {
110             job.cancel(true);
111             discoveryJob = null;
112         }
113     }
114
115     @Override
116     public void deactivate() {
117         super.deactivate();
118     }
119
120     @Override
121     public void setThingHandler(@Nullable ThingHandler handler) {
122         if (handler instanceof ElroConnectsBridgeHandler) {
123             bridgeHandler = (ElroConnectsBridgeHandler) handler;
124         }
125     }
126
127     @Override
128     public @Nullable ThingHandler getThingHandler() {
129         return bridgeHandler;
130     }
131 }