]> git.basschouten.com Git - openhab-addons.git/blob
a6635208f68fc712d9e553f78d2ddf59d6a49f3a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 static final 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         switch (type) {
51             case TYPE_WALLCONTROLLER:
52                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);
53                 break;
54             case TYPE_SWITCH:
55                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
56                 break;
57             case TYPE_DIMMER:
58                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
59                 break;
60             case TYPE_SHUTTER:
61                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
62                 break;
63             case TYPE_ALARMSENSOR:
64                 Gson builder = new GsonBuilder()
65                         .registerTypeAdapter(TouchWandUnitDataAlarmSensor.class, new AlarmSensorUnitDataDeserializer())
66                         .create();
67                 touchWandUnit = builder.fromJson(jsonUnit, TouchWandUnitDataAlarmSensor.class);
68                 break;
69             case TYPE_BSENSOR:
70                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandBSensorUnitData.class);
71                 break;
72             case TYPE_THERMOSTAT:
73                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandThermostatUnitData.class);
74                 break;
75             case TYPE_UNKNOWN:
76                 touchWandUnit = new TouchWandUnknownTypeUnitData();
77                 break;
78             default:
79                 touchWandUnit = new TouchWandUnknownTypeUnitData();
80         }
81
82         if (touchWandUnit == null) {
83             touchWandUnit = new TouchWandUnknownTypeUnitData();
84         }
85
86         return touchWandUnit;
87     }
88
89     public static TouchWandUnitData parseResponse(String JsonUnit) {
90         TouchWandUnitData myTouchWandUnitData;
91         JsonObject unitObj;
92         try {
93             unitObj = JsonParser.parseString(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 }