]> git.basschouten.com Git - openhab-addons.git/blob
bcb72356d6d3d366f44dfdf8e8c5bdad4a1808f4
[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.tradfri.internal.model;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonSyntaxException;
26
27 /**
28  * The {@link TradfriDeviceData} class is a Java wrapper for the raw JSON data about the device state.
29  *
30  * @author Kai Kreuzer - Initial contribution
31  * @author Christoph Weitkamp - Restructuring and refactoring of the binding
32  */
33 @NonNullByDefault
34 public abstract class TradfriDeviceData {
35
36     private final Logger logger = LoggerFactory.getLogger(TradfriDeviceData.class);
37
38     protected JsonObject root;
39     protected JsonArray array;
40     protected JsonObject attributes;
41     protected JsonObject generalInfo;
42
43     public TradfriDeviceData(String attributesNodeName) {
44         root = new JsonObject();
45         array = new JsonArray();
46         attributes = new JsonObject();
47         array.add(attributes);
48         root.add(attributesNodeName, array);
49         generalInfo = new JsonObject();
50         root.add(DEVICE, generalInfo);
51     }
52
53     public TradfriDeviceData(String attributesNodeName, JsonElement json) {
54         try {
55             root = json.getAsJsonObject();
56             if (root.has(attributesNodeName)) {
57                 array = root.getAsJsonArray(attributesNodeName);
58                 attributes = array.get(0).getAsJsonObject();
59             } else {
60                 array = new JsonArray();
61                 attributes = new JsonObject();
62                 array.add(attributes);
63             }
64             generalInfo = root.getAsJsonObject(DEVICE);
65         } catch (JsonSyntaxException e) {
66             logger.warn("JSON error: {}", e.getMessage(), e);
67             throw new IllegalArgumentException(e);
68         }
69     }
70
71     public Integer getDeviceId() {
72         return root.get(INSTANCE_ID).getAsInt();
73     }
74
75     public boolean getReachabilityStatus() {
76         if (root.get(REACHABILITY_STATE) != null) {
77             return root.get(REACHABILITY_STATE).getAsInt() == 1;
78         } else {
79             return false;
80         }
81     }
82
83     public @Nullable String getFirmwareVersion() {
84         if (generalInfo.get(DEVICE_FIRMWARE) != null) {
85             return generalInfo.get(DEVICE_FIRMWARE).getAsString();
86         } else {
87             return null;
88         }
89     }
90
91     public @Nullable String getModelId() {
92         if (generalInfo.get(DEVICE_MODEL) != null) {
93             return generalInfo.get(DEVICE_MODEL).getAsString();
94         } else {
95             return null;
96         }
97     }
98
99     public @Nullable String getVendor() {
100         if (generalInfo.get(DEVICE_VENDOR) != null) {
101             return generalInfo.get(DEVICE_VENDOR).getAsString();
102         } else {
103             return null;
104         }
105     }
106 }