]> git.basschouten.com Git - openhab-addons.git/blob
207fa89077602da2c1ba31c51119c6443098949f
[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             if (ctd != null && cmd != null) {
98                 if (jsonOutputs != null) {
99                     if (jsonOutputs.isJsonArray()) {
100                         JsonArray jsonOutputsArray = jsonOutputs.getAsJsonArray();
101                         message = new QbusMessageListMap();
102                         message.setCmd(cmd);
103                         message.setSn(ctd);
104
105                         List<Map<String, String>> outputsList = new ArrayList<>();
106                         for (int i = 0; i < jsonOutputsArray.size(); i++) {
107                             JsonObject jsonOutputsObject = jsonOutputsArray.get(i).getAsJsonObject();
108
109                             Map<String, String> outputs = new HashMap<>();
110                             for (Entry<String, JsonElement> entry : jsonOutputsObject.entrySet()) {
111                                 outputs.put(entry.getKey(), entry.getValue().getAsString());
112                             }
113                             outputsList.add(outputs);
114                         }
115                         ((QbusMessageListMap) message).setOutputs(outputsList);
116                     }
117                 } else {
118                     message = new QbusMessageMap();
119
120                     message.setCmd(cmd);
121                     message.setSn(ctd);
122
123                     if (id != null) {
124                         message.setId(id);
125                     }
126
127                     if (state != null) {
128                         message.setState(state);
129                     }
130
131                     if (slats != null) {
132                         message.setSlatState(slats);
133                     }
134
135                     if (mode != null) {
136                         message.setMode(mode);
137                     }
138
139                     if (measured != null) {
140                         message.setMeasured(measured);
141                     }
142
143                     if (setpoint != null) {
144                         message.setSetPoint(setpoint);
145                     }
146                 }
147             }
148             return message;
149         } catch (IllegalStateException e) {
150             String mess = e.getMessage();
151             throw new JsonParseException("Unexpected Json format  " + mess + " for " + jsonObject.toString());
152         }
153     }
154 }