]> git.basschouten.com Git - openhab-addons.git/blob
c4c5a8fd4a44ef349162778861bb90498efca5b2
[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.netatmo.internal.deserialization;
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.netatmo.internal.api.data.EventType;
20 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27
28 /**
29  * Specialized deserializer for push_type field
30  *
31  * @author GaĆ«l L'hopital - Initial contribution
32  */
33 @NonNullByDefault
34 class NAPushTypeDeserializer implements JsonDeserializer<NAPushType> {
35
36     private final Logger logger = LoggerFactory.getLogger(NAPushTypeDeserializer.class);
37
38     @Override
39     public @Nullable NAPushType deserialize(JsonElement json, Type clazz, JsonDeserializationContext context) {
40         final String string = json.getAsString();
41         final String[] elements = string.split("-");
42         ModuleType moduleType = ModuleType.UNKNOWN;
43         EventType eventType = EventType.UNKNOWN;
44
45         if (elements.length == 2) {
46             moduleType = fromNetatmoObject(elements[0]);
47             eventType = fromEvent(elements[1]);
48         } else if (elements.length == 1) {
49             eventType = fromEvent(string);
50             moduleType = eventType.getFirstModule();
51         }
52
53         if (moduleType.equals(ModuleType.UNKNOWN) || eventType.equals(EventType.UNKNOWN)) {
54             logger.warn("Unknown module or event type: {}, deserialized to '{}-{}'", string, moduleType, eventType);
55         }
56
57         return new NAPushType(moduleType, eventType);
58     }
59
60     /**
61      * @param apiName : Netatmo Object name (NSD, NACamera...)
62      * @return moduletype value if found, or else Unknown
63      */
64     public static ModuleType fromNetatmoObject(String apiName) {
65         return ModuleType.AS_SET.stream().filter(mt -> apiName.equals(mt.apiName)).findFirst()
66                 .orElse(ModuleType.UNKNOWN);
67     }
68
69     /**
70      * @param apiName : Netatmo Event name (hush, off, on ...)
71      * @return eventType value if found, or else Unknown
72      */
73     public static EventType fromEvent(String apiName) {
74         return EventType.AS_SET.stream().filter(et -> apiName.equalsIgnoreCase(et.name())).findFirst()
75                 .orElse(EventType.UNKNOWN);
76     }
77 }