]> git.basschouten.com Git - openhab-addons.git/blob
db1ff2c48405e941f7357e9a4f4d495b09671834
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.freeathomesystem.internal;
14
15 import java.time.Instant;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ScheduledFuture;
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.freeathomesystem.internal.datamodel.FreeAtHomeDeviceDescription;
26 import org.openhab.binding.freeathomesystem.internal.handler.FreeAtHomeBridgeHandler;
27 import org.openhab.binding.freeathomesystem.internal.util.FreeAtHomeHttpCommunicationException;
28 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The {@link FreeAtHomeSystemDiscoveryService} is responsible for performing discovery of things
38  *
39  * @author Andras Uhrin - Initial contribution
40  */
41 @NonNullByDefault
42 public class FreeAtHomeSystemDiscoveryService extends AbstractThingHandlerDiscoveryService<FreeAtHomeBridgeHandler> {
43
44     private final Logger logger = LoggerFactory.getLogger(FreeAtHomeSystemDiscoveryService.class);
45     private @Nullable ScheduledFuture<?> backgroundDiscoveryJob = null;
46
47     private static final long BACKGROUND_DISCOVERY_DELAY = 1L;
48     private boolean isScanTerminated;
49
50     Runnable runnable = new Runnable() {
51         @Override
52         public void run() {
53             ThingUID bridgeUID = thingHandler.getThing().getUID();
54
55             List<String> deviceList;
56
57             try {
58                 deviceList = thingHandler.getDeviceDeviceList();
59
60                 for (int i = 0; (i < deviceList.size()) && !isScanTerminated; i++) {
61                     FreeAtHomeDeviceDescription device = thingHandler.getFreeatHomeDeviceDescription(deviceList.get(i));
62
63                     ThingUID uid = new ThingUID(FreeAtHomeSystemBindingConstants.DEVICE_TYPE_UID, bridgeUID,
64                             device.deviceId);
65                     Map<String, Object> properties = new HashMap<>(1);
66                     properties.put("deviceId", device.deviceId);
67                     properties.put("interface", device.interfaceType);
68
69                     String deviceLabel = device.deviceLabel;
70
71                     DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(uid).withLabel(deviceLabel)
72                             .withRepresentationProperty("deviceId").withBridge(bridgeUID).withProperties(properties)
73                             .build();
74
75                     thingDiscovered(discoveryResult);
76
77                     logger.debug("Thing discovered - DeviceId: {} - Device label: {}", device.getDeviceId(),
78                             device.getDeviceLabel());
79                 }
80
81                 stopScan();
82             } catch (FreeAtHomeHttpCommunicationException e) {
83                 logger.debug("Communication error in device discovery with the bridge: {}",
84                         thingHandler.getThing().getLabel());
85             } catch (RuntimeException e) {
86                 logger.debug("Scanning interrupted");
87             }
88         }
89     };
90
91     public FreeAtHomeSystemDiscoveryService(int timeout) {
92         super(FreeAtHomeBridgeHandler.class, FreeAtHomeSystemBindingConstants.SUPPORTED_THING_TYPES_UIDS, timeout,
93                 false);
94     }
95
96     public FreeAtHomeSystemDiscoveryService() {
97         this(90);
98     }
99
100     @Override
101     public Set<ThingTypeUID> getSupportedThingTypes() {
102         return Set.of(FreeAtHomeSystemBindingConstants.BRIDGE_TYPE_UID);
103     }
104
105     @Override
106     protected void startScan() {
107         if (backgroundDiscoveryJob == null) {
108             this.removeOlderResults(Instant.now().toEpochMilli());
109
110             isScanTerminated = false;
111             backgroundDiscoveryJob = scheduler.schedule(runnable, BACKGROUND_DISCOVERY_DELAY, TimeUnit.SECONDS);
112         }
113     }
114
115     @Override
116     protected synchronized void stopScan() {
117         super.stopScan();
118
119         isScanTerminated = true;
120
121         ScheduledFuture<?> localDiscoveryJob = backgroundDiscoveryJob;
122
123         if (localDiscoveryJob != null) {
124             localDiscoveryJob.cancel(true);
125         }
126
127         backgroundDiscoveryJob = null;
128
129         removeOlderResults(Instant.now().toEpochMilli());
130     }
131
132     @Override
133     public void deactivate() {
134         removeOlderResults(Instant.now().toEpochMilli());
135     }
136 }