]> git.basschouten.com Git - openhab-addons.git/blob
21dcdc79ff0df60a51d32c266c12b30ca0bfc207
[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.orbitbhyve.internal.discovery;
14
15 import static org.openhab.binding.orbitbhyve.internal.OrbitBhyveBindingConstants.THING_TYPE_SPRINKLER;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.orbitbhyve.internal.handler.OrbitBhyveBridgeHandler;
28 import org.openhab.binding.orbitbhyve.internal.model.OrbitBhyveDevice;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link OrbitBhyveDiscoveryService} discovers sprinklers
43  * associated with your Orbit B-Hyve cloud account.
44  *
45  * @author Ondrej Pecta - Initial contribution
46  */
47 @NonNullByDefault
48 public class OrbitBhyveDiscoveryService extends AbstractDiscoveryService
49         implements DiscoveryService, ThingHandlerService {
50
51     private final Logger logger = LoggerFactory.getLogger(OrbitBhyveDiscoveryService.class);
52
53     private @Nullable OrbitBhyveBridgeHandler bridgeHandler;
54
55     private @Nullable ScheduledFuture<?> discoveryJob;
56
57     private static final int DISCOVERY_TIMEOUT_SEC = 10;
58     private static final int DISCOVERY_REFRESH_SEC = 1800;
59
60     public OrbitBhyveDiscoveryService() {
61         super(DISCOVERY_TIMEOUT_SEC);
62         logger.debug("Creating discovery service");
63     }
64
65     @Override
66     protected void startScan() {
67         runDiscovery();
68     }
69
70     @Override
71     public void activate() {
72         super.activate(null);
73     }
74
75     @Override
76     public void deactivate() {
77         super.deactivate();
78     }
79
80     @Override
81     public void setThingHandler(@Nullable ThingHandler thingHandler) {
82         if (thingHandler instanceof OrbitBhyveBridgeHandler) {
83             bridgeHandler = (OrbitBhyveBridgeHandler) thingHandler;
84         }
85     }
86
87     @Override
88     public @Nullable ThingHandler getThingHandler() {
89         return bridgeHandler;
90     }
91
92     @Override
93     protected void startBackgroundDiscovery() {
94         logger.debug("Starting Orbit B-Hyve background discovery");
95
96         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
97         if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
98             discoveryJob = scheduler.scheduleWithFixedDelay(this::runDiscovery, 10, DISCOVERY_REFRESH_SEC,
99                     TimeUnit.SECONDS);
100         }
101     }
102
103     @Override
104     protected void stopBackgroundDiscovery() {
105         logger.debug("Stopping Orbit B-Hyve background discovery");
106         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
107         if (localDiscoveryJob != null && !localDiscoveryJob.isCancelled()) {
108             localDiscoveryJob.cancel(true);
109         }
110     }
111
112     private synchronized void runDiscovery() {
113         OrbitBhyveBridgeHandler localBridgeHandler = bridgeHandler;
114         if (localBridgeHandler != null && ThingStatus.ONLINE == localBridgeHandler.getThing().getStatus()) {
115             List<OrbitBhyveDevice> devices = localBridgeHandler.getDevices();
116             logger.debug("Discovered total of {} devices", devices.size());
117             for (OrbitBhyveDevice device : devices) {
118                 sprinklerDiscovered(device);
119             }
120         }
121     }
122
123     private void sprinklerDiscovered(OrbitBhyveDevice device) {
124         OrbitBhyveBridgeHandler localBridgeHandler = bridgeHandler;
125         if (localBridgeHandler != null) {
126             Map<String, Object> properties = new HashMap<>();
127             properties.put("id", device.getId());
128             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, device.getFwVersion());
129             properties.put(Thing.PROPERTY_HARDWARE_VERSION, device.getHwVersion());
130             properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getMacAddress());
131             properties.put(Thing.PROPERTY_MODEL_ID, device.getType());
132             properties.put("Zones", device.getNumStations());
133             properties.put("Active zones", device.getZones().size());
134
135             ThingUID thingUID = new ThingUID(THING_TYPE_SPRINKLER, localBridgeHandler.getThing().getUID(),
136                     device.getId());
137
138             logger.debug("Detected a/an {} - label: {} id: {}", THING_TYPE_SPRINKLER.getId(), device.getName(),
139                     device.getId());
140             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_SPRINKLER)
141                     .withProperties(properties).withRepresentationProperty("id").withLabel(device.getName())
142                     .withBridge(localBridgeHandler.getThing().getUID()).build());
143         }
144     }
145
146     @Override
147     public Set<ThingTypeUID> getSupportedThingTypes() {
148         return Collections.singleton(THING_TYPE_SPRINKLER);
149     }
150 }