]> git.basschouten.com Git - openhab-addons.git/blob
83c9066be5e5dbd6eb9cca4e4b52f8212a2b7132
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.touchwand.internal;
14
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.CHANNEL_WALLCONTROLLER_ACTION;
16
17 import java.time.Instant;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.touchwand.internal.dto.Csc;
21 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
22 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitDataWallController;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25
26 /**
27  * The {@link TouchWandWallControllerHandler} is responsible for handling commands and triggers
28  *
29  * for WallController units
30  *
31  * @author Roie Geron - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class TouchWandWallControllerHandler extends TouchWandBaseUnitHandler {
36
37     private long timeLastEventMs;
38     private static final int ADJACENT_EVENT_FILTER_TIME_MILLISEC = 2000; // 2 seconds
39
40     public TouchWandWallControllerHandler(Thing thing) {
41         super(thing);
42         timeLastEventMs = Instant.now().toEpochMilli();
43     }
44
45     @Override
46     void touchWandUnitHandleCommand(Command command) {
47     }
48
49     @Override
50     void updateTouchWandUnitState(TouchWandUnitData unitData) {
51         if (unitData instanceof TouchWandUnitDataWallController) {
52             Csc status = ((TouchWandUnitDataWallController) unitData).getCurrStatus();
53             long ts = status.getTs();
54             long timeDiff = ts - timeLastEventMs;
55             if ((timeDiff) > ADJACENT_EVENT_FILTER_TIME_MILLISEC) {
56                 int value = status.getKeyAttr();
57                 String action = (value <= 100) ? "SHORT" : "LONG";
58                 triggerChannel(CHANNEL_WALLCONTROLLER_ACTION, action);
59             }
60             timeLastEventMs = status.getTs();
61         } else {
62             logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance");
63         }
64     }
65 }