]> git.basschouten.com Git - openhab-addons.git/blob
12d22b85360d296d5510a00fe1e75222a58025a3
[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 static org.openhab.binding.mihome.internal.XiaomiGatewayBindingConstants.*;
16
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.types.Command;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonObject;
26
27 /**
28  * Abstract base class for battery powered Xiaomi sensor devices
29  *
30  * @author Dieter Schmidt - Initial contribution
31  */
32 public abstract class XiaomiSensorBaseHandler extends XiaomiDeviceBaseHandler {
33
34     private static final int VOLTAGE_MAX_MILLIVOLTS = 3100;
35     private static final int VOLTAGE_MIN_MILLIVOLTS = 2700;
36     private static final int BATT_LEVEL_LOW = 20;
37     private static final int BATT_LEVEL_LOW_HYS = 5;
38
39     private static final String STATUS = "status";
40     private static final String VOLTAGE = "voltage";
41
42     private boolean isBatteryLow = false;
43
44     private final Logger logger = LoggerFactory.getLogger(XiaomiSensorBaseHandler.class);
45
46     public XiaomiSensorBaseHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     void parseHeartbeat(JsonObject data) {
52         parseDefault(data);
53     }
54
55     @Override
56     void parseReadAck(JsonObject data) {
57         parseDefault(data);
58     }
59
60     @Override
61     void parseDefault(JsonObject data) {
62         if (data.get(VOLTAGE) != null) {
63             Integer voltage = data.get(VOLTAGE).getAsInt();
64             calculateBatteryLevelFromVoltage(voltage);
65         }
66         if (data.get(STATUS) != null) {
67             logger.trace(
68                     "Got status {} - Apart from \"report\" all other status updates for sensors seem not right (Firmware 1.4.1.145)",
69                     data.get(STATUS));
70         }
71     }
72
73     void calculateBatteryLevelFromVoltage(Integer voltage) {
74         int limVoltage = voltage;
75         limVoltage = Math.min(VOLTAGE_MAX_MILLIVOLTS, limVoltage);
76         limVoltage = Math.max(VOLTAGE_MIN_MILLIVOLTS, limVoltage);
77         Integer battLevel = (int) ((float) (limVoltage - VOLTAGE_MIN_MILLIVOLTS)
78                 / (float) (VOLTAGE_MAX_MILLIVOLTS - VOLTAGE_MIN_MILLIVOLTS) * 100);
79         updateState(CHANNEL_BATTERY_LEVEL, new DecimalType(battLevel));
80         int lowThreshold = isBatteryLow ? BATT_LEVEL_LOW + BATT_LEVEL_LOW_HYS : BATT_LEVEL_LOW;
81         if (battLevel <= lowThreshold) {
82             updateState(CHANNEL_LOW_BATTERY, OnOffType.ON);
83             isBatteryLow = true;
84         } else {
85             updateState(CHANNEL_LOW_BATTERY, OnOffType.OFF);
86             isBatteryLow = false;
87         }
88     }
89
90     @Override
91     void execute(ChannelUID channelUID, Command command) {
92         logger.warn("Cannot execute command - Sensors by definition only have read only channels");
93     }
94 }