]> git.basschouten.com Git - openhab-addons.git/blob
10fee8ddd22de0c40a5ff34a396041eaa2a09c54
[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      * @return a NeoHubGetEngData wrapper around the JSON string
60      * @throws JsonSyntaxException
61      * 
62      */
63     public NeoHubGetEngineersData(String fromJson) throws JsonSyntaxException {
64         deviceRecords = GSON.fromJson(fromJson, EngineersRecords.class);
65     }
66
67     public static @Nullable NeoHubGetEngineersData createEngineersData(String fromJson) throws JsonSyntaxException {
68         return new NeoHubGetEngineersData(fromJson);
69     }
70
71     /**
72      * returns the device record corresponding to a given device name
73      * 
74      * @param deviceName the device name
75      * @return its respective device information record
76      */
77     private @Nullable EngineersRecord getDevice(String deviceName) {
78         EngineersRecords deviceRecords = this.deviceRecords;
79         return deviceRecords != null ? deviceRecords.get(deviceName) : null;
80     }
81
82     /**
83      * returns the deviceType corresponding to a given device name
84      * 
85      * @param deviceName the device name
86      * @return its respective device information record
87      */
88     public int getDeviceType(String deviceName) {
89         EngineersRecord record = getDevice(deviceName);
90         return record != null ? record.getDeviceType() : -1;
91     }
92 }