2 * Copyright (c) 2010-2022 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.handler;
15 import static org.openhab.binding.tellstick.internal.TellstickBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Calendar;
22 import org.openhab.binding.tellstick.internal.TellstickBindingConstants;
23 import org.openhab.binding.tellstick.internal.live.xml.DataTypeValue;
24 import org.openhab.binding.tellstick.internal.live.xml.TellstickNetSensor;
25 import org.openhab.binding.tellstick.internal.live.xml.TellstickNetSensorEvent;
26 import org.openhab.binding.tellstick.internal.local.dto.LocalDataTypeValueDTO;
27 import org.openhab.binding.tellstick.internal.local.dto.TellstickLocalSensorDTO;
28 import org.openhab.binding.tellstick.internal.local.dto.TellstickLocalSensorEventDTO;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.library.types.DateTimeType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.PercentType;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.library.unit.SIUnits;
35 import org.openhab.core.library.unit.Units;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.openhab.core.thing.ThingStatusInfo;
42 import org.openhab.core.thing.binding.BaseThingHandler;
43 import org.openhab.core.types.Command;
44 import org.openhab.core.types.RefreshType;
45 import org.openhab.core.types.State;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.tellstick.device.TellstickDeviceEvent;
49 import org.tellstick.device.TellstickException;
50 import org.tellstick.device.TellstickSensor;
51 import org.tellstick.device.TellstickSensorEvent;
52 import org.tellstick.device.iface.Device;
53 import org.tellstick.device.iface.DimmableDevice;
54 import org.tellstick.device.iface.TellstickEvent;
55 import org.tellstick.enums.DataType;
56 import org.tellstick.enums.DeviceType;
59 * Handler for telldus and tellstick devices. This sends the commands to the correct bridge.
61 * @author Jarle Hjortland - Initial contribution
63 public class TelldusDevicesHandler extends BaseThingHandler implements DeviceStatusListener {
65 private Logger logger = LoggerFactory.getLogger(TelldusDevicesHandler.class);
66 private String deviceId;
67 private Boolean isDimmer = Boolean.FALSE;
68 private int resend = 1;
69 private TelldusBridgeHandler bridgeHandler = null;
70 private final ChannelUID stateChannel;
71 private final ChannelUID dimChannel;
72 private final ChannelUID humidityChannel;
73 private final ChannelUID tempChannel;
74 private final ChannelUID raintTotChannel;
75 private final ChannelUID rainRateChannel;
76 private final ChannelUID windAverageChannel;
77 private final ChannelUID windDirectionChannel;
78 private final ChannelUID windGuestChannel;
79 private final ChannelUID wattChannel;
80 private final ChannelUID ampereChannel;
81 private final ChannelUID luxChannel;
82 private final ChannelUID timestampChannel;
84 public TelldusDevicesHandler(Thing thing) {
86 stateChannel = new ChannelUID(getThing().getUID(), CHANNEL_STATE);
87 dimChannel = new ChannelUID(getThing().getUID(), CHANNEL_DIMMER);
88 humidityChannel = new ChannelUID(getThing().getUID(), CHANNEL_HUMIDITY);
89 tempChannel = new ChannelUID(getThing().getUID(), CHANNEL_TEMPERATURE);
90 raintTotChannel = new ChannelUID(getThing().getUID(), CHANNEL_RAINTOTAL);
91 rainRateChannel = new ChannelUID(getThing().getUID(), CHANNEL_RAINRATE);
92 windAverageChannel = new ChannelUID(getThing().getUID(), CHANNEL_WINDAVERAGE);
93 windDirectionChannel = new ChannelUID(getThing().getUID(), CHANNEL_WINDDIRECTION);
94 windGuestChannel = new ChannelUID(getThing().getUID(), CHANNEL_WINDGUST);
95 wattChannel = new ChannelUID(getThing().getUID(), CHANNEL_WATT);
96 ampereChannel = new ChannelUID(getThing().getUID(), CHANNEL_AMPERE);
97 timestampChannel = new ChannelUID(getThing().getUID(), CHANNEL_TIMESTAMP);
98 luxChannel = new ChannelUID(getThing().getUID(), CHANNEL_LUX);
102 public void handleCommand(ChannelUID channelUID, Command command) {
103 logger.debug("Handle event {} for {}", command, channelUID);
104 TelldusBridgeHandler bridgeHandler = getTellstickBridgeHandler();
105 if (bridgeHandler == null) {
106 logger.warn("Tellstick bridge handler not found. Cannot handle command without bridge.");
109 Device dev = getDevice(bridgeHandler, deviceId);
112 logger.warn("Device not found. Can't send command to device '{}'", deviceId);
115 if (command instanceof RefreshType) {
116 Bridge bridge = getBridge();
117 if (bridge != null) {
118 TelldusBridgeHandler localBridgeHandler = (TelldusBridgeHandler) bridge.getHandler();
119 if (localBridgeHandler != null) {
120 localBridgeHandler.handleCommand(channelUID, command);
126 if (channelUID.getId().equals(CHANNEL_DIMMER) || channelUID.getId().equals(CHANNEL_STATE)) {
128 if (dev.getDeviceType() == DeviceType.DEVICE) {
129 getTellstickBridgeHandler().getController().handleSendEvent(dev, resend, isDimmer, command);
131 logger.warn("{} is not an updateable device. Read-only", dev);
133 } catch (TellstickException e) {
134 logger.debug("Failed to send command to tellstick", e);
135 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
138 logger.warn("Setting of channel {} not possible. Read-only", channelUID);
142 private void refreshDevice(Device dev) {
143 if (deviceId != null && isSensor()) {
144 updateSensorStates(dev);
145 } else if (deviceId != null) {
146 updateDeviceState(dev);
151 public void initialize() {
152 Configuration config = getConfig();
153 logger.debug("Initialize TelldusDeviceHandler {}. class {}", config, config.getClass());
154 final Object configDeviceId = config.get(TellstickBindingConstants.DEVICE_ID);
155 if (configDeviceId != null) {
156 deviceId = configDeviceId.toString();
158 logger.debug("Initialized TellStick device missing serialNumber configuration... troubles ahead");
159 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
161 final Boolean isADimmer = (Boolean) config.get(TellstickBindingConstants.DEVICE_ISDIMMER);
162 if (isADimmer != null) {
163 this.isDimmer = isADimmer;
165 final BigDecimal repeatCount = (BigDecimal) config.get(TellstickBindingConstants.DEVICE_RESEND_COUNT);
166 if (repeatCount != null) {
167 resend = repeatCount.intValue();
169 Bridge bridge = getBridge();
170 if (bridge != null) {
171 bridgeStatusChanged(bridge.getStatusInfo());
176 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
177 logger.debug("device: {} bridgeStatusChanged: {}", deviceId, bridgeStatusInfo);
178 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
180 Bridge localBridge = getBridge();
181 if (localBridge != null) {
182 TelldusBridgeHandler telldusBridgeHandler = (TelldusBridgeHandler) localBridge.getHandler();
183 logger.debug("Init bridge for {}, bridge:{}", deviceId, telldusBridgeHandler);
184 if (telldusBridgeHandler != null) {
185 this.bridgeHandler = telldusBridgeHandler;
186 this.bridgeHandler.registerDeviceStatusListener(this);
187 Configuration config = editConfiguration();
188 Device dev = getDevice(telldusBridgeHandler, deviceId);
190 if (dev.getName() != null) {
191 config.put(TellstickBindingConstants.DEVICE_NAME, dev.getName());
193 if (dev.getProtocol() != null) {
194 config.put(TellstickBindingConstants.DEVICE_PROTOCOL, dev.getProtocol());
196 if (dev.getModel() != null) {
197 config.put(TellstickBindingConstants.DEVICE_MODEL, dev.getModel());
199 updateConfiguration(config);
201 updateStatus(ThingStatus.ONLINE);
204 "Could not find {}, please make sure it is defined and that telldus service is running",
206 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
210 } catch (Exception e) {
211 logger.error("Failed to init bridge for {}", deviceId, e);
212 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR);
215 updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail());
219 private Device getDevice(TelldusBridgeHandler tellHandler, String deviceId) {
221 if (deviceId != null) {
223 dev = tellHandler.getSensor(deviceId);
225 dev = tellHandler.getDevice(deviceId);
226 updateDeviceState(dev);
232 private boolean isSensor() {
233 return (getThing().getThingTypeUID().equals(TellstickBindingConstants.SENSOR_THING_TYPE)
234 || getThing().getThingTypeUID().equals(TellstickBindingConstants.RAINSENSOR_THING_TYPE)
235 || getThing().getThingTypeUID().equals(TellstickBindingConstants.WINDSENSOR_THING_TYPE)
236 || getThing().getThingTypeUID().equals(TellstickBindingConstants.POWERSENSOR_THING_TYPE));
239 private void updateSensorStates(Device dev) {
240 if (dev instanceof TellstickSensor) {
241 updateStatus(ThingStatus.ONLINE);
242 for (DataType type : ((TellstickSensor) dev).getData().keySet()) {
243 updateSensorDataState(type, ((TellstickSensor) dev).getData(type));
245 } else if (dev instanceof TellstickNetSensor) {
246 if (((TellstickNetSensor) dev).getOnline()) {
247 updateStatus(ThingStatus.ONLINE);
249 updateStatus(ThingStatus.OFFLINE);
251 for (DataTypeValue type : ((TellstickNetSensor) dev).getData()) {
252 updateSensorDataState(type);
254 } else if (dev instanceof TellstickLocalSensorDTO) {
255 for (LocalDataTypeValueDTO type : ((TellstickLocalSensorDTO) dev).getData()) {
256 updateSensorDataState(type);
261 private synchronized TelldusBridgeHandler getTellstickBridgeHandler() {
262 if (this.bridgeHandler == null) {
263 logger.debug("No available bridge handler found for {} bridge {} .", deviceId, getBridge());
265 return this.bridgeHandler;
269 public void onDeviceStateChanged(Bridge bridge, Device device, TellstickEvent event) {
270 logger.debug("Updating states of ({} {} ({}) id: {} or {}", device.getDeviceType(), device.getName(),
271 device.getUUId(), getThing().getUID(), deviceId);
272 if (device.getUUId().equals(deviceId)) {
273 if (event instanceof TellstickDeviceEvent) {
274 updateDeviceState(device);
275 } else if (event instanceof TellstickNetSensorEvent) {
276 TellstickNetSensorEvent sensorevent = (TellstickNetSensorEvent) event;
277 updateSensorDataState(sensorevent.getDataTypeValue());
278 } else if (event instanceof TellstickLocalSensorEventDTO) {
279 TellstickLocalSensorEventDTO sensorevent = (TellstickLocalSensorEventDTO) event;
280 updateSensorDataState(sensorevent.getDataTypeValue());
281 } else if (event instanceof TellstickSensorEvent) {
282 TellstickSensorEvent sensorevent = (TellstickSensorEvent) event;
283 updateSensorDataState(sensorevent.getDataType(), sensorevent.getData());
285 logger.debug("Unhandled Device {}.", device.getDeviceType());
287 Calendar cal = Calendar.getInstance();
288 cal.setTimeInMillis(event.getTimestamp());
289 updateState(timestampChannel,
290 new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault())));
294 private void updateSensorDataState(DataType dataType, String data) {
297 updateState(humidityChannel, new QuantityType<>(new BigDecimal(data), HUMIDITY_UNIT));
300 updateState(tempChannel, new QuantityType<>(new BigDecimal(data), SIUnits.CELSIUS));
303 updateState(rainRateChannel, new QuantityType<>(new BigDecimal(data), RAIN_UNIT));
306 updateState(raintTotChannel, new QuantityType<>(new BigDecimal(data), RAIN_UNIT));
309 updateState(windAverageChannel, new QuantityType<>(new BigDecimal(data), WIND_SPEED_UNIT_MS));
312 updateState(windDirectionChannel, new QuantityType<>(new BigDecimal(data), WIND_DIRECTION_UNIT));
315 updateState(windGuestChannel, new QuantityType<>(new BigDecimal(data), WIND_SPEED_UNIT_MS));
321 private void updateSensorDataState(DataTypeValue dataType) {
322 switch (dataType.getName()) {
324 updateState(humidityChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), HUMIDITY_UNIT));
327 updateState(tempChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), SIUnits.CELSIUS));
330 updateState(rainRateChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
333 updateState(raintTotChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
336 updateState(windAverageChannel,
337 new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
340 updateState(windDirectionChannel,
341 new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_DIRECTION_UNIT));
344 updateState(windGuestChannel,
345 new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
348 if (dataType.getUnit() != null && dataType.getUnit().equals("A")) {
349 updateState(ampereChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), ELECTRIC_UNIT));
351 updateState(wattChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), POWER_UNIT));
355 updateState(luxChannel, new QuantityType<>(new DecimalType(dataType.getValue()), LUX_UNIT));
361 private void updateSensorDataState(LocalDataTypeValueDTO dataType) {
362 switch (dataType.getName()) {
364 updateState(humidityChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), HUMIDITY_UNIT));
367 updateState(tempChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), SIUnits.CELSIUS));
370 updateState(rainRateChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
373 updateState(raintTotChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
376 updateState(windAverageChannel,
377 new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
380 updateState(windDirectionChannel,
381 new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_DIRECTION_UNIT));
384 updateState(windGuestChannel,
385 new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
388 if (dataType.getScale() == 5) {
389 updateState(ampereChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), ELECTRIC_UNIT));
390 } else if (dataType.getScale() == 2) {
391 updateState(wattChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), Units.WATT));
395 updateState(luxChannel, new QuantityType<>(new DecimalType(dataType.getValue()), LUX_UNIT));
401 private void updateDeviceState(Device device) {
402 if (device != null) {
403 logger.debug("Updating state of {} {} ({}) id: {}", device.getDeviceType(), device.getName(),
404 device.getUUId(), getThing().getUID());
405 TelldusBridgeHandler bridgeHandler = getTellstickBridgeHandler();
407 if (bridgeHandler != null && bridgeHandler.getController() != null) {
408 st = bridgeHandler.getController().calcState(device);
410 if (st != null && bridgeHandler != null) {
411 BigDecimal dimValue = bridgeHandler.getController().calcDimValue(device);
412 updateState(stateChannel, st);
413 if (device instanceof DimmableDevice) {
414 updateState(dimChannel, new PercentType(dimValue));
416 updateStatus(ThingStatus.ONLINE);
418 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
421 updateStatus(ThingStatus.REMOVED);
426 public void onDeviceRemoved(Bridge bridge, Device device) {
427 if (device.getUUId().equals(deviceId)) {
428 updateStatus(ThingStatus.REMOVED);
433 public void onDeviceAdded(Bridge bridge, Device device) {