]> git.basschouten.com Git - openhab-addons.git/blob
f56ef281622cdac84961948fbe1d009d6befe11a
[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.pixometer.internal.serializer;
14
15 import java.lang.reflect.Type;
16 import java.time.Instant;
17 import java.time.ZonedDateTime;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.pixometer.internal.config.ReadingInstance;
21
22 import com.google.gson.JsonDeserializationContext;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
27
28 /**
29  * Custom Deserializer for meter reading api responses
30  *
31  * @author Jerome Luckenbach - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class CustomReadingInstanceDeserializer implements JsonDeserializer<ReadingInstance> {
36
37     private static final String COUNT = "count";
38     private static final String RESULTS = "results";
39     private static final String URL = "url";
40     private static final String APPLIED_METHOD = "applied_method";
41     private static final String READING_DATE = "reading_date";
42     private static final String VALUE = "value";
43     private static final String VALUE_SECOND_TARIFF = "value_second_tariff";
44     private static final String PROVIDED_FRACTION_DIGITS = "provided_fraction_digits";
45     private static final String PROVIDED_FRACTION_DIGITS_SECOND_TARIFF = "provided_fraction_digits_second_tariff";
46
47     @Override
48     @NonNullByDefault({})
49     public ReadingInstance deserialize(final JsonElement json, final Type typeOfT,
50             final JsonDeserializationContext context) throws JsonParseException {
51         final JsonObject jsonObject = json.getAsJsonObject();
52
53         if (!jsonObject.has(COUNT)) {
54             throw new JsonParseException("Invalid Json Response");
55         }
56
57         ReadingInstance result = new ReadingInstance();
58         int resultCount = Integer.parseInt(jsonObject.get(COUNT).toString());
59
60         // No readings provided yet
61         if (resultCount < 1) {
62             result.setReadingDate(ZonedDateTime.from(Instant.EPOCH));
63             result.setValue(0);
64         }
65
66         // Fist result is the last reading instance
67         JsonObject latestReading = jsonObject.getAsJsonArray(RESULTS).get(0).getAsJsonObject();
68
69         result.setUrl(getStringFromJson(latestReading, URL));
70         result.setAppliedMethod(getStringFromJson(latestReading, APPLIED_METHOD));
71         result.setReadingDate(ZonedDateTime.parse(getStringFromJson(latestReading, READING_DATE)));
72         result.setValue(Double.parseDouble(getStringFromJson(latestReading, VALUE)));
73
74         // Not all meters provide useful data for second tariff and fraction digits , so zero should be used in case of
75         // a null value.
76         String secondTariffValue = getStringFromJson(latestReading, VALUE_SECOND_TARIFF);
77         result.setValueSecondTariff(
78                 checkStringForNullValues(secondTariffValue) ? 0 : Double.parseDouble(secondTariffValue));
79
80         String providedFractionDigits = getStringFromJson(latestReading, PROVIDED_FRACTION_DIGITS);
81         result.setProvidedFractionDigits(
82                 checkStringForNullValues(providedFractionDigits) ? 0 : Integer.parseInt(providedFractionDigits));
83
84         String secondprovidedFractionDigits = getStringFromJson(latestReading, PROVIDED_FRACTION_DIGITS_SECOND_TARIFF);
85         result.setProvidedFractionDigitsSecondTariff(checkStringForNullValues(secondprovidedFractionDigits) ? 0
86                 : Integer.parseInt(secondprovidedFractionDigits));
87
88         return result;
89     }
90
91     /**
92      * Returns the node value and removes possible added quotation marks, which would lead to parse errors.
93      *
94      * @param data The Json source to get the string from
95      * @param key The key for the wanted Json Node
96      * @return The wanted string without unnecessary quotation marks
97      */
98     private String getStringFromJson(JsonObject data, String key) {
99         return data.get(key).toString().replaceAll("\"", "");
100     }
101
102     /**
103      * @param s the striong to check
104      * @return returns true if null values have been found, false otherwise
105      */
106     private boolean checkStringForNullValues(String s) {
107         return (s == null || s.isEmpty() || s.equals("null"));
108     }
109 }