]> git.basschouten.com Git - openhab-addons.git/blob
84fe4136432779d64868a7ec86f385373b3276a9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.boschshc.internal.serialization;
14
15 import java.lang.reflect.Type;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
20 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Scenario;
21 import org.openhab.binding.boschshc.internal.devices.bridge.dto.UserDefinedState;
22 import org.openhab.binding.boschshc.internal.services.dto.BoschSHCServiceState;
23
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParseException;
29
30 /**
31  * Utility class for JSON deserialization of device data and triggered scenarios using Google Gson.
32  *
33  * @author Patrick Gell - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class BoschServiceDataDeserializer implements JsonDeserializer<BoschSHCServiceState> {
38
39     @Nullable
40     @Override
41     public BoschSHCServiceState deserialize(JsonElement jsonElement, Type type,
42             JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
43         JsonObject jsonObject = jsonElement.getAsJsonObject();
44         JsonElement dataType = jsonObject.get("@type");
45         switch (dataType.getAsString()) {
46             case "DeviceServiceData" -> {
47                 var deviceServiceData = new DeviceServiceData();
48                 deviceServiceData.deviceId = jsonObject.get("deviceId").getAsString();
49                 deviceServiceData.state = jsonObject.get("state");
50                 deviceServiceData.id = jsonObject.get("id").getAsString();
51                 deviceServiceData.path = jsonObject.get("path").getAsString();
52                 return deviceServiceData;
53             }
54             case "scenarioTriggered" -> {
55                 var scenario = new Scenario();
56                 scenario.id = jsonObject.get("id").getAsString();
57                 scenario.name = jsonObject.get("name").getAsString();
58                 scenario.lastTimeTriggered = jsonObject.get("lastTimeTriggered").getAsString();
59                 return scenario;
60             }
61             case "userDefinedState" -> {
62                 var state = new UserDefinedState();
63                 state.setId(jsonObject.get("id").getAsString());
64                 state.setName(jsonObject.get("name").getAsString());
65                 state.setState(jsonObject.get("state").getAsBoolean());
66                 return state;
67             }
68             default -> {
69                 return new BoschSHCServiceState(dataType.getAsString());
70             }
71         }
72     }
73 }