]> git.basschouten.com Git - openhab-addons.git/blob
a5a5a5e5527f0c8145f7667280961c9ef5b22c31
[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.structure.devices.deviceparameters.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.serverconnection.constants.JSONApiResponseKeysEnum;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.CachedMeteringValue;
22 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum;
23 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringUnitsEnum;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.gson.JsonObject;
28
29 /**
30  * The {@link JSONCachedMeteringValueImpl} is the implementation of the {@link CachedMeteringValue}.
31  *
32  * @author Alexander Betker - Initial contribution
33  * @author Michael Ochel - change from SimpleJSON to GSON, add getDateAsDate()
34  * @author Matthias Siegele - change from SimpleJSON to GSON, add getDateAsDate()
35  */
36 public class JSONCachedMeteringValueImpl implements CachedMeteringValue {
37
38     private DSID dsid;
39     private DSUID dsuid;
40     private double value = 0;
41     private String date;
42     private final MeteringTypeEnum meteringType;
43     private MeteringUnitsEnum meteringUnit;
44     private final Logger logger = LoggerFactory.getLogger(JSONCachedMeteringValueImpl.class);
45
46     /**
47      * Creates a new {@link JSONCachedMeteringValueImpl}.
48      *
49      * @param jObject must not be null
50      * @param meteringType must not be null
51      * @param meteringUnit must not be null
52      */
53     public JSONCachedMeteringValueImpl(JsonObject jObject, MeteringTypeEnum meteringType,
54             MeteringUnitsEnum meteringUnit) {
55         this.meteringType = meteringType;
56         if (meteringUnit != null) {
57             this.meteringUnit = meteringUnit;
58         } else {
59             this.meteringUnit = MeteringUnitsEnum.WH;
60         }
61         if (jObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()) != null) {
62             this.dsid = new DSID(jObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()).getAsString());
63         }
64         if (jObject.get(JSONApiResponseKeysEnum.DSUID.getKey()) != null) {
65             this.dsuid = new DSUID(jObject.get(JSONApiResponseKeysEnum.DSUID.getKey()).getAsString());
66         }
67         if (jObject.get(JSONApiResponseKeysEnum.VALUE.getKey()) != null) {
68             this.value = jObject.get(JSONApiResponseKeysEnum.VALUE.getKey()).getAsDouble();
69         }
70         if (jObject.get(JSONApiResponseKeysEnum.DATE.getKey()) != null) {
71             this.date = jObject.get(JSONApiResponseKeysEnum.DATE.getKey()).getAsString();
72         }
73     }
74
75     @Override
76     public DSUID getDsuid() {
77         return dsuid;
78     }
79
80     @Override
81     @Deprecated(since = "value removed in API since dss v1.19.2")
82     public DSID getDsid() {
83         return dsid;
84     }
85
86     @Override
87     public double getValue() {
88         return value;
89     }
90
91     @Override
92     public String getDate() {
93         return date;
94     }
95
96     @Override
97     public Date getDateAsDate() {
98         DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
99         try {
100             return formatter.parse(date);
101         } catch (ParseException e) {
102             logger.error("A ParseException occurred by parsing date string: {}", date, e);
103         }
104         return null;
105     }
106
107     @Override
108     public MeteringTypeEnum getMeteringType() {
109         return meteringType;
110     }
111
112     @Override
113     public MeteringUnitsEnum getMeteringUnit() {
114         return meteringUnit;
115     }
116
117     @Override
118     public String toString() {
119         return "dSUID: " + this.getDsuid() + ", dSID: " + this.getDsid() + ", metering-type " + meteringType.toString()
120                 + ", metering-unit " + meteringUnit + ", date: " + this.getDate() + ", value: " + this.getValue();
121     }
122 }