Skip to content

Commit

Permalink
Pzem shunt should work now
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Feb 19, 2021
1 parent 326e876 commit aa21ce7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract class PacketHandlingOptionBase extends TimeZoneOptionBase implements Pa
@JsonProperty
private Integer unique = null;

@JsonProperty("request")
private @Nullable List<DataRequester> dataRequesterList;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import me.retrodaredevil.solarthing.annotations.JsonExplicit;

@JsonSubTypes({
@JsonSubTypes.Type(MateProgramOptions.class),
Expand All @@ -12,6 +13,7 @@
@JsonSubTypes.Type(AutomationProgramOptions.class),
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonExplicit
public interface ProgramOptions {
ProgramType getProgramType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import me.retrodaredevil.solarthing.packets.handling.PacketListReceiver;
import me.retrodaredevil.solarthing.solar.pzem.modbus.PzemShuntModbusSlaveRead;

@JsonSubTypes({
@JsonSubTypes.Type(RaspberryPiCpuTemperatureDataRequester.class),
@JsonSubTypes.Type(W1TemperatureDataRequester.class),
@JsonSubTypes.Type(BatteryVoltageIODataRequester.class),
@JsonSubTypes.Type(PzemShuntModbusSlaveRead.class),
@JsonSubTypes.Type(PzemShuntDataRequester.class),
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface DataRequester {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class ImmutablePzemShuntStatusPacket implements PzemShuntStatusPacket {
private final int modbusAddress;
@JsonCreator
public ImmutablePzemShuntStatusPacket(
@JsonProperty("dataId") int dataId,
@JsonProperty("voltageValueRaw") int voltageValueRaw, @JsonProperty("currentValueRaw") int currentValueRaw,
@JsonProperty("powerValueRaw") int powerValueRaw, @JsonProperty("energyValueRaw") int energyValueRaw,
@JsonProperty("highVoltageAlarmStatus") int highVoltageAlarmStatus, @JsonProperty("lowVoltageAlarmStatus") int lowVoltageAlarmStatus,
@JsonProperty("modbusAddress") int modbusAddress) {
@JsonProperty(value = "dataId", required = true) int dataId,
@JsonProperty(value = "voltageValueRaw", required = true) int voltageValueRaw, @JsonProperty(value = "currentValueRaw", required = true) int currentValueRaw,
@JsonProperty(value = "powerValueRaw", required = true) int powerValueRaw, @JsonProperty(value = "energyValueRaw", required = true) int energyValueRaw,
@JsonProperty(value = "highVoltageAlarmStatus", required = true) int highVoltageAlarmStatus, @JsonProperty(value = "lowVoltageAlarmStatus", required = true) int lowVoltageAlarmStatus,
@JsonProperty(value = "modbusAddress", required = true) int modbusAddress) {
this.identifier = new DataIdentifier(dataId);
this.identityInfo = new PzemShuntIdentityInfo(dataId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import me.retrodaredevil.io.serial.SerialConfig;
import me.retrodaredevil.io.serial.SerialConfigBuilder;
import me.retrodaredevil.solarthing.solar.common.Shunt;
import me.retrodaredevil.solarthing.annotations.JsonExplicit;

@JsonExplicit
public interface PzemShuntReadTable {
SerialConfig SERIAL_CONFIG = new SerialConfigBuilder(9600).setDataBits(8).setStopBits(SerialConfig.StopBits.TWO).build();

Expand All @@ -13,20 +14,25 @@ public interface PzemShuntReadTable {
default float getVoltage() {
return getVoltageValueRaw() / 100.0f;
}
@JsonProperty("currentValueRaw")
int getCurrentValueRaw();
default float getCurrentAmps() {
return getCurrentValueRaw() / 100.0f;
}
@JsonProperty("powerValueRaw")
int getPowerValueRaw();
default float getPowerWatts() {
return getPowerValueRaw() / 10.0f;
}

@JsonProperty("energyValueRaw")
int getEnergyValueRaw();
default int getEnergyWattHours() { return getEnergyValueRaw(); }
default float getEnergyKWH() { return getEnergyValueRaw() / 1000.0f; }

@JsonProperty("highVoltageAlarmStatus")
int getHighVoltageAlarmStatus();
@JsonProperty("lowVoltageAlarmStatus")
int getLowVoltageAlarmStatus();

default boolean isHighVoltageAlarm() { return getHighVoltageAlarmStatus() != 0; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.retrodaredevil.solarthing.solar.pzem;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.retrodaredevil.solarthing.util.JacksonUtil;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class PzemTest {
@Test
void test() throws JsonProcessingException {

String json = "{\n" +
" \n" +
"\"packetType\" : \"PZEM_SHUNT\",\n" +
" \"dataId\" : 1,\n" +
" \"voltageValueRaw\" : 25,\n" +
" \"currentValueRaw\" : 4,\n" +
" \"powerValueRaw\" : 100,\n" +
" \"energyValueRaw\" : 2210,\n" +
" \"highVoltageAlarmStatus\" : 0,\n" +
" \"lowVoltageAlarmStatus\" : 0,\n" +
" \"modbusAddress\" : 1\n" +
"}";
ObjectMapper mapper = JacksonUtil.defaultMapper();
PzemShuntStatusPacket parsedPacket = mapper.readValue(json, PzemShuntStatusPacket.class);
String generatedJson = mapper.writeValueAsString(parsedPacket);
PzemShuntStatusPacket reparsedPacket = mapper.readValue(generatedJson, PzemShuntStatusPacket.class);
for (PzemShuntStatusPacket packet : new PzemShuntStatusPacket[] { parsedPacket, reparsedPacket}) {
assertEquals(1, packet.getDataId());
assertEquals(25, packet.getVoltageValueRaw());
assertEquals(4, packet.getCurrentValueRaw());
assertEquals(100, packet.getPowerValueRaw());
assertEquals(2210, packet.getEnergyValueRaw());
assertEquals(0, packet.getHighVoltageAlarmStatus());
assertEquals(0, packet.getLowVoltageAlarmStatus());
assertFalse(packet.isHighVoltageAlarm());
assertFalse(packet.isLowVoltageAlarm());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void test() throws JsonProcessingException {
assertNull(roverStatusPacket.getSensingTimeDelayRaw());
assertNull(roverStatusPacket.getLEDLoadCurrentRaw());
assertNull(roverStatusPacket.getSpecialPowerControlE02DRaw());
assertNull(roverStatusPacket.getPacketVersion());
assertFalse(roverStatusPacket.supportsMesLoad());
}
}

0 comments on commit aa21ce7

Please sign in to comment.