Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/samples/sample_modbus/sample_modbus/bat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_bat_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_modbus.sample_modbus.config import SampleBatSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand Down Expand Up @@ -42,8 +42,8 @@ def __init__(self, component_config: SampleBatSetup, **kwargs: Any) -> None:
def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.client: ModbusTcpClient_ = self.kwargs['client']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")
self.store = get_bat_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.BAT, self.component_config.id, self.fault_state)

Expand Down
6 changes: 3 additions & 3 deletions docs/samples/sample_modbus/sample_modbus/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_counter_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_modbus.sample_modbus.config import SampleCounterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand Down Expand Up @@ -46,8 +46,8 @@ def __init__(self, component_config: SampleCounterSetup, **kwargs: Any) -> None:
def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.client: ModbusTcpClient_ = self.kwargs['client']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.store = get_counter_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.COUNTER, self.component_config.id, self.fault_state)

Expand Down
6 changes: 3 additions & 3 deletions docs/samples/sample_modbus/sample_modbus/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_inverter_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_modbus.sample_modbus.config import SampleInverterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand Down Expand Up @@ -40,8 +40,8 @@ def __init__(self, component_config: SampleInverterSetup, **kwargs: Any) -> None
def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.client: ModbusTcpClient_ = self.kwargs['client']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv")
self.store = get_inverter_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.INVERTER, self.component_config.id, self.fault_state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_bat_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_request_by_component.sample_request_by_component.config import SampleBatSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand All @@ -25,8 +25,8 @@ def __init__(self, component_config: SampleBatSetup, **kwargs: Any) -> None:
def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.ip_address: str = self.kwargs['ip_address']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")
self.store = get_bat_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.BAT, self.component_config.id, self.fault_state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_counter_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_request_by_component.sample_request_by_component.config import SampleCounterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand All @@ -25,8 +25,8 @@ def __init__(self, component_config: SampleCounterSetup, **kwargs: Any) -> None:
def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.ip_address: str = self.kwargs['ip_address']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.store = get_counter_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.COUNTER, self.component_config.id, self.fault_state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_inverter_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_request_by_component.sample_request_by_component.config import SampleInverterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand All @@ -25,8 +25,8 @@ def __init__(self, component_config: SampleInverterSetup, **kwargs: Any) -> None
def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.ip_address: str = self.kwargs['ip_address']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv")
self.store = get_inverter_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.INVERTER, self.component_config.id, self.fault_state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_bat_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_request_by_device.sample_request_by_device.config import SampleBatSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand All @@ -22,8 +22,8 @@ def __init__(self, component_config: SampleBatSetup, **kwargs: Any) -> None:

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")
self.store = get_bat_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.BAT, self.component_config.id, self.fault_state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_counter_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_request_by_device.sample_request_by_device.config import SampleCounterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand All @@ -22,8 +22,8 @@ def __init__(self, component_config: SampleCounterSetup, **kwargs: Any) -> None:

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.store = get_counter_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.COUNTER, self.component_config.id, self.fault_state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_inverter_value_store
from modules.common.store import get_component_value_store
from modules.devices.sample_request_by_device.sample_request_by_device.config import SampleInverterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType
Expand All @@ -22,8 +22,8 @@ def __init__(self, component_config: SampleInverterSetup, **kwargs: Any) -> None

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv")
self.store = get_inverter_value_store(self.component_config.id)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type)
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.INVERTER, self.component_config.id, self.fault_state)

Expand Down
2 changes: 1 addition & 1 deletion packages/control/counter_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self):
self.connected_counters = []
self.connected_chargepoints = []
self.childless = []
self.sim_counter = SimCounter("", "", prefix="bezug")
self.sim_counter = SimCounter("", "", ComponentType.COUNTER.value)
self.sim_counter.topic = "openWB/set/counter/set/"

def get_evu_counter(self) -> Counter:
Expand Down
20 changes: 12 additions & 8 deletions packages/modules/common/simcount/_simcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import logging
import math
import time
from typing import Optional

from control import data as data_module
from modules.common.component_type import ComponentType
from modules.common.simcount._calculate import calculate_import_export
from modules.common.simcount.simcounter_state import SimCounterState
from modules.common.simcount._simcounter_store import get_sim_counter_store, restore_last_energy
from modules.common.simcount._simcounter_store import SimCounterStoreBroker, restore_last_energy

log = logging.getLogger(__name__)


def sim_count(power_present: float, topic: str = "", data: SimCounterState = None, prefix: str = "") -> SimCounterState:
def sim_count(power_present: float,
topic: str = "",
previous_state: SimCounterState = None,
component_type: Optional[ComponentType] = None) -> SimCounterState:
""" emulate import export

Parameters
Expand All @@ -27,25 +32,24 @@ def sim_count(power_present: float, topic: str = "", data: SimCounterState = Non
------
neuer state
"""
store = get_sim_counter_store()
store = SimCounterStoreBroker()
timestamp_present = time.time()
previous_state = store.load(prefix, topic) if data is None else data

if math.isnan(power_present):
raise ValueError("power_present is NaN.")

if isinstance(power_present, (int, float)):
if previous_state is None:
log.debug("No previous state found. Starting new simulation.")
return store.initialize(prefix, topic, power_present, timestamp_present)
return store.initialize(component_type, topic, power_present, timestamp_present)
else:
log.debug("Previous state: %s", previous_state)
if math.isnan(previous_state.imported):
log.error("imported is NaN. Reset simcount state.")
previous_state.imported = restore_last_energy(topic, "imported")
previous_state.imported = restore_last_energy(topic, "imported", component_type)
if math.isnan(previous_state.exported):
log.error("exported is NaN. Reset simcount state.")
previous_state.exported = restore_last_energy(topic, "exported")
previous_state.exported = restore_last_energy(topic, "exported", component_type)
control_interval = data_module.data.general_data.data.control_interval
if 2 * control_interval < timestamp_present - previous_state.timestamp:
log.warning("Time difference between previous state and current state is too large. "
Expand All @@ -63,5 +67,5 @@ def sim_count(power_present: float, topic: str = "", data: SimCounterState = Non
previous_state.exported + exported
)
log.debug("imported: %g Wh, exported: %g Wh, new state: %s", imported, exported, current_state)
store.save(prefix, topic, current_state)
store.save(topic, current_state)
return current_state
12 changes: 7 additions & 5 deletions packages/modules/common/simcount/_simcounter.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from typing import Tuple, Optional

from modules.common.component_type import ComponentType, special_to_general_type_mapping
from modules.common.simcount._simcount import sim_count
from modules.common.simcount.simcounter_state import SimCounterState


class SimCounter:
def __init__(self, device_id: int, component_id: int, prefix: str):
def __init__(self, device_id: int, component_id: int, component_type: str):
self.topic = "openWB/set/system/device/{}/component/{}/".format(device_id, component_id)
self.prefix = "pv2" if prefix == "pv" and component_id != 1 else prefix
self.component_type = special_to_general_type_mapping(
component_type.value if isinstance(component_type, ComponentType) else component_type)
self.data: Optional[SimCounterState] = None

def sim_count(self, power: float, dc_power: Optional[float] = None) -> Tuple[float, float]:
if (self.prefix == "pv" or self.prefix == "pv2") and dc_power is not None and dc_power == 0:
if self.component_type != ComponentType.INVERTER and dc_power is not None and dc_power == 0:
power = 0
self.data = sim_count(power, self.topic, self.data, self.prefix)
self.data = sim_count(power, self.topic, self.data, self.component_type)
return self.data.imported, self.data.exported


Expand All @@ -23,5 +25,5 @@ def __init__(self, chargepoint_id: int):
self.data = None # type: Optional[SimCounterState]

def sim_count(self, power: float) -> Tuple[float, float]:
self.data = sim_count(power, self.topic, self.data, "")
self.data = sim_count(power, self.topic, self.data, None)
return self.data.imported, self.data.exported
Loading