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.time.Instant;
18 import javax.measure.Unit;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.unit.ImperialUnits;
23 import org.openhab.core.library.unit.SIUnits;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonSyntaxException;
27 import com.google.gson.annotations.SerializedName;
30 * A wrapper around the JSON response to the JSON READ_DCB and GET_SYSTEM
33 * @author Andrew Fiddian-Green - Initial contribution
36 public class NeoHubReadDcbResponse {
38 private static final Gson GSON = new Gson();
40 @SerializedName("CORF")
41 private @Nullable String degreesCorF;
43 @SerializedName("Firmware version")
44 private @Nullable BigDecimal firmwareVersionNew;
46 @SerializedName("HUB_VERSION")
47 private @Nullable BigDecimal firmwareVersionOld;
50 * note: time-stamps are measured in seconds from 1970-01-01T00:00:00Z
52 * this time-stamp is the moment of creation of this class instance; it is used
53 * to compare with the system last change time-stamp reported by the hub
55 public final long timeStamp = Instant.now().getEpochSecond();
57 public Unit<?> getTemperatureUnit() {
58 return "F".equalsIgnoreCase(degreesCorF) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
61 public @Nullable String getFirmwareVersion() {
62 BigDecimal firmwareVersionNew = this.firmwareVersionNew;
63 if (firmwareVersionNew != null) {
64 return firmwareVersionNew.toString();
66 BigDecimal firmwareVersionOld = this.firmwareVersionOld;
67 if (firmwareVersionOld != null) {
68 return firmwareVersionOld.toString();
74 * Create wrapper around a JSON string
76 * @param fromJson the JSON string
77 * @return a NeoHubReadDcbResponse wrapper around the JSON string
78 * @throws JsonSyntaxException
81 public static @Nullable NeoHubReadDcbResponse createSystemData(String fromJson) throws JsonSyntaxException {
82 return GSON.fromJson(fromJson, NeoHubReadDcbResponse.class);