Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElementToChannelConverter: use static imports & add mathematical operators #2197

Merged
merged 10 commits into from
Jun 1, 2023
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.openems.edge.battery.bmw;

import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.DIRECT_1_TO_1;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.INVERT;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_2;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_MINUS_1;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_MINUS_2;

import java.time.LocalDateTime;

import org.osgi.service.cm.ConfigurationAdmin;
Expand Down Expand Up @@ -27,7 +33,6 @@
import io.openems.edge.battery.bmw.enums.State;
import io.openems.edge.bridge.modbus.api.AbstractOpenemsModbusComponent;
import io.openems.edge.bridge.modbus.api.BridgeModbus;
import io.openems.edge.bridge.modbus.api.ElementToChannelConverter;
import io.openems.edge.bridge.modbus.api.ModbusComponent;
import io.openems.edge.bridge.modbus.api.ModbusProtocol;
import io.openems.edge.bridge.modbus.api.element.BitsWordElement;
Expand Down Expand Up @@ -351,8 +356,7 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
m(BmwChannelId.HEART_BEAT, new UnsignedWordElement(1399)), //
m(BmwChannelId.BMS_STATE_COMMAND, new UnsignedWordElement(1400)), //
m(BmwChannelId.OPERATING_STATE_INVERTER, new UnsignedWordElement(1401)), //
m(BmwChannelId.DC_LINK_VOLTAGE, new UnsignedWordElement(1402),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(BmwChannelId.DC_LINK_VOLTAGE, new UnsignedWordElement(1402), SCALE_FACTOR_MINUS_1), //
m(BmwChannelId.DC_LINK_CURRENT, new UnsignedWordElement(1403)), //
m(BmwChannelId.OPERATION_MODE_REQUEST_GRANTED, new UnsignedWordElement(1404)), //
m(BmwChannelId.OPERATION_MODE_REQUEST_CANCELED, new UnsignedWordElement(1405)), //
Expand All @@ -375,23 +379,18 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
m(BmwChannelId.INFO_BITS, new UnsignedWordElement(1005)),
m(BmwChannelId.MAXIMUM_OPERATING_CURRENT, new SignedWordElement(1006)), //
m(BmwChannelId.MINIMUM_OPERATING_CURRENT, new SignedWordElement(1007)), //
m(Battery.ChannelId.CHARGE_MAX_VOLTAGE, new UnsignedWordElement(1008),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MIN_VOLTAGE, new UnsignedWordElement(1009),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.CHARGE_MAX_VOLTAGE, new UnsignedWordElement(1008), SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MIN_VOLTAGE, new UnsignedWordElement(1009), SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MAX_CURRENT, new SignedWordElement(1010)), //
m(Battery.ChannelId.CHARGE_MAX_CURRENT, new SignedWordElement(1011),
ElementToChannelConverter.INVERT), //
m(Battery.ChannelId.CHARGE_MAX_CURRENT, new SignedWordElement(1011), INVERT), //
m(BmwChannelId.MAXIMUM_LIMIT_DYNAMIC_VOLTAGE, new UnsignedWordElement(1012),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
SCALE_FACTOR_MINUS_1), //
m(BmwChannelId.MINIMUM_LIMIT_DYNAMIC_VOLTAGE, new UnsignedWordElement(1013),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
SCALE_FACTOR_MINUS_1), //
m(BmwChannelId.NUMBER_OF_STRINGS_CONNECTED, new UnsignedWordElement(1014)), //
m(BmwChannelId.NUMBER_OF_STRINGS_INSTALLED, new UnsignedWordElement(1015)), //
m(BmwChannelId.SOC_ALL_STRINGS, new UnsignedWordElement(1016),
ElementToChannelConverter.SCALE_FACTOR_MINUS_2), //
m(Battery.ChannelId.SOC, new UnsignedWordElement(1017),
ElementToChannelConverter.SCALE_FACTOR_MINUS_2), //
m(BmwChannelId.SOC_ALL_STRINGS, new UnsignedWordElement(1016), SCALE_FACTOR_MINUS_2), //
m(Battery.ChannelId.SOC, new UnsignedWordElement(1017), SCALE_FACTOR_MINUS_2), //
m(BmwChannelId.REMAINING_CHARGE_CAPACITY, new UnsignedWordElement(1018)), //
m(BmwChannelId.REMAINING_DISCHARGE_CAPACITY, new UnsignedWordElement(1019)), //
m(BmwChannelId.REMAINING_CHARGE_ENERGY, new UnsignedWordElement(1020)), //
Expand All @@ -400,50 +399,43 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
m(BmwChannelId.TOTAL_ENERGY, new UnsignedWordElement(1023)), //
m(BmwChannelId.NOMINAL_CAPACITY, new UnsignedWordElement(1024)), //
m(Battery.ChannelId.CAPACITY, new UnsignedWordElement(1025)), //
m(Battery.ChannelId.SOH, new UnsignedWordElement(1026),
ElementToChannelConverter.SCALE_FACTOR_MINUS_2), //
m(Battery.ChannelId.VOLTAGE, new UnsignedWordElement(1027),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(BmwChannelId.DC_VOLTAGE_AVERAGE, new UnsignedWordElement(1028),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1)), //
m(Battery.ChannelId.SOH, new UnsignedWordElement(1026), SCALE_FACTOR_MINUS_2), //
m(Battery.ChannelId.VOLTAGE, new UnsignedWordElement(1027), SCALE_FACTOR_MINUS_1), //
m(BmwChannelId.DC_VOLTAGE_AVERAGE, new UnsignedWordElement(1028), SCALE_FACTOR_MINUS_1)), //
new FC4ReadInputRegistersTask(1029, Priority.HIGH, //
m(new SignedWordElement(1029)) //
.m(BmwChannelId.DC_CURRENT, ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
.m(Battery.ChannelId.CURRENT, ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
.m(BmwChannelId.DC_CURRENT, SCALE_FACTOR_MINUS_1) //
.m(Battery.ChannelId.CURRENT, SCALE_FACTOR_MINUS_1) //
.build()), //
new FC4ReadInputRegistersTask(1030, Priority.HIGH, //
m(BmwChannelId.AVERAGE_TEMPERATURE, new SignedWordElement(1030))), //
new FC4ReadInputRegistersTask(1031, Priority.HIGH, //
m(new SignedWordElement(1031)) //
.m(BmwChannelId.MINIMUM_TEMPERATURE, ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_TEMPERATURE, ElementToChannelConverter.DIRECT_1_TO_1) //
.m(BmwChannelId.MINIMUM_TEMPERATURE, DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_TEMPERATURE, DIRECT_1_TO_1) //
.build()), //
new FC4ReadInputRegistersTask(1032, Priority.HIGH, //
m(new SignedWordElement(1032)) //
.m(BmwChannelId.MAXIMUM_TEMPERATURE, ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MAX_CELL_TEMPERATURE, ElementToChannelConverter.DIRECT_1_TO_1) //
.m(BmwChannelId.MAXIMUM_TEMPERATURE, DIRECT_1_TO_1) //
.m(Battery.ChannelId.MAX_CELL_TEMPERATURE, DIRECT_1_TO_1) //
.build()), //
new FC4ReadInputRegistersTask(1033, Priority.HIGH,
m(Battery.ChannelId.MIN_CELL_VOLTAGE, new UnsignedWordElement(1033)), //
m(Battery.ChannelId.MAX_CELL_VOLTAGE, new UnsignedWordElement(1034)), //
m(BmwChannelId.AVERAGE_CELL_VOLTAGE, new UnsignedWordElement(1035)), //
// not defined by "BCS_HL-SW_Operating-Instructions_V1.0.2_under_work_ChL.pdf"
m(BmwChannelId.INTERNAL_RESISTANCE, new UnsignedWordElement(1036)),
m(BmwChannelId.INSULATION_RESISTANCE, new UnsignedWordElement(1037),
ElementToChannelConverter.DIRECT_1_TO_1), //
m(BmwChannelId.INSULATION_RESISTANCE, new UnsignedWordElement(1037), DIRECT_1_TO_1), //
// not defined by "BCS_HL-SW_Operating-Instructions_V1.0.2_under_work_ChL.pdf"
m(BmwChannelId.CONTAINER_TEMPERATURE, new UnsignedWordElement(1038),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1),
m(BmwChannelId.CONTAINER_TEMPERATURE, new UnsignedWordElement(1038), SCALE_FACTOR_MINUS_1),
// not defined by "BCS_HL-SW_Operating-Instructions_V1.0.2_under_work_ChL.pdf"
m(BmwChannelId.AMBIENT_TEMPERATURE, new UnsignedWordElement(1039),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1),
m(BmwChannelId.AMBIENT_TEMPERATURE, new UnsignedWordElement(1039), SCALE_FACTOR_MINUS_1),
// not defined by "BCS_HL-SW_Operating-Instructions_V1.0.2_under_work_ChL.pdf"
m(BmwChannelId.HUMIDITY_CONTAINER, new UnsignedWordElement(1040),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1),
m(BmwChannelId.HUMIDITY_CONTAINER, new UnsignedWordElement(1040), SCALE_FACTOR_MINUS_1),
m(BmwChannelId.MAXIMUM_LIMIT_DYNAMIC_CURRENT_HIGH_RES, new SignedWordElement(1041),
ElementToChannelConverter.SCALE_FACTOR_2), //
SCALE_FACTOR_2), //
m(BmwChannelId.MINIMUM_LIMIT_DYNAMIC_CURRENT_HIGH_RES, new SignedWordElement(1042),
ElementToChannelConverter.SCALE_FACTOR_2), //
SCALE_FACTOR_2), //
m(BmwChannelId.FULL_CYCLE_COUNT, new UnsignedWordElement(1043)), //
// not defined by "BCS_HL-SW_Operating-Instructions_V1.0.2_under_work_ChL.pdf"
m(BmwChannelId.OPERATING_TIME_COUNT, new UnsignedDoublewordElement(1044)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.openems.edge.battery.bydcommercial;

import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.DIRECT_1_TO_1;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_2;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_MINUS_1;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

Expand Down Expand Up @@ -30,7 +34,6 @@
import io.openems.edge.battery.protection.BatteryProtection;
import io.openems.edge.bridge.modbus.api.AbstractOpenemsModbusComponent;
import io.openems.edge.bridge.modbus.api.BridgeModbus;
import io.openems.edge.bridge.modbus.api.ElementToChannelConverter;
import io.openems.edge.bridge.modbus.api.ModbusComponent;
import io.openems.edge.bridge.modbus.api.ModbusProtocol;
import io.openems.edge.bridge.modbus.api.ModbusUtils;
Expand Down Expand Up @@ -188,44 +191,38 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
), //
new FC3ReadRegistersTask(0x2100, Priority.HIGH, //
m(new UnsignedWordElement(0x2100).onUpdateCallback(this.onRegister0x2100Update)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_VOLTAGE, ElementToChannelConverter.SCALE_FACTOR_2) // [mV]
.m(Battery.ChannelId.VOLTAGE, ElementToChannelConverter.SCALE_FACTOR_MINUS_1) // [V]
.m(BatteryBoxC130.ChannelId.CLUSTER_1_VOLTAGE, SCALE_FACTOR_2) // [mV]
.m(Battery.ChannelId.VOLTAGE, SCALE_FACTOR_MINUS_1) // [V]
.build(), //
m(new SignedWordElement(0x2101)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_CURRENT, ElementToChannelConverter.SCALE_FACTOR_2) // [mA]
.m(Battery.ChannelId.CURRENT, ElementToChannelConverter.SCALE_FACTOR_MINUS_1) // [A]
.m(BatteryBoxC130.ChannelId.CLUSTER_1_CURRENT, SCALE_FACTOR_2) // [mA]
.m(Battery.ChannelId.CURRENT, SCALE_FACTOR_MINUS_1) // [A]
.build(), //
m(BatteryBoxC130.ChannelId.BATTERY_WORK_STATE, new UnsignedWordElement(0x2102)), //
m(Battery.ChannelId.SOC, new UnsignedWordElement(0x2103)), //
m(new UnsignedWordElement(0x2104)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_SOH, ElementToChannelConverter.DIRECT_1_TO_1) // [%]
.m(Battery.ChannelId.SOH, ElementToChannelConverter.DIRECT_1_TO_1) // [%]
.m(BatteryBoxC130.ChannelId.CLUSTER_1_SOH, DIRECT_1_TO_1) // [%]
.m(Battery.ChannelId.SOH, DIRECT_1_TO_1) // [%]
.build(), //
m(BatteryBoxC130.ChannelId.CLUSTER_1_MAX_CELL_VOLTAGE_ID, new UnsignedWordElement(0x2105)), //
m(new UnsignedWordElement(0x2106)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MAX_CELL_VOLTAGE,
ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MAX_CELL_VOLTAGE, ElementToChannelConverter.DIRECT_1_TO_1) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MAX_CELL_VOLTAGE, DIRECT_1_TO_1) //
.m(Battery.ChannelId.MAX_CELL_VOLTAGE, DIRECT_1_TO_1) //
.build(), //
m(BatteryBoxC130.ChannelId.CLUSTER_1_MIN_CELL_VOLTAGE_ID, new UnsignedWordElement(0x2107)), //
m(new UnsignedWordElement(0x2108)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MIN_CELL_VOLTAGE,
ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_VOLTAGE, ElementToChannelConverter.DIRECT_1_TO_1) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MIN_CELL_VOLTAGE, DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_VOLTAGE, DIRECT_1_TO_1) //
.build(), //
m(BatteryBoxC130.ChannelId.CLUSTER_1_MAX_CELL_TEMPERATURE_ID, new UnsignedWordElement(0x2109)), //
m(new SignedWordElement(0x210A)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MAX_CELL_TEMPERATURE,
ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MAX_CELL_TEMPERATURE,
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MAX_CELL_TEMPERATURE, DIRECT_1_TO_1) //
.m(Battery.ChannelId.MAX_CELL_TEMPERATURE, SCALE_FACTOR_MINUS_1) //
.build(), //
m(BatteryBoxC130.ChannelId.CLUSTER_1_MIN_CELL_TEMPERATURE_ID, new UnsignedWordElement(0x210B)), //
m(new SignedWordElement(0x210C)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MIN_CELL_TEMPERATURE,
ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_TEMPERATURE,
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_MIN_CELL_TEMPERATURE, DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_TEMPERATURE, SCALE_FACTOR_MINUS_1) //
.build()), //

new FC3ReadRegistersTask(0x211D, Priority.HIGH, //
Expand Down Expand Up @@ -325,9 +322,9 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
), //
new FC3ReadRegistersTask(0x216C, Priority.HIGH, //
m(BatteryProtection.ChannelId.BP_CHARGE_BMS, new UnsignedWordElement(0x216C),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
SCALE_FACTOR_MINUS_1), //
m(BatteryProtection.ChannelId.BP_DISCHARGE_BMS, new UnsignedWordElement(0x216D),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
SCALE_FACTOR_MINUS_1) //
), //

new FC3ReadRegistersTask(0x2183, Priority.LOW, //
Expand Down Expand Up @@ -717,10 +714,10 @@ public StartStop getStartStopTarget() {
new UnsignedWordElement(0x210E))), //
new FC3ReadRegistersTask(0x216E, Priority.LOW, //
m(Battery.ChannelId.CHARGE_MAX_VOLTAGE, new UnsignedWordElement(0x216E), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MIN_VOLTAGE,
new UnsignedWordElement(0x216F), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
SCALE_FACTOR_MINUS_1) //
));
} catch (OpenemsException e) {
BatteryBoxC130Impl.this.logError(BatteryBoxC130Impl.this.log,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openems.edge.battery.fenecon.commercial;

import static io.openems.common.types.OpenemsType.BOOLEAN;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.DIRECT_1_TO_1;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_1;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_MINUS_1;
import static io.openems.edge.bridge.modbus.api.ElementToChannelConverter.SCALE_FACTOR_MINUS_2;
Expand Down Expand Up @@ -136,8 +137,7 @@ protected void deactivate() {
super.deactivate();
}

private final ElementToChannelConverter ignoreZero = IgnoreZeroConverter.from(this,
ElementToChannelConverter.DIRECT_1_TO_1);
private final ElementToChannelConverter ignoreZero = IgnoreZeroConverter.from(this, DIRECT_1_TO_1);

private final ElementToChannelConverter ignoreZeroAndScaleFactorMinus2 = IgnoreZeroConverter.from(this,
SCALE_FACTOR_MINUS_2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.openems.edge.battery.fenecon.commercial;

import io.openems.edge.bridge.modbus.api.ElementToChannelConverter;
import io.openems.edge.bridge.modbus.api.ElementToChannelConverterChain;

/**
* Before the battery is started, values are wrongly received as 'zero' via
Expand All @@ -24,10 +23,10 @@ public class IgnoreZeroConverter extends ElementToChannelConverter {
*/
public static ElementToChannelConverter from(FeneconCommercialBatteryImpl parent,
ElementToChannelConverter converter) {
if (converter == ElementToChannelConverter.DIRECT_1_TO_1) {
if (converter == DIRECT_1_TO_1) {
return new IgnoreZeroConverter(parent);
}
return new ElementToChannelConverterChain(new IgnoreZeroConverter(parent), converter);
return ElementToChannelConverter.chain(new IgnoreZeroConverter(parent), converter);
}

private IgnoreZeroConverter(FeneconCommercialBatteryImpl parent) {
Expand Down
Loading