]> git.basschouten.com Git - openhab-addons.git/blob
cbd25007c9f71e6a7bbfa504dac0eba357c65c37
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.time.Instant;
16
17 import javax.measure.Unit;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.unit.ImperialUnits;
22 import org.openhab.core.library.unit.SIUnits;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonSyntaxException;
26 import com.google.gson.annotations.SerializedName;
27
28 /**
29  * A wrapper around the JSON response to the JSON READ_DCB and GET_SYSTEM
30  * request
31  *
32  * @author Andrew Fiddian-Green - Initial contribution
33  */
34 @NonNullByDefault
35 public class NeoHubReadDcbResponse {
36
37     private static final Gson GSON = new Gson();
38
39     @SerializedName("CORF")
40     private @Nullable String degreesCorF;
41
42     /*
43      * note: time-stamps are measured in seconds from 1970-01-01T00:00:00Z
44      *
45      * this time-stamp is the moment of creation of this class instance; it is used
46      * to compare with the system last change time-stamp reported by the hub
47      */
48     public final long timeStamp = Instant.now().getEpochSecond();
49
50     public Unit<?> getTemperatureUnit() {
51         return "F".equalsIgnoreCase(degreesCorF) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
52     }
53
54     /**
55      * Create wrapper around a JSON string
56      * 
57      * @param fromJson the JSON string
58      * @return a NeoHubReadDcbResponse wrapper around the JSON string
59      * @throws JsonSyntaxException
60      * 
61      */
62     public static @Nullable NeoHubReadDcbResponse createSystemData(String fromJson) throws JsonSyntaxException {
63         return GSON.fromJson(fromJson, NeoHubReadDcbResponse.class);
64     }
65 }