2 * Copyright (c) 2010-2021 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 {
39 private double value = 0;
41 private final MeteringTypeEnum meteringType;
42 private MeteringUnitsEnum meteringUnit;
43 private final Logger logger = LoggerFactory.getLogger(JSONCachedMeteringValueImpl.class);
46 * Creates a new {@link JSONCachedMeteringValueImpl}.
48 * @param jObject must not be null
49 * @param meteringType must not be null
50 * @param meteringUnit must not be null
52 public JSONCachedMeteringValueImpl(JsonObject jObject, MeteringTypeEnum meteringType,
53 MeteringUnitsEnum meteringUnit) {
54 this.meteringType = meteringType;
55 if (meteringUnit != null) {
56 this.meteringUnit = meteringUnit;
58 this.meteringUnit = MeteringUnitsEnum.WH;
60 if (jObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()) != null) {
61 this.dsid = new DSID(jObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()).getAsString());
63 if (jObject.get(JSONApiResponseKeysEnum.VALUE.getKey()) != null) {
64 this.value = jObject.get(JSONApiResponseKeysEnum.VALUE.getKey()).getAsDouble();
66 if (jObject.get(JSONApiResponseKeysEnum.DATE.getKey()) != null) {
67 this.date = jObject.get(JSONApiResponseKeysEnum.DATE.getKey()).getAsString();
72 public DSID getDsid() {
77 public double getValue() {
82 public String getDate() {
87 public Date getDateAsDate() {
88 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
90 return formatter.parse(date);
91 } catch (ParseException e) {
92 logger.error("A ParseException occurred by parsing date string: {}", date, e);
98 public MeteringTypeEnum getMeteringType() {
103 public MeteringUnitsEnum getMeteringUnit() {
108 public String toString() {
109 return "dSID: " + this.getDsid() + ", metering-type " + meteringType.toString() + ", metering-unit "
110 + meteringUnit + ", date: " + this.getDate() + ", value: " + this.getValue();