forked from snaptec/openWB
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add solarlog * n * fixes * SolarLog * restructure legacy code * flake8
- Loading branch information
Showing
9 changed files
with
199 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Optional | ||
|
||
from modules.common.component_setup import ComponentSetup | ||
|
||
|
||
class SolarLogConfiguration: | ||
def __init__(self, ip_address: Optional[str] = None): | ||
self.ip_address = ip_address | ||
|
||
|
||
class SolarLog: | ||
def __init__(self, | ||
name: str = "Solar-Log", | ||
type: str = "solar_log", | ||
id: int = 0, | ||
configuration: SolarLogConfiguration = None) -> None: | ||
self.name = name | ||
self.type = type | ||
self.id = id | ||
self.configuration = configuration or SolarLogConfiguration() | ||
|
||
|
||
class SolarLogCounterConfiguration: | ||
def __init__(self): | ||
pass | ||
|
||
|
||
class SolarLogCounterSetup(ComponentSetup[SolarLogCounterConfiguration]): | ||
def __init__(self, | ||
name: str = "Solar-Log Zähler", | ||
type: str = "counter", | ||
id: int = 0, | ||
configuration: SolarLogCounterConfiguration = None) -> None: | ||
super().__init__(name, type, id, configuration or SolarLogCounterConfiguration()) | ||
|
||
|
||
class SolarLogInverterConfiguration: | ||
def __init__(self): | ||
pass | ||
|
||
|
||
class SolarLogInverterSetup(ComponentSetup[SolarLogInverterConfiguration]): | ||
def __init__(self, | ||
name: str = "Solar-Log Wechselrichter", | ||
type: str = "inverter", | ||
id: int = 0, | ||
configuration: SolarLogInverterConfiguration = None) -> None: | ||
super().__init__(name, type, id, configuration or SolarLogInverterConfiguration()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
import logging | ||
from typing import Dict, Union | ||
|
||
|
||
from dataclass_utils import dataclass_from_dict | ||
from modules.common.component_state import CounterState | ||
from modules.common.component_type import ComponentDescriptor | ||
from modules.common.fault_state import ComponentInfo | ||
from modules.common.simcount import SimCounter | ||
from modules.common.store import get_counter_value_store | ||
from modules.devices.solar_log.config import SolarLogCounterSetup | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class SolarLogCounter: | ||
def __init__(self, | ||
device_id: int, | ||
component_config: Union[Dict, SolarLogCounterSetup]) -> None: | ||
self.component_config = dataclass_from_dict(SolarLogCounterSetup, component_config) | ||
self.sim_counter = SimCounter(device_id, self.component_config.id, prefix="bezug") | ||
self.store = get_counter_value_store(self.component_config.id) | ||
self.component_info = ComponentInfo.from_component_config(self.component_config) | ||
|
||
def update(self, response: Dict) -> None: | ||
self.store_values(self.get_power(response)) | ||
|
||
def store_values(self, power) -> None: | ||
imported, exported = self.sim_counter.sim_count(power) | ||
|
||
self.store.set(CounterState( | ||
imported=imported, | ||
exported=exported, | ||
power=power | ||
)) | ||
|
||
def get_power(self, response: Dict) -> CounterState: | ||
return int(float(response["801"]["170"]["110"])) | ||
|
||
|
||
component_descriptor = ComponentDescriptor(configuration_factory=SolarLogCounterSetup) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env python3 | ||
import json | ||
import logging | ||
from typing import List, Optional, Union | ||
|
||
from helpermodules.cli import run_using_positional_cli_args | ||
from modules.common.abstract_device import DeviceDescriptor | ||
from modules.common.component_context import SingleComponentUpdateContext | ||
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater | ||
from modules.common import req | ||
from modules.devices.solar_log.counter import SolarLogCounter | ||
from modules.devices.solar_log.config import SolarLog, SolarLogCounterSetup, SolarLogInverterSetup | ||
from modules.devices.solar_log.inverter import SolarLogInverter | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
def create_device(device_config: SolarLog): | ||
def create_counter_component(component_config: SolarLogCounterSetup): | ||
return SolarLogCounter(device_config.id, component_config) | ||
|
||
def create_inverter_component(component_config: SolarLogInverterSetup): | ||
return SolarLogInverter(device_config.id, component_config) | ||
|
||
def update_components(components: Union[SolarLogCounter, SolarLogInverter]): | ||
response = req.get_http_session().post('http://'+device_config.ip_adress+'/getjp', | ||
data=json.dumps({"801": {"170": None}}), timeout=5).json() | ||
for component in components: | ||
component.update(response) | ||
|
||
return ConfigurableDevice( | ||
device_config=device_config, | ||
component_factory=ComponentFactoryByType( | ||
counter=create_counter_component, | ||
inverter=create_inverter_component, | ||
), | ||
component_updater=MultiComponentUpdater(update_components) | ||
) | ||
|
||
|
||
def read_legacy(component_type: str, ip_address: str, note_bat: Optional[int] = 0) -> None: | ||
log.debug('Solar-Log ip_address: ' + ip_address) | ||
if component_type == "inverter": | ||
inverter = SolarLogInverter(None, SolarLogInverterSetup(id=1)) | ||
with SingleComponentUpdateContext(inverter.component_info): | ||
response = req.get_http_session().post('http://'+ip_address+'/getjp', | ||
data=json.dumps({"801": {"170": None}}), timeout=5).json() | ||
inverter.update(response) | ||
elif component_type == "counter": | ||
inverter = SolarLogInverter(None, SolarLogInverterSetup(id=1)) | ||
counter = SolarLogCounter(None, SolarLogCounterSetup(id=None)) | ||
with SingleComponentUpdateContext(counter.component_info): | ||
# WR bei WR oder EVU-Modul immer auslesen | ||
response = req.get_http_session().post('http://'+ip_address+'/getjp', | ||
data=json.dumps({"801": {"170": None}}), timeout=5).json() | ||
inverter.update(response) | ||
power = counter.get_power(response) | ||
pvwatt = int(float(response["801"]["170"]["101"])) | ||
power = power - pvwatt | ||
|
||
if note_bat == 1: | ||
with open("ramdisk/speicherleistung", "r") as f: | ||
speicherleistung = int(float(f.read())) | ||
power = power + speicherleistung | ||
counter.store_values(power) | ||
|
||
|
||
def main(argv: List[str]): | ||
run_using_positional_cli_args(read_legacy, argv) | ||
|
||
|
||
device_descriptor = DeviceDescriptor(configuration_factory=SolarLog) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python3 | ||
import logging | ||
from typing import Dict, Union | ||
|
||
from dataclass_utils import dataclass_from_dict | ||
from modules.common.component_state import InverterState | ||
from modules.common.component_type import ComponentDescriptor | ||
from modules.common.fault_state import ComponentInfo | ||
from modules.common.store import get_inverter_value_store | ||
from modules.devices.solar_log.config import SolarLogInverterSetup | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class SolarLogInverter: | ||
def __init__(self, | ||
device_id: int, | ||
component_config: Union[Dict, SolarLogInverterSetup]) -> None: | ||
self.component_config = dataclass_from_dict(SolarLogInverterSetup, component_config) | ||
self.store = get_inverter_value_store(self.component_config.id) | ||
self.component_info = ComponentInfo.from_component_config(self.component_config) | ||
|
||
def update(self, response: Dict) -> None: | ||
self.store.set(self.get_values(response)) | ||
|
||
def get_values(self, response: Dict) -> InverterState: | ||
return InverterState( | ||
exported=float(response["801"]["170"]["109"]), | ||
power=-abs(float(response["801"]["170"]["101"])) | ||
) | ||
|
||
|
||
component_descriptor = ComponentDescriptor(configuration_factory=SolarLogInverterSetup) |