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.qbus.internal.protocol;
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
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;
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.
36 * @author Koen Schockaert - Initial Contribution
41 class QbusMessageDeserializer implements JsonDeserializer<QbusMessageBase> {
44 public @Nullable QbusMessageBase deserialize(final JsonElement json, final Type typeOfT,
45 final JsonDeserializationContext context) throws JsonParseException {
46 final JsonObject jsonObject = json.getAsJsonObject();
53 Double measured = null;
54 Double setpoint = null;
57 QbusMessageBase message = null;
59 JsonElement jsonOutputs = null;
61 if (jsonObject.has("CTD")) {
62 ctd = jsonObject.get("CTD").getAsString();
65 if (jsonObject.has("cmd")) {
66 cmd = jsonObject.get("cmd").getAsString();
69 if (jsonObject.has("id")) {
70 id = jsonObject.get("id").getAsInt();
73 if (jsonObject.has("state")) {
74 state = jsonObject.get("state").getAsInt();
77 if (jsonObject.has("mode")) {
78 mode = jsonObject.get("mode").getAsInt();
81 if (jsonObject.has("measured")) {
82 measured = jsonObject.get("measured").getAsDouble();
85 if (jsonObject.has("setpoint")) {
86 setpoint = jsonObject.get("setpoint").getAsDouble();
89 if (jsonObject.has("slats")) {
90 slats = jsonObject.get("slats").getAsInt();
93 if (jsonObject.has("outputs")) {
94 jsonOutputs = jsonObject.get("outputs");
97 if (ctd != null && cmd != null) {
98 if (jsonOutputs != null) {
99 if (jsonOutputs.isJsonArray()) {
100 JsonArray jsonOutputsArray = jsonOutputs.getAsJsonArray();
101 message = new QbusMessageListMap();
105 List<Map<String, String>> outputsList = new ArrayList<>();
106 for (int i = 0; i < jsonOutputsArray.size(); i++) {
107 JsonObject jsonOutputsObject = jsonOutputsArray.get(i).getAsJsonObject();
109 Map<String, String> outputs = new HashMap<>();
110 for (Entry<String, JsonElement> entry : jsonOutputsObject.entrySet()) {
111 outputs.put(entry.getKey(), entry.getValue().getAsString());
113 outputsList.add(outputs);
115 ((QbusMessageListMap) message).setOutputs(outputsList);
118 message = new QbusMessageMap();
128 message.setState(state);
132 message.setSlatState(slats);
136 message.setMode(mode);
139 if (measured != null) {
140 message.setMeasured(measured);
143 if (setpoint != null) {
144 message.setSetPoint(setpoint);
149 } catch (IllegalStateException e) {
150 String mess = e.getMessage();
151 throw new JsonParseException("Unexpected Json format " + mess + " for " + jsonObject.toString());