Skip to content

Commit

Permalink
Merge pull request snaptec#2775 from LKuemmel/cp_power_factor
Browse files Browse the repository at this point in the history
add power factor, powers and frequency to cp
  • Loading branch information
LKuemmel authored Aug 24, 2023
2 parents a88f2c9 + 21cdf94 commit 4e1842c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 38 deletions.
38 changes: 38 additions & 0 deletions packages/modules/common/abstract_counter.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 11 additions & 6 deletions packages/modules/common/b23.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
52 changes: 27 additions & 25 deletions packages/modules/common/component_state.py
Original file line number Diff line number Diff line change
@@ -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__(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -115,25 +119,23 @@ 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
if power_factors is None:
power_factors = [0.0]*3
self.power_factors = power_factors
rfid: Optional[str] = None,
frequency: float = 50):
self.currents, self.powers, self.voltages = _calculate_powers_and_currents(currents, powers, voltages)
self.frequency = frequency
self.imported = imported
self.exported = exported
self.power = power
self.phases_in_use = phases_in_use
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
3 changes: 2 additions & 1 deletion packages/modules/common/lovato.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/modules/common/mpm3pm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/modules/common/sdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/modules/common/store/_chargepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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/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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion runs/isss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4e1842c

Please sign in to comment.