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.hydrawise.internal.discovery;
15 import java.util.Collections;
16 import java.util.Date;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.hydrawise.internal.HydrawiseBindingConstants;
22 import org.openhab.binding.hydrawise.internal.HydrawiseControllerListener;
23 import org.openhab.binding.hydrawise.internal.api.graphql.dto.Controller;
24 import org.openhab.binding.hydrawise.internal.api.graphql.dto.Customer;
25 import org.openhab.binding.hydrawise.internal.handler.HydrawiseAccountHandler;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerService;
31 import org.osgi.service.component.annotations.Component;
35 * @author Dan Cunningham - Initial contribution
40 @Component(service = ThingHandlerService.class)
41 public class HydrawiseCloudControllerDiscoveryService extends AbstractDiscoveryService
42 implements HydrawiseControllerListener, ThingHandlerService {
44 private static final int TIMEOUT = 5;
46 HydrawiseAccountHandler handler;
48 public HydrawiseCloudControllerDiscoveryService() {
49 super(Collections.singleton(HydrawiseBindingConstants.THING_TYPE_CONTROLLER), TIMEOUT, true);
53 protected void startScan() {
54 HydrawiseAccountHandler localHandler = this.handler;
55 if (localHandler != null) {
56 Customer data = localHandler.lastData();
58 data.controllers.forEach(controller -> addDiscoveryResults(controller));
64 public void deactivate() {
65 HydrawiseAccountHandler localHandler = this.handler;
66 if (localHandler != null) {
67 removeOlderResults(new Date().getTime(), localHandler.getThing().getUID());
72 protected synchronized void stopScan() {
74 HydrawiseAccountHandler localHandler = this.handler;
75 if (localHandler != null) {
76 removeOlderResults(getTimestampOfLastScan(), localHandler.getThing().getUID());
81 public void onData(List<Controller> controllers) {
82 controllers.forEach(controller -> addDiscoveryResults(controller));
86 public void setThingHandler(ThingHandler handler) {
87 this.handler = (HydrawiseAccountHandler) handler;
88 this.handler.addControllerListeners(this);
92 public @Nullable ThingHandler getThingHandler() {
96 private void addDiscoveryResults(Controller controller) {
97 HydrawiseAccountHandler localHandler = this.handler;
98 if (localHandler != null) {
99 String label = String.format("Hydrawise Controller %s", controller.name);
100 int id = controller.id;
101 ThingUID bridgeUID = localHandler.getThing().getUID();
102 ThingUID thingUID = new ThingUID(HydrawiseBindingConstants.THING_TYPE_CONTROLLER, bridgeUID,
104 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withLabel(label).withBridge(bridgeUID)
105 .withProperty(HydrawiseBindingConstants.CONFIG_CONTROLLER_ID, id)
106 .withRepresentationProperty(String.valueOf(HydrawiseBindingConstants.CONFIG_CONTROLLER_ID))