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