|
| 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