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