Skip to content

Commit

Permalink
Unix timestamps (#753)
Browse files Browse the repository at this point in the history
* unix timestamps

* fix

* fix

* fix
  • Loading branch information
LKuemmel authored Dec 20, 2023
1 parent 632881f commit 9438ab4
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 198 deletions.
2 changes: 1 addition & 1 deletion packages/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def mock_today(monkeypatch) -> None:
datetime_mock.today.return_value = datetime.datetime(2022, 5, 16, 8, 40, 52)
monkeypatch.setattr(datetime, "datetime", datetime_mock)
mock_today_timestamp = Mock(return_value=1652683252)
monkeypatch.setattr(timecheck, "create_timestamp_unix", mock_today_timestamp)
monkeypatch.setattr(timecheck, "create_timestamp", mock_today_timestamp)


@pytest.fixture(autouse=True)
Expand Down
3 changes: 1 addition & 2 deletions packages/control/algorithm/filter_chargepoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from control import data
from control.algorithm import common
from control.chargepoint.chargepoint import Chargepoint
from helpermodules.timecheck import convert_to_unix_timestamp

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,7 +94,7 @@ def _get_preferenced_chargepoint(valid_chargepoints: List[Chargepoint]) -> List:
chargepoints.update(
(cp, cp.data.set.charging_ev_data.data.get.soc) for cp in chargepoints.keys())
elif condition_types[condition] == "plug_in":
chargepoints.update((cp, convert_to_unix_timestamp(cp.data.set.plug_time))
chargepoints.update((cp, cp.data.set.plug_time)
for cp in chargepoints.keys())
elif condition_types[condition] == "imported_since_plugged":
chargepoints.update((cp, cp.data.set.log.imported_since_plugged) for cp in chargepoints.keys())
Expand Down
12 changes: 6 additions & 6 deletions packages/control/algorithm/integration_test/pv_charging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def test_start_pv_delay(all_cp_pv_charging_3p, all_cp_not_charging, monkeypatch)
for i in range(3, 6):
assert data.data.cp_data[f"cp{i}"].data.set.current == 0
assert data.data.cp_data[
"cp3"].data.control_parameter.timestamp_switch_on_off == "05/16/2022, 08:40:52"
"cp3"].data.control_parameter.timestamp_switch_on_off == 1652683252.0
assert data.data.cp_data[
"cp4"].data.control_parameter.timestamp_switch_on_off == "05/16/2022, 08:40:52"
"cp4"].data.control_parameter.timestamp_switch_on_off == 1652683252.0
assert data.data.cp_data[
"cp5"].data.control_parameter.timestamp_switch_on_off is None
assert data.data.counter_data["counter0"].data.set.raw_power_left == 31775
Expand All @@ -145,12 +145,12 @@ def test_pv_delay_expired(all_cp_pv_charging_3p, all_cp_not_charging, monkeypatc
data.data.counter_data["counter6"].data.set.raw_currents_left = [16, 12, 14]
data.data.counter_data["counter0"].data.set.reserved_surplus = 9000
data.data.cp_data[
"cp3"].data.control_parameter.timestamp_switch_on_off = "05/16/2022, 08:39:45"
"cp3"].data.control_parameter.timestamp_switch_on_off = 1652683185.0
data.data.cp_data[
"cp3"].data.control_parameter.state = ChargepointState.SWITCH_ON_DELAY
# nicht genug Überschuss für beide
data.data.cp_data[
"cp4"].data.control_parameter.timestamp_switch_on_off = "05/16/2022, 08:40:52"
"cp4"].data.control_parameter.timestamp_switch_on_off = 1652683252.0
data.data.cp_data[
"cp4"].data.control_parameter.state = ChargepointState.SWITCH_ON_DELAY
data.data.cp_data[
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_surplus(params: ParamsSurplus, all_cp_pv_charging_3p, all_cp_charging_3
raw_power_left=32580,
raw_currents_left_counter0=[40]*3,
raw_currents_left_counter6=[16]*3,
expected_timestamp_auto_phase_switch_cp3="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch_cp3=1652683252.0,
expected_timestamp_auto_phase_switch_cp4=None,
expected_timestamp_auto_phase_switch_cp5=None,
expected_current_cp3=10,
Expand All @@ -253,7 +253,7 @@ def test_surplus(params: ParamsSurplus, all_cp_pv_charging_3p, all_cp_charging_3
raw_power_left=42580,
raw_currents_left_counter0=[40]*3,
raw_currents_left_counter6=[16]*3,
expected_timestamp_auto_phase_switch_cp3="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch_cp3=1652683252.0,
expected_timestamp_auto_phase_switch_cp4=None,
expected_timestamp_auto_phase_switch_cp5=None,
expected_current_cp3=32,
Expand Down
28 changes: 14 additions & 14 deletions packages/control/auto_phase_switch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Params:
def __init__(self,
name: str,
max_current_single_phase: int,
timestamp_auto_phase_switch: Optional[str],
timestamp_auto_phase_switch: Optional[float],
phases_to_use: int,
required_current: float,
evu_surplus: int,
Expand All @@ -43,7 +43,7 @@ def __init__(self,
expected_current: float,
expected_state: ChargepointState,
expected_message: Optional[str] = None,
expected_timestamp_auto_phase_switch: Optional[str] = None) -> None:
expected_timestamp_auto_phase_switch: Optional[float] = None) -> None:
self.name = name
self.max_current_single_phase = max_current_single_phase
self.timestamp_auto_phase_switch = timestamp_auto_phase_switch
Expand All @@ -66,28 +66,28 @@ def __init__(self,
phases_to_use=1, required_current=6, evu_surplus=-800, reserved_evu_overhang=0, get_currents=[15.6, 0, 0],
get_power=3450, state=ChargepointState.CHARGING_ALLOWED, expected_phases_to_use=1, expected_current=6,
expected_message="Umschaltverzögerung von 1 auf 3 Phasen für 7.0 Min aktiv.",
expected_timestamp_auto_phase_switch="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("1to3, not enough power, start timer", max_current_single_phase=16, timestamp_auto_phase_switch=None,
phases_to_use=1, required_current=6, evu_surplus=-300, reserved_evu_overhang=0, get_currents=[15.6, 0, 0],
get_power=3450, state=ChargepointState.CHARGING_ALLOWED, expected_phases_to_use=1, expected_current=6,
expected_state=ChargepointState.CHARGING_ALLOWED),
Params("1to3, enough power, timer not expired", max_current_single_phase=16,
timestamp_auto_phase_switch="05/16/2022, 08:35:52", phases_to_use=1, required_current=6,
timestamp_auto_phase_switch=1652682952.0, phases_to_use=1, required_current=6,
evu_surplus=-1200, reserved_evu_overhang=460, get_currents=[15.6, 0, 0], get_power=3450,
state=ChargepointState.PHASE_SWITCH_DELAY, expected_phases_to_use=1, expected_current=6,
expected_message="Umschaltverzögerung von 1 auf 3 Phasen für 7.0 Min aktiv.",
expected_timestamp_auto_phase_switch="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("1to3, not enough power, timer not expired", max_current_single_phase=16,
timestamp_auto_phase_switch="05/16/2022, 08:35:52", phases_to_use=1, required_current=6,
timestamp_auto_phase_switch=1652682952.0, phases_to_use=1, required_current=6,
evu_surplus=0, reserved_evu_overhang=460, get_currents=[15.6, 0, 0], get_power=3450,
state=ChargepointState.PHASE_SWITCH_DELAY, expected_phases_to_use=1, expected_current=6,
expected_message=f"Umschaltverzögerung von 1 auf 3 Phasen abgebrochen{Ev.NOT_ENOUGH_POWER}",
expected_timestamp_auto_phase_switch="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.CHARGING_ALLOWED),
Params("1to3, enough power, timer expired", max_current_single_phase=16,
timestamp_auto_phase_switch="05/16/2022, 08:32:52", phases_to_use=1, required_current=6,
timestamp_auto_phase_switch=1652682772.0, phases_to_use=1, required_current=6,
evu_surplus=-1200, reserved_evu_overhang=460, get_currents=[15.6, 0, 0], get_power=3450,
state=ChargepointState.PHASE_SWITCH_DELAY,
expected_phases_to_use=3, expected_current=6, expected_state=ChargepointState.PHASE_SWITCH_DELAY_EXPIRED),
Expand All @@ -97,25 +97,25 @@ def __init__(self,
get_currents=[4.5, 4.4, 5.8], get_power=3381, state=ChargepointState.CHARGING_ALLOWED,
expected_phases_to_use=3, expected_current=6,
expected_message="Umschaltverzögerung von 3 auf 1 Phasen für 9.0 Min aktiv.",
expected_timestamp_auto_phase_switch="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("3to1, not enough power, timer not expired", max_current_single_phase=16,
timestamp_auto_phase_switch="05/16/2022, 08:35:52",
timestamp_auto_phase_switch=1652682952.0,
phases_to_use=3, required_current=6, evu_surplus=0, reserved_evu_overhang=-460,
get_currents=[4.5, 4.4, 5.8], get_power=3381, state=ChargepointState.PHASE_SWITCH_DELAY,
expected_phases_to_use=3, expected_current=6,
expected_message="Umschaltverzögerung von 3 auf 1 Phasen für 9.0 Min aktiv.",
expected_timestamp_auto_phase_switch="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("3to1, enough power, timer not expired", max_current_single_phase=16,
timestamp_auto_phase_switch="05/16/2022, 08:35:52", phases_to_use=3, required_current=6,
timestamp_auto_phase_switch=1652682952.0, phases_to_use=3, required_current=6,
evu_surplus=-860, reserved_evu_overhang=0, get_currents=[4.5, 4.4, 5.8],
get_power=3381, state=ChargepointState.PHASE_SWITCH_DELAY, expected_phases_to_use=3, expected_current=6,
expected_message=f"Umschaltverzögerung von 3 auf 1 Phasen abgebrochen{Ev.ENOUGH_POWER}",
expected_timestamp_auto_phase_switch="05/16/2022, 08:40:52",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.CHARGING_ALLOWED),
Params("3to1, not enough power, timer expired", max_current_single_phase=16,
timestamp_auto_phase_switch="05/16/2022, 08:29:52", phases_to_use=3, required_current=6,
timestamp_auto_phase_switch=1652682592.0, phases_to_use=3, required_current=6,
evu_surplus=0, reserved_evu_overhang=-460, get_currents=[4.5, 4.4, 5.8],
get_power=3381, state=ChargepointState.PHASE_SWITCH_DELAY, expected_phases_to_use=1, expected_current=16,
expected_state=ChargepointState.PHASE_SWITCH_DELAY_EXPIRED),
Expand Down
7 changes: 4 additions & 3 deletions packages/control/chargelog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import json
import logging
import math
Expand Down Expand Up @@ -53,7 +54,7 @@ def collect_data(chargepoint):
f"counter {chargepoint.data.get.imported}")
log_data.range_charged = log_data.imported_since_mode_switch / \
charging_ev.ev_template.data.average_consump * 100
log_data.time_charged, _ = timecheck.get_difference_to_now(log_data.timestamp_start_charging)
log_data.time_charged = timecheck.get_difference_to_now(log_data.timestamp_start_charging)[0]
Pub().pub(f"openWB/set/chargepoint/{chargepoint.num}/set/log", asdict(log_data))
except Exception:
log.exception("Fehler im Ladelog-Modul")
Expand Down Expand Up @@ -111,8 +112,8 @@ def save_data(chargepoint, charging_ev, immediately: bool = True, reset: bool =
},
"time":
{
"begin": log_data.timestamp_start_charging,
"end": timecheck.create_timestamp(),
"begin": datetime.fromtimestamp(log_data.timestamp_start_charging).strftime("%m/%d/%Y, %H:%M:%S"),
"end": datetime.fromtimestamp(timecheck.create_timestamp()).strftime("%m/%d/%Y, %H:%M:%S"),
"time_charged": log_data.time_charged
},
"data":
Expand Down
8 changes: 4 additions & 4 deletions packages/control/chargepoint/chargepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class Log:
imported_since_mode_switch: float = 0
imported_since_plugged: float = 0
range_charged: float = 0
time_charged: str = "00:00"
timestamp_start_charging: Optional[str] = None
time_charged: float = 0
timestamp_start_charging: Optional[float] = None


def connected_vehicle_factory() -> ConnectedVehicle:
Expand All @@ -142,7 +142,7 @@ class Get:
phases_in_use: int = 0
plug_state: bool = False
power: float = 0
rfid_timestamp: Optional[str] = None
rfid_timestamp: Optional[float] = None
rfid: Optional[str] = None
soc: Optional[float] = None
soc_timestamp: Optional[int] = None
Expand Down Expand Up @@ -170,7 +170,7 @@ class Set:
manual_lock: bool = False
phases_to_use: int = 0
plug_state_prev: bool = False
plug_time: Optional[str] = None
plug_time: Optional[float] = None
required_power: float = 0
rfid: Optional[str] = None
target_current: float = 0 # Sollstrom aus fest vorgegebener Stromstärke
Expand Down
6 changes: 3 additions & 3 deletions packages/control/chargepoint/control_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class ControlParameter:
submode: Chargemode_enum = field(
default=Chargemode_enum.STOP,
metadata={"topic": "control_parameter/submode", "mutable_by_algorithm": True})
timestamp_auto_phase_switch: Optional[str] = field(
timestamp_auto_phase_switch: Optional[float] = field(
default=None,
metadata={"topic": "control_parameter/timestamp_auto_phase_switch", "mutable_by_algorithm": True})
timestamp_perform_phase_switch: Optional[str] = field(
timestamp_perform_phase_switch: Optional[float] = field(
default=None,
metadata={"topic": "control_parameter/timestamp_perform_phase_switch", "mutable_by_algorithm": True})
timestamp_switch_on_off: Optional[str] = field(
timestamp_switch_on_off: Optional[float] = field(
default=None,
metadata={"topic": "control_parameter/timestamp_switch_on_off", "mutable_by_algorithm": True})

Expand Down
12 changes: 6 additions & 6 deletions packages/control/counter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ class Params:

cases = [
Params("Einschaltschwelle wurde unterschritten, Timer zurücksetzen", False, 1500, -119,
1500, '05/16/2022, 08:40:50', ChargepointState.SWITCH_ON_DELAY,
1500, 1652683250.0, ChargepointState.SWITCH_ON_DELAY,
Counter.SWITCH_ON_FALLEN_BELOW.format(1500), None, 0),
Params("Timer starten", False, 0, 1501, 1500, None, ChargepointState.NO_CHARGING_ALLOWED,
Counter.SWITCH_ON_WAITING.format(30), '05/16/2022, 08:40:52', 1500),
Counter.SWITCH_ON_WAITING.format(30), 1652683252.0, 1500),
Params("Einschaltschwelle nicht erreicht", False, 0, 1499, 1500,
None, ChargepointState.NO_CHARGING_ALLOWED, Counter.SWITCH_ON_NOT_EXCEEDED.format(1500), None, 0),
Params("Einschaltschwelle läuft", False, 1500, 121, 1500,
'05/16/2022, 08:40:50', ChargepointState.SWITCH_ON_DELAY, None, '05/16/2022, 08:40:50', 1500),
1652683250.0, ChargepointState.SWITCH_ON_DELAY, None, 1652683250.0, 1500),
Params("Feed_in_limit, Einschaltschwelle wurde unterschritten, Timer zurücksetzen", True, 1500,
-681, 15000, '05/16/2022, 08:40:50', ChargepointState.SWITCH_ON_DELAY,
-681, 15000, 1652683250.0, ChargepointState.SWITCH_ON_DELAY,
Counter.SWITCH_ON_FALLEN_BELOW.format(1500), None, 0),
Params("Feed_in_limit, Timer starten", True, 0, 15001, 15000, None, ChargepointState.NO_CHARGING_ALLOWED,
Counter.SWITCH_ON_WAITING.format(30), '05/16/2022, 08:40:52', 1500),
Counter.SWITCH_ON_WAITING.format(30), 1652683252.0, 1500),
Params("Feed_in_limit, Einschaltschwelle nicht erreicht", True, 0, 14999,
15000, None, ChargepointState.NO_CHARGING_ALLOWED, Counter.SWITCH_ON_NOT_EXCEEDED.format(1500), None, 0),
Params("Feed_in_limit, Einschaltschwelle läuft", True, 1500, 15001,
15000, '05/16/2022, 08:40:50', ChargepointState.SWITCH_ON_DELAY, None, '05/16/2022, 08:40:50', 1500),
15000, 1652683250.0, ChargepointState.SWITCH_ON_DELAY, None, 1652683250.0, 1500),
]


Expand Down
2 changes: 1 addition & 1 deletion packages/control/ev.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def set_factory() -> Set:
@dataclass
class Get:
soc: int = 0
soc_timestamp: Optional[str] = None
soc_timestamp: float = 0
force_soc_update: bool = False
range: float = 0
fault_state: int = 0
Expand Down
13 changes: 7 additions & 6 deletions packages/control/ev_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from unittest.mock import Mock

import pytest
Expand All @@ -11,15 +12,15 @@

@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"),
[pytest.param(False, False, None, True, id="no soc_timestamp"),
pytest.param(True, False, 100, False, id="not charging, not expired"),
pytest.param(False, False, 100, True, id="not charging, expired"),
pytest.param(True, True, 100, False, id="charging, not expired"),
pytest.param(False, True, 100, True, id="charging, expired"),
])
def test_soc_interval_expired(check_timestamp: bool,
charge_state: bool,
soc_timestamp: str,
soc_timestamp: Optional[float],
expected_request_soc: bool,
monkeypatch):
# setup
Expand Down
2 changes: 1 addition & 1 deletion packages/control/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GeneralData:
grid_protection_active: bool = False
grid_protection_configured: bool = True
grid_protection_random_stop: int = 0
grid_protection_timestamp: Optional[str] = ""
grid_protection_timestamp: Optional[float] = ""
mqtt_bridge: bool = False
price_kwh: float = 0.3
range_unit: str = "km"
Expand Down
2 changes: 1 addition & 1 deletion packages/helpermodules/measurement_logging/write_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def save_log(folder):
date = timecheck.create_timestamp_time()
else:
date = timecheck.create_timestamp_YYYYMMDD()
current_timestamp = timecheck.create_timestamp_unix()
current_timestamp = timecheck.create_timestamp()
cp_dict = {}
for cp in data.data.cp_data:
try:
Expand Down
2 changes: 1 addition & 1 deletion packages/helpermodules/modbusserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def read_data_store(slave_id, function_code, address):
"""" Return value of address. """
if address > 10099:
Pub().pub("openWB/set/internal_chargepoint/global_data",
{"heartbeat": timecheck.create_timestamp_unix(), "parent_ip": None})
{"heartbeat": timecheck.create_timestamp(), "parent_ip": None})
chargepoint = SubData.internal_chargepoint_data[f"cp{_get_pos(address, 2)}"]
askedvalue = int(str(address)[-2:])
if askedvalue == 00:
Expand Down
Loading

0 comments on commit 9438ab4

Please sign in to comment.