2 * Copyright (c) 2010-2023 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.fineoffsetweatherstation.internal.domain.response;
15 import static org.openhab.binding.fineoffsetweatherstation.internal.Utils.toUInt8;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
21 * The status of the sensors' battery.
23 * @author Andreas Berger - Initial contribution
26 public class BatteryStatus {
30 * 1: BATT low, 0: normal
35 * level0~5,{@literal <=1} for BATT low
40 * level0~6,{@literal <=1} for BATT low, 6 = dc power supply
50 * val*0.02V if {@literal v<=1.2V} BATT low
55 private @Nullable Integer level;
56 private @Nullable Double voltage;
57 private final boolean low;
60 public BatteryStatus(Type type, byte data) {
61 int value = toUInt8(data);
76 case VOLTAGE_BROAD_STEPS:
77 this.voltage = voltage = value * 0.1;
80 case VOLTAGE_FINE_STEPS:
81 this.voltage = voltage = value * 0.02;
85 throw new IllegalArgumentException("Unsupported type " + type);
90 * @return level 0 - 5 or null f not available
92 public @Nullable Integer getLevel() {
97 * @return voltage of the battery or null if not available
99 public @Nullable Double getVoltage() {
104 * @return true, if the battery is low
106 public boolean isLow() {
111 * @return true, if device is DC connected
113 public boolean isDc() {
117 public @Nullable Integer getPercentage() {
121 Integer currentLevel = level;
122 if (currentLevel != null) {
123 return (currentLevel * 100 / 5);
129 public String toString() {
130 String status = low ? "LOW" : "OK";
132 return "DC connected";
134 if (voltage != null) {
135 return "Battery " + voltage + " V " + status;
138 return "Battery " + level + "/ 5" + " " + status;
140 return "Battery " + status;