]> git.basschouten.com Git - openhab-addons.git/blob
255b9fb2396d76a6716956620c1cb7920172837f
[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 com.google.gson.Gson;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23
24 /**
25  * The {@link TouchWandUnitFromJson} parse Json unit data
26  *
27  * @author Roie Geron - Initial contribution
28  */
29 public class TouchWandUnitFromJson {
30
31     public TouchWandUnitFromJson() {
32     }
33
34     public static TouchWandUnitData parseResponse(JsonObject jsonUnit) {
35         final Gson gson = new Gson();
36         TouchWandUnitData touchWandUnit;
37         String type = jsonUnit.get("type").getAsString();
38         if (!Arrays.asList(SUPPORTED_TOUCHWAND_TYPES).contains(type)) {
39             return null;
40         }
41
42         if (!jsonUnit.has("currStatus") || (jsonUnit.get("currStatus") == null)) {
43             return null;
44         }
45
46         switch (type) {
47             case TYPE_WALLCONTROLLER:
48                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);
49                 break;
50             case TYPE_SWITCH:
51                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
52                 break;
53             case TYPE_DIMMER:
54                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
55                 break;
56             case TYPE_SHUTTER:
57                 touchWandUnit = gson.fromJson(jsonUnit, TouchWandShutterSwitchUnitData.class);
58                 break;
59             default:
60                 return null;
61         }
62
63         return touchWandUnit;
64     }
65
66     public static TouchWandUnitData parseResponse(String JsonUnit) {
67         final JsonParser jsonParser = new JsonParser();
68         JsonObject unitObj = jsonParser.parse(JsonUnit).getAsJsonObject();
69         return parseResponse(unitObj);
70     }
71 }