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.GsonBuilder;
23 import com.google.gson.JsonSyntaxException;
24 import com.google.gson.annotations.SerializedName;
27 * A wrapper around the JSON response to the JSON INFO request
29 * @author Sebastian Prehn - Initial contribution
30 * @author Andrew Fiddian-Green - Refactoring for openHAB v2.x
34 public class NeoHubInfoResponse extends NeoHubAbstractDeviceData {
36 private static final Gson GSON = new GsonBuilder()
37 .registerTypeAdapter(NeohubBool.class, new NeohubBoolDeserializer()).create();
39 @SerializedName("devices")
40 private @Nullable List<InfoRecord> deviceRecords;
42 @SuppressWarnings("null")
44 static class StatMode {
45 @SerializedName("MANUAL_OFF")
46 private @Nullable NeohubBool manualOff;
47 @SerializedName("MANUAL_ON")
48 private @Nullable NeohubBool manualOn;
50 private boolean stateManualOn() {
51 NeohubBool manualOn = this.manualOn;
52 return (manualOn == null ? false : manualOn.value);
55 private boolean stateManualOff() {
56 NeohubBool manualOff = this.manualOff;
57 return (manualOff == null ? false : manualOff.value);
61 @SuppressWarnings("null")
63 public static class InfoRecord extends AbstractRecord {
64 @SerializedName("device")
65 private @Nullable String deviceName;
66 @SerializedName("CURRENT_SET_TEMPERATURE")
67 private @Nullable BigDecimal currentSetTemperature;
68 @SerializedName("CURRENT_TEMPERATURE")
69 private @Nullable BigDecimal currentTemperature;
70 @SerializedName("CURRENT_FLOOR_TEMPERATURE")
71 private @Nullable BigDecimal currentFloorTemperature;
72 @SerializedName("COOL_INP")
73 private @Nullable NeohubBool coolInput;
74 @SerializedName("LOW_BATTERY")
75 private @Nullable NeohubBool batteryLow;
76 @SerializedName("STANDBY")
77 private @Nullable NeohubBool standby;
78 @SerializedName("HEATING")
79 private @Nullable NeohubBool heating;
80 @SerializedName("PREHEAT")
81 private @Nullable NeohubBool preHeat;
82 @SerializedName("TIMER")
83 private @Nullable NeohubBool timerOn;
84 @SerializedName("DEVICE_TYPE")
85 private @Nullable BigDecimal deviceType;
86 @SerializedName("OFFLINE")
87 private @Nullable NeohubBool offline;
88 @SerializedName("STAT_MODE")
89 private @Nullable StatMode statMode = new StatMode();
91 private boolean safeBoolean(@Nullable NeohubBool value) {
92 return (value == null ? false : value.value);
96 public String getDeviceName() {
97 String deviceName = this.deviceName;
98 return deviceName != null ? deviceName : "";
102 public BigDecimal getTargetTemperature() {
103 return safeBigDecimal(currentSetTemperature);
107 public BigDecimal getActualTemperature() {
108 return safeBigDecimal(currentTemperature);
112 public BigDecimal getFloorTemperature() {
113 return safeBigDecimal(currentFloorTemperature);
117 public boolean isStandby() {
118 return safeBoolean(standby);
122 public boolean isHeating() {
123 return safeBoolean(heating);
127 public boolean isPreHeating() {
128 return safeBoolean(preHeat);
132 public boolean isTimerOn() {
133 return safeBoolean(timerOn);
137 public boolean offline() {
138 return safeBoolean(offline);
142 public boolean stateManual() {
143 StatMode statMode = this.statMode;
144 return (statMode != null && statMode.stateManualOn());
148 public boolean stateAuto() {
149 StatMode statMode = this.statMode;
150 return (statMode != null && statMode.stateManualOff());
154 public boolean isWindowOpen() {
155 // legacy API misuses the cool input parameter
156 return safeBoolean(coolInput);
160 public boolean isBatteryLow() {
161 return safeBoolean(batteryLow);
164 public int getDeviceType() {
165 BigDecimal deviceType = this.deviceType;
166 return deviceType != null ? deviceType.intValue() : -1;
171 * Create wrapper around a JSON string
173 * @param fromJson the JSON string
174 * @return a NeoHubInfoResponse wrapper around the JSON
175 * @throws JsonSyntaxException
178 public static @Nullable NeoHubInfoResponse createDeviceData(String fromJson) throws JsonSyntaxException {
179 return GSON.fromJson(fromJson, NeoHubInfoResponse.class);
183 * returns the device record corresponding to a given device name
185 * @param deviceName the device name
186 * @return its respective device record
189 public @Nullable AbstractRecord getDeviceRecord(String deviceName) {
190 List<InfoRecord> deviceRecords = this.deviceRecords;
191 if (deviceRecords != null) {
192 for (AbstractRecord deviceRecord : deviceRecords) {
193 if (deviceName.equals(deviceRecord.getDeviceName())) {
202 * @return the full list of device records
205 public @Nullable List<InfoRecord> getDevices() {
206 return deviceRecords;