2 * Copyright (c) 2010-2020 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.avmfritz.internal.handler;
15 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.List;
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;
36 * Handler for a FRITZ! buttons. Handles commands, which are sent to one of the channels.
38 * @author Christoph Weitkamp - Initial contribution
41 public class AVMFritzButtonHandler extends DeviceHandler {
43 private final Logger logger = LoggerFactory.getLogger(AVMFritzButtonHandler.class);
45 * keeps track of the last timestamp for handling trigger events
47 private Instant lastTimestamp;
52 * @param thing Thing object representing a FRITZ! button
54 public AVMFritzButtonHandler(Thing thing) {
56 lastTimestamp = Instant.now();
60 public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
61 if (thing.getUID().equals(thingUID)) {
62 super.onDeviceUpdated(thingUID, device);
64 if (device instanceof DeviceModel) {
65 DeviceModel deviceModel = (DeviceModel) device;
66 if (deviceModel.isHANFUNButton()) {
67 updateHANFUNButton(deviceModel.getButtons());
69 if (deviceModel.isButton()) {
70 updateShortLongPressButton(deviceModel.getButtons());
71 updateBattery(deviceModel);
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())
84 if (lastPressedButton != null) {
85 updateButton(lastPressedButton,
86 lastPressedButton.equals(shortPressButton) ? CommonTriggerEvents.SHORT_PRESSED
87 : CommonTriggerEvents.LONG_PRESSED);
91 private void updateHANFUNButton(List<ButtonModel> buttons) {
92 if (!buttons.isEmpty()) {
93 updateButton(buttons.get(0), CommonTriggerEvents.PRESSED);
97 private void updateButton(ButtonModel buttonModel, String event) {
98 int lastPressedTimestamp = buttonModel.getLastpressedtimestamp();
99 if (lastPressedTimestamp == 0) {
100 updateThingChannelState(CHANNEL_LAST_CHANGE, UnDefType.UNDEF);
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
107 if (then.isAfter(lastTimestamp)) {
108 lastTimestamp = then;
109 triggerThingChannel(CHANNEL_PRESS, event);
111 updateThingChannelState(CHANNEL_LAST_CHANGE, new DateTimeType(timestamp));
116 * Triggers thing channels.
118 * @param channelId ID of the channel to be triggered.
119 * @param event Event to emit
121 private void triggerThingChannel(String channelId, String event) {
122 Channel channel = thing.getChannel(channelId);
123 if (channel != null) {
124 triggerChannel(channel.getUID(), event);
126 logger.debug("Channel '{}' in thing '{}' does not exist.", channelId, thing.getUID());