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