]> git.basschouten.com Git - openhab-addons.git/blob
2154a01c413edaf667dafc6690a8938c2367e313
[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.time.Instant;
17
18 import javax.measure.Unit;
19
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;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonSyntaxException;
27 import com.google.gson.annotations.SerializedName;
28
29 /**
30  * A wrapper around the JSON response to the JSON READ_DCB and GET_SYSTEM
31  * request
32  *
33  * @author Andrew Fiddian-Green - Initial contribution
34  */
35 @NonNullByDefault
36 public class NeoHubReadDcbResponse {
37
38     private static final Gson GSON = new Gson();
39
40     @SerializedName("CORF")
41     private @Nullable String degreesCorF;
42
43     @SerializedName("Firmware version")
44     private @Nullable BigDecimal firmwareVersionNew;
45
46     @SerializedName("HUB_VERSION")
47     private @Nullable BigDecimal firmwareVersionOld;
48
49     /*
50      * note: time-stamps are measured in seconds from 1970-01-01T00:00:00Z
51      *
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
54      */
55     public final long timeStamp = Instant.now().getEpochSecond();
56
57     public Unit<?> getTemperatureUnit() {
58         return "F".equalsIgnoreCase(degreesCorF) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
59     }
60
61     public @Nullable String getFirmwareVersion() {
62         BigDecimal firmwareVersionNew = this.firmwareVersionNew;
63         if (firmwareVersionNew != null) {
64             return firmwareVersionNew.toString();
65         }
66         BigDecimal firmwareVersionOld = this.firmwareVersionOld;
67         if (firmwareVersionOld != null) {
68             return firmwareVersionOld.toString();
69         }
70         return null;
71     }
72
73     /**
74      * Create wrapper around a JSON string
75      *
76      * @param fromJson the JSON string
77      * @return a NeoHubReadDcbResponse wrapper around the JSON string
78      * @throws JsonSyntaxException
79      *
80      */
81     public static @Nullable NeoHubReadDcbResponse createSystemData(String fromJson) throws JsonSyntaxException {
82         return GSON.fromJson(fromJson, NeoHubReadDcbResponse.class);
83     }
84 }