]> git.basschouten.com Git - openhab-addons.git/blob
97cef0fa2ea1b799fea4ad4d8fe40b74718b33a4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
14 package org.openhab.binding.touchwand.internal.dto;
15
16 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
17
18 import java.util.Arrays;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
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;
29
30 /**
31  * The {@link TouchWandUnitFromJson} parse Json unit data
32  *
33  * @author Roie Geron - Initial contribution
34  */
35 @NonNullByDefault
36 public class TouchWandUnitFromJson {
37
38     private final static Logger logger = LoggerFactory.getLogger(TouchWandUnitFromJson.class);
39
40     public TouchWandUnitFromJson() {
41     }
42
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)) {
48             type = TYPE_UNKNOWN;
49         }
50
51         if (!jsonUnit.has("currStatus") || (jsonUnit.get("currStatus") == null)) {
52             type = TYPE_UNKNOWN;
53         }
54
55         switch (type) {
56             case TYPE_WALLCONTROLLER:
57                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);
58                 break;
59             case TYPE_SWITCH:
60                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
61                 break;
62             case TYPE_DIMMER:
63                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
64                 break;
65             case TYPE_SHUTTER:
66                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
67                 break;
68             case TYPE_ALARMSENSOR:
69                 Gson builder = new GsonBuilder()
70                         .registerTypeAdapter(TouchWandUnitDataAlarmSensor.class, new AlarmSensorUnitDataDeserializer())
71                         .create();
72                 touchWandUnit = builder.fromJson(jsonUnit, TouchWandUnitDataAlarmSensor.class);
73                 break;
74             case TYPE_UNKNOWN:
75                 touchWandUnit = new TouchWandUnknownTypeUnitData();
76                 break;
77             default:
78                 touchWandUnit = new TouchWandUnknownTypeUnitData();
79         }
80
81         if (touchWandUnit == null) {
82             touchWandUnit = new TouchWandUnknownTypeUnitData();
83         }
84
85         return touchWandUnit;
86     }
87
88     public static TouchWandUnitData parseResponse(String JsonUnit) {
89         final JsonParser jsonParser = new JsonParser();
90         TouchWandUnitData myTouchWandUnitData;
91         JsonObject unitObj;
92         try {
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
98         }
99         return myTouchWandUnitData;
100     }
101 }