]> git.basschouten.com Git - openhab-addons.git/blob
e527d82627454b088c5d47282ab7aa5e0f820cee
[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.qbus.internal.protocol;
14
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonDeserializationContext;
27 import com.google.gson.JsonDeserializer;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonParseException;
31
32 /**
33  * Class {@link QbusMessageDeserializer} deserializes all json messages from Qbus. Various json
34  * message formats are supported. The format is selected based on the content of the cmd and event json objects.
35  *
36  * @author Koen Schockaert - Initial Contribution
37  *
38  */
39
40 @NonNullByDefault
41 class QbusMessageDeserializer implements JsonDeserializer<QbusMessageBase> {
42
43     @Override
44     public @Nullable QbusMessageBase deserialize(final JsonElement json, final Type typeOfT,
45             final JsonDeserializationContext context) throws JsonParseException {
46         final JsonObject jsonObject = json.getAsJsonObject();
47
48         String ctd = null;
49         String cmd = null;
50         Integer id = null;
51         Integer state = null;
52         Integer mode = null;
53         Double measured = null;
54         Double setpoint = null;
55         Integer slats = null;
56
57         QbusMessageBase message = null;
58
59         JsonElement jsonOutputs = null;
60         try {
61             if (jsonObject.has("CTD")) {
62                 ctd = jsonObject.get("CTD").getAsString();
63             }
64
65             if (jsonObject.has("cmd")) {
66                 cmd = jsonObject.get("cmd").getAsString();
67             }
68
69             if (jsonObject.has("id")) {
70                 id = jsonObject.get("id").getAsInt();
71             }
72
73             if (jsonObject.has("state")) {
74                 state = jsonObject.get("state").getAsInt();
75             }
76
77             if (jsonObject.has("mode")) {
78                 mode = jsonObject.get("mode").getAsInt();
79             }
80
81             if (jsonObject.has("measured")) {
82                 measured = jsonObject.get("measured").getAsDouble();
83             }
84
85             if (jsonObject.has("setpoint")) {
86                 setpoint = jsonObject.get("setpoint").getAsDouble();
87             }
88
89             if (jsonObject.has("slats")) {
90                 slats = jsonObject.get("slats").getAsInt();
91             }
92
93             if (jsonObject.has("outputs")) {
94                 jsonOutputs = jsonObject.get("outputs");
95
96             }
97
98             if (ctd != null && cmd != null) {
99                 if (jsonOutputs != null) {
100                     if (jsonOutputs.isJsonArray()) {
101                         JsonArray jsonOutputsArray = jsonOutputs.getAsJsonArray();
102                         message = new QbusMessageListMap();
103                         message.setCmd(cmd);
104                         message.setSn(ctd);
105
106                         List<Map<String, String>> outputsList = new ArrayList<>();
107                         for (int i = 0; i < jsonOutputsArray.size(); i++) {
108                             JsonObject jsonOutputsObject = jsonOutputsArray.get(i).getAsJsonObject();
109
110                             Map<String, String> outputs = new HashMap<>();
111                             for (Entry<String, JsonElement> entry : jsonOutputsObject.entrySet()) {
112                                 outputs.put(entry.getKey(), entry.getValue().getAsString());
113                             }
114                             outputsList.add(outputs);
115                         }
116                         ((QbusMessageListMap) message).setOutputs(outputsList);
117                     }
118
119                 } else {
120                     message = new QbusMessageMap();
121
122                     message.setCmd(cmd);
123                     message.setSn(ctd);
124
125                     if (id != null) {
126                         message.setId(id);
127                     }
128
129                     if (state != null) {
130                         message.setState(state);
131                     }
132
133                     if (slats != null) {
134                         message.setSlatState(slats);
135                     }
136
137                     if (mode != null) {
138                         message.setMode(mode);
139                     }
140
141                     if (measured != null) {
142                         message.setMeasured(measured);
143                     }
144
145                     if (setpoint != null) {
146                         message.setSetPoint(setpoint);
147                     }
148
149                 }
150             }
151             return message;
152         } catch (IllegalStateException e) {
153             String mess = e.getMessage();
154             throw new JsonParseException("Unexpected Json format  " + mess + " for " + jsonObject.toString());
155         }
156     }
157 }