Skip to content

Commit 8f17645

Browse files
committed
split store.py into a module with multiple small files
1 parent bea61e5 commit 8f17645

File tree

12 files changed

+234
-205
lines changed

12 files changed

+234
-205
lines changed

.flake8

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
per-file-ignores = */__init__.py:F401
3+
max-line-length = 120

.github/workflows/github-actions-python.yml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
uses: TrueBrain/[email protected]
2020
with:
2121
path: packages
22-
max_line_length: 120
2322
- name: Test with pytest
2423
run: |
2524
cd packages && python -m pytest

packages/modules/common/store.py

-204
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from modules.common.store._api import ValueStore
2+
from modules.common.store._battery import get_bat_value_store
3+
from modules.common.store._car import get_car_value_store
4+
from modules.common.store._counter import get_counter_value_store
5+
from modules.common.store._inverter import get_inverter_value_store

packages/modules/common/store/_api.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from abc import abstractmethod
2+
from typing import Generic, TypeVar
3+
4+
T = TypeVar("T")
5+
6+
7+
class ValueStore(Generic[T]):
8+
@abstractmethod
9+
def set(self, state: T) -> None:
10+
pass
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from helpermodules import log, compatibility
2+
from modules.common.component_state import BatState
3+
from modules.common.store import ValueStore
4+
from modules.common.store._broker import pub_to_broker
5+
from modules.common.store._ramdisk import write_to_file
6+
from modules.common.store._util import process_error
7+
8+
9+
class BatteryValueStoreRamdisk(ValueStore[BatState]):
10+
def __init__(self, component_num: int) -> None:
11+
self.num = component_num
12+
13+
def set(self, bat_state: BatState):
14+
try:
15+
power = write_to_file("/speicherleistung", bat_state.power, 0)
16+
write_to_file("/speichersoc", bat_state.soc, 0)
17+
write_to_file("/speicherikwh", bat_state.imported, 2)
18+
write_to_file("/speicherekwh", bat_state.exported, 2)
19+
log.MainLogger().info('BAT Watt: ' + str(power))
20+
log.MainLogger().info('BAT Einspeisung: ' + str(bat_state.exported))
21+
log.MainLogger().info('BAT Bezug: ' + str(bat_state.imported))
22+
except Exception as e:
23+
process_error(e)
24+
25+
26+
class BatteryValueStoreBroker(ValueStore[BatState]):
27+
def __init__(self, component_num: int) -> None:
28+
self.num = component_num
29+
30+
def set(self, bat_state: BatState):
31+
try:
32+
pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/power", bat_state.power, 2)
33+
pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/soc", bat_state.soc, 0)
34+
pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/imported", bat_state.imported, 2)
35+
pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/exported", bat_state.exported, 2)
36+
except Exception as e:
37+
process_error(e)
38+
39+
40+
def get_bat_value_store(component_num: int) -> ValueStore[BatState]:
41+
if compatibility.is_ramdisk_in_use():
42+
return BatteryValueStoreRamdisk(component_num)
43+
return BatteryValueStoreBroker(component_num)
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Union
2+
3+
from helpermodules.pub import Pub
4+
from modules.common.store._util import get_rounding_function_by_digits, process_error
5+
6+
7+
def pub_to_broker(topic: str, value, digits: Union[int, None] = None) -> None:
8+
rounding = get_rounding_function_by_digits(digits)
9+
try:
10+
if isinstance(value, list):
11+
Pub().pub(topic, [rounding(v) for v in value])
12+
else:
13+
Pub().pub(topic, rounding(value))
14+
except Exception as e:
15+
process_error(e)

packages/modules/common/store/_car.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from helpermodules import compatibility
2+
from modules.common.component_state import CarState
3+
from modules.common.store import ValueStore
4+
from modules.common.store._broker import pub_to_broker
5+
from modules.common.store._ramdisk import write_to_file
6+
7+
8+
class CarValueStoreRamdisk(ValueStore[CarState]):
9+
def __init__(self, charge_point: int):
10+
self.filename = "soc" if charge_point == 1 else "soc1"
11+
12+
def set(self, state: CarState) -> None:
13+
write_to_file(self.filename, state.soc, 0)
14+
15+
16+
class CarValueStoreBroker(ValueStore[CarState]):
17+
def __init__(self, vehicle_id: int):
18+
self.topic = "openWB/set/ev/{}/get/counter".format(vehicle_id)
19+
20+
def set(self, state: CarState) -> None:
21+
pub_to_broker(self.topic, state.soc)
22+
23+
24+
def get_car_value_store(id: int) -> ValueStore[CarState]:
25+
if compatibility.is_ramdisk_in_use():
26+
return CarValueStoreRamdisk(id)
27+
return CarValueStoreBroker(id)
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from helpermodules import log, compatibility
2+
from modules.common.component_state import CounterState
3+
from modules.common.store import ValueStore
4+
from modules.common.store._broker import pub_to_broker
5+
from modules.common.store._ramdisk import write_array_to_files, write_to_file
6+
from modules.common.store._util import process_error
7+
8+
9+
class CounterValueStoreRamdisk(ValueStore[CounterState]):
10+
def __init__(self, component_num: int) -> None:
11+
self.num = component_num
12+
13+
def set(self, counter_state: CounterState):
14+
try:
15+
write_array_to_files("/evuv", counter_state.voltages, 1)
16+
write_array_to_files("/bezuga", counter_state.currents, 1)
17+
write_array_to_files("/bezugw", counter_state.powers, 0)
18+
write_array_to_files("/evupf", counter_state.power_factors, 2)
19+
imported = write_to_file("/bezugkwh", counter_state.imported)
20+
exported = write_to_file("/einspeisungkwh", counter_state.exported)
21+
power_all = write_to_file("/wattbezug", counter_state.power_all, 0)
22+
write_to_file("/evuhz", counter_state.frequency, 2)
23+
log.MainLogger().info('EVU Watt: ' + str(power_all))
24+
log.MainLogger().info('EVU Bezug: ' + str(imported))
25+
log.MainLogger().info('EVU Einspeisung: ' + str(exported))
26+
except Exception as e:
27+
process_error(e)
28+
29+
30+
class CounterValueStoreBroker(ValueStore[CounterState]):
31+
def __init__(self, component_num: int) -> None:
32+
self.num = component_num
33+
34+
def set(self, counter_state: CounterState):
35+
try:
36+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/voltage", counter_state.voltages, 2)
37+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/current", counter_state.currents, 2)
38+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/power_phase", counter_state.powers, 2)
39+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/power_factors", counter_state.power_factors, 2)
40+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/imported", counter_state.imported)
41+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/exported", counter_state.exported)
42+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/power_all", counter_state.power_all)
43+
pub_to_broker("openWB/set/counter/" + str(self.num) + "/get/frequency", counter_state.frequency)
44+
except Exception as e:
45+
process_error(e)
46+
47+
48+
def get_counter_value_store(component_num: int) -> ValueStore[CounterState]:
49+
if compatibility.is_ramdisk_in_use():
50+
return CounterValueStoreRamdisk(component_num)
51+
return CounterValueStoreBroker(component_num)

0 commit comments

Comments
 (0)