]> git.basschouten.com Git - openhab-addons.git/blob
0f5b0e48f0b99c340e522e3461189fe1d893d856
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.rfxcom.internal.discovery;
14
15 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.ID_DELIMITER;
16
17 import java.util.Set;
18
19 import org.openhab.binding.rfxcom.internal.DeviceMessageListener;
20 import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.handler.RFXComBridgeHandler;
23 import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link RFXComDeviceDiscoveryService} class is used to discover RFXCOM
33  * devices that send messages to RFXCOM bridge.
34  *
35  * @author Pauli Anttila - Initial contribution
36  */
37 public class RFXComDeviceDiscoveryService extends AbstractDiscoveryService implements DeviceMessageListener {
38     private final Logger logger = LoggerFactory.getLogger(RFXComDeviceDiscoveryService.class);
39     private final int DISCOVERY_TTL = 3600;
40
41     private RFXComBridgeHandler bridgeHandler;
42
43     public RFXComDeviceDiscoveryService(RFXComBridgeHandler rfxcomBridgeHandler) {
44         super(null, 1, false);
45         this.bridgeHandler = rfxcomBridgeHandler;
46     }
47
48     public void activate() {
49         bridgeHandler.registerDeviceStatusListener(this);
50     }
51
52     @Override
53     public void deactivate() {
54         bridgeHandler.unregisterDeviceStatusListener(this);
55     }
56
57     @Override
58     public Set<ThingTypeUID> getSupportedThingTypes() {
59         return RFXComBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
60     }
61
62     @Override
63     protected void startScan() {
64         // this can be ignored here as we discover devices from received messages
65     }
66
67     @Override
68     public void onDeviceMessageReceived(ThingUID bridge, RFXComDeviceMessage message) throws RFXComException {
69         logger.trace("Received: bridge: {} message: {}", bridge, message);
70
71         String id = message.getDeviceId();
72         ThingTypeUID uid = RFXComBindingConstants.PACKET_TYPE_THING_TYPE_UID_MAP.get(message.getPacketType());
73         ThingUID thingUID = new ThingUID(uid, bridge, id.replace(ID_DELIMITER, "_"));
74
75         if (!bridgeHandler.getConfiguration().disableDiscovery) {
76             logger.trace("Adding new RFXCOM {} with id '{}' to smarthome inbox", thingUID, id);
77             DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(thingUID).withBridge(bridge)
78                     .withTTL(DISCOVERY_TTL);
79             message.addDevicePropertiesTo(discoveryResultBuilder);
80
81             thingDiscovered(discoveryResultBuilder.build());
82         } else {
83             logger.trace("Ignoring RFXCOM {} with id '{}' - discovery disabled", thingUID, id);
84         }
85     }
86 }