/
TellstickDiscoveryService.java
186 lines (169 loc) · 8.13 KB
/
TellstickDiscoveryService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tellstick.internal.discovery;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.tellstick.internal.TellstickBindingConstants;
import org.openhab.binding.tellstick.internal.handler.DeviceStatusListener;
import org.openhab.binding.tellstick.internal.handler.TelldusBridgeHandler;
import org.openhab.binding.tellstick.internal.live.xml.LiveDataType;
import org.openhab.binding.tellstick.internal.live.xml.TellstickNetDevice;
import org.openhab.binding.tellstick.internal.live.xml.TellstickNetSensor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tellstick.JNA;
import org.tellstick.device.TellstickSensor;
import org.tellstick.device.iface.Device;
import org.tellstick.device.iface.DimmableDevice;
import org.tellstick.device.iface.SwitchableDevice;
import org.tellstick.device.iface.TellstickEvent;
import org.tellstick.enums.DataType;
/**
* The {@link TellstickDiscoveryService} class is used to discover Tellstick
* devices that are connected to the Lan gateway.
*
* @author Jarle Hjortland - Initial contribution
*/
public class TellstickDiscoveryService extends AbstractDiscoveryService implements DeviceStatusListener {
private static final long DEFAULT_TTL = 60 * 60; // 1 Hour
public TellstickDiscoveryService(int timeout) throws IllegalArgumentException {
super(timeout);
}
private final Logger logger = LoggerFactory.getLogger(TellstickDiscoveryService.class);
private List<TelldusBridgeHandler> telldusBridgeHandlers = new Vector<TelldusBridgeHandler>();
public TellstickDiscoveryService(TelldusBridgeHandler telldusBridgeHandler) {
super(TellstickBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS, 10, true);
this.telldusBridgeHandlers.add(telldusBridgeHandler);
}
public void activate() {
for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
telldusBridgeHandler.registerDeviceStatusListener(this);
}
}
@Override
public void deactivate() {
for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
telldusBridgeHandler.unregisterDeviceStatusListener(this);
}
}
@Override
public Set<ThingTypeUID> getSupportedThingTypes() {
return TellstickBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
}
@Override
public void onDeviceAdded(Bridge bridge, Device device) {
logger.debug("Adding new TellstickDevice! '{}' with id '{}' and type '{}' to smarthome inbox", device,
device.getId(), device.getDeviceType());
ThingUID thingUID = getThingUID(bridge, device);
logger.debug("Detected thingUID: {}", thingUID);
if (thingUID != null) {
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withTTL(DEFAULT_TTL)
.withProperty(TellstickBindingConstants.DEVICE_ID, device.getUUId())
.withProperty(TellstickBindingConstants.DEVICE_NAME, device.getName()).withBridge(bridge.getUID())
.withLabel(device.getDeviceType() + ": " + device.getName()).build();
thingDiscovered(discoveryResult);
} else {
logger.warn("Discovered Tellstick! device is unsupported: type '{}' with id '{}'", device.getDeviceType(),
device.getId());
}
}
@Override
protected void startScan() {
for (TelldusBridgeHandler telldusBridgeHandler : telldusBridgeHandlers) {
telldusBridgeHandler.rescanTelldusDevices();
}
}
@Override
public void onDeviceStateChanged(Bridge bridge, Device device, TellstickEvent event) {
// this can be ignored here
}
@Override
public void onDeviceRemoved(Bridge bridge, Device device) {
ThingUID thingUID = getThingUID(bridge, device);
if (thingUID != null) {
thingRemoved(thingUID);
} else {
logger.warn("Removed Tellstick! device is unsupported: type '{}' with id '{}'", device.getDeviceType(),
device.getId());
}
}
private ThingUID getThingUID(Bridge bridge, Device device) {
ThingUID thingUID = null;
switch (device.getDeviceType()) {
case SENSOR:
ThingTypeUID sensorThingId = findSensorType(device);
thingUID = new ThingUID(sensorThingId, bridge.getUID(), device.getUUId());
break;
case DEVICE:
if (device instanceof DimmableDevice) {
thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
device.getUUId());
} else if (device instanceof SwitchableDevice) {
thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
device.getUUId());
} else if (device instanceof TellstickNetDevice) {
if ((((TellstickNetDevice) device).getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
device.getUUId());
} else {
thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
device.getUUId());
}
}
break;
default:
break;
}
return thingUID;
}
private ThingTypeUID findSensorType(Device device) {
logger.debug("Device: {}", device);
ThingTypeUID sensorThingId;
if (device instanceof TellstickSensor) {
TellstickSensor sensor = (TellstickSensor) device;
logger.debug("Sensor: {}", device);
if (sensor.getData(DataType.WINDAVERAGE) != null || sensor.getData(DataType.WINDGUST) != null
|| sensor.getData(DataType.WINDDIRECTION) != null) {
sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;
} else if (sensor.getData(DataType.RAINTOTAL) != null || sensor.getData(DataType.RAINRATE) != null) {
sensorThingId = TellstickBindingConstants.RAINSENSOR_THING_TYPE;
} else {
sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
}
} else {
TellstickNetSensor sensor = (TellstickNetSensor) device;
if (sensor.isSensorOfType(LiveDataType.WINDAVERAGE) || sensor.isSensorOfType(LiveDataType.WINDDIRECTION)
|| sensor.isSensorOfType(LiveDataType.WINDGUST)) {
sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;
} else if (sensor.isSensorOfType(LiveDataType.RAINRATE) || sensor.isSensorOfType(LiveDataType.RAINTOTAL)) {
sensorThingId = TellstickBindingConstants.RAINSENSOR_THING_TYPE;
} else if (sensor.isSensorOfType(LiveDataType.WATT)) {
sensorThingId = TellstickBindingConstants.POWERSENSOR_THING_TYPE;
} else {
sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
}
}
return sensorThingId;
}
public void addBridgeHandler(TelldusBridgeHandler tellstickBridgeHandler) {
telldusBridgeHandlers.add(tellstickBridgeHandler);
tellstickBridgeHandler.registerDeviceStatusListener(this);
}
}