2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.orbitbhyve.internal.discovery;
15 import static org.openhab.binding.orbitbhyve.internal.OrbitBhyveBindingConstants.THING_TYPE_SPRINKLER;
17 import java.util.HashMap;
18 import java.util.List;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
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;
41 * The {@link OrbitBhyveDiscoveryService} discovers sprinklers
42 * associated with your Orbit B-Hyve cloud account.
44 * @author Ondrej Pecta - Initial contribution
47 public class OrbitBhyveDiscoveryService extends AbstractDiscoveryService
48 implements DiscoveryService, ThingHandlerService {
50 private final Logger logger = LoggerFactory.getLogger(OrbitBhyveDiscoveryService.class);
52 private @Nullable OrbitBhyveBridgeHandler bridgeHandler;
54 private @Nullable ScheduledFuture<?> discoveryJob;
56 private static final int DISCOVERY_TIMEOUT_SEC = 10;
57 private static final int DISCOVERY_REFRESH_SEC = 1800;
59 public OrbitBhyveDiscoveryService() {
60 super(DISCOVERY_TIMEOUT_SEC);
61 logger.debug("Creating discovery service");
65 protected void startScan() {
70 public void activate() {
75 public void deactivate() {
80 public void setThingHandler(@Nullable ThingHandler thingHandler) {
81 if (thingHandler instanceof OrbitBhyveBridgeHandler bridgeHandler) {
82 this.bridgeHandler = bridgeHandler;
87 public @Nullable ThingHandler getThingHandler() {
92 protected void startBackgroundDiscovery() {
93 logger.debug("Starting Orbit B-Hyve background discovery");
95 ScheduledFuture<?> localDiscoveryJob = discoveryJob;
96 if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
97 discoveryJob = scheduler.scheduleWithFixedDelay(this::runDiscovery, 10, DISCOVERY_REFRESH_SEC,
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);
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);
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());
134 ThingUID thingUID = new ThingUID(THING_TYPE_SPRINKLER, localBridgeHandler.getThing().getUID(),
137 logger.debug("Detected a/an {} - label: {} id: {}", THING_TYPE_SPRINKLER.getId(), device.getName(),
139 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_SPRINKLER)
140 .withProperties(properties).withRepresentationProperty("id").withLabel(device.getName())
141 .withBridge(localBridgeHandler.getThing().getUID()).build());
146 public Set<ThingTypeUID> getSupportedThingTypes() {
147 return Set.of(THING_TYPE_SPRINKLER);