Skip to content

Commit

Permalink
Started adding pzem shunt code, fixed bug that affected many rover re…
Browse files Browse the repository at this point in the history
…adings
  • Loading branch information
retrodaredevil committed Feb 18, 2021
1 parent c569b3e commit 326e876
Show file tree
Hide file tree
Showing 22 changed files with 418 additions and 65 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/josh.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
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),
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface DataRequester {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.retrodaredevil.solarthing.config.request;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import me.retrodaredevil.io.IOBundle;
import me.retrodaredevil.io.modbus.*;
import me.retrodaredevil.solarthing.annotations.JsonExplicit;
import me.retrodaredevil.solarthing.packets.handling.PacketListReceiver;
import me.retrodaredevil.solarthing.program.PzemShuntPacketListUpdater;
import me.retrodaredevil.solarthing.program.SolarMain;
import me.retrodaredevil.solarthing.solar.pzem.PzemShuntReadTable;
import me.retrodaredevil.solarthing.solar.pzem.modbus.PzemShuntModbusSlaveRead;

import java.io.File;

@JsonTypeName("pzem")
@JsonExplicit
public class PzemShuntDataRequester implements DataRequester {
private final File ioBundleFile;
private final int dataId;
private final int modbusAddress;

@JsonCreator
public PzemShuntDataRequester(
@JsonProperty(value = "io", required = true) File ioBundleFile,
@JsonProperty(value = "data_id", required = true) int dataId,
@JsonProperty(value = "modbus", required = true) int modbusAddress) {
this.ioBundleFile = ioBundleFile;
this.dataId = dataId;
this.modbusAddress = modbusAddress;
}

@Override
public PacketListReceiver createPacketListReceiver(PacketListReceiver eventPacketReceiver) {
final IOBundle ioBundle = SolarMain.createIOBundle(ioBundleFile, PzemShuntReadTable.SERIAL_CONFIG);
ModbusSlaveBus bus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder());
ModbusSlave slave = new ImmutableAddressModbusSlave(modbusAddress, bus);
return new PzemShuntPacketListUpdater(dataId, modbusAddress, new PzemShuntModbusSlaveRead(slave));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.retrodaredevil.solarthing.program;

import me.retrodaredevil.io.modbus.ModbusRuntimeException;
import me.retrodaredevil.solarthing.InstantType;
import me.retrodaredevil.solarthing.packets.Packet;
import me.retrodaredevil.solarthing.packets.handling.PacketListReceiver;
import me.retrodaredevil.solarthing.solar.pzem.ImmutablePzemShuntStatusPacket;
import me.retrodaredevil.solarthing.solar.pzem.PzemShuntReadTable;
import me.retrodaredevil.solarthing.solar.pzem.PzemShuntStatusPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class PzemShuntPacketListUpdater implements PacketListReceiver {
private static final Logger LOGGER = LoggerFactory.getLogger(PzemShuntPacketListUpdater.class);
private final int dataId;
private final int modbusAddress;
private final PzemShuntReadTable read;

public PzemShuntPacketListUpdater(int dataId, int modbusAddress, PzemShuntReadTable read) {
this.dataId = dataId;
this.modbusAddress = modbusAddress;
this.read = read;
}

@Override
public void receive(List<Packet> packets, InstantType instantType) {
final PzemShuntStatusPacket packet;
try {
packet = ImmutablePzemShuntStatusPacket.createFromReadTable(dataId, modbusAddress, read);
} catch (ModbusRuntimeException ex) {
LOGGER.error("Modbus exception", ex);
return;
}
packets.add(packet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import javax.annotation.meta.TypeQualifierNickname;
import java.lang.annotation.*;

//@Target({ TYPE, ANNOTATION_TYPE, PARAMETER, TYPE_USE })
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@TypeQualifierNickname
@Nonnull
public @interface NotNull { // our goal with this is to be compatible with keep-79: https://github.com/Kotlin/KEEP/issues/79 // https://kotlinlang.org/docs/java-interop.html#jsr-305-support
public @interface NotNull {
// our goal with this is to be compatible with keep-79: https://github.com/Kotlin/KEEP/issues/79
// https://kotlinlang.org/docs/java-interop.html#jsr-305-support
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import javax.annotation.meta.TypeQualifierNickname;
import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;

//@Target({ TYPE, ANNOTATION_TYPE, PARAMETER, TYPE_USE, METHOD})
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.retrodaredevil.solarthing.packets;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import me.retrodaredevil.solarthing.annotations.Nullable;

@JsonPropertyOrder({"packetType", "packetVersion"})
public interface VersionedPacket extends DocumentedPacket {

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("packetVersion")
@Nullable Integer getPacketVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public enum SolarStatusPacketType implements DocumentedPacketType {
*/
RENOGY_ROVER_STATUS,

BATTERY_VOLTAGE_ONLY
BATTERY_VOLTAGE_ONLY,
PZEM_SHUNT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package me.retrodaredevil.solarthing.solar.common;

import me.retrodaredevil.solarthing.packets.identification.Identifiable;

public interface Shunt extends Identifiable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package me.retrodaredevil.solarthing.solar.pzem;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import me.retrodaredevil.solarthing.annotations.NotNull;
import me.retrodaredevil.solarthing.misc.common.DataIdentifier;
import me.retrodaredevil.solarthing.packets.identification.IdentityInfo;

public class ImmutablePzemShuntStatusPacket implements PzemShuntStatusPacket {
private final DataIdentifier identifier;
private final IdentityInfo identityInfo;
private final int voltageValueRaw, currentValueRaw, powerValueRaw, energyValueRaw;
private final int highVoltageAlarmStatus, lowVoltageAlarmStatus;
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) {
this.identifier = new DataIdentifier(dataId);
this.identityInfo = new PzemShuntIdentityInfo(dataId);

this.voltageValueRaw = voltageValueRaw;
this.currentValueRaw = currentValueRaw;
this.powerValueRaw = powerValueRaw;
this.energyValueRaw = energyValueRaw;
this.highVoltageAlarmStatus = highVoltageAlarmStatus;
this.lowVoltageAlarmStatus = lowVoltageAlarmStatus;
this.modbusAddress = modbusAddress;
}
public static ImmutablePzemShuntStatusPacket createFromReadTable(int dataId, int modbusAddress, PzemShuntReadTable read) {
return new ImmutablePzemShuntStatusPacket(
dataId,
read.getVoltageValueRaw(), read.getCurrentValueRaw(), read.getPowerValueRaw(), read.getEnergyValueRaw(),
read.getHighVoltageAlarmStatus(), read.getLowVoltageAlarmStatus(),
modbusAddress
);
}
@Override
public @NotNull DataIdentifier getIdentifier() {
return identifier;
}

@Override
public @NotNull IdentityInfo getIdentityInfo() {
return identityInfo;
}

@Override
public int getDataId() {
return identifier.getDataId();
}

@Override
public int getVoltageValueRaw() {
return voltageValueRaw;
}

@Override
public int getCurrentValueRaw() {
return currentValueRaw;
}

@Override
public int getPowerValueRaw() {
return powerValueRaw;
}

@Override
public int getEnergyValueRaw() {
return energyValueRaw;
}

@Override
public int getHighVoltageAlarmStatus() {
return highVoltageAlarmStatus;
}

@Override
public int getLowVoltageAlarmStatus() {
return lowVoltageAlarmStatus;
}

@Override
public int getModbusAddress() {
return modbusAddress;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.retrodaredevil.solarthing.solar.pzem;

import me.retrodaredevil.solarthing.packets.identification.IdentityInfo;

public class PzemShuntIdentityInfo implements IdentityInfo {
private final int dataId;

public PzemShuntIdentityInfo(int dataId) {
this.dataId = dataId;
}

@Override
public String getName() {
return "Pzem Shunt";
}

@Override
public String getSuffix() {
return "" + dataId;
}

@Override
public String getShortName() {
return "PZ";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.retrodaredevil.solarthing.solar.pzem;

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;

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

@JsonProperty("voltageValueRaw")
int getVoltageValueRaw();
default float getVoltage() {
return getVoltageValueRaw() / 100.0f;
}
int getCurrentValueRaw();
default float getCurrentAmps() {
return getCurrentValueRaw() / 100.0f;
}
int getPowerValueRaw();
default float getPowerWatts() {
return getPowerValueRaw() / 10.0f;
}

int getEnergyValueRaw();
default int getEnergyWattHours() { return getEnergyValueRaw(); }
default float getEnergyKWH() { return getEnergyValueRaw() / 1000.0f; }

int getHighVoltageAlarmStatus();
int getLowVoltageAlarmStatus();

default boolean isHighVoltageAlarm() { return getHighVoltageAlarmStatus() != 0; }
default boolean isLowVoltageAlarm() { return getLowVoltageAlarmStatus() != 0; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.retrodaredevil.solarthing.solar.pzem;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import me.retrodaredevil.solarthing.annotations.NotNull;
import me.retrodaredevil.solarthing.misc.common.DataIdentifiable;
import me.retrodaredevil.solarthing.solar.SolarStatusPacket;
import me.retrodaredevil.solarthing.solar.SolarStatusPacketType;
import me.retrodaredevil.solarthing.solar.common.Shunt;

@JsonDeserialize(as = ImmutablePzemShuntStatusPacket.class)
@JsonTypeName("PZEM_SHUNT")
public interface PzemShuntStatusPacket extends SolarStatusPacket, PzemShuntReadTable, Shunt, DataIdentifiable {
@Override
default @NotNull SolarStatusPacketType getPacketType() {
return SolarStatusPacketType.PZEM_SHUNT;
}

@JsonProperty("modbusAddress")
int getModbusAddress();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.retrodaredevil.solarthing.solar.pzem;

public interface PzemShuntWriteTable {
void setHighVoltageAlarmThresholdRaw(int voltageRaw);
void setLowVoltageAlarmThresholdRaw(int voltageRaw);
void setModbusAddress(int modbusAddress);

/**
* Only works on PZEM-017s
* @param currentRangeRawValue 0 is 100A, 1 is 50A, 2 is 200A, 3 is 300A
*/
void setCurrentRangeRaw(int currentRangeRawValue);
}
Loading

0 comments on commit 326e876

Please sign in to comment.