From d81a9a009454e8d27b9f47ecf8af984c383220e4 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 8 Aug 2023 12:30:59 +0200 Subject: [PATCH 1/4] add power factor to cp --- packages/modules/common/abstract_counter.py | 38 +++++++++++++++++ packages/modules/common/b23.py | 17 +++++--- packages/modules/common/component_state.py | 42 +++++++++---------- packages/modules/common/lovato.py | 3 +- packages/modules/common/mpm3pm.py | 3 +- packages/modules/common/sdm.py | 3 +- packages/modules/common/store/_chargepoint.py | 1 + .../chargepoint_module.py | 9 ++-- 8 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 packages/modules/common/abstract_counter.py diff --git a/packages/modules/common/abstract_counter.py b/packages/modules/common/abstract_counter.py new file mode 100644 index 000000000..b6b01ce24 --- /dev/null +++ b/packages/modules/common/abstract_counter.py @@ -0,0 +1,38 @@ +from abc import abstractmethod +from typing import List, Tuple + +from modules.common import modbus + + +class AbstractCounter: + @abstractmethod + def __init__(self, modbus_id: int, client: modbus.ModbusTcpClient_) -> None: + pass + + @abstractmethod + def get_currents(self) -> List[float]: + return [0]*3 + + @abstractmethod + def get_exported(self) -> float: + return 0 + + @abstractmethod + def get_frequency(self) -> float: + return 50 + + @abstractmethod + def get_imported(self) -> float: + return 0 + + @abstractmethod + def get_power(self) -> Tuple[List[float], float]: + return [0]*3, 0 + + @abstractmethod + def get_power_factors(self) -> List[float]: + return [0]*3 + + @abstractmethod + def get_voltages(self) -> List[float]: + return [230]*3 diff --git a/packages/modules/common/b23.py b/packages/modules/common/b23.py index f162a7dd1..e10ae9118 100644 --- a/packages/modules/common/b23.py +++ b/packages/modules/common/b23.py @@ -2,28 +2,33 @@ from typing import List, Tuple from modules.common import modbus +from modules.common.abstract_counter import AbstractCounter from modules.common.modbus import ModbusDataType -class B23: +class B23(AbstractCounter): def __init__(self, modbus_id: int, client: modbus.ModbusTcpClient_) -> None: self.client = client self.id = modbus_id - def get_imported(self) -> float: - return self.client.read_holding_registers(0x5000, ModbusDataType.UINT_64, unit=self.id) * 10 + def get_currents(self) -> List[float]: + return [val / 100 for val in self.client.read_holding_registers( + 0x5B0C, [ModbusDataType.UINT_32]*3, unit=self.id)] def get_frequency(self) -> float: return self.client.read_holding_registers(0x5B2C, ModbusDataType.INT_16, unit=self.id) / 100 - def get_currents(self) -> List[float]: - return [val / 100 for val in self.client.read_holding_registers( - 0x5B0C, [ModbusDataType.UINT_32]*3, unit=self.id)] + def get_imported(self) -> float: + return self.client.read_holding_registers(0x5000, ModbusDataType.UINT_64, unit=self.id) * 10 def get_power(self) -> Tuple[List[float], float]: power = self.client.read_holding_registers(0x5B14, ModbusDataType.INT_32, unit=self.id) / 100 return [0]*3, power + def get_power_factors(self) -> List[float]: + return [val / 1000 for val in self.client.read_holding_registers( + 0x5B3B, [ModbusDataType.UINT_32]*3, unit=self.id)] + def get_voltages(self) -> List[float]: return [val / 10 for val in self.client.read_holding_registers( 0x5B00, [ModbusDataType.UINT_32]*3, unit=self.id)] diff --git a/packages/modules/common/component_state.py b/packages/modules/common/component_state.py index 215ec9f26..b765160b0 100644 --- a/packages/modules/common/component_state.py +++ b/packages/modules/common/component_state.py @@ -1,8 +1,25 @@ -from typing import List, Optional +from typing import List, Optional, Tuple from helpermodules.auto_str import auto_str +def _calculate_powers_and_currents(currents: Optional[List[float]], + powers: Optional[List[float]], + voltages: Optional[List[float]]) -> Tuple[List[float]]: + if voltages is None: + voltages = [230.0]*3 + if powers is None: + if currents is None: + powers = [0.0]*3 + else: + powers = [currents[i]*voltages[i] for i in range(0, 3)] + if currents is None and powers: + currents = [powers[i]/voltages[i] for i in range(0, 3)] + if currents and powers: + currents = [currents[i]*-1 if powers[i] < 0 and currents[i] > 0 else currents[i] for i in range(0, 3)] + return currents, powers, voltages + + @auto_str class BatState: def __init__( @@ -47,20 +64,7 @@ def __init__( power_factors: actual power factors for 3 phases frequency: actual grid frequency in Hz """ - if voltages is None: - voltages = [230.0]*3 - self.voltages = voltages - if powers is None: - if currents is None: - powers = [0.0]*3 - else: - powers = [currents[i]*voltages[i] for i in range(0, 3)] - self.powers = powers - if currents is None and powers: - currents = [powers[i]/voltages[i] for i in range(0, 3)] - if currents and powers: - currents = [currents[i]*-1 if powers[i] < 0 and currents[i] > 0 else currents[i] for i in range(0, 3)] - self.currents = currents + self.currents, self.powers, self.voltages = _calculate_powers_and_currents(currents, powers, voltages) if power_factors is None: power_factors = [0.0]*3 self.power_factors = power_factors @@ -115,18 +119,14 @@ def __init__(self, imported: float = 0, exported: float = 0, power: float = 0, + powers: Optional[List[float]] = None, voltages: Optional[List[float]] = None, currents: Optional[List[float]] = None, power_factors: Optional[List[float]] = None, charge_state: bool = False, plug_state: bool = False, rfid: Optional[str] = None): - if voltages is None: - voltages = [0.0]*3 - self.voltages = voltages - if currents is None: - currents = [0.0]*3 - self.currents = currents + self.currents, self.powers, self.voltages = _calculate_powers_and_currents(currents, powers, voltages) if power_factors is None: power_factors = [0.0]*3 self.power_factors = power_factors diff --git a/packages/modules/common/lovato.py b/packages/modules/common/lovato.py index 153b4dd90..504f37687 100644 --- a/packages/modules/common/lovato.py +++ b/packages/modules/common/lovato.py @@ -2,10 +2,11 @@ from modules.common import modbus from typing import List, Tuple +from modules.common.abstract_counter import AbstractCounter from modules.common.modbus import ModbusDataType -class Lovato: +class Lovato(AbstractCounter): def __init__(self, modbus_id: int, client: modbus.ModbusTcpClient_) -> None: self.client = client self.id = modbus_id diff --git a/packages/modules/common/mpm3pm.py b/packages/modules/common/mpm3pm.py index 56ea2b907..13ff71b77 100644 --- a/packages/modules/common/mpm3pm.py +++ b/packages/modules/common/mpm3pm.py @@ -2,10 +2,11 @@ from typing import List, Tuple from modules.common import modbus +from modules.common.abstract_counter import AbstractCounter from modules.common.modbus import ModbusDataType -class Mpm3pm: +class Mpm3pm(AbstractCounter): def __init__(self, modbus_id: int, client: modbus.ModbusTcpClient_) -> None: self.client = client self.id = modbus_id diff --git a/packages/modules/common/sdm.py b/packages/modules/common/sdm.py index 4512dc38c..bdfef8f8e 100644 --- a/packages/modules/common/sdm.py +++ b/packages/modules/common/sdm.py @@ -2,10 +2,11 @@ from typing import List, Tuple from modules.common import modbus +from modules.common.abstract_counter import AbstractCounter from modules.common.modbus import ModbusDataType -class Sdm: +class Sdm(AbstractCounter): def __init__(self, modbus_id: int, client: modbus.ModbusTcpClient_) -> None: self.client = client self.id = modbus_id diff --git a/packages/modules/common/store/_chargepoint.py b/packages/modules/common/store/_chargepoint.py index 00a79dbff..665a93893 100644 --- a/packages/modules/common/store/_chargepoint.py +++ b/packages/modules/common/store/_chargepoint.py @@ -31,6 +31,7 @@ def set(self, state: ChargepointState) -> None: pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/imported", state.imported, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/exported", state.exported, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/power", state.power, 2) + pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/powers", state.powers, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/phases_in_use", state.phases_in_use, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/charge_state", state.charge_state, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/plug_state", state.plug_state, 2) diff --git a/packages/modules/internal_chargepoint_handler/chargepoint_module.py b/packages/modules/internal_chargepoint_handler/chargepoint_module.py index aea0a475f..035c5f0a3 100644 --- a/packages/modules/internal_chargepoint_handler/chargepoint_module.py +++ b/packages/modules/internal_chargepoint_handler/chargepoint_module.py @@ -34,12 +34,14 @@ def set_current(self, current: float) -> None: def get_values(self, phase_switch_cp_active: bool) -> Tuple[ChargepointState, float]: try: - _, power = self.__client.meter_client.get_power() + powers, power = self.__client.meter_client.get_power() if power < self.PLUG_STANDBY_POWER_THRESHOLD: power = 0 voltages = self.__client.meter_client.get_voltages() currents = self.__client.meter_client.get_currents() imported = self.__client.meter_client.get_imported() + power_factors = self.__client.meter_client.get_power_factors() + frequency = self.__client.meter_client.get_frequency() phases_in_use = sum(1 for current in currents if current > 3) time.sleep(0.1) @@ -63,12 +65,13 @@ def get_values(self, phase_switch_cp_active: bool) -> Tuple[ChargepointState, fl currents=currents, imported=imported, exported=0, - # powers=powers, + powers=powers, voltages=voltages, - # frequency=frequency, + frequency=frequency, plug_state=plug_state, charge_state=charge_state, phases_in_use=phases_in_use, + power_factors=power_factors, rfid=rfid ) except Exception as e: From cd8f1e7ca40f155b1269d53673637c49c0e879d6 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 8 Aug 2023 12:40:02 +0200 Subject: [PATCH 2/4] fix --- packages/modules/common/component_state.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/modules/common/component_state.py b/packages/modules/common/component_state.py index b765160b0..eb08b92ec 100644 --- a/packages/modules/common/component_state.py +++ b/packages/modules/common/component_state.py @@ -125,11 +125,10 @@ def __init__(self, power_factors: Optional[List[float]] = None, charge_state: bool = False, plug_state: bool = False, - rfid: Optional[str] = None): + rfid: Optional[str] = None, + frequency: float = 50): self.currents, self.powers, self.voltages = _calculate_powers_and_currents(currents, powers, voltages) - if power_factors is None: - power_factors = [0.0]*3 - self.power_factors = power_factors + self.frequency = frequency self.imported = imported self.exported = exported self.power = power @@ -137,3 +136,6 @@ def __init__(self, self.charge_state = charge_state self.plug_state = plug_state self.rfid = rfid + if power_factors is None: + power_factors = [0.0]*3 + self.power_factors = power_factors From 374879ab2f31f8f64ddafa31b11fc485a4986786 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 8 Aug 2023 12:42:41 +0200 Subject: [PATCH 3/4] fix --- runs/isss.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runs/isss.py b/runs/isss.py index e56cb4d0c..549ce7c6e 100755 --- a/runs/isss.py +++ b/runs/isss.py @@ -84,7 +84,7 @@ def pub_value(topic: str, value): if self.parent_wb != "localhost": pub_single("openWB/lp/"+self.cp_num_str+"/"+topic, payload=str(value), hostname=self.parent_wb, no_json=True) - topic = self.MAP_KEY_TO_OLD_TOPIC[key] + topic = self.MAP_KEY_TO_OLD_TOPIC.get(key) rounding = get_rounding_function_by_digits(2) if topic is not None: if isinstance(topic, List): From 21cdf94dd12fe6f6208a1f2a974765e3c6c0ff27 Mon Sep 17 00:00:00 2001 From: LKuemmel <76958050+LKuemmel@users.noreply.github.com> Date: Tue, 22 Aug 2023 13:43:05 +0200 Subject: [PATCH 4/4] Update packages/modules/common/store/_chargepoint.py Co-authored-by: benderl --- packages/modules/common/store/_chargepoint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/modules/common/store/_chargepoint.py b/packages/modules/common/store/_chargepoint.py index 665a93893..422f5be03 100644 --- a/packages/modules/common/store/_chargepoint.py +++ b/packages/modules/common/store/_chargepoint.py @@ -32,6 +32,7 @@ def set(self, state: ChargepointState) -> None: pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/exported", state.exported, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/power", state.power, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/powers", state.powers, 2) + pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/frequency", state.frequency, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/phases_in_use", state.phases_in_use, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/charge_state", state.charge_state, 2) pub_to_broker("openWB/set/chargepoint/" + str(self.num) + "/get/plug_state", state.plug_state, 2)