]> git.basschouten.com Git - openhab-addons.git/blob
91ef5de5f058b1ba2824e32147bc6381d0364567
[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.mihome.internal.handler;
14
15 import java.util.Timer;
16 import java.util.TimerTask;
17
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.thing.Thing;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link XiaomiSensorBaseHandlerWithTimer} is an abstract class for sensor devices
25  * which use a timer to update a certain channel. The user can configure the timer via an item
26  * value.
27  *
28  * @author Dieter Schmidt - Initial contribution
29  *
30  */
31 public abstract class XiaomiSensorBaseHandlerWithTimer extends XiaomiSensorBaseHandler {
32
33     private int defaultTimer;
34     private int minTimer;
35     private Integer timerSetpoint;
36     private final String setpointChannel;
37     private boolean timerIsRunning;
38     private Timer trigger = new Timer();
39
40     private final Logger logger = LoggerFactory.getLogger(XiaomiSensorBaseHandlerWithTimer.class);
41
42     public XiaomiSensorBaseHandlerWithTimer(Thing thing, int defaultTimer, int minTimer, String setpointChannel) {
43         super(thing);
44         this.defaultTimer = defaultTimer;
45         this.minTimer = minTimer;
46         this.timerSetpoint = defaultTimer;
47         this.setpointChannel = setpointChannel;
48     }
49
50     class TimerAction extends TimerTask {
51         @Override
52         public synchronized void run() {
53             onTimer();
54             timerIsRunning = false;
55         }
56     }
57
58     synchronized void startTimer() {
59         cancelRunningTimer();
60         logger.debug("Setting timer to {}s", timerSetpoint);
61         trigger.schedule(new TimerAction(), timerSetpoint * 1000);
62         timerIsRunning = true;
63     }
64
65     synchronized void cancelRunningTimer() {
66         if (timerIsRunning) {
67             trigger.cancel();
68             logger.debug("Cancelled running timer");
69             trigger = new Timer();
70             timerIsRunning = false;
71         }
72     }
73
74     abstract void onTimer();
75
76     void setTimerFromDecimalType(DecimalType value) {
77         try {
78             int newValue = value.intValue();
79             timerSetpoint = newValue < minTimer ? minTimer : newValue;
80             if (timerSetpoint == minTimer) {
81                 updateState(setpointChannel, new DecimalType(timerSetpoint));
82             }
83         } catch (NumberFormatException e) {
84             logger.debug("Cannot parse the value {} to an Integer", value);
85             timerSetpoint = defaultTimer;
86         }
87     }
88 }