]> git.basschouten.com Git - openhab-addons.git/blob
e59f2edcdcc2cd3a11ab6809784fb842839c825c
[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.avmfritz.internal.handler;
14
15 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
16
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
24 import org.openhab.binding.avmfritz.internal.dto.ButtonModel;
25 import org.openhab.binding.avmfritz.internal.dto.DeviceModel;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.CommonTriggerEvents;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.types.UnDefType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Handler for a FRITZ! buttons. Handles commands, which are sent to one of the channels.
37  *
38  * @author Christoph Weitkamp - Initial contribution
39  */
40 @NonNullByDefault
41 public class AVMFritzButtonHandler extends DeviceHandler {
42
43     private final Logger logger = LoggerFactory.getLogger(AVMFritzButtonHandler.class);
44     /**
45      * keeps track of the last timestamp for handling trigger events
46      */
47     private Instant lastTimestamp;
48
49     /**
50      * Constructor
51      *
52      * @param thing Thing object representing a FRITZ! button
53      */
54     public AVMFritzButtonHandler(Thing thing) {
55         super(thing);
56         lastTimestamp = Instant.now();
57     }
58
59     @Override
60     public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
61         if (thing.getUID().equals(thingUID)) {
62             super.onDeviceUpdated(thingUID, device);
63
64             if (device instanceof DeviceModel) {
65                 DeviceModel deviceModel = (DeviceModel) device;
66                 if (deviceModel.isHANFUNButton()) {
67                     updateHANFUNButton(deviceModel.getButtons());
68                 }
69                 if (deviceModel.isButton()) {
70                     updateShortLongPressButton(deviceModel.getButtons());
71                     updateBattery(deviceModel);
72                 }
73             }
74         }
75     }
76
77     private void updateShortLongPressButton(List<ButtonModel> buttons) {
78         ButtonModel shortPressButton = buttons.size() > 0 ? buttons.get(0) : null;
79         ButtonModel longPressButton = buttons.size() > 1 ? buttons.get(1) : null;
80         ButtonModel lastPressedButton = shortPressButton != null && (longPressButton == null
81                 || shortPressButton.getLastpressedtimestamp() > longPressButton.getLastpressedtimestamp())
82                         ? shortPressButton
83                         : longPressButton;
84         if (lastPressedButton != null) {
85             updateButton(lastPressedButton,
86                     lastPressedButton.equals(shortPressButton) ? CommonTriggerEvents.SHORT_PRESSED
87                             : CommonTriggerEvents.LONG_PRESSED);
88         }
89     }
90
91     private void updateHANFUNButton(List<ButtonModel> buttons) {
92         if (!buttons.isEmpty()) {
93             updateButton(buttons.get(0), CommonTriggerEvents.PRESSED);
94         }
95     }
96
97     private void updateButton(ButtonModel buttonModel, String event) {
98         int lastPressedTimestamp = buttonModel.getLastpressedtimestamp();
99         if (lastPressedTimestamp == 0) {
100             updateThingChannelState(CHANNEL_LAST_CHANGE, UnDefType.UNDEF);
101         } else {
102             ZonedDateTime timestamp = ZonedDateTime.ofInstant(Instant.ofEpochSecond(lastPressedTimestamp),
103                     ZoneId.systemDefault());
104             Instant then = timestamp.toInstant();
105             // Avoid dispatching events if "lastpressedtimestamp" is older than now "lastTimestamp" (e.g. during
106             // restart)
107             if (then.isAfter(lastTimestamp)) {
108                 lastTimestamp = then;
109                 triggerThingChannel(CHANNEL_PRESS, event);
110             }
111             updateThingChannelState(CHANNEL_LAST_CHANGE, new DateTimeType(timestamp));
112         }
113     }
114
115     /**
116      * Triggers thing channels.
117      *
118      * @param channelId ID of the channel to be triggered.
119      * @param event Event to emit
120      */
121     private void triggerThingChannel(String channelId, String event) {
122         Channel channel = thing.getChannel(channelId);
123         if (channel != null) {
124             triggerChannel(channel.getUID(), event);
125         } else {
126             logger.debug("Channel '{}' in thing '{}' does not exist.", channelId, thing.getUID());
127         }
128     }
129 }