2 * Copyright (c) 2010-2021 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.touchwand.internal.dto;
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
17 import java.util.Arrays;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonParser;
30 * The {@link TouchWandUnitFromJson} parse Json unit data
32 * @author Roie Geron - Initial contribution
35 public class TouchWandUnitFromJson {
37 private final static Logger logger = LoggerFactory.getLogger(TouchWandUnitFromJson.class);
39 public TouchWandUnitFromJson() {
42 public static TouchWandUnitData parseResponse(JsonObject jsonUnit) {
43 final Gson gson = new Gson();
44 TouchWandUnitData touchWandUnit;
45 String type = jsonUnit.get("type").getAsString();
46 if (!Arrays.asList(SUPPORTED_TOUCHWAND_TYPES).contains(type)) {
50 if (!jsonUnit.has("currStatus") || (jsonUnit.get("currStatus") == null)) {
55 case TYPE_WALLCONTROLLER:
56 touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);
59 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
62 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
65 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
67 case TYPE_ALARMSENSOR:
68 Gson builder = new GsonBuilder()
69 .registerTypeAdapter(TouchWandUnitDataAlarmSensor.class, new AlarmSensorUnitDataDeserializer())
71 touchWandUnit = builder.fromJson(jsonUnit, TouchWandUnitDataAlarmSensor.class);
74 touchWandUnit = new TouchWandUnknownTypeUnitData();
77 touchWandUnit = new TouchWandUnknownTypeUnitData();
80 if (touchWandUnit == null) {
81 touchWandUnit = new TouchWandUnknownTypeUnitData();
87 public static TouchWandUnitData parseResponse(String JsonUnit) {
88 final JsonParser jsonParser = new JsonParser();
89 TouchWandUnitData myTouchWandUnitData;
92 unitObj = jsonParser.parse(JsonUnit).getAsJsonObject();
93 myTouchWandUnitData = parseResponse(unitObj);
94 } catch (JsonParseException | IllegalStateException e) {
95 logger.warn("Could not parse response {}", JsonUnit);
96 myTouchWandUnitData = new TouchWandUnknownTypeUnitData(); // Return unknown type
98 return myTouchWandUnitData;