Skip to content

Commit 0b42a12

Browse files
authored
Json optimization (#2384)
* cache jq.compile * add power factors and voltages to json counter * cleanup empty settings * add currents for json inverter
1 parent 34bb8da commit 0b42a12

4 files changed

Lines changed: 93 additions & 37 deletions

File tree

packages/modules/devices/generic/json/bat.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,36 @@ def __init__(self, component_config: JsonBatSetup, **kwargs: Any) -> None:
2020
self.component_config = component_config
2121
self.kwargs: KwargsDict = kwargs
2222

23+
def _compile_jq_filters(self) -> None:
24+
config = self.component_config.configuration
25+
self.jq_power = jq.compile(config.jq_power)
26+
self.jq_soc = jq.compile(config.jq_soc) if config.jq_soc else None
27+
self.jq_currents = [jq.compile(c) for c in config.jq_currents] if all(config.jq_currents) else []
28+
self.jq_imported = jq.compile(config.jq_imported) if config.jq_imported else None
29+
self.jq_exported = jq.compile(config.jq_exported) if config.jq_exported else None
30+
2331
def initialize(self) -> None:
2432
self.__device_id: int = self.kwargs['device_id']
2533
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")
2634
self.store = get_bat_value_store(self.component_config.id)
35+
self._compile_jq_filters()
2736
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
2837

2938
def update(self, response) -> None:
30-
config = self.component_config.configuration
39+
currents = (
40+
[float(j.input(response).first()) for j in self.jq_currents]
41+
if len(self.jq_currents) == 3 else None
42+
)
3143

32-
currents = [0] * 3
33-
for i, c in enumerate(config.jq_currents):
34-
if c is not None:
35-
currents[i] = float(jq.compile(c).input(response).first())
44+
power = float(self.jq_power.input(response).first())
3645

37-
power = float(jq.compile(config.jq_power).input(response).first())
38-
if config.jq_soc != "":
39-
soc = float(jq.compile(config.jq_soc).input(response).first())
40-
else:
41-
soc = 0
46+
soc = float(self.jq_soc.input(response).first()) if self.jq_soc else 0
4247

43-
if config.jq_imported is not None and config.jq_exported is not None:
44-
imported = float(jq.compile(config.jq_imported).input(response).first())
45-
exported = float(jq.compile(config.jq_exported).input(response).first())
46-
else:
48+
if self.jq_imported is None or self.jq_exported is None:
4749
imported, exported = self.sim_counter.sim_count(power)
50+
else:
51+
imported = float(self.jq_imported.input(response).first())
52+
exported = float(self.jq_exported.input(response).first())
4853

4954
bat_state = BatState(
5055
currents=currents,

packages/modules/devices/generic/json/config.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,29 @@ def __init__(self,
4848

4949

5050
class JsonCounterConfiguration:
51-
def __init__(self, jq_power: str = "", jq_exported: Optional[str] = None, jq_imported: Optional[str] = None,
51+
def __init__(self,
52+
jq_power: str = "",
53+
jq_exported: Optional[str] = None,
54+
jq_imported: Optional[str] = None,
5255
jq_power_l1: Optional[str] = None,
5356
jq_power_l2: Optional[str] = None,
5457
jq_power_l3: Optional[str] = None,
58+
jq_power_factor_l1: Optional[str] = None,
59+
jq_power_factor_l2: Optional[str] = None,
60+
jq_power_factor_l3: Optional[str] = None,
5561
jq_current_l1: Optional[str] = None,
5662
jq_current_l2: Optional[str] = None,
57-
jq_current_l3: Optional[str] = None):
63+
jq_current_l3: Optional[str] = None,
64+
jq_voltage_l1: Optional[str] = None,
65+
jq_voltage_l2: Optional[str] = None,
66+
jq_voltage_l3: Optional[str] = None):
5867
self.jq_power = jq_power
5968
self.jq_exported = jq_exported
6069
self.jq_imported = jq_imported
6170
self.jq_powers = (jq_power_l1, jq_power_l2, jq_power_l3)
71+
self.jq_power_factors = (jq_power_factor_l1, jq_power_factor_l2, jq_power_factor_l3)
6272
self.jq_currents = (jq_current_l1, jq_current_l2, jq_current_l3)
73+
self.jq_voltages = (jq_voltage_l1, jq_voltage_l2, jq_voltage_l3)
6374

6475

6576
class JsonCounterSetup(ComponentSetup[JsonCounterConfiguration]):
@@ -72,9 +83,15 @@ def __init__(self,
7283

7384

7485
class JsonInverterConfiguration:
75-
def __init__(self, jq_power: str = "", jq_exported: Optional[str] = None):
86+
def __init__(self,
87+
jq_power: str = "",
88+
jq_exported: Optional[str] = None,
89+
jq_current_l1: Optional[str] = None,
90+
jq_current_l2: Optional[str] = None,
91+
jq_current_l3: Optional[str] = None):
7692
self.jq_power = jq_power
7793
self.jq_exported = jq_exported
94+
self.jq_currents = (jq_current_l1, jq_current_l2, jq_current_l3)
7895

7996

8097
class JsonInverterSetup(ComponentSetup[JsonInverterConfiguration]):

packages/modules/devices/generic/json/counter.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,61 @@ def __init__(self, component_config: JsonCounterSetup, **kwargs: Any) -> None:
2020
self.component_config = component_config
2121
self.kwargs: KwargsDict = kwargs
2222

23+
def _compile_jq_filters(self) -> None:
24+
config = self.component_config.configuration
25+
self.jq_power = jq.compile(config.jq_power)
26+
self.jq_powers = [jq.compile(p) for p in config.jq_powers] if all(config.jq_powers) else None
27+
self.jq_power_factors = [
28+
jq.compile(pf) for pf in config.jq_power_factors] if all(config.jq_power_factors) else None
29+
self.jq_currents = [jq.compile(c) for c in config.jq_currents] if all(config.jq_currents) else None
30+
self.jq_voltages = [jq.compile(v) for v in config.jq_voltages] if all(config.jq_voltages) else None
31+
self.jq_imported = jq.compile(config.jq_imported) if config.jq_imported else None
32+
self.jq_exported = jq.compile(config.jq_exported) if config.jq_exported else None
33+
2334
def initialize(self) -> None:
2435
self.__device_id: int = self.kwargs['device_id']
2536
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
2637
self.store = get_counter_value_store(self.component_config.id)
38+
self._compile_jq_filters()
2739
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
2840

2941
def update(self, response) -> None:
30-
config = self.component_config.configuration
42+
power = float(self.jq_power.input(response).first())
3143

32-
power = float(jq.compile(config.jq_power).input(response).first())
44+
powers = (
45+
[float(j.input(response).first()) for j in self.jq_powers]
46+
if self.jq_powers is not None else None
47+
)
3348

34-
if all(config.jq_powers):
35-
powers = [float(jq.compile(p).input(response).first()) for p in config.jq_powers]
36-
else:
37-
powers = None
49+
currents = (
50+
[float(j.input(response).first()) for j in self.jq_currents]
51+
if self.jq_currents is not None else None
52+
)
3853

39-
if all(config.jq_currents):
40-
currents = [float(jq.compile(c).input(response).first()) for c in config.jq_currents]
41-
else:
42-
currents = None
54+
power_factors = (
55+
[float(j.input(response).first()) for j in self.jq_power_factors]
56+
if self.jq_power_factors is not None else None
57+
)
58+
59+
voltages = (
60+
[float(j.input(response).first()) for j in self.jq_voltages]
61+
if self.jq_voltages is not None else None
62+
)
4363

44-
if config.jq_imported is None or config.jq_exported is None:
64+
if self.jq_imported is None or self.jq_exported is None:
4565
imported, exported = self.sim_counter.sim_count(power)
4666
else:
47-
imported = float(jq.compile(config.jq_imported).input(response).first())
48-
exported = float(jq.compile(config.jq_exported).input(response).first())
67+
imported = float(self.jq_imported.input(response).first())
68+
exported = float(self.jq_exported.input(response).first())
4969

5070
counter_state = CounterState(
5171
imported=imported,
5272
exported=exported,
5373
power=power,
5474
powers=powers,
55-
currents=currents
75+
currents=currents,
76+
power_factors=power_factors,
77+
voltages=voltages
5678
)
5779
self.store.set(counter_state)
5880

packages/modules/devices/generic/json/inverter.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,38 @@ def __init__(self, component_config: JsonInverterSetup, **kwargs: Any) -> None:
2020
self.component_config = component_config
2121
self.kwargs: KwargsDict = kwargs
2222

23+
def _compile_jq_filters(self) -> None:
24+
config = self.component_config.configuration
25+
self.jq_power = jq.compile(config.jq_power)
26+
self.jq_exported = jq.compile(config.jq_exported) if config.jq_exported else None
27+
self.jq_currents = [jq.compile(c) for c in config.jq_currents] if all(config.jq_currents) else None
28+
2329
def initialize(self) -> None:
2430
self.__device_id: int = self.kwargs['device_id']
2531
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv")
2632
self.store = get_inverter_value_store(self.component_config.id)
33+
self._compile_jq_filters()
2734
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
2835

2936
def update(self, response) -> None:
30-
config = self.component_config.configuration
31-
32-
power = float(jq.compile(config.jq_power).input(response).first())
37+
power = float(self.jq_power.input(response).first())
3338
if power >= 0:
3439
power = power * -1
35-
if config.jq_exported is None:
40+
41+
currents = (
42+
[float(j.input(response).first()) for j in self.jq_currents]
43+
if self.jq_currents is not None else None
44+
)
45+
46+
if self.jq_exported is None:
3647
_, exported = self.sim_counter.sim_count(power)
3748
else:
38-
exported = float(jq.compile(config.jq_exported).input(response).first())
49+
exported = float(self.jq_exported.input(response).first())
3950

4051
inverter_state = InverterState(
4152
power=power,
42-
exported=exported
53+
exported=exported,
54+
currents=currents
4355
)
4456
self.store.set(inverter_state)
4557

0 commit comments

Comments
 (0)