2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.neohub.internal;
15 import java.math.BigDecimal;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
23 import com.google.gson.annotations.SerializedName;
26 * A wrapper around the JSON response to the JSON GET_LIVE_DATA request
28 * @author Andrew Fiddian-Green - Initial contribution
31 public class NeoHubLiveDeviceData extends NeoHubAbstractDeviceData {
33 private static final Gson GSON = new Gson();
35 @SerializedName("TIMESTAMP_ENGINEERS")
36 private @Nullable BigDecimal timestampEngineers;
37 @SerializedName("TIMESTAMP_SYSTEM")
38 private @Nullable BigDecimal timestampSystem;
40 @SerializedName("devices")
41 private @Nullable List<LiveDataRecord> deviceRecords;
43 @SuppressWarnings("null")
45 public static class LiveDataRecord extends AbstractRecord {
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;
75 private boolean safeBoolean(@Nullable Boolean value) {
76 return (value == null ? false : value.booleanValue());
80 public String getDeviceName() {
81 String deviceName = this.deviceName;
82 return deviceName != null ? deviceName : "";
86 public BigDecimal getTargetTemperature() {
87 return safeBigDecimal(currentSetTemperature);
91 public BigDecimal getActualTemperature() {
92 return safeBigDecimal(currentTemperature);
96 public BigDecimal getFloorTemperature() {
97 return safeBigDecimal(currentFloorTemperature);
101 public boolean isStandby() {
102 return safeBoolean(standby);
106 public boolean isHeating() {
107 return safeBoolean(heating);
111 public boolean isPreHeating() {
112 return safeBoolean(preHeat);
116 public boolean isTimerOn() {
117 return safeBoolean(timerOn);
121 public boolean offline() {
122 return safeBoolean(offline);
126 public boolean stateManual() {
127 return safeBoolean(manualOn);
131 public boolean stateAuto() {
132 return safeBoolean(manualOff);
136 public boolean isWindowOpen() {
137 return safeBoolean(windowOpen);
141 public boolean isBatteryLow() {
142 return safeBoolean(batteryLow);
146 public long getTimestampEngineers() {
147 BigDecimal timestampEngineers = this.timestampEngineers;
148 return timestampEngineers != null ? timestampEngineers.longValue() : 0;
151 public long getTimestampSystem() {
152 BigDecimal timestampSystem = this.timestampSystem;
153 return timestampSystem != null ? timestampSystem.longValue() : 0;
157 * Create wrapper around a JSON string
159 * @param fromJson the JSON string
160 * @return a NeoHubGetLiveDataResponse wrapper around the JSON string
161 * @throws JsonSyntaxException
164 public static @Nullable NeoHubLiveDeviceData createDeviceData(String fromJson) throws JsonSyntaxException {
165 return GSON.fromJson(fromJson, NeoHubLiveDeviceData.class);
169 * returns the device record corresponding to a given device name
171 * @param deviceName the device name
172 * @return its respective device record
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())) {
188 * @return the full list of device records
191 public @Nullable List<LiveDataRecord> getDevices() {
192 return deviceRecords;