2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.deserialization;
15 import java.lang.reflect.Type;
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;
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
29 * Specialized deserializer for push_type field
31 * @author Gaƫl L'hopital - Initial contribution
34 class NAPushTypeDeserializer implements JsonDeserializer<NAPushType> {
36 private final Logger logger = LoggerFactory.getLogger(NAPushTypeDeserializer.class);
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;
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();
53 if (moduleType.equals(ModuleType.UNKNOWN) || eventType.equals(EventType.UNKNOWN)) {
54 logger.warn("Unknown module or event type: {}, deserialized to '{}-{}'", string, moduleType, eventType);
57 return new NAPushType(moduleType, eventType);
61 * @param apiName : Netatmo Object name (NSD, NACamera...)
62 * @return moduletype value if found, or else Unknown
64 public static ModuleType fromNetatmoObject(String apiName) {
65 return ModuleType.AS_SET.stream().filter(mt -> apiName.equals(mt.apiName)).findFirst()
66 .orElse(ModuleType.UNKNOWN);
70 * @param apiName : Netatmo Event name (hush, off, on ...)
71 * @return eventType value if found, or else Unknown
73 public static EventType fromEvent(String apiName) {
74 return EventType.AS_SET.stream().filter(et -> apiName.equalsIgnoreCase(et.name())).findFirst()
75 .orElse(EventType.UNKNOWN);