]> git.basschouten.com Git - openhab-addons.git/blob
5531e45a756095d3dab00d7abd46526f2c756139
[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.neohub.internal;
14
15 import java.math.BigDecimal;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
23 import com.google.gson.annotations.SerializedName;
24
25 /**
26  * A wrapper around the JSON response to the JSON GET_LIVE_DATA request
27  *
28  * @author Andrew Fiddian-Green - Initial contribution
29  */
30 @NonNullByDefault
31 public class NeoHubLiveDeviceData extends NeoHubAbstractDeviceData {
32
33     private static final Gson GSON = new Gson();
34
35     @SerializedName("TIMESTAMP_ENGINEERS")
36     private @Nullable BigDecimal timestampEngineers;
37     @SerializedName("TIMESTAMP_SYSTEM")
38     private @Nullable BigDecimal timestampSystem;
39
40     @SerializedName("devices")
41     private @Nullable List<LiveDataRecord> deviceRecords;
42
43     @SuppressWarnings("null")
44     @NonNullByDefault
45     public static class LiveDataRecord extends AbstractRecord {
46
47         // "alternate" is a special kludge for technical devices
48         @SerializedName(value = "ZONE_NAME", alternate = { "device" })
49         private @Nullable String deviceName;
50         @SerializedName("SET_TEMP")
51         private @Nullable BigDecimal currentSetTemperature;
52         @SerializedName("ACTUAL_TEMP")
53         private @Nullable BigDecimal currentTemperature;
54         @SerializedName("CURRENT_FLOOR_TEMPERATURE")
55         private @Nullable BigDecimal currentFloorTemperature;
56         @SerializedName("WINDOW_OPEN")
57         private @Nullable Boolean windowOpen;
58         @SerializedName("LOW_BATTERY")
59         private @Nullable Boolean batteryLow;
60         @SerializedName("STANDBY")
61         private @Nullable Boolean standby;
62         @SerializedName("HEAT_ON")
63         private @Nullable Boolean heating;
64         @SerializedName("PREHEAT_ACTIVE")
65         private @Nullable Boolean preHeat;
66         @SerializedName("TIMER_ON")
67         private @Nullable Boolean timerOn;
68         @SerializedName("OFFLINE")
69         private @Nullable Boolean offline;
70         @SerializedName("MANUAL_OFF")
71         private @Nullable Boolean manualOff;
72         @SerializedName("MANUAL_ON")
73         private @Nullable Boolean manualOn;
74
75         private boolean safeBoolean(@Nullable Boolean value) {
76             return (value == null ? false : value.booleanValue());
77         }
78
79         @Override
80         public String getDeviceName() {
81             String deviceName = this.deviceName;
82             return deviceName != null ? deviceName : "";
83         }
84
85         @Override
86         public BigDecimal getTargetTemperature() {
87             return safeBigDecimal(currentSetTemperature);
88         }
89
90         @Override
91         public BigDecimal getActualTemperature() {
92             return safeBigDecimal(currentTemperature);
93         }
94
95         @Override
96         public BigDecimal getFloorTemperature() {
97             return safeBigDecimal(currentFloorTemperature);
98         }
99
100         @Override
101         public boolean isStandby() {
102             return safeBoolean(standby);
103         }
104
105         @Override
106         public boolean isHeating() {
107             return safeBoolean(heating);
108         }
109
110         @Override
111         public boolean isPreHeating() {
112             return safeBoolean(preHeat);
113         }
114
115         @Override
116         public boolean isTimerOn() {
117             return safeBoolean(timerOn);
118         }
119
120         @Override
121         public boolean offline() {
122             return safeBoolean(offline);
123         }
124
125         @Override
126         public boolean stateManual() {
127             return safeBoolean(manualOn);
128         }
129
130         @Override
131         public boolean stateAuto() {
132             return safeBoolean(manualOff);
133         }
134
135         @Override
136         public boolean isWindowOpen() {
137             return safeBoolean(windowOpen);
138         }
139
140         @Override
141         public boolean isBatteryLow() {
142             return safeBoolean(batteryLow);
143         }
144     }
145
146     public long getTimestampEngineers() {
147         BigDecimal timestampEngineers = this.timestampEngineers;
148         return timestampEngineers != null ? timestampEngineers.longValue() : 0;
149     }
150
151     public long getTimestampSystem() {
152         BigDecimal timestampSystem = this.timestampSystem;
153         return timestampSystem != null ? timestampSystem.longValue() : 0;
154     }
155
156     /**
157      * Create wrapper around a JSON string
158      * 
159      * @param fromJson the JSON string
160      * @return a NeoHubGetLiveDataResponse wrapper around the JSON string
161      * @throws JsonSyntaxException
162      * 
163      */
164     public static @Nullable NeoHubLiveDeviceData createDeviceData(String fromJson) throws JsonSyntaxException {
165         return GSON.fromJson(fromJson, NeoHubLiveDeviceData.class);
166     }
167
168     /**
169      * returns the device record corresponding to a given device name
170      * 
171      * @param deviceName the device name
172      * @return its respective device record
173      */
174     @Override
175     public @Nullable AbstractRecord getDeviceRecord(String deviceName) {
176         List<LiveDataRecord> deviceRecords = this.deviceRecords;
177         if (deviceRecords != null) {
178             for (AbstractRecord deviceRecord : deviceRecords) {
179                 if (deviceName.equals(deviceRecord.getDeviceName())) {
180                     return deviceRecord;
181                 }
182             }
183         }
184         return null;
185     }
186
187     /**
188      * @return the full list of device records
189      */
190     @Override
191     public @Nullable List<LiveDataRecord> getDevices() {
192         return deviceRecords;
193     }
194 }