Skip to content

Commit

Permalink
Merge pull request #615 from LKuemmel/fix_soc_request
Browse files Browse the repository at this point in the history
fix soc request
  • Loading branch information
LKuemmel committed Nov 8, 2022
2 parents 018e233 + c5ecb2e commit ddd9f22
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
33 changes: 9 additions & 24 deletions packages/control/ev.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,6 @@ class ChargeTemplateData:
chargemode: Chargemode = field(default_factory=chargemode_factory)


@dataclass
class Soc:
request_interval_charging: int = 5
request_interval_not_charging: int = 720
request_only_plugged: bool = False


def soc_factory() -> Soc:
return Soc()


@dataclass
class EvTemplateData:
name: str = "Standard-Fahrzeug-Vorlage"
Expand All @@ -141,7 +130,6 @@ class EvTemplateData:
max_current_one_phase: int = 32
battery_capacity: float = 82
nominal_difference: int = 2
soc: Soc = field(default_factory=soc_factory)


def ev_template_data_factory() -> EvTemplateData:
Expand Down Expand Up @@ -171,23 +159,20 @@ class EvTemplate:
data: EvTemplateData = field(default_factory=ev_template_data_factory)
et_num: int = 0

def soc_interval_expired(
self, plug_state: bool, charge_state: bool, soc_timestamp: str) -> bool:
def soc_interval_expired(self, charge_state: bool, soc_timestamp: str) -> bool:
request_soc = False
if soc_timestamp == "":
# Initiale Abfrage
request_soc = True
else:
if (self.data.soc.request_only_plugged is False or
(self.data.soc.request_only_plugged is True and plug_state is True)):
if charge_state is True:
interval = self.data.soc.request_interval_charging
else:
interval = self.data.soc.request_interval_not_charging
# Zeitstempel prüfen, ob wieder abgefragt werden muss.
if timecheck.check_timestamp(soc_timestamp, interval*60-5) is False:
# Zeit ist abgelaufen
request_soc = True
if charge_state is True:
interval = 5
else:
interval = 720
# Zeitstempel prüfen, ob wieder abgefragt werden muss.
if timecheck.check_timestamp(soc_timestamp, interval*60-5) is False:
# Zeit ist abgelaufen
request_soc = True
return request_soc


Expand Down
31 changes: 31 additions & 0 deletions packages/control/ev_template_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import Mock

import pytest

from control.ev import EvTemplate
from helpermodules import timecheck


@pytest.mark.parametrize(
"check_timestamp, charge_state, soc_timestamp, expected_request_soc",
[pytest.param(False, False, "", True, id="no soc_timestamp"),
pytest.param(True, False, "2022/05/16, 8:30:52", False, id="not charging, not expired"),
pytest.param(False, False, "2022/05/15, 20:30:52", True, id="not charging, expired"),
pytest.param(True, True, "2022/05/16, 8:36:52", False, id="charging, not expired"),
pytest.param(False, True, "2022/05/16, 8:35:50", True, id="charging, expired"),
])
def test_soc_interval_expired(check_timestamp: bool,
charge_state: bool,
soc_timestamp: str,
expected_request_soc: bool,
monkeypatch):
# setup
et = EvTemplate()
check_timestamp_mock = Mock(return_value=check_timestamp)
monkeypatch.setattr(timecheck, "check_timestamp", check_timestamp_mock)

# execution
request_soc = et.soc_interval_expired(charge_state, soc_timestamp)

# evaluation
assert request_soc == expected_request_soc
2 changes: 0 additions & 2 deletions packages/modules/common/store/_car.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from helpermodules import compatibility
from helpermodules import timecheck
from modules.common.component_state import CarState
from modules.common.fault_state import FaultState
from modules.common.store import ValueStore
Expand Down Expand Up @@ -28,7 +27,6 @@ def update(self):
pub_to_broker("openWB/set/vehicle/"+str(self.vehicle_id)+"/get/soc", self.state.soc)
if self.state.range:
pub_to_broker("openWB/set/vehicle/"+str(self.vehicle_id)+"/get/range", self.state.range)
pub_to_broker("openWB/set/vehicle/"+str(self.vehicle_id)+"/get/soc_timestamp", timecheck.create_timestamp())
except Exception as e:
raise FaultState.from_exception(e)

Expand Down
4 changes: 1 addition & 3 deletions packages/modules/mqtt/soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dataclass_utils import dataclass_from_dict
from modules.mqtt.config import MqttSocSetup
from helpermodules.cli import run_using_positional_cli_args
from modules.common import store
from modules.common.abstract_device import DeviceDescriptor
from modules.common.abstract_soc import AbstractSoc

Expand All @@ -18,7 +17,6 @@ class Soc(AbstractSoc):
def __init__(self, device_config: Union[dict, MqttSocSetup], vehicle: int):
self.config = dataclass_from_dict(MqttSocSetup, device_config)
self.vehicle = vehicle
self.store = store.get_car_value_store(self.vehicle)
self.component_info = ComponentInfo(self.vehicle, self.config.name, "vehicle")

def update(self, charge_state: bool = False) -> None:
Expand All @@ -30,7 +28,7 @@ def mqtt_update(akey: str, token: str, charge_point: int):


def main(argv: List[str]):
log.debug('Mqtt SOC main, argv: ' + sys.argv[1:])
log.debug('Mqtt SOC main, argv: ' + str(sys.argv[1:]))
run_using_positional_cli_args(mqtt_update, argv)


Expand Down
8 changes: 5 additions & 3 deletions packages/modules/update_soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from control import data
from control.chargepoint import AllChargepoints
from helpermodules import timecheck
from helpermodules.pub import Pub
from helpermodules.utils import thread_handler

Expand Down Expand Up @@ -43,16 +44,17 @@ def __get_threads(self) -> Tuple[List[threading.Thread], List[threading.Thread]]
if not isinstance(cp, AllChargepoints):
if cp.data.set.charging_ev == ev.num:
charge_state = cp.data.get.charge_state
plug_state = cp.data.get.plug_state
break
else:
charge_state = False
plug_state = False
if (ev.ev_template.soc_interval_expired(plug_state, charge_state, ev.data.get.soc_timestamp) or
if (ev.ev_template.soc_interval_expired(charge_state, ev.data.get.soc_timestamp) or
ev.data.get.force_soc_update):
if ev.data.get.force_soc_update:
ev.data.get.force_soc_update = False
Pub().pub(f"openWB/set/vehicle/{ev.num}/get/force_soc_update", False)
# Es wird ein Zeitstempel gesetzt, unabhängig ob die Abfrage erfolgreich war, da einige
# Hersteller bei zu häufigen Abfragen Accounts sperren.
Pub().pub(f"openWB/set/vehicle/{ev.num}/get/soc_timestamp", timecheck.create_timestamp())
threads_set.append(threading.Thread(target=ev.soc_module.update,
args=(charge_state,), name=f"soc_ev{ev.num}"))
threads_update.append(threading.Thread(target=ev.soc_module.store.update,
Expand Down

0 comments on commit ddd9f22

Please sign in to comment.