diff --git a/packages/helpermodules/update_config.py b/packages/helpermodules/update_config.py index 21f0082a69..3e8385d5a0 100644 --- a/packages/helpermodules/update_config.py +++ b/packages/helpermodules/update_config.py @@ -17,6 +17,7 @@ from control.chargepoint.chargepoint_template import get_chargepoint_template_default from helpermodules import timecheck from helpermodules import hardware_configuration +from helpermodules import pub from helpermodules.broker import BrokerClient from helpermodules.abstract_plans import Limit from helpermodules.constants import NO_ERROR @@ -56,7 +57,7 @@ class UpdateConfig: - DATASTORE_VERSION = 87 + DATASTORE_VERSION = 88 valid_topic = [ "^openWB/bat/config/bat_control_permitted$", @@ -2338,3 +2339,21 @@ def upgrade_datastore_86(self) -> None: "für die Speichersteuerung. Die Speichersteuerung war bisher bereits verfügbar, ist" " jedoch bis zum Akzeptieren standardmäßig deaktiviert.", MessageType.WARNING) self.__update_topic("openWB/system/datastore_version", 87) + + def upgrade_datastore_87(self) -> None: + def upgrade(topic: str, payload) -> None: + if re.search("openWB/chargepoint/[0-9]+/config", topic) is not None: + config = decode_payload(payload) + if config["type"] == "external_openwb": + log.info(f"Update an LP {config['name']} angestoßen.") + ip_address = config["configuration"]["ip_address"] + pub.pub_single("openWB/set/command/primary/todo", + json.dumps({"command": "systemUpdate", + "data": {"branch": decode_payload(self.all_received_topics[ + "openWB/system/current_branch"]), + "tag": "*HEAD*"}}), + ip_address, + no_json=True) + time.sleep(2) + self._loop_all_received_topics(upgrade) + self.__update_topic("openWB/system/datastore_version", 88) diff --git a/packages/modules/chargepoints/openwb_pro/chargepoint_module.py b/packages/modules/chargepoints/openwb_pro/chargepoint_module.py index da8c7e0791..5aac5315e7 100644 --- a/packages/modules/chargepoints/openwb_pro/chargepoint_module.py +++ b/packages/modules/chargepoints/openwb_pro/chargepoint_module.py @@ -36,13 +36,12 @@ def __init__(self, config: OpenWBPro) -> None: 'http://' + self.config.configuration.ip_address + '/connect.php', data={'heartbeatenabled': '1'}) - def set_internal_context_handlers(self, hierarchy_id: int, internal_cp: InternalChargepoint, parent_hostname: str): + def set_internal_context_handlers(self, hierarchy_id: int, internal_cp: InternalChargepoint): self.fault_state = FaultState(ComponentInfo( self.config.id, "Ladepunkt "+str(self.config.id), "internal_chargepoint", - hierarchy_id=hierarchy_id, - parent_hostname=parent_hostname)) + hierarchy_id=hierarchy_id)) self.client_error_context = ErrorTimerContext( f"openWB/set/internal_chargepoint/{self.config.id}/get/error_timestamp", CP_ERROR, hide_exception=True) self.client_error_context.error_timestamp = internal_cp.get.error_timestamp diff --git a/packages/modules/common/fault_state.py b/packages/modules/common/fault_state.py index 9b121d3719..7c4756174a 100644 --- a/packages/modules/common/fault_state.py +++ b/packages/modules/common/fault_state.py @@ -17,24 +17,20 @@ def __init__(self, name: str, type: str, hostname: str = "localhost", - hierarchy_id: Optional[int] = None, - parent_hostname: Optional[str] = None) -> None: + hierarchy_id: Optional[int] = None) -> None: self.id = id self.name = name self.type = type self.hostname = hostname self.hierarchy_id = hierarchy_id - self.parent_hostname = parent_hostname @staticmethod def from_component_config(component_config: ComponentSetup, - hostname: str = "localhost", - parent_hostname: Optional[str] = None): + hostname: str = "localhost"): return ComponentInfo(component_config.id, component_config.name, component_config.type, - hostname, - parent_hostname) + hostname) class FaultState(Exception): diff --git a/packages/modules/internal_chargepoint_handler/chargepoint_module.py b/packages/modules/internal_chargepoint_handler/chargepoint_module.py index 2420e2a79b..4cb49cefe0 100644 --- a/packages/modules/internal_chargepoint_handler/chargepoint_module.py +++ b/packages/modules/internal_chargepoint_handler/chargepoint_module.py @@ -26,7 +26,6 @@ class ChargepointModule(AbstractChargepoint): def __init__(self, local_charge_point_num: int, client_handler: ClientHandler, - parent_hostname: str, internal_cp: InternalChargepoint, hierarchy_id: int) -> None: self.local_charge_point_num = local_charge_point_num @@ -34,8 +33,7 @@ def __init__(self, local_charge_point_num: int, local_charge_point_num, "Ladepunkt "+str(local_charge_point_num), "internal_chargepoint", - hierarchy_id=hierarchy_id, - parent_hostname=parent_hostname)) + hierarchy_id=hierarchy_id)) self.store_internal = get_internal_chargepoint_value_store(local_charge_point_num) self.store = get_chargepoint_value_store(hierarchy_id) self.client_error_context = ErrorTimerContext( diff --git a/packages/modules/internal_chargepoint_handler/internal_chargepoint_handler.py b/packages/modules/internal_chargepoint_handler/internal_chargepoint_handler.py index eb4d337859..535f7a1f9d 100644 --- a/packages/modules/internal_chargepoint_handler/internal_chargepoint_handler.py +++ b/packages/modules/internal_chargepoint_handler/internal_chargepoint_handler.py @@ -12,8 +12,6 @@ from modules.chargepoints.internal_openwb.config import InternalChargepointMode from modules.common.component_context import SingleComponentUpdateContext from modules.common.fault_state import ComponentInfo, FaultState -from modules.common.store._util import get_rounding_function_by_digits -from modules.common.component_state import ChargepointState from modules.internal_chargepoint_handler import chargepoint_module from modules.internal_chargepoint_handler.clients import ClientHandler, client_factory from modules.internal_chargepoint_handler.pro_plus import ProPlus @@ -28,54 +26,6 @@ log.info("failed to import RPi.GPIO! maybe we are not running on a pi") -class UpdateValues: - def __init__(self, - local_charge_point_num: int, - parent_ip: Optional[str], - parent_cp: str, - hierarchy_id: int) -> None: - self.local_charge_point_num_str = str(local_charge_point_num) - self.old_chargepoint_state = None - self.parent_ip = parent_ip - self.parent_cp = parent_cp - self.hierarchy_id = hierarchy_id - - def update_values(self, chargepoint_state: ChargepointState, heartbeat_expired: bool) -> None: - if self.parent_ip is not None: - if self.old_chargepoint_state: - # iterate over counter state - vars_old_counter_state = vars(self.old_chargepoint_state) - for key, value in vars(chargepoint_state).items(): - # Zählerstatus immer veröffentlichen für Ladelog-Einträge - if value != vars_old_counter_state[key] or key == "imported": - self._pub_values_to_2(key, value) - else: - # Bei Neustart alles veröffentlichen - for key, value in vars(chargepoint_state).items(): - self._pub_values_to_2(key, value) - if heartbeat_expired is False: - # Nur wenn eine Verbindung zum Master besteht, die veröffentlichten Werte speichern. - self.old_chargepoint_state = chargepoint_state - - def _pub_values_to_2(self, topic: str, value) -> None: - rounding = get_rounding_function_by_digits(2) - # fix rfid default value - if topic == "rfid" and value == "0": - value = None - if isinstance(value, (str, bool, type(None))): - payload = value - else: - # qos 2 reicht nicht, da die Daten zwar auf dem Broker ankommen, aber nicht verarbeitet werden. - if isinstance(value, list): - payload = [rounding(v) for v in value] - else: - payload = rounding(value) - pub_single(f"openWB/set/chargepoint/{self.parent_cp}/get/{topic}", payload=payload, hostname=self.parent_ip) - if self.parent_ip != "localhost": - pub_single(f"openWB/set/chargepoint/{self.hierarchy_id}/get/state_str", - payload="Statusmeldungen bitte auf der Primary-openWB einsehen.") - - class UpdateState: def __init__(self, cp_module: chargepoint_module.ChargepointModule, hierarchy_id: int) -> None: self.old_phases_to_use = 0 @@ -147,11 +97,9 @@ def __init__(self, self.event_stop = event_stop self.heartbeat = False self.fault_state_info_cp0 = FaultState( - ComponentInfo(0, "Interner Ladepunkt 0", "internal_chargepoint", hierarchy_id=hierarchy_id_cp0, - parent_hostname=global_data.parent_ip)) + ComponentInfo(0, "Interner Ladepunkt 0", "internal_chargepoint", hierarchy_id=hierarchy_id_cp0)) fault_state_info_cp1 = FaultState( - ComponentInfo(1, "Interner Ladepunkt 1", "internal_chargepoint", hierarchy_id=hierarchy_id_cp1, - parent_hostname=global_data.parent_ip)) + ComponentInfo(1, "Interner Ladepunkt 1", "internal_chargepoint", hierarchy_id=hierarchy_id_cp1)) with SingleComponentUpdateContext(self.fault_state_info_cp0, reraise=True): self.init_gpio() try: @@ -245,23 +193,21 @@ def __init__(self, self.mode = mode if local_charge_point_num == 0: if mode == InternalChargepointMode.SOCKET.value: - self.module = Socket(local_charge_point_num, client_handler, - global_data.parent_ip, internal_cp, hierarchy_id) + self.module = Socket(local_charge_point_num, client_handler, internal_cp, hierarchy_id) elif mode == InternalChargepointMode.PRO_PLUS.value: - self.module = ProPlus(local_charge_point_num, global_data.parent_ip, internal_cp, hierarchy_id) + self.module = ProPlus(local_charge_point_num, internal_cp, hierarchy_id) else: self.module = chargepoint_module.ChargepointModule( - local_charge_point_num, client_handler, global_data.parent_ip, internal_cp, hierarchy_id) + local_charge_point_num, client_handler, internal_cp, hierarchy_id) else: self.module = chargepoint_module.ChargepointModule( - local_charge_point_num, client_handler, global_data.parent_ip, internal_cp, hierarchy_id) + local_charge_point_num, client_handler, internal_cp, hierarchy_id) with SingleComponentUpdateContext(self.module.fault_state): - self.update_values = UpdateValues(local_charge_point_num, - global_data.parent_ip, - internal_cp.data.parent_cp, - hierarchy_id) self.update_state = UpdateState(self.module, hierarchy_id) self.old_plug_state = False + if global_data.parent_ip != "localhost": + pub_single(f"openWB/set/chargepoint/{hierarchy_id}/get/state_str", + payload="Statusmeldungen bitte auf der Primary-openWB einsehen.") def update(self, global_data: GlobalHandlerData, data: InternalChargepointData, rfid_data: RfidData) -> bool: def __thread_active(thread: Optional[Thread]) -> bool: @@ -277,8 +223,6 @@ def __thread_active(thread: Optional[Thread]) -> bool: state = self.module.get_values(phase_switch_cp_active, rfid_data.last_tag) log.debug("Published plug state "+str(state.plug_state)) heartbeat_expired = self._check_heartbeat_expired(global_data.heartbeat) - if global_data.parent_ip is not None: - self.update_values.update_values(state, heartbeat_expired) self.update_state.update_state(data, heartbeat_expired) return True return False diff --git a/packages/modules/internal_chargepoint_handler/pro_plus.py b/packages/modules/internal_chargepoint_handler/pro_plus.py index cea03ab13e..cb8de42f05 100644 --- a/packages/modules/internal_chargepoint_handler/pro_plus.py +++ b/packages/modules/internal_chargepoint_handler/pro_plus.py @@ -9,7 +9,6 @@ class ProPlus(ChargepointModule): def __init__(self, local_charge_point_num: int, - parent_hostname: str, internal_cp: InternalChargepoint, hierarchy_id: int) -> None: self.local_charge_point_num = local_charge_point_num @@ -18,7 +17,7 @@ def __init__(self, local_charge_point_num: int, self.old_chargepoint_state = None super().__init__(OpenWBPro(configuration=OpenWBProConfiguration(ip_address="192.168.192.50"))) - super().set_internal_context_handlers(hierarchy_id, internal_cp, parent_hostname) + super().set_internal_context_handlers(hierarchy_id, internal_cp) def get_values(self, phase_switch_cp_active: bool, last_tag: str) -> ChargepointState: def store_state(chargepoint_state: ChargepointState) -> None: diff --git a/packages/modules/internal_chargepoint_handler/socket.py b/packages/modules/internal_chargepoint_handler/socket.py index 205496b1dd..dca1a00e80 100644 --- a/packages/modules/internal_chargepoint_handler/socket.py +++ b/packages/modules/internal_chargepoint_handler/socket.py @@ -51,13 +51,12 @@ class Socket(ChargepointModule): def __init__(self, local_charge_point_num: int, client_handler: ClientHandler, - parent_hostname: str, internal_cp: InternalChargepoint, hierarchy_id: int) -> None: self.socket_max_current = get_hardware_configuration_setting("max_c_socket") with ModifyLoglevelContext(log, logging.DEBUG): log.info(f"Konfiguration als Buchse mit maximal {self.socket_max_current}A Ladestrom je Phase.") - super().__init__(local_charge_point_num, client_handler, parent_hostname, internal_cp, hierarchy_id) + super().__init__(local_charge_point_num, client_handler, internal_cp, hierarchy_id) def set_current(self, current: float) -> None: with SingleComponentUpdateContext(self.fault_state, update_always=False): diff --git a/packages/modules/internal_chargepoint_handler/update_values_test.py b/packages/modules/internal_chargepoint_handler/update_values_test.py deleted file mode 100644 index 44e5317532..0000000000 --- a/packages/modules/internal_chargepoint_handler/update_values_test.py +++ /dev/null @@ -1,42 +0,0 @@ -import copy -from unittest.mock import Mock -import pytest - -from modules.common.component_state import ChargepointState -from modules.conftest import SAMPLE_IP -from modules.internal_chargepoint_handler.internal_chargepoint_handler import UpdateValues -from modules.internal_chargepoint_handler import internal_chargepoint_handler - -CHARGEPOINT_STATE = ChargepointState( - power=1430, - currents=[6]*3, - imported=100, - exported=0, - voltages=[230]*3, - plug_state=True, - charge_state=True, - phases_in_use=3, - rfid="2") - -OLD_CHARGEPOINT_STATE = copy.deepcopy(CHARGEPOINT_STATE) -OLD_CHARGEPOINT_STATE.imported = 80 - - -@pytest.mark.parametrize( - "old_chargepoint_state, published_topics", - [(None, 50), - (OLD_CHARGEPOINT_STATE, 2)] - -) -def test_update_values(old_chargepoint_state, published_topics, monkeypatch): - # setup - mock_pub_single = Mock() - monkeypatch.setattr(internal_chargepoint_handler, "pub_single", mock_pub_single) - u = UpdateValues(0, SAMPLE_IP, "1", 1) - u.old_chargepoint_state = old_chargepoint_state - - # execution - u.update_values(CHARGEPOINT_STATE, False) - - # evaluation - assert mock_pub_single.call_count == published_topics