]> git.basschouten.com Git - openhab-addons.git/blob
0254b257653db7ed39fab05b5927e8ac20bb004e
[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.neohub.internal;
14
15 import java.math.BigDecimal;
16 import java.util.HashMap;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
23 import com.google.gson.annotations.SerializedName;
24
25 /**
26  * A wrapper around the JSON response to the JSON GET_ENGINEERS request
27  *
28  * @author Andrew Fiddian-Green - Initial contribution
29  */
30 @NonNullByDefault
31 public class NeoHubGetEngineersData {
32
33     private static final Gson GSON = new Gson();
34
35     @SuppressWarnings("null")
36     @NonNullByDefault
37     private static class EngineersRecords extends HashMap<String, EngineersRecord> {
38         private static final long serialVersionUID = 1L;
39     }
40
41     @SuppressWarnings("null")
42     @NonNullByDefault
43     public class EngineersRecord {
44         @SerializedName("DEVICE_TYPE")
45         private @Nullable BigDecimal deviceType;
46
47         public int getDeviceType() {
48             BigDecimal deviceType = this.deviceType;
49             return deviceType != null ? deviceType.intValue() : -1;
50         }
51     }
52
53     private @Nullable EngineersRecords deviceRecords;
54
55     /**
56      * Create wrapper around a JSON string
57      * 
58      * @param fromJson the JSON string
59      * @throws JsonSyntaxException
60      * 
61      */
62     public NeoHubGetEngineersData(String fromJson) throws JsonSyntaxException {
63         deviceRecords = GSON.fromJson(fromJson, EngineersRecords.class);
64     }
65
66     public static @Nullable NeoHubGetEngineersData createEngineersData(String fromJson) throws JsonSyntaxException {
67         return new NeoHubGetEngineersData(fromJson);
68     }
69
70     /**
71      * returns the device record corresponding to a given device name
72      * 
73      * @param deviceName the device name
74      * @return its respective device information record
75      */
76     private @Nullable EngineersRecord getDevice(String deviceName) {
77         EngineersRecords deviceRecords = this.deviceRecords;
78         return deviceRecords != null ? deviceRecords.get(deviceName) : null;
79     }
80
81     /**
82      * returns the deviceType corresponding to a given device name
83      * 
84      * @param deviceName the device name
85      * @return its respective device information record
86      */
87     public int getDeviceType(String deviceName) {
88         EngineersRecord record = getDevice(deviceName);
89         return record != null ? record.getDeviceType() : -1;
90     }
91 }