]> git.basschouten.com Git - openhab-addons.git/blob
461791abbcbf3488953d01bb733b3a3f270165b6
[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.boschshc.internal.services.batterylevel;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
18 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Fault;
19 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Faults;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.UnDefType;
24
25 /**
26  * Possible battery levels.
27  *
28  * @author David Pace - Initial contribution
29  *
30  */
31 @NonNullByDefault
32 public enum BatteryLevel {
33     OK,
34     LOW_BATTERY,
35     CRITICAL_LOW,
36     CRITICALLY_LOW_BATTERY,
37     NOT_AVAILABLE;
38
39     /**
40      * Derives a battery level by analyzing the fault elements in the given device service data.
41      * <p>
42      * Note that no fault elements are present when the battery level is OK.
43      *
44      * @param deviceServiceData a device service data model
45      * @return the derived battery level
46      */
47     public static BatteryLevel fromDeviceServiceData(DeviceServiceData deviceServiceData) {
48         Faults faults = deviceServiceData.faults;
49         if (faults == null || faults.entries == null || faults.entries.isEmpty()) {
50             return OK;
51         }
52
53         for (Fault faultEntry : faults.entries) {
54             if ("warning".equalsIgnoreCase(faultEntry.category)) {
55                 BatteryLevel batteryLevelState = BatteryLevel.get(faultEntry.type);
56                 if (batteryLevelState != null) {
57                     return batteryLevelState;
58                 }
59             }
60         }
61
62         return OK;
63     }
64
65     /**
66      * Returns the corresponding battery level for the given string or <code>null</code> if no state matches.
67      *
68      * @param identifier the battery level identifier
69      *
70      * @return the matching battery level or <code>null</code>
71      */
72     public static @Nullable BatteryLevel get(String identifier) {
73         for (BatteryLevel batteryLevelState : values()) {
74             if (batteryLevelState.toString().equalsIgnoreCase(identifier)) {
75                 return batteryLevelState;
76             }
77         }
78
79         return null;
80     }
81
82     /**
83      * Transforms a Bosch-specific battery level to a percentage for the <code>system.battery-level</code> channel.
84      *
85      * @return a percentage between 0 and 100 as integer
86      */
87     public State toState() {
88         switch (this) {
89             case LOW_BATTERY:
90                 return new DecimalType(10);
91             case CRITICAL_LOW, CRITICALLY_LOW_BATTERY:
92                 return new DecimalType(1);
93             case NOT_AVAILABLE:
94                 return UnDefType.UNDEF;
95             default:
96                 return new DecimalType(100);
97         }
98     }
99
100     /**
101      * Transforms a Bosch-specific battery level to an <code>ON</code>/<code>OFF</code> state for the
102      * <code>system.low-battery</code> channel.
103      * <p>
104      * If the result is <code>ON</code>, the battery is low; if the result is <code>OFF</code> the battery level is OK.
105      *
106      * @return
107      */
108     public OnOffType toLowBatteryState() {
109         switch (this) {
110             case LOW_BATTERY, CRITICAL_LOW, CRITICALLY_LOW_BATTERY:
111                 return OnOffType.ON;
112             default:
113                 return OnOffType.OFF;
114         }
115     }
116 }