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.digitalstrom.internal.lib.structure.devices.deviceparameters.impl;
15 import java.text.DateFormat;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
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;
27 import com.google.gson.JsonObject;
30 * The {@link JSONCachedMeteringValueImpl} is the implementation of the {@link CachedMeteringValue}.
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()
36 public class JSONCachedMeteringValueImpl implements CachedMeteringValue {
40 private double value = 0;
42 private final MeteringTypeEnum meteringType;
43 private MeteringUnitsEnum meteringUnit;
44 private final Logger logger = LoggerFactory.getLogger(JSONCachedMeteringValueImpl.class);
47 * Creates a new {@link JSONCachedMeteringValueImpl}.
49 * @param jObject must not be null
50 * @param meteringType must not be null
51 * @param meteringUnit must not be null
53 public JSONCachedMeteringValueImpl(JsonObject jObject, MeteringTypeEnum meteringType,
54 MeteringUnitsEnum meteringUnit) {
55 this.meteringType = meteringType;
56 if (meteringUnit != null) {
57 this.meteringUnit = meteringUnit;
59 this.meteringUnit = MeteringUnitsEnum.WH;
61 if (jObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()) != null) {
62 this.dsid = new DSID(jObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()).getAsString());
64 if (jObject.get(JSONApiResponseKeysEnum.DSUID.getKey()) != null) {
65 this.dsuid = new DSUID(jObject.get(JSONApiResponseKeysEnum.DSUID.getKey()).getAsString());
67 if (jObject.get(JSONApiResponseKeysEnum.VALUE.getKey()) != null) {
68 this.value = jObject.get(JSONApiResponseKeysEnum.VALUE.getKey()).getAsDouble();
70 if (jObject.get(JSONApiResponseKeysEnum.DATE.getKey()) != null) {
71 this.date = jObject.get(JSONApiResponseKeysEnum.DATE.getKey()).getAsString();
76 public DSUID getDsuid() {
81 @Deprecated(since = "value removed in API since dss v1.19.2")
82 public DSID getDsid() {
87 public double getValue() {
92 public String getDate() {
97 public Date getDateAsDate() {
98 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
100 return formatter.parse(date);
101 } catch (ParseException e) {
102 logger.error("A ParseException occurred by parsing date string: {}", date, e);
108 public MeteringTypeEnum getMeteringType() {
113 public MeteringUnitsEnum getMeteringUnit() {
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();