]> git.basschouten.com Git - openhab-addons.git/blob
e1dcdf69ef27fb28be5014b3533d1b8c35b14f54
[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.netatmo.internal.deserialization;
14
15 import java.time.Instant;
16 import java.time.ZonedDateTime;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.NetatmoException;
21 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
22 import org.openhab.binding.netatmo.internal.api.dto.HomeData;
23 import org.openhab.core.i18n.TimeZoneProvider;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29
30 import com.google.gson.FieldNamingPolicy;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonDeserializer;
34 import com.google.gson.JsonSyntaxException;
35
36 /**
37  * The {@link NADeserializer} is responsible to instantiate suitable Gson (de)serializer
38  *
39  * @author GaĆ«l L'hopital - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = NADeserializer.class)
43 public class NADeserializer {
44     private final Gson gson;
45
46     @Activate
47     public NADeserializer(@Reference TimeZoneProvider timeZoneProvider) {
48         gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
49                 .registerTypeAdapterFactory(new StrictEnumTypeAdapterFactory())
50                 .registerTypeAdapter(NAObjectMap.class, new NAObjectMapDeserializer())
51                 .registerTypeAdapter(NAPushType.class, new NAPushTypeDeserializer())
52                 .registerTypeAdapter(ModuleType.class, new ModuleTypeDeserializer())
53                 .registerTypeAdapter(HomeData.class,
54                         (JsonDeserializer<HomeData>) (json, type, context) -> context.deserialize(json,
55                                 json.getAsJsonObject().has("therm_mode") ? HomeData.Energy.class
56                                         : HomeData.Security.class))
57                 .registerTypeAdapter(ZonedDateTime.class, (JsonDeserializer<ZonedDateTime>) (json, type, context) -> {
58                     long netatmoTS = json.getAsJsonPrimitive().getAsLong();
59                     Instant i = Instant.ofEpochSecond(netatmoTS);
60                     return ZonedDateTime.ofInstant(i, timeZoneProvider.getTimeZone());
61                 })
62                 .registerTypeAdapter(OnOffType.class,
63                         (JsonDeserializer<OnOffType>) (json, type, context) -> OnOffType
64                                 .from(json.getAsJsonPrimitive().getAsString()))
65                 .registerTypeAdapter(OpenClosedType.class, (JsonDeserializer<OpenClosedType>) (json, type, context) -> {
66                     String value = json.getAsJsonPrimitive().getAsString().toUpperCase();
67                     return "TRUE".equals(value) || "1".equals(value) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
68                 }).create();
69     }
70
71     public <T> T deserialize(Class<T> clazz, String json) throws NetatmoException {
72         try {
73             @Nullable
74             T result = gson.fromJson(json, clazz);
75             if (result != null) {
76                 return result;
77             }
78             throw new NetatmoException("Deserialization of '%s' resulted in null value", json);
79         } catch (JsonSyntaxException e) {
80             throw new NetatmoException(e, "Unexpected error deserializing '%s'", json);
81         }
82     }
83 }