]> git.basschouten.com Git - openhab-addons.git/blob
9fe78ef929a6cc93042140456770beb160a75f0b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nikobus.internal.discovery;
14
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.nikobus.internal.handler.NikobusPcLinkHandler;
24 import org.openhab.binding.nikobus.internal.utils.Utils;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link NikobusDiscoveryService} discovers push button things for Nikobus switches.
35  * Buttons are not discovered via scan but only when physical button is pressed and a new
36  * nikobus push button bus address is detected.
37  *
38  * @author Boris Krivonog - Initial contribution
39  */
40 @NonNullByDefault
41 public class NikobusDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
42
43     private final Logger logger = LoggerFactory.getLogger(NikobusDiscoveryService.class);
44     private @Nullable NikobusPcLinkHandler bridgeHandler;
45
46     public NikobusDiscoveryService() throws IllegalArgumentException {
47         super(Set.of(THING_TYPE_PUSH_BUTTON), 0);
48     }
49
50     @Override
51     protected void startScan() {
52     }
53
54     @Override
55     protected void stopBackgroundDiscovery() {
56         NikobusPcLinkHandler handler = bridgeHandler;
57         if (handler != null) {
58             handler.resetUnhandledCommandProcessor();
59         }
60     }
61
62     @Override
63     protected void startBackgroundDiscovery() {
64         NikobusPcLinkHandler handler = bridgeHandler;
65         if (handler != null) {
66             handler.setUnhandledCommandProcessor(this::process);
67         }
68     }
69
70     private void process(String command) {
71         if (command.length() <= 2 || !command.startsWith("#N")) {
72             logger.debug("Ignoring command() '{}'", command);
73         }
74
75         String address = command.substring(2);
76         logger.debug("Received address = '{}'", address);
77
78         NikobusPcLinkHandler handler = bridgeHandler;
79         if (handler != null) {
80             ThingUID thingUID = new ThingUID(THING_TYPE_PUSH_BUTTON, handler.getThing().getUID(), address);
81
82             Map<String, Object> properties = new HashMap<>();
83             properties.put(CONFIG_ADDRESS, address);
84
85             String humanReadableNikobusAddress = Utils.convertToHumanReadableNikobusAddress(address).toUpperCase();
86             logger.debug("Detected Nikobus Push Button: '{}'", humanReadableNikobusAddress);
87             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_PUSH_BUTTON)
88                     .withLabel("Nikobus Push Button " + humanReadableNikobusAddress).withProperties(properties)
89                     .withRepresentationProperty(CONFIG_ADDRESS).withBridge(handler.getThing().getUID()).build());
90         }
91     }
92
93     @Override
94     public void setThingHandler(ThingHandler handler) {
95         if (handler instanceof NikobusPcLinkHandler pcLinkHandler) {
96             bridgeHandler = pcLinkHandler;
97         }
98     }
99
100     @Override
101     public @Nullable ThingHandler getThingHandler() {
102         return bridgeHandler;
103     }
104
105     @Override
106     public void activate() {
107         super.activate(null);
108     }
109
110     @Override
111     public void deactivate() {
112         super.deactivate();
113     }
114 }