2 * Copyright (c) 2010-2023 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.Collections;
18 import java.util.HashMap;
19 import java.util.List;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
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;
42 * The {@link OrbitBhyveDiscoveryService} discovers sprinklers
43 * associated with your Orbit B-Hyve cloud account.
45 * @author Ondrej Pecta - Initial contribution
48 public class OrbitBhyveDiscoveryService extends AbstractDiscoveryService
49 implements DiscoveryService, ThingHandlerService {
51 private final Logger logger = LoggerFactory.getLogger(OrbitBhyveDiscoveryService.class);
53 private @Nullable OrbitBhyveBridgeHandler bridgeHandler;
55 private @Nullable ScheduledFuture<?> discoveryJob;
57 private static final int DISCOVERY_TIMEOUT_SEC = 10;
58 private static final int DISCOVERY_REFRESH_SEC = 1800;
60 public OrbitBhyveDiscoveryService() {
61 super(DISCOVERY_TIMEOUT_SEC);
62 logger.debug("Creating discovery service");
66 protected void startScan() {
71 public void activate() {
76 public void deactivate() {
81 public void setThingHandler(@Nullable ThingHandler thingHandler) {
82 if (thingHandler instanceof OrbitBhyveBridgeHandler) {
83 bridgeHandler = (OrbitBhyveBridgeHandler) thingHandler;
88 public @Nullable ThingHandler getThingHandler() {
93 protected void startBackgroundDiscovery() {
94 logger.debug("Starting Orbit B-Hyve background discovery");
96 ScheduledFuture<?> localDiscoveryJob = discoveryJob;
97 if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
98 discoveryJob = scheduler.scheduleWithFixedDelay(this::runDiscovery, 10, DISCOVERY_REFRESH_SEC,
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);
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);
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());
135 ThingUID thingUID = new ThingUID(THING_TYPE_SPRINKLER, localBridgeHandler.getThing().getUID(),
138 logger.debug("Detected a/an {} - label: {} id: {}", THING_TYPE_SPRINKLER.getId(), device.getName(),
140 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_SPRINKLER)
141 .withProperties(properties).withRepresentationProperty("id").withLabel(device.getName())
142 .withBridge(localBridgeHandler.getThing().getUID()).build());
147 public Set<ThingTypeUID> getSupportedThingTypes() {
148 return Collections.singleton(THING_TYPE_SPRINKLER);