]> git.basschouten.com Git - openhab-addons.git/blob
02dfc870bbd4874b026e1c05c2488424076af435
[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.lutron.internal.hw;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.concurrent.atomic.AtomicBoolean;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.LutronHandlerFactory;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  *
34  * The Discovery Service for Lutron HomeWorks processors. There is no great way to automatically
35  * discover modules in the legacy HomeWorks processor (that I know of) so this service simply iterates
36  * through possible addresses and asks for status on that address. If it's a valid module, the processor will return
37  * with the dimmer status and it will be discovered.
38  *
39  * @author Andrew Shilliday - Initial contribution
40  */
41 public class HwDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
42     private Logger logger = LoggerFactory.getLogger(HwDiscoveryService.class);
43
44     private final AtomicBoolean isScanning = new AtomicBoolean(false);
45
46     private @NonNullByDefault({}) HwSerialBridgeHandler handler;
47
48     public HwDiscoveryService() {
49         super(LutronHandlerFactory.HW_DISCOVERABLE_DEVICE_TYPES_UIDS, 10);
50     }
51
52     @Override
53     public void setThingHandler(@Nullable ThingHandler handler) {
54         if (handler instanceof HwSerialBridgeHandler bridgeHandler) {
55             this.handler = bridgeHandler;
56             this.handler.setDiscoveryService(this);
57         }
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return handler;
63     }
64
65     @Override
66     public void activate() {
67         super.activate(null);
68     }
69
70     @Override
71     public void deactivate() {
72         super.deactivate();
73     }
74
75     @Override
76     protected void startScan() {
77         scheduler.submit(() -> {
78             if (isScanning.compareAndSet(false, true)) {
79                 try {
80                     logger.debug("Starting scan for HW Dimmers");
81                     for (int m = 1; m <= 8; m++) { // Modules
82                         for (int o = 1; o <= 4; o++) { // Outputs
83                             String address = String.format("[01:01:00:%02d:%02d]", m, o);
84                             handler.sendCommand("RDL, " + address);
85                             Thread.sleep(5);
86                         }
87                     }
88                 } catch (InterruptedException e) {
89                     logger.debug("Scan interrupted");
90                 } finally {
91                     isScanning.set(false);
92                 }
93             }
94         });
95     }
96
97     /**
98      * Called by the bridge when it receives a status update for a dimmer that is not registered.
99      */
100     public void declareUnknownDimmer(String address) {
101         if (address == null) {
102             logger.info("Discovered HomeWorks dimmer with no address");
103             return;
104         }
105         String addressUid = address.replaceAll("[\\[\\]]", "").replace(":", "-");
106         ThingUID bridgeUID = this.handler.getThing().getUID();
107         ThingUID uid = new ThingUID(HwConstants.THING_TYPE_HWDIMMER, bridgeUID, addressUid);
108
109         Map<String, Object> props = new HashMap<>();
110
111         props.put("address", address);
112         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withProperties(props)
113                 .withRepresentationProperty("address").build();
114
115         thingDiscovered(result);
116
117         logger.debug("Discovered {}", uid);
118     }
119 }