From 82ca3311f116f014b15f8725e9d8b14bb72ca908 Mon Sep 17 00:00:00 2001 From: Konstantin Polihronov Date: Thu, 22 Jun 2023 14:17:23 +0300 Subject: [PATCH] Added a raw data channel that contains the raw data as JSON string Signed-off-by: Konstantin Polihronov --- .../solax/internal/SolaxBindingConstants.java | 1 + .../solax/internal/SolaxInverterHandler.java | 2 ++ .../rawdata/LocalConnectRawDataBean.java | 29 +++++++++++++++---- .../connectivity/rawdata/RawDataBean.java | 6 ++-- .../solax/internal/model/InverterData.java | 3 +- .../resources/OH-INF/thing/channel_types.xml | 5 ++++ .../main/resources/OH-INF/thing/inverter.xml | 2 ++ 7 files changed, 39 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxBindingConstants.java b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxBindingConstants.java index dfa63e28eb549..34e11e71c76ae 100644 --- a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxBindingConstants.java +++ b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxBindingConstants.java @@ -67,4 +67,5 @@ public class SolaxBindingConstants { public static final String FEED_IN_POWER = "feedInPower"; public static final String TIMESTAMP = "lastUpdateTime"; + public static final String RAW_DATA = "rawData"; } diff --git a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxInverterHandler.java b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxInverterHandler.java index da79e98d15e33..9689b6ac8a66f 100644 --- a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxInverterHandler.java +++ b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/SolaxInverterHandler.java @@ -18,6 +18,7 @@ import org.openhab.binding.solax.internal.model.InverterData; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.QuantityType; +import org.openhab.core.library.types.StringType; import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.Units; import org.openhab.core.thing.ChannelUID; @@ -96,6 +97,7 @@ private void transferInverterDataToChannels(InverterData data) { updateState(SolaxBindingConstants.FEED_IN_POWER, new QuantityType<>(data.getFeedInPower(), Units.WATT)); updateState(SolaxBindingConstants.TIMESTAMP, new DateTimeType(ZonedDateTime.now())); + updateState(SolaxBindingConstants.RAW_DATA, new StringType(data.getRawData())); } @Override diff --git a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/LocalConnectRawDataBean.java b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/LocalConnectRawDataBean.java index 9092160c9f641..905ed1599daf3 100644 --- a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/LocalConnectRawDataBean.java +++ b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/LocalConnectRawDataBean.java @@ -19,6 +19,8 @@ import org.openhab.binding.solax.internal.model.InverterData; import org.openhab.binding.solax.internal.model.InverterType; import org.openhab.binding.solax.internal.util.GsonSupplier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; @@ -31,11 +33,15 @@ */ @NonNullByDefault public class LocalConnectRawDataBean implements RawDataBean, InverterData { + + private final static Logger logger = LoggerFactory.getLogger(LocalConnectRawDataBean.class); + private @Nullable String sn; private @Nullable String ver; private int type; private short @Nullable [] Data; private String @Nullable [] Information; + private @Nullable String rawData; @Override public String toString() { @@ -83,6 +89,15 @@ public void setInformation(String @Nullable [] information) { Information = information; } + @Override + public @Nullable String getRawData() { + return rawData; + } + + public void setRawData(String rawData) { + this.rawData = rawData; + } + public static @Nullable LocalConnectRawDataBean fromJson(@Nullable String json) { if (json == null) { throw new IllegalArgumentException("Provided input JSON is null."); @@ -90,9 +105,11 @@ public void setInformation(String @Nullable [] information) { try { Gson gson = GsonSupplier.getInstance(); - return gson.fromJson(json, LocalConnectRawDataBean.class); + LocalConnectRawDataBean deserializedObject = gson.fromJson(json, LocalConnectRawDataBean.class); + deserializedObject.setRawData(json); + return deserializedObject; } catch (JsonSyntaxException e) { - // TODO log it + logger.warn("Unable to deserialize from JSON. Exception:{}", e); return null; } } @@ -215,12 +232,12 @@ public long getTotalConsumption() { } private short getData(int index) { - if (Data != null) { - try { + try { + if (Data != null) { return Data[index]; - } catch (IndexOutOfBoundsException e) { - // TODO log it } + } catch (IndexOutOfBoundsException e) { + logger.debug("Tried to get data out of bounds of the raw data array. Exception: {}", e); } return 0; } diff --git a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/RawDataBean.java b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/RawDataBean.java index 772432734c771..5edcddf4824b1 100644 --- a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/RawDataBean.java +++ b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/connectivity/rawdata/RawDataBean.java @@ -13,14 +13,16 @@ package org.openhab.binding.solax.internal.connectivity.rawdata; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; /** * The {@link RawDataBean} is interface which should be implemented by all types of raw information that is retrieved - * (currently we retrieve a data from a Solax inverter locally or their cloud API) + * (the idea is to retrieve a raw data from a Solax inverter locally or their cloud API) * * @author Konstantin Polihronov - Initial contribution */ @NonNullByDefault public interface RawDataBean { - + @Nullable + String getRawData(); } diff --git a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/model/InverterData.java b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/model/InverterData.java index 140176496ac9e..6d4427268f1d4 100644 --- a/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/model/InverterData.java +++ b/bundles/org.openhab.binding.solax/src/main/java/org/openhab/binding/solax/internal/model/InverterData.java @@ -14,6 +14,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.solax.internal.connectivity.rawdata.RawDataBean; /** * The {@link InverterData} interface should implement the interface that returns the parsed data in human readable code @@ -22,7 +23,7 @@ * @author Konstantin Polihronov - Initial contribution */ @NonNullByDefault -public interface InverterData { +public interface InverterData extends RawDataBean { @Nullable String getWifiSerial(); diff --git a/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/channel_types.xml b/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/channel_types.xml index a9dc615995c7a..c2b496ce07f4a 100644 --- a/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/channel_types.xml +++ b/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/channel_types.xml @@ -19,5 +19,10 @@ Last time with a successful retrieval of data + + String + + Raw data retrieved from system as JSON + diff --git a/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/inverter.xml b/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/inverter.xml index 8c7f7b6eb15d2..f22214da55636 100644 --- a/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/inverter.xml +++ b/bundles/org.openhab.binding.solax/src/main/resources/OH-INF/thing/inverter.xml @@ -38,6 +38,8 @@ + +