]> git.basschouten.com Git - openhab-addons.git/blob
e1b57874cc7e56846d6119cb23ae733f1f459ed4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.handler;
14
15 import static org.openhab.binding.tellstick.internal.TellstickBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Calendar;
21
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;
57
58 /**
59  * Handler for telldus and tellstick devices. This sends the commands to the correct bridge.
60  *
61  * @author Jarle Hjortland - Initial contribution
62  */
63 public class TelldusDevicesHandler extends BaseThingHandler implements DeviceStatusListener {
64
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;
83
84     public TelldusDevicesHandler(Thing thing) {
85         super(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);
99     }
100
101     @Override
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.");
107             return;
108         }
109         Device dev = getDevice(bridgeHandler, deviceId);
110
111         if (dev == null) {
112             logger.warn("Device not found. Can't send command to device '{}'", deviceId);
113             return;
114         }
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);
121                     refreshDevice(dev);
122                     return;
123                 }
124             }
125         }
126         if (channelUID.getId().equals(CHANNEL_DIMMER) || channelUID.getId().equals(CHANNEL_STATE)) {
127             try {
128                 if (dev.getDeviceType() == DeviceType.DEVICE) {
129                     getTellstickBridgeHandler().getController().handleSendEvent(dev, resend, isDimmer, command);
130                 } else {
131                     logger.warn("{} is not an updateable device. Read-only", dev);
132                 }
133             } catch (TellstickException e) {
134                 logger.debug("Failed to send command to tellstick", e);
135                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
136             }
137         } else {
138             logger.warn("Setting of channel {} not possible. Read-only", channelUID);
139         }
140     }
141
142     private void refreshDevice(Device dev) {
143         if (deviceId != null && isSensor()) {
144             updateSensorStates(dev);
145         } else if (deviceId != null) {
146             updateDeviceState(dev);
147         }
148     }
149
150     @Override
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();
157         } else {
158             logger.debug("Initialized TellStick device missing serialNumber configuration... troubles ahead");
159             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
160         }
161         final Boolean isADimmer = (Boolean) config.get(TellstickBindingConstants.DEVICE_ISDIMMER);
162         if (isADimmer != null) {
163             this.isDimmer = isADimmer;
164         }
165         final BigDecimal repeatCount = (BigDecimal) config.get(TellstickBindingConstants.DEVICE_RESEND_COUNT);
166         if (repeatCount != null) {
167             resend = repeatCount.intValue();
168         }
169         Bridge bridge = getBridge();
170         if (bridge != null) {
171             bridgeStatusChanged(bridge.getStatusInfo());
172         }
173     }
174
175     @Override
176     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
177         logger.debug("device: {} bridgeStatusChanged: {}", deviceId, bridgeStatusInfo);
178         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
179             try {
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);
189                         if (dev != null) {
190                             if (dev.getName() != null) {
191                                 config.put(TellstickBindingConstants.DEVICE_NAME, dev.getName());
192                             }
193                             if (dev.getProtocol() != null) {
194                                 config.put(TellstickBindingConstants.DEVICE_PROTOCOL, dev.getProtocol());
195                             }
196                             if (dev.getModel() != null) {
197                                 config.put(TellstickBindingConstants.DEVICE_MODEL, dev.getModel());
198                             }
199                             updateConfiguration(config);
200
201                             updateStatus(ThingStatus.ONLINE);
202                         } else {
203                             logger.warn(
204                                     "Could not find {}, please make sure it is defined and that telldus service is running",
205                                     deviceId);
206                             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
207                         }
208                     }
209                 }
210             } catch (Exception e) {
211                 logger.error("Failed to init bridge for {}", deviceId, e);
212                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR);
213             }
214         } else {
215             updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail());
216         }
217     }
218
219     private Device getDevice(TelldusBridgeHandler tellHandler, String deviceId) {
220         Device dev = null;
221         if (deviceId != null) {
222             if (isSensor()) {
223                 dev = tellHandler.getSensor(deviceId);
224             } else {
225                 dev = tellHandler.getDevice(deviceId);
226                 updateDeviceState(dev);
227             }
228         }
229         return dev;
230     }
231
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));
237     }
238
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));
244             }
245         } else if (dev instanceof TellstickNetSensor) {
246             if (((TellstickNetSensor) dev).getOnline()) {
247                 updateStatus(ThingStatus.ONLINE);
248             } else {
249                 updateStatus(ThingStatus.OFFLINE);
250             }
251             for (DataTypeValue type : ((TellstickNetSensor) dev).getData()) {
252                 updateSensorDataState(type);
253             }
254         } else if (dev instanceof TellstickLocalSensorDTO) {
255             for (LocalDataTypeValueDTO type : ((TellstickLocalSensorDTO) dev).getData()) {
256                 updateSensorDataState(type);
257             }
258         }
259     }
260
261     private synchronized TelldusBridgeHandler getTellstickBridgeHandler() {
262         if (this.bridgeHandler == null) {
263             logger.debug("No available bridge handler found for {} bridge {} .", deviceId, getBridge());
264         }
265         return this.bridgeHandler;
266     }
267
268     @Override
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());
284             } else {
285                 logger.debug("Unhandled Device {}.", device.getDeviceType());
286             }
287             Calendar cal = Calendar.getInstance();
288             cal.setTimeInMillis(event.getTimestamp());
289             updateState(timestampChannel,
290                     new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault())));
291         }
292     }
293
294     private void updateSensorDataState(DataType dataType, String data) {
295         switch (dataType) {
296             case HUMIDITY:
297                 updateState(humidityChannel, new QuantityType<>(new BigDecimal(data), HUMIDITY_UNIT));
298                 break;
299             case TEMPERATURE:
300                 updateState(tempChannel, new QuantityType<>(new BigDecimal(data), SIUnits.CELSIUS));
301                 break;
302             case RAINRATE:
303                 updateState(rainRateChannel, new QuantityType<>(new BigDecimal(data), RAIN_UNIT));
304                 break;
305             case RAINTOTAL:
306                 updateState(raintTotChannel, new QuantityType<>(new BigDecimal(data), RAIN_UNIT));
307                 break;
308             case WINDAVERAGE:
309                 updateState(windAverageChannel, new QuantityType<>(new BigDecimal(data), WIND_SPEED_UNIT_MS));
310                 break;
311             case WINDDIRECTION:
312                 updateState(windDirectionChannel, new QuantityType<>(new BigDecimal(data), WIND_DIRECTION_UNIT));
313                 break;
314             case WINDGUST:
315                 updateState(windGuestChannel, new QuantityType<>(new BigDecimal(data), WIND_SPEED_UNIT_MS));
316                 break;
317             default:
318         }
319     }
320
321     private void updateSensorDataState(DataTypeValue dataType) {
322         switch (dataType.getName()) {
323             case HUMIDITY:
324                 updateState(humidityChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), HUMIDITY_UNIT));
325                 break;
326             case TEMPERATURE:
327                 updateState(tempChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), SIUnits.CELSIUS));
328                 break;
329             case RAINRATE:
330                 updateState(rainRateChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
331                 break;
332             case RAINTOTAL:
333                 updateState(raintTotChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
334                 break;
335             case WINDAVERAGE:
336                 updateState(windAverageChannel,
337                         new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
338                 break;
339             case WINDDIRECTION:
340                 updateState(windDirectionChannel,
341                         new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_DIRECTION_UNIT));
342                 break;
343             case WINDGUST:
344                 updateState(windGuestChannel,
345                         new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
346                 break;
347             case WATT:
348                 if (dataType.getUnit() != null && dataType.getUnit().equals("A")) {
349                     updateState(ampereChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), ELECTRIC_UNIT));
350                 } else {
351                     updateState(wattChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), POWER_UNIT));
352                 }
353                 break;
354             case LUMINATION:
355                 updateState(luxChannel, new QuantityType<>(new DecimalType(dataType.getValue()), LUX_UNIT));
356                 break;
357             default:
358         }
359     }
360
361     private void updateSensorDataState(LocalDataTypeValueDTO dataType) {
362         switch (dataType.getName()) {
363             case HUMIDITY:
364                 updateState(humidityChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), HUMIDITY_UNIT));
365                 break;
366             case TEMPERATURE:
367                 updateState(tempChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), SIUnits.CELSIUS));
368                 break;
369             case RAINRATE:
370                 updateState(rainRateChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
371                 break;
372             case RAINTOTAL:
373                 updateState(raintTotChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
374                 break;
375             case WINDAVERAGE:
376                 updateState(windAverageChannel,
377                         new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
378                 break;
379             case WINDDIRECTION:
380                 updateState(windDirectionChannel,
381                         new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_DIRECTION_UNIT));
382                 break;
383             case WINDGUST:
384                 updateState(windGuestChannel,
385                         new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
386                 break;
387             case WATT:
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));
392                 }
393                 break;
394             case LUMINATION:
395                 updateState(luxChannel, new QuantityType<>(new DecimalType(dataType.getValue()), LUX_UNIT));
396                 break;
397             default:
398         }
399     }
400
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();
406             State st = null;
407             if (bridgeHandler != null && bridgeHandler.getController() != null) {
408                 st = bridgeHandler.getController().calcState(device);
409             }
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));
415                 }
416                 updateStatus(ThingStatus.ONLINE);
417             } else {
418                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
419             }
420         } else {
421             updateStatus(ThingStatus.REMOVED);
422         }
423     }
424
425     @Override
426     public void onDeviceRemoved(Bridge bridge, Device device) {
427         if (device.getUUId().equals(deviceId)) {
428             updateStatus(ThingStatus.REMOVED);
429         }
430     }
431
432     @Override
433     public void onDeviceAdded(Bridge bridge, Device device) {
434     }
435 }