]> git.basschouten.com Git - openhab-addons.git/blob
8d67f4a96237d6a4c8ee7fbfab2c7093b2417c7f
[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.tradfri.internal.model;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Time;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonPrimitive;
33
34 /**
35  * The {@link TradfriAirPurifierData} class is a Java wrapper for the raw JSON data about the air purifier state.
36  *
37  * @author Vivien Boistuaud - Initial contribution
38  */
39 @NonNullByDefault
40 public class TradfriAirPurifierData extends TradfriDeviceData {
41
42     private final Logger logger = LoggerFactory.getLogger(TradfriAirPurifierData.class);
43
44     public TradfriAirPurifierData() {
45         super(AIR_PURIFIER);
46     }
47
48     public TradfriAirPurifierData(JsonElement json) {
49         super(AIR_PURIFIER, json);
50     }
51
52     public String getJsonString() {
53         return root.toString();
54     }
55
56     public @Nullable DecimalType getFanMode() {
57         JsonElement fanMode = attributes.get(FAN_MODE);
58         if (fanMode != null) {
59             int modeValue = fanMode.getAsInt();
60             if (AIR_PURIFIER_FANMODE.contains(modeValue)) {
61                 return new DecimalType(modeValue);
62             } else {
63                 logger.debug("Invalid speedMode is '{}': unknown value", modeValue);
64                 return null;
65             }
66         } else {
67             return null;
68         }
69     }
70
71     public TradfriAirPurifierData setFanMode(Number speedValue) {
72         int speed = speedValue.intValue();
73         if (AIR_PURIFIER_FANMODE.contains(speed)) {
74             attributes.add(FAN_MODE, new JsonPrimitive(speed));
75         } else {
76             logger.debug("Could not set fanMode to '{}': unknown value", speed);
77         }
78         return this;
79     }
80
81     public @Nullable DecimalType getFanSpeed() {
82         JsonElement fanSpeed = attributes.get(FAN_SPEED);
83         if (fanSpeed != null) {
84             int speedValue = fanSpeed.getAsInt();
85             return new DecimalType(speedValue);
86         } else {
87             return null;
88         }
89     }
90
91     public @Nullable OnOffType getDisableLed() {
92         JsonElement ledOnOff = attributes.get(LED_DISABLE);
93         if (ledOnOff != null) {
94             boolean ledStatus = ledOnOff.getAsInt() != 0;
95             return OnOffType.from(ledStatus);
96         } else {
97             return null;
98         }
99     }
100
101     public TradfriAirPurifierData setDisableLed(OnOffType disableOnOff) {
102         attributes.add(LED_DISABLE, new JsonPrimitive(OnOffType.ON.equals(disableOnOff) ? 1 : 0));
103         return this;
104     }
105
106     public @Nullable OnOffType getLockPhysicalButton() {
107         JsonElement lockPhysicalButton = attributes.get(LOCK_PHYSICAL_BUTTON);
108         if (lockPhysicalButton != null) {
109             boolean isLocked = lockPhysicalButton.getAsInt() != 0;
110             return OnOffType.from(isLocked);
111         } else {
112             return null;
113         }
114     }
115
116     public TradfriAirPurifierData setLockPhysicalButton(OnOffType lockPhysicalButton) {
117         attributes.add(LOCK_PHYSICAL_BUTTON, new JsonPrimitive(OnOffType.ON.equals(lockPhysicalButton) ? 1 : 0));
118         return this;
119     }
120
121     public @Nullable State getAirQualityPM25() {
122         JsonElement airQuality = attributes.get(AIR_QUALITY);
123         if (airQuality != null) {
124             int pm25InPpm = airQuality.getAsInt();
125             if (pm25InPpm != AIR_PURIFIER_AIR_QUALITY_UNDEFINED) {
126                 return new QuantityType<Dimensionless>(pm25InPpm, Units.PARTS_PER_MILLION);
127             } else {
128                 return UnDefType.UNDEF;
129             }
130         } else {
131             return null;
132         }
133     }
134
135     public @Nullable State getAirQualityRating() {
136         State pm25State = getAirQualityPM25();
137         if (pm25State != null) {
138             if (pm25State instanceof Number) {
139                 int pm25Value = ((Number) pm25State).intValue();
140                 int qualityRating = 1;
141
142                 if (pm25Value >= AIR_PURIFIER_AIR_QUALITY_BAD) {
143                     qualityRating = 3;
144                 } else if (pm25Value >= AIR_PURIFIER_AIR_QUALITY_OK) {
145                     qualityRating = 2;
146                 }
147
148                 return new DecimalType(qualityRating);
149             }
150             return UnDefType.UNDEF;
151         } else {
152             return null;
153         }
154     }
155
156     public @Nullable QuantityType<Time> getNextFilterCheckTTL() {
157         JsonElement nextFilterCheckTTL = attributes.get(NEXT_FILTER_CHECK);
158         if (nextFilterCheckTTL != null) {
159             int remainingMinutes = nextFilterCheckTTL.getAsInt();
160             return new QuantityType<Time>(remainingMinutes, Units.MINUTE);
161         } else {
162             return null;
163         }
164     }
165
166     public @Nullable OnOffType getFilterCheckAlarm() {
167         QuantityType<Time> ttl = getNextFilterCheckTTL();
168         if (ttl != null) {
169             int ttlValue = ttl.intValue();
170             return ttlValue < 0 ? OnOffType.ON : OnOffType.OFF;
171         } else {
172             return null;
173         }
174     }
175
176     public @Nullable QuantityType<Time> getFilterUptime() {
177         JsonElement filterUptime = attributes.get(FILTER_UPTIME);
178         if (filterUptime != null) {
179             int filterUptimeMinutes = filterUptime.getAsInt();
180             return new QuantityType<Time>(filterUptimeMinutes, Units.MINUTE);
181         } else {
182             return null;
183         }
184     }
185 }