]> git.basschouten.com Git - openhab-addons.git/blob
fe8c8c11e0626d88fef8c743dbcebdd89ed3c727
[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.hydrawise.internal.discovery;
14
15 import java.util.Date;
16 import java.util.List;
17 import java.util.Set;
18
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;
32
33 /**
34  *
35  * @author Dan Cunningham - Initial contribution
36  *
37  */
38
39 @NonNullByDefault
40 @Component(service = ThingHandlerService.class)
41 public class HydrawiseCloudControllerDiscoveryService extends AbstractDiscoveryService
42         implements HydrawiseControllerListener, ThingHandlerService {
43
44     private static final int TIMEOUT = 5;
45     @Nullable
46     HydrawiseAccountHandler handler;
47
48     public HydrawiseCloudControllerDiscoveryService() {
49         super(Set.of(HydrawiseBindingConstants.THING_TYPE_CONTROLLER), TIMEOUT, true);
50     }
51
52     @Override
53     protected void startScan() {
54         HydrawiseAccountHandler localHandler = this.handler;
55         if (localHandler != null) {
56             Customer data = localHandler.lastData();
57             if (data != null) {
58                 data.controllers.forEach(controller -> addDiscoveryResults(controller));
59             }
60         }
61     }
62
63     @Override
64     public void deactivate() {
65         HydrawiseAccountHandler localHandler = this.handler;
66         if (localHandler != null) {
67             removeOlderResults(new Date().getTime(), localHandler.getThing().getUID());
68         }
69     }
70
71     @Override
72     protected synchronized void stopScan() {
73         super.stopScan();
74         HydrawiseAccountHandler localHandler = this.handler;
75         if (localHandler != null) {
76             removeOlderResults(getTimestampOfLastScan(), localHandler.getThing().getUID());
77         }
78     }
79
80     @Override
81     public void onData(List<Controller> controllers) {
82         controllers.forEach(controller -> addDiscoveryResults(controller));
83     }
84
85     @Override
86     public void setThingHandler(ThingHandler handler) {
87         this.handler = (HydrawiseAccountHandler) handler;
88         this.handler.addControllerListeners(this);
89     }
90
91     @Override
92     public @Nullable ThingHandler getThingHandler() {
93         return handler;
94     }
95
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,
103                     String.valueOf(id));
104             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withLabel(label).withBridge(bridgeUID)
105                     .withProperty(HydrawiseBindingConstants.CONFIG_CONTROLLER_ID, id)
106                     .withRepresentationProperty(HydrawiseBindingConstants.CONFIG_CONTROLLER_ID).build());
107         }
108     }
109 }