]> git.basschouten.com Git - openhab-addons.git/blob
b9280258ad9a373f884db6366ff397861f9cda34
[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.digitalstrom.internal.lib.climate.jsonresponsecontainer.impl;
14
15 import java.text.DateFormat;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
19
20 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.BaseSensorValues;
21 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonObject;
26
27 /**
28  * The {@link WeatherSensorData} acts as container for the digitalSTROM json-method <i>getSensorValues</i>. The
29  * {@link WeatherSensorData} contains all
30  * {@link org.openhab.binding.digitalstrom.internal.lib.climate.datatypes.CachedSensorValue}s and weather service
31  * information of the digitalSTROM-server, if a weather service is set.
32  *
33  * @author Michael Ochel - Initial contribution
34  * @author Matthias Siegele - Initial contribution
35  */
36 public class WeatherSensorData extends BaseSensorValues {
37
38     private final Logger logger = LoggerFactory.getLogger(WeatherSensorData.class);
39
40     private String weatherIconId;
41     private String weatherConditionId;
42     private String weatherServiceId;
43     private String weatherServiceTime;
44
45     /**
46      * Creates a new {@link SensorValues} through the {@link JsonObject} that will be returned by an apartment call.
47      *
48      * @param jObject must not be null
49      */
50     public WeatherSensorData(JsonObject jObject) {
51         super.addSensorValue(jObject, true);
52         if (jObject.get(JSONApiResponseKeysEnum.WEATHER_ICON_ID.getKey()) != null) {
53             weatherIconId = jObject.get(JSONApiResponseKeysEnum.WEATHER_ICON_ID.getKey()).getAsString();
54         }
55         if (jObject.get(JSONApiResponseKeysEnum.WEATHER_CONDITION_ID.getKey()) != null) {
56             weatherConditionId = jObject.get(JSONApiResponseKeysEnum.WEATHER_CONDITION_ID.getKey()).getAsString();
57         }
58         if (jObject.get(JSONApiResponseKeysEnum.WEATHER_SERVICE_ID.getKey()) != null) {
59             weatherServiceId = jObject.get(JSONApiResponseKeysEnum.WEATHER_SERVICE_ID.getKey()).getAsString();
60         }
61         if (jObject.get(JSONApiResponseKeysEnum.WEATHER_SERVICE_TIME.getKey()) != null) {
62             weatherServiceTime = jObject.get(JSONApiResponseKeysEnum.WEATHER_SERVICE_TIME.getKey()).getAsString();
63         }
64     }
65
66     /**
67      * Returns the weather icon id of the set weather service.
68      *
69      * @return the weatherIconId
70      */
71     public String getWeatherIconId() {
72         return weatherIconId;
73     }
74
75     /**
76      * Returns the weather condition id of the set weather service.
77      *
78      * @return the weatherConditionId
79      */
80     public String getWeatherConditionId() {
81         return weatherConditionId;
82     }
83
84     /**
85      * Returns the weather service id of the set weather service.
86      *
87      * @return the weatherServiceId
88      */
89     public String getWeatherServiceId() {
90         return weatherServiceId;
91     }
92
93     /**
94      * Returns the weather service time as {@link String} of the set weather service.
95      *
96      * @return the weatherServiceTime as {@link String}
97      */
98     public String getWeatherServiceTimeAsSting() {
99         return weatherServiceTime;
100     }
101
102     /**
103      * Returns the weather service time as {@link Date} of the set weather service.
104      *
105      * @return the weatherServiceTime as {@link Date}
106      */
107     public Date getWeatherServiceTimeAsDate() {
108         DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SS");
109         try {
110             return formatter.parse(weatherServiceTime);
111         } catch (ParseException e) {
112             logger.error("A ParseException occurred by parsing date string: {}", weatherServiceTime, e);
113         }
114         return null;
115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see java.lang.Object#toString()
121      */
122     @Override
123     public String toString() {
124         return "WeatherSensorData [weatherIconId=" + weatherIconId + ", weatherConditionId=" + weatherConditionId
125                 + ", weatherServiceId=" + weatherServiceId + ", weatherServiceTime=" + weatherServiceTime + ", "
126                 + super.toString() + "]";
127     }
128 }