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