]> git.basschouten.com Git - openhab-addons.git/blob
71fd9804fb1cbf18433fab4630db3b66be850d0f
[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.TapoThingConstants.*;
16 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 import com.google.gson.JsonObject;
21
22 /**
23  * Tapo-Energy-Monitor Structure Class
24  *
25  * @author Christian Wild - Initial contribution
26  */
27 @NonNullByDefault
28 public class TapoEnergyData {
29     private Number currentPower = 0;
30     private Number todayEnergy = 0;
31     private Number monthEnergy = 0;
32     private Number todayRuntime = 0;
33     private Number monthRuntime = 0;
34     private Number[] past24h = new Number[24];
35     private Number[] past30d = new Number[30];
36     private Number[] past1y = new Number[12];
37
38     private JsonObject jsonObject = new JsonObject();
39
40     /**
41      * INIT
42      */
43     public TapoEnergyData() {
44         setData();
45     }
46
47     /**
48      * Init DeviceInfo with new Data;
49      * 
50      * @param jso JsonObject new Data
51      */
52     public TapoEnergyData(JsonObject jso) {
53         setData(jso);
54     }
55
56     /**
57      * Set Data (new JsonObject)
58      * 
59      * @param jso JsonObject new Data
60      */
61     public TapoEnergyData setData(JsonObject jso) {
62         /* create empty jsonObject to set efault values if has no energydata */
63         if (jso.has(JSON_KEY_ENERGY_POWER)) {
64             this.jsonObject = jso;
65         } else {
66             this.jsonObject = new JsonObject();
67         }
68         setData();
69         return this;
70     }
71
72     private void setData() {
73         this.currentPower = (float) jsonObjectToInt(jsonObject, JSON_KEY_ENERGY_POWER) / 1000;
74
75         this.todayEnergy = jsonObjectToInt(jsonObject, JSON_KEY_ENERGY_ENERGY_TODAY);
76         this.monthEnergy = jsonObjectToInt(jsonObject, JSON_KEY_ENERGY_ENERGY_MONTH);
77         this.todayRuntime = jsonObjectToInt(jsonObject, JSON_KEY_ENERGY_RUNTIME_TODAY);
78         this.monthRuntime = jsonObjectToInt(jsonObject, JSON_KEY_ENERGY_RUNTIME_MONTH);
79         this.past24h = new Number[24];
80         this.past30d = new Number[30];
81         this.past1y = new Number[12];
82     }
83
84     /***********************************
85      *
86      * GET VALUES
87      *
88      ************************************/
89
90     public Number getCurrentPower() {
91         return currentPower;
92     }
93
94     public Number getTodayEnergy() {
95         return todayEnergy;
96     }
97
98     public Number getMonthEnergy() {
99         return monthEnergy;
100     }
101
102     public Number getYearEnergy() {
103         int sum = 0;
104         for (int i = 0; i < past1y.length; i++) {
105             sum += past1y[i].intValue();
106         }
107         return sum;
108     }
109
110     public Number getTodayRuntime() {
111         return todayRuntime;
112     }
113
114     public Number getMonthRuntime() {
115         return monthRuntime;
116     }
117
118     public Number[] getPast24hUsage() {
119         return past24h;
120     }
121
122     public Number[] getPast30dUsage() {
123         return past30d;
124     }
125
126     public Number[] getPast1yUsage() {
127         return past1y;
128     }
129 }