Skip to content

Commit

Permalink
Added a raw data channel that contains the raw data as JSON string
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Polihronov <polychronov@gmail.com>
  • Loading branch information
theater committed Jun 22, 2023
1 parent a8c5813 commit 82ca331
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -83,16 +89,27 @@ 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.");
}

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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +23,7 @@
* @author Konstantin Polihronov - Initial contribution
*/
@NonNullByDefault
public interface InverterData {
public interface InverterData extends RawDataBean {
@Nullable
String getWifiSerial();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
<label>Last retrieve time stamp</label>
<description>Last time with a successful retrieval of data</description>
</channel-type>
<channel-type id="rawDataType">
<item-type>String</item-type>
<label>Raw Data</label>
<description>Raw data retrieved from system as JSON</description>
</channel-type>

</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

<channel id="lastUpdateTime" typeId="lastRetrieveTimeStamp"/>

<channel id="rawData" typeId="rawDataType"/>

<channel id="onGridTotalYield" typeId="system.electric-power"/>
<channel id="onGridDailyYield" typeId="system.electric-power"/>
<channel id="onTotalFeedinEnergy" typeId="system.electric-power"/>
Expand Down

0 comments on commit 82ca331

Please sign in to comment.