2 * Copyright (c) 2010-2020 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
14 package org.openhab.binding.touchwand.internal.dto;
16 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
18 import java.util.Arrays;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParseException;
28 import com.google.gson.JsonParser;
31 * The {@link TouchWandUnitFromJson} parse Json unit data
33 * @author Roie Geron - Initial contribution
36 public class TouchWandUnitFromJson {
38 private final static Logger logger = LoggerFactory.getLogger(TouchWandUnitFromJson.class);
40 public TouchWandUnitFromJson() {
43 public static TouchWandUnitData parseResponse(JsonObject jsonUnit) {
44 final Gson gson = new Gson();
45 TouchWandUnitData touchWandUnit;
46 String type = jsonUnit.get("type").getAsString();
47 if (!Arrays.asList(SUPPORTED_TOUCHWAND_TYPES).contains(type)) {
51 if (!jsonUnit.has("currStatus") || (jsonUnit.get("currStatus") == null)) {
56 case TYPE_WALLCONTROLLER:
57 touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);
60 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
63 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
66 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
68 case TYPE_ALARMSENSOR:
69 Gson builder = new GsonBuilder()
70 .registerTypeAdapter(TouchWandUnitDataAlarmSensor.class, new AlarmSensorUnitDataDeserializer())
72 touchWandUnit = builder.fromJson(jsonUnit, TouchWandUnitDataAlarmSensor.class);
75 touchWandUnit = new TouchWandUnknownTypeUnitData();
78 touchWandUnit = new TouchWandUnknownTypeUnitData();
81 if (touchWandUnit == null) {
82 touchWandUnit = new TouchWandUnknownTypeUnitData();
88 public static TouchWandUnitData parseResponse(String JsonUnit) {
89 final JsonParser jsonParser = new JsonParser();
90 TouchWandUnitData myTouchWandUnitData;
93 unitObj = jsonParser.parse(JsonUnit).getAsJsonObject();
94 myTouchWandUnitData = parseResponse(unitObj);
95 } catch (JsonParseException | IllegalStateException e) {
96 logger.warn("Could not parse response {}", JsonUnit);
97 myTouchWandUnitData = new TouchWandUnknownTypeUnitData(); // Return unknown type
99 return myTouchWandUnitData;