2 * Copyright (c) 2010-2024 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.nikobus.internal.discovery;
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.*;
17 import java.util.HashMap;
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;
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.
38 * @author Boris Krivonog - Initial contribution
41 public class NikobusDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
43 private final Logger logger = LoggerFactory.getLogger(NikobusDiscoveryService.class);
44 private @Nullable NikobusPcLinkHandler bridgeHandler;
46 public NikobusDiscoveryService() throws IllegalArgumentException {
47 super(Set.of(THING_TYPE_PUSH_BUTTON), 0);
51 protected void startScan() {
55 protected void stopBackgroundDiscovery() {
56 NikobusPcLinkHandler handler = bridgeHandler;
57 if (handler != null) {
58 handler.resetUnhandledCommandProcessor();
63 protected void startBackgroundDiscovery() {
64 NikobusPcLinkHandler handler = bridgeHandler;
65 if (handler != null) {
66 handler.setUnhandledCommandProcessor(this::process);
70 private void process(String command) {
71 if (command.length() <= 2 || !command.startsWith("#N")) {
72 logger.debug("Ignoring command() '{}'", command);
75 String address = command.substring(2);
76 logger.debug("Received address = '{}'", address);
78 NikobusPcLinkHandler handler = bridgeHandler;
79 if (handler != null) {
80 ThingUID thingUID = new ThingUID(THING_TYPE_PUSH_BUTTON, handler.getThing().getUID(), address);
82 Map<String, Object> properties = new HashMap<>();
83 properties.put(CONFIG_ADDRESS, address);
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());
94 public void setThingHandler(ThingHandler handler) {
95 if (handler instanceof NikobusPcLinkHandler pcLinkHandler) {
96 bridgeHandler = pcLinkHandler;
101 public @Nullable ThingHandler getThingHandler() {
102 return bridgeHandler;
106 public void activate() {
107 super.activate(null);
111 public void deactivate() {