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