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.rfxcom.internal.discovery;
15 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.ID_DELIMITER;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.rfxcom.internal.DeviceMessageListener;
22 import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
24 import org.openhab.binding.rfxcom.internal.handler.RFXComBridgeHandler;
25 import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link RFXComDeviceDiscoveryService} class is used to discover RFXCOM
37 * devices that send messages to RFXCOM bridge.
39 * @author Pauli Anttila - Initial contribution
40 * @author Laurent Garnier - use ThingHandlerService
43 public class RFXComDeviceDiscoveryService extends AbstractDiscoveryService
44 implements DeviceMessageListener, ThingHandlerService {
45 private final Logger logger = LoggerFactory.getLogger(RFXComDeviceDiscoveryService.class);
46 private final int DISCOVERY_TTL = 3600;
48 private @Nullable RFXComBridgeHandler bridgeHandler;
50 public RFXComDeviceDiscoveryService() {
51 super(null, 1, false);
55 public void setThingHandler(ThingHandler handler) {
56 if (handler instanceof RFXComBridgeHandler) {
57 bridgeHandler = (RFXComBridgeHandler) handler;
62 public @Nullable ThingHandler getThingHandler() {
67 public void activate() {
69 RFXComBridgeHandler handler = bridgeHandler;
70 if (handler != null) {
71 handler.registerDeviceStatusListener(this);
76 public void deactivate() {
77 RFXComBridgeHandler handler = bridgeHandler;
78 if (handler != null) {
79 handler.unregisterDeviceStatusListener(this);
85 public Set<ThingTypeUID> getSupportedThingTypes() {
86 return RFXComBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
90 protected void startScan() {
91 // this can be ignored here as we discover devices from received messages
95 public void onDeviceMessageReceived(ThingUID bridge, RFXComDeviceMessage message) throws RFXComException {
96 logger.trace("Received: bridge: {} message: {}", bridge, message);
98 String id = message.getDeviceId();
99 ThingTypeUID uid = RFXComBindingConstants.PACKET_TYPE_THING_TYPE_UID_MAP.get(message.getPacketType());
101 logger.debug("cannot find uid for message {}", message);
104 ThingUID thingUID = new ThingUID(uid, bridge, id.replace(ID_DELIMITER, "_"));
106 RFXComBridgeHandler handler = bridgeHandler;
107 if (handler == null) {
108 logger.trace("Ignoring RFXCOM {} with id '{}' - bridge handler is null", thingUID, id);
109 } else if (!handler.getConfiguration().disableDiscovery) {
110 logger.trace("Adding new RFXCOM {} with id '{}' to inbox", thingUID, id);
111 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(thingUID).withBridge(bridge)
112 .withTTL(DISCOVERY_TTL);
113 message.addDevicePropertiesTo(discoveryResultBuilder);
115 thingDiscovered(discoveryResultBuilder.build());
117 logger.trace("Ignoring RFXCOM {} with id '{}' - discovery disabled", thingUID, id);