2 * Copyright (c) 2010-2021 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.tellstick.internal.discovery;
15 import java.util.List;
17 import java.util.Vector;
19 import org.openhab.binding.tellstick.internal.TellstickBindingConstants;
20 import org.openhab.binding.tellstick.internal.handler.DeviceStatusListener;
21 import org.openhab.binding.tellstick.internal.handler.TelldusBridgeHandler;
22 import org.openhab.binding.tellstick.internal.live.xml.LiveDataType;
23 import org.openhab.binding.tellstick.internal.live.xml.TellstickNetDevice;
24 import org.openhab.binding.tellstick.internal.live.xml.TellstickNetSensor;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.tellstick.JNA;
34 import org.tellstick.device.TellstickSensor;
35 import org.tellstick.device.iface.Device;
36 import org.tellstick.device.iface.DimmableDevice;
37 import org.tellstick.device.iface.SwitchableDevice;
38 import org.tellstick.device.iface.TellstickEvent;
39 import org.tellstick.enums.DataType;
42 * The {@link TellstickDiscoveryService} class is used to discover Tellstick
43 * devices that are connected to the Lan gateway.
45 * @author Jarle Hjortland - Initial contribution
47 public class TellstickDiscoveryService extends AbstractDiscoveryService implements DeviceStatusListener {
48 private static final long DEFAULT_TTL = 60 * 60; // 1 Hour
50 public TellstickDiscoveryService(int timeout) throws IllegalArgumentException {
54 private final Logger logger = LoggerFactory.getLogger(TellstickDiscoveryService.class);
56 private List<TelldusBridgeHandler> telldusBridgeHandlers = new Vector<>();
58 public TellstickDiscoveryService(TelldusBridgeHandler telldusBridgeHandler) {
59 super(TellstickBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS, 10, true);
60 this.telldusBridgeHandlers.add(telldusBridgeHandler);
63 public void activate() {
64 for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
65 telldusBridgeHandler.registerDeviceStatusListener(this);
70 public void deactivate() {
71 for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
72 telldusBridgeHandler.unregisterDeviceStatusListener(this);
77 public Set<ThingTypeUID> getSupportedThingTypes() {
78 return TellstickBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
82 public void onDeviceAdded(Bridge bridge, Device device) {
83 logger.debug("Adding new TellstickDevice! '{}' with id '{}' and type '{}' to inbox", device, device.getId(),
84 device.getDeviceType());
85 ThingUID thingUID = getThingUID(bridge, device);
86 logger.debug("Detected thingUID: {}", thingUID);
87 if (thingUID != null) {
88 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withTTL(DEFAULT_TTL)
89 .withProperty(TellstickBindingConstants.DEVICE_ID, device.getUUId())
90 .withProperty(TellstickBindingConstants.DEVICE_NAME, device.getName()).withBridge(bridge.getUID())
91 .withLabel(device.getDeviceType() + ": " + device.getName()).build();
92 thingDiscovered(discoveryResult);
94 logger.warn("Discovered Tellstick! device is unsupported: type '{}' with id '{}'", device.getDeviceType(),
100 protected void startScan() {
101 for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
102 telldusBridgeHandler.rescanTelldusDevices();
107 public void onDeviceStateChanged(Bridge bridge, Device device, TellstickEvent event) {
108 // this can be ignored here
112 public void onDeviceRemoved(Bridge bridge, Device device) {
113 ThingUID thingUID = getThingUID(bridge, device);
114 if (thingUID != null) {
115 thingRemoved(thingUID);
117 logger.warn("Removed Tellstick! device is unsupported: type '{}' with id '{}'", device.getDeviceType(),
122 private ThingUID getThingUID(Bridge bridge, Device device) {
123 ThingUID thingUID = null;
124 switch (device.getDeviceType()) {
126 ThingTypeUID sensorThingId = findSensorType(device);
127 thingUID = new ThingUID(sensorThingId, bridge.getUID(), device.getUUId());
130 if (device instanceof DimmableDevice) {
131 thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
133 } else if (device instanceof SwitchableDevice) {
134 thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
136 } else if (device instanceof TellstickNetDevice) {
137 if ((((TellstickNetDevice) device).getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
138 thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
141 thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
152 private ThingTypeUID findSensorType(Device device) {
153 logger.debug("Device: {}", device);
154 ThingTypeUID sensorThingId;
155 if (device instanceof TellstickSensor) {
156 TellstickSensor sensor = (TellstickSensor) device;
157 logger.debug("Sensor: {}", device);
158 if (sensor.getData(DataType.WINDAVERAGE) != null || sensor.getData(DataType.WINDGUST) != null
159 || sensor.getData(DataType.WINDDIRECTION) != null) {
160 sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;
161 } else if (sensor.getData(DataType.RAINTOTAL) != null || sensor.getData(DataType.RAINRATE) != null) {
162 sensorThingId = TellstickBindingConstants.RAINSENSOR_THING_TYPE;
164 sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
167 TellstickNetSensor sensor = (TellstickNetSensor) device;
168 if (sensor.isSensorOfType(LiveDataType.WINDAVERAGE) || sensor.isSensorOfType(LiveDataType.WINDDIRECTION)
169 || sensor.isSensorOfType(LiveDataType.WINDGUST)) {
170 sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;
171 } else if (sensor.isSensorOfType(LiveDataType.RAINRATE) || sensor.isSensorOfType(LiveDataType.RAINTOTAL)) {
172 sensorThingId = TellstickBindingConstants.RAINSENSOR_THING_TYPE;
173 } else if (sensor.isSensorOfType(LiveDataType.WATT)) {
174 sensorThingId = TellstickBindingConstants.POWERSENSOR_THING_TYPE;
176 sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
179 return sensorThingId;
182 public void addBridgeHandler(TelldusBridgeHandler tellstickBridgeHandler) {
183 telldusBridgeHandlers.add(tellstickBridgeHandler);
184 tellstickBridgeHandler.registerDeviceStatusListener(this);