]> git.basschouten.com Git - openhab-addons.git/blob
752827606ee7949eefd9b1a853dba14e0fe258e4
[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.tapocontrol.internal.structures;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
17 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.HSBType;
22 import org.openhab.core.library.types.PercentType;
23
24 import com.google.gson.JsonObject;
25
26 /**
27  * Tapo-Device Information class
28  *
29  * @author Christian Wild - Initial contribution
30  */
31 @NonNullByDefault
32 public class TapoDeviceInfo {
33     private Boolean deviceOn = false;
34     private Boolean overheated = false;
35     private Integer brightness = 0;
36     private Integer colorTemp = 0;
37     private Integer hue = 0;
38     private Integer rssi = 0;
39     private Integer saturation = 100;
40     private Integer signalLevel = 0;
41     private Number onTime = 0;
42     private Number timeUsagePast7 = 0;
43     private Number timeUsagePast30 = 0;
44     private Number timeUsageToday = 0;
45     private String deviceId = "";
46     private String fwVer = "";
47     private String hwVer = "";
48     private String ip = "";
49     private String mac = "";
50     private String model = "";
51     private String nickname = "";
52     private String region = "";
53     private String type = "";
54     private TapoLightEffect lightEffect = new TapoLightEffect();
55
56     private JsonObject jsonObject = new JsonObject();
57
58     /**
59      * INIT
60      */
61     public TapoDeviceInfo() {
62         setData();
63     }
64
65     /**
66      * Init DeviceInfo with new Data;
67      * 
68      * @param jso JsonObject new Data
69      */
70     public TapoDeviceInfo(JsonObject jso) {
71         jsonObject = jso;
72         setData();
73     }
74
75     /**
76      * Set Data (new JsonObject)
77      * 
78      * @param jso JsonObject new Data
79      */
80     public TapoDeviceInfo setData(JsonObject jso) {
81         this.jsonObject = jso;
82         setData();
83         return this;
84     }
85
86     private void setData() {
87         this.brightness = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_BRIGHTNES);
88         this.colorTemp = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_COLORTEMP, BULB_MIN_COLORTEMP);
89         this.deviceId = jsonObjectToString(jsonObject, DEVICE_PROPERTY_ID);
90         this.deviceOn = jsonObjectToBool(jsonObject, DEVICE_PROPERTY_ON);
91         this.fwVer = jsonObjectToString(jsonObject, DEVICE_PROPERTY_FW);
92         this.hue = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_HUE);
93         this.hwVer = jsonObjectToString(jsonObject, DEVICE_PROPERTY_HW);
94         this.ip = jsonObjectToString(jsonObject, DEVICE_PROPERTY_IP);
95         this.mac = jsonObjectToString(jsonObject, DEVICE_PROPERTY_MAC);
96         this.model = jsonObjectToString(jsonObject, DEVICE_PROPERTY_MODEL);
97         this.nickname = jsonObjectToString(jsonObject, DEVICE_PROPERTY_NICKNAME);
98         this.onTime = jsonObjectToNumber(jsonObject, DEVICE_PROPERTY_ONTIME);
99         this.overheated = jsonObjectToBool(jsonObject, DEVICE_PROPERTY_OVERHEAT);
100         this.region = jsonObjectToString(jsonObject, DEVICE_PROPERTY_REGION);
101         this.saturation = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_SATURATION);
102         this.signalLevel = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_SIGNAL);
103         this.rssi = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_SIGNAL_RSSI);
104         this.timeUsagePast7 = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_USAGE_7);
105         this.timeUsagePast30 = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_USAGE_30);
106         this.timeUsageToday = jsonObjectToInt(jsonObject, DEVICE_PROPERTY_USAGE_TODAY);
107         this.type = jsonObjectToString(jsonObject, DEVICE_PROPERTY_TYPE);
108
109         if (this.hasLightEffect()) {
110             this.lightEffect = lightEffect.setData(jsonObject);
111         }
112     }
113
114     /***********************************
115      *
116      * CHECK FOR CHILD TYPES
117      *
118      ************************************/
119     public Boolean hasLightEffect() {
120         return this.jsonObject.has(DEVICE_PROPERTY_EFFECT);
121     }
122
123     /***********************************
124      *
125      * GET VALUES
126      *
127      ************************************/
128
129     public Integer getBrightness() {
130         return brightness;
131     }
132
133     public Integer getColorTemp() {
134         return colorTemp;
135     }
136
137     public String getFirmwareVersion() {
138         return fwVer;
139     }
140
141     public String getHardwareVersion() {
142         return hwVer;
143     }
144
145     public HSBType getHSB() {
146         DecimalType h = new DecimalType(hue);
147         PercentType s = new PercentType(saturation);
148         PercentType b = new PercentType(brightness);
149         return new HSBType(h, s, b);
150     }
151
152     public Integer getHue() {
153         return hue;
154     }
155
156     public TapoLightEffect getLightEffect() {
157         return lightEffect;
158     }
159
160     public String getIP() {
161         return ip;
162     }
163
164     public Boolean isOff() {
165         return !deviceOn;
166     }
167
168     public Boolean isOn() {
169         return deviceOn;
170     }
171
172     public Boolean isOverheated() {
173         return overheated;
174     }
175
176     public String getMAC() {
177         return formatMac(mac, MAC_DIVISION_CHAR);
178     }
179
180     public String getModel() {
181         return model.replace(" Series", "");
182     }
183
184     public String getNickname() {
185         return nickname;
186     }
187
188     public Number getOnTime() {
189         return onTime;
190     }
191
192     public String getRegion() {
193         return region;
194     }
195
196     public String getRepresentationProperty() {
197         return getMAC();
198     }
199
200     public Integer getSaturation() {
201         return saturation;
202     }
203
204     public String getSerial() {
205         return deviceId;
206     }
207
208     public Integer getSignalLevel() {
209         return signalLevel;
210     }
211
212     public Integer getRSSI() {
213         return rssi;
214     }
215
216     public Number getTimeUsagePast7() {
217         return timeUsagePast7;
218     }
219
220     public Number getTimeUsagePast30() {
221         return timeUsagePast30;
222     }
223
224     public Number getTimeUsagePastToday() {
225         return timeUsageToday;
226     }
227
228     public String getType() {
229         return type;
230     }
231
232     @Override
233     public String toString() {
234         return jsonObject.toString();
235     }
236 }