]> git.basschouten.com Git - openhab-addons.git/blob
c04d0bd89a70f207e50b06ff5da579366fd6b0ae
[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.services.dto.BoschSHCServiceState;
22
23 import com.google.gson.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParseException;
28
29 /**
30  * Utility class for JSON deserialization of device data and triggered scenarios using Google Gson.
31  *
32  * @author Patrick Gell - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class BoschServiceDataDeserializer implements JsonDeserializer<BoschSHCServiceState> {
37
38     @Nullable
39     @Override
40     public BoschSHCServiceState deserialize(JsonElement jsonElement, Type type,
41             JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
42
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             default -> {
62                 return new BoschSHCServiceState(dataType.getAsString());
63             }
64         }
65     }
66 }