Skip to content

Commit

Permalink
Backport GoodWe Grid.Meter fix; relates to #2895
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeilmeier committed Dec 10, 2024
1 parent bde9ed5 commit 6728b59
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,7 @@ protected ModbusProtocol defineModbusProtocol() {
m(GoodWeGridMeter.ChannelId.METER_POWER_FACTOR, new UnsignedWordElement(36013),
this.ignoreZeroAndScaleFactorMinus2), //
m(ElectricityMeter.ChannelId.FREQUENCY, new UnsignedWordElement(36014),
this.ignoreZeroAndScaleFactor1)),

new FC3ReadRegistersTask(47456, Priority.LOW, //
m(GoodWeGridMeter.ChannelId.EXTERNAL_METER_RATIO, new UnsignedWordElement(47456)) //
),

// Cannot be written for GoodWe Gen2
new FC6WriteRegisterTask(47456,
m(GoodWeGridMeter.ChannelId.EXTERNAL_METER_RATIO, new UnsignedWordElement(47456)) //
));
this.ignoreZeroAndScaleFactor1)));

// Handles different DSP versions
readElementOnce(FC3, protocol, ModbusUtils::retryOnNull, new UnsignedWordElement(35016))
Expand All @@ -195,6 +186,12 @@ protected ModbusProtocol defineModbusProtocol() {
}
});

switch (this.config.goodWeMeterCategory()) {
case COMMERCIAL_METER -> this.handleExternalMeter(protocol);
case SMART_METER, INTEGRATED_METER -> {
}
}

return protocol;
}

Expand Down Expand Up @@ -223,24 +220,40 @@ private void handleDspVersion4(ModbusProtocol protocol) {
createAdjustCurrentSign(this.getActivePowerL3Channel()::getNextValue))))); //
}

private void handleExternalMeter(ModbusProtocol protocol) {

protocol.addTask(//
new FC6WriteRegisterTask(47456,
m(GoodWeGridMeter.ChannelId.EXTERNAL_METER_RATIO, new UnsignedWordElement(47456)) //
)); //

protocol.addTask(//
new FC3ReadRegistersTask(47456, Priority.LOW, //
m(GoodWeGridMeter.ChannelId.EXTERNAL_METER_RATIO, new UnsignedWordElement(47456)) //
)); //
}

@Override
public void handleEvent(Event event) {
switch (event.getTopic()) {
case EdgeEventConstants.TOPIC_CYCLE_BEFORE_PROCESS_IMAGE -> this.handleMeterRatio();
case EdgeEventConstants.TOPIC_CYCLE_BEFORE_PROCESS_IMAGE -> {

switch (this.config.goodWeMeterCategory()) {
case COMMERCIAL_METER -> this.setExternalMeterValue();
case SMART_METER, INTEGRATED_METER -> {
}
}
}
case EdgeEventConstants.TOPIC_CYCLE_AFTER_PROCESS_IMAGE -> this.calculateEnergy();
}
}

/**
* Set channel for external meter if configured.
*/
private void handleMeterRatio() {
Integer meterCtRatio = switch (this.config.goodWeMeterCategory()) {
case COMMERCIAL_METER ->
calculateRatio(this.config.externalMeterRatioValueA(), this.config.externalMeterRatioValueB());
case INTEGRATED_METER -> 90;
case SMART_METER -> 3000;
};
protected void setExternalMeterValue() {
final var meterCtRatio = calculateRatio(this.config.externalMeterRatioValueA(),
this.config.externalMeterRatioValueB());

try {
ChannelUtils.setWriteValueIfNotRead(this.getExternalMeterRatioChannel(), meterCtRatio);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testReadFromModbus() throws Exception {
.output(ElectricityMeter.ChannelId.ACTIVE_POWER_L3, -1610) //
.output(ElectricityMeter.ChannelId.ACTIVE_POWER, 710)) //

.next(new TestCase(), 5) // Wait for 36052
.next(new TestCase(), 3) // Wait for 36052
.next(new TestCase() //
.output(ElectricityMeter.ChannelId.VOLTAGE_L1, 200_000) //
.output(ElectricityMeter.ChannelId.VOLTAGE_L2, 220_000) //
Expand Down Expand Up @@ -189,7 +189,7 @@ public void testExternalMeterRatio() throws Exception {
.setExternalMeterRatioValueB(5) //
.build()) //
.next(new TestCase() //
.output(EXTERNAL_METER_RATIO, 3000));
.output(EXTERNAL_METER_RATIO, null));
}

@Test
Expand Down

0 comments on commit 6728b59

Please sign in to comment.