]> git.basschouten.com Git - openhab-addons.git/blob
05d4f8f6b22c35d1bddfaa56eb01ad6f90d2dd33
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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         if (firmwareVersionNew != null) {
63             return firmwareVersionNew.toString();
64         }
65         if (firmwareVersionOld != null) {
66             return firmwareVersionOld.toString();
67         }
68         return null;
69     }
70
71     /**
72      * Create wrapper around a JSON string
73      *
74      * @param fromJson the JSON string
75      * @return a NeoHubReadDcbResponse wrapper around the JSON string
76      * @throws JsonSyntaxException
77      *
78      */
79     public static @Nullable NeoHubReadDcbResponse createSystemData(String fromJson) throws JsonSyntaxException {
80         return GSON.fromJson(fromJson, NeoHubReadDcbResponse.class);
81     }
82 }