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.tradfri.internal.model;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonSyntaxException;
28 * The {@link TradfriDeviceData} class is a Java wrapper for the raw JSON data about the device state.
30 * @author Kai Kreuzer - Initial contribution
31 * @author Christoph Weitkamp - Restructuring and refactoring of the binding
34 public abstract class TradfriDeviceData {
36 private final Logger logger = LoggerFactory.getLogger(TradfriDeviceData.class);
38 protected JsonObject root;
39 protected JsonArray array;
40 protected JsonObject attributes;
41 protected JsonObject generalInfo;
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);
53 public TradfriDeviceData(String attributesNodeName, JsonElement json) {
55 root = json.getAsJsonObject();
56 if (root.has(attributesNodeName)) {
57 array = root.getAsJsonArray(attributesNodeName);
58 attributes = array.get(0).getAsJsonObject();
60 array = new JsonArray();
61 attributes = new JsonObject();
62 array.add(attributes);
64 generalInfo = root.getAsJsonObject(DEVICE);
65 } catch (JsonSyntaxException e) {
66 logger.warn("JSON error: {}", e.getMessage(), e);
67 throw new IllegalArgumentException(e);
71 public Integer getDeviceId() {
72 return root.get(INSTANCE_ID).getAsInt();
75 public boolean getReachabilityStatus() {
76 if (root.get(REACHABILITY_STATE) != null) {
77 return root.get(REACHABILITY_STATE).getAsInt() == 1;
83 public @Nullable String getFirmwareVersion() {
84 if (generalInfo.get(DEVICE_FIRMWARE) != null) {
85 return generalInfo.get(DEVICE_FIRMWARE).getAsString();
91 public @Nullable String getModelId() {
92 if (generalInfo.get(DEVICE_MODEL) != null) {
93 return generalInfo.get(DEVICE_MODEL).getAsString();
99 public @Nullable String getVendor() {
100 if (generalInfo.get(DEVICE_VENDOR) != null) {
101 return generalInfo.get(DEVICE_VENDOR).getAsString();