]> git.basschouten.com Git - openhab-addons.git/blob
433565a29a9a5607e991270ff705cf7335ff5ce0
[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.fineoffsetweatherstation.internal.domain.response;
14
15 import static org.openhab.binding.fineoffsetweatherstation.internal.Utils.toUInt8;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19
20 /**
21  * The status of the sensors' battery.
22  *
23  * @author Andreas Berger - Initial contribution
24  */
25 @NonNullByDefault
26 public class BatteryStatus {
27
28     public enum Type {
29         /**
30          * 1: BATT low, 0: normal
31          */
32         LOW_HIGH,
33
34         /**
35          * level0~5,{@literal <=1} for BATT low
36          */
37         LEVEL,
38
39         /**
40          * level0~6,{@literal <=1} for BATT low, 6 = dc power supply
41          */
42         LEVEL_OR_DC,
43
44         /**
45          * val * 0.1v
46          */
47         VOLTAGE_BROAD_STEPS,
48
49         /**
50          * val*0.02V if {@literal v<=1.2V} BATT low
51          */
52         VOLTAGE_FINE_STEPS
53     }
54
55     private @Nullable Integer level;
56     private @Nullable Double voltage;
57     private final boolean low;
58     private boolean dc;
59
60     public BatteryStatus(Type type, byte data) {
61         int value = toUInt8(data);
62         double voltage;
63         switch (type) {
64             case LOW_HIGH:
65                 low = value == 1;
66                 break;
67             case LEVEL:
68                 level = value;
69                 low = value <= 1;
70                 break;
71             case LEVEL_OR_DC:
72                 dc = value == 6;
73                 level = value;
74                 low = value <= 1;
75                 break;
76             case VOLTAGE_BROAD_STEPS:
77                 this.voltage = voltage = value * 0.1;
78                 low = voltage <= 1.2;
79                 break;
80             case VOLTAGE_FINE_STEPS:
81                 this.voltage = voltage = value * 0.02;
82                 low = voltage <= 1.2;
83                 break;
84             default:
85                 throw new IllegalArgumentException("Unsupported type " + type);
86         }
87     }
88
89     /**
90      * @return level 0 - 5 or null f not available
91      */
92     public @Nullable Integer getLevel() {
93         return level;
94     }
95
96     /**
97      * @return voltage of the battery or null if not available
98      */
99     public @Nullable Double getVoltage() {
100         return voltage;
101     }
102
103     /**
104      * @return true, if the battery is low
105      */
106     public boolean isLow() {
107         return low;
108     }
109
110     /**
111      * @return true, if device is DC connected
112      */
113     public boolean isDc() {
114         return dc;
115     }
116
117     public @Nullable Integer getPercentage() {
118         if (dc) {
119             return 100;
120         }
121         Integer currentLevel = level;
122         if (currentLevel != null) {
123             return (currentLevel * 100 / 5);
124         }
125         return null;
126     }
127
128     @Override
129     public String toString() {
130         String status = low ? "LOW" : "OK";
131         if (dc) {
132             return "DC connected";
133         }
134         if (voltage != null) {
135             return "Battery " + voltage + " V " + status;
136         }
137         if (level != null) {
138             return "Battery " + level + "/ 5" + " " + status;
139         }
140         return "Battery " + status;
141     }
142 }