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.neohub.internal;
15 import java.math.BigDecimal;
16 import java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
23 import com.google.gson.annotations.SerializedName;
26 * A wrapper around the JSON response to the JSON GET_ENGINEERS request
28 * @author Andrew Fiddian-Green - Initial contribution
31 public class NeoHubGetEngineersData {
33 private static final Gson GSON = new Gson();
35 @SuppressWarnings("null")
37 private static class EngineersRecords extends HashMap<String, EngineersRecord> {
38 private static final long serialVersionUID = 1L;
41 @SuppressWarnings("null")
43 public class EngineersRecord {
44 @SerializedName("DEVICE_TYPE")
45 private @Nullable BigDecimal deviceType;
47 public int getDeviceType() {
48 BigDecimal deviceType = this.deviceType;
49 return deviceType != null ? deviceType.intValue() : -1;
53 private @Nullable EngineersRecords deviceRecords;
56 * Create wrapper around a JSON string
58 * @param fromJson the JSON string
59 * @throws JsonSyntaxException
62 public NeoHubGetEngineersData(String fromJson) throws JsonSyntaxException {
63 deviceRecords = GSON.fromJson(fromJson, EngineersRecords.class);
66 public static @Nullable NeoHubGetEngineersData createEngineersData(String fromJson) throws JsonSyntaxException {
67 return new NeoHubGetEngineersData(fromJson);
71 * returns the device record corresponding to a given device name
73 * @param deviceName the device name
74 * @return its respective device information record
76 private @Nullable EngineersRecord getDevice(String deviceName) {
77 EngineersRecords deviceRecords = this.deviceRecords;
78 return deviceRecords != null ? deviceRecords.get(deviceName) : null;
82 * returns the deviceType corresponding to a given device name
84 * @param deviceName the device name
85 * @return its respective device information record
87 public int getDeviceType(String deviceName) {
88 EngineersRecord record = getDevice(deviceName);
89 return record != null ? record.getDeviceType() : -1;