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