]> git.basschouten.com Git - openhab-addons.git/blob
e1ddda7eff2443ead91ec1fb07f34b4f54e75e42
[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.tellstick.internal.discovery;
14
15 import java.util.List;
16 import java.util.Set;
17 import java.util.Vector;
18
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;
40
41 /**
42  * The {@link TellstickDiscoveryService} class is used to discover Tellstick
43  * devices that are connected to the Lan gateway.
44  *
45  * @author Jarle Hjortland - Initial contribution
46  */
47 public class TellstickDiscoveryService extends AbstractDiscoveryService implements DeviceStatusListener {
48     private static final long DEFAULT_TTL = 60 * 60; // 1 Hour
49
50     public TellstickDiscoveryService(int timeout) throws IllegalArgumentException {
51         super(timeout);
52     }
53
54     private final Logger logger = LoggerFactory.getLogger(TellstickDiscoveryService.class);
55
56     private List<TelldusBridgeHandler> telldusBridgeHandlers = new Vector<>();
57
58     public TellstickDiscoveryService(TelldusBridgeHandler telldusBridgeHandler) {
59         super(TellstickBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS, 10, true);
60         this.telldusBridgeHandlers.add(telldusBridgeHandler);
61     }
62
63     public void activate() {
64         for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
65             telldusBridgeHandler.registerDeviceStatusListener(this);
66         }
67     }
68
69     @Override
70     public void deactivate() {
71         for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
72             telldusBridgeHandler.unregisterDeviceStatusListener(this);
73         }
74     }
75
76     @Override
77     public Set<ThingTypeUID> getSupportedThingTypes() {
78         return TellstickBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
79     }
80
81     @Override
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);
93         } else {
94             logger.warn("Discovered Tellstick! device is unsupported: type '{}' with id '{}'", device.getDeviceType(),
95                     device.getId());
96         }
97     }
98
99     @Override
100     protected void startScan() {
101         for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
102             telldusBridgeHandler.rescanTelldusDevices();
103         }
104     }
105
106     @Override
107     public void onDeviceStateChanged(Bridge bridge, Device device, TellstickEvent event) {
108         // this can be ignored here
109     }
110
111     @Override
112     public void onDeviceRemoved(Bridge bridge, Device device) {
113         ThingUID thingUID = getThingUID(bridge, device);
114         if (thingUID != null) {
115             thingRemoved(thingUID);
116         } else {
117             logger.warn("Removed Tellstick! device is unsupported: type '{}' with id '{}'", device.getDeviceType(),
118                     device.getId());
119         }
120     }
121
122     private ThingUID getThingUID(Bridge bridge, Device device) {
123         ThingUID thingUID = null;
124         switch (device.getDeviceType()) {
125             case SENSOR:
126                 ThingTypeUID sensorThingId = findSensorType(device);
127                 thingUID = new ThingUID(sensorThingId, bridge.getUID(), device.getUUId());
128                 break;
129             case DEVICE:
130                 if (device instanceof DimmableDevice) {
131                     thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
132                             device.getUUId());
133                 } else if (device instanceof SwitchableDevice) {
134                     thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
135                             device.getUUId());
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(),
139                                 device.getUUId());
140                     } else {
141                         thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
142                                 device.getUUId());
143                     }
144                 }
145                 break;
146             default:
147                 break;
148         }
149         return thingUID;
150     }
151
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;
163             } else {
164                 sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
165             }
166         } else {
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;
175             } else {
176                 sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
177             }
178         }
179         return sensorThingId;
180     }
181
182     public void addBridgeHandler(TelldusBridgeHandler tellstickBridgeHandler) {
183         telldusBridgeHandlers.add(tellstickBridgeHandler);
184         tellstickBridgeHandler.registerDeviceStatusListener(this);
185     }
186 }