Skip to content

Commit

Permalink
Varta (#2591)
Browse files Browse the repository at this point in the history
* Varta

* remove varta from loadvars

* review fix
  • Loading branch information
LKuemmel authored May 30, 2023
1 parent 2ddbf11 commit 40b3a02
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 100 deletions.
4 changes: 2 additions & 2 deletions loadvars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ loadvars(){
fi
echo $hausverbrauch > /var/www/html/openWB/ramdisk/hausverbrauch
usesimbezug=0
if [[ $wattbezugmodul == "bezug_solarwatt" ]]|| [[ $wattbezugmodul == "bezug_rct" ]]|| [[ $wattbezugmodul == "bezug_varta" ]] || [[ $wattbezugmodul == "bezug_kostalplenticoreem300haus" ]] || [[ $wattbezugmodul == "bezug_solarlog" ]] ; then
if [[ $wattbezugmodul == "bezug_solarwatt" ]]|| [[ $wattbezugmodul == "bezug_rct" ]]|| [[ $wattbezugmodul == "bezug_kostalplenticoreem300haus" ]] || [[ $wattbezugmodul == "bezug_solarlog" ]] ; then
usesimbezug=1
fi
if ((usesimbezug == 1)); then
Expand Down Expand Up @@ -1182,7 +1182,7 @@ loadvars(){
echo "$pvallwh" > /var/www/html/openWB/ramdisk/pvallwh
fi

if [[ $speichermodul == "speicher_tesvoltsma" ]] || [[ $speichermodul == "speicher_solarwatt" ]] || [[ $speichermodul == "speicher_rct" ]]|| [[ $speichermodul == "speicher_kostalplenticore" ]] || [[ $speichermodul == "speicher_varta" ]] ; then
if [[ $speichermodul == "speicher_tesvoltsma" ]] || [[ $speichermodul == "speicher_solarwatt" ]] || [[ $speichermodul == "speicher_rct" ]]|| [[ $speichermodul == "speicher_kostalplenticore" ]] ; then
ra='^-?[0-9]+$'
watt2=$(</var/www/html/openWB/ramdisk/speicherleistung)
if [[ -e /var/www/html/openWB/ramdisk/speicherwatt0pos ]]; then
Expand Down
2 changes: 1 addition & 1 deletion modules/bezug_varta/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ else
MYLOGFILE="${RAMDISKDIR}/evu.log"
fi

bash "$OPENWBBASEDIR/packages/legacy_run.sh" "bezug_varta.varta" "${vartaspeicherip}" >>"${MYLOGFILE}" 2>&1
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.varta.device" "counter" "${vartaspeicherip}" >>"${MYLOGFILE}" 2>&1
ret=$?

openwbDebugLog ${DMOD} 2 "RET: ${ret}"
Expand Down
30 changes: 0 additions & 30 deletions modules/bezug_varta/varta.py

This file was deleted.

23 changes: 6 additions & 17 deletions modules/speicher_varta/main.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd)
RAMDISKDIR="${OPENWBBASEDIR}/ramdisk"
#DMOD="BAT"
#DMOD="BATT"
DMOD="MAIN"

if [ ${DMOD} == "MAIN" ]; then
Expand All @@ -13,21 +14,9 @@ fi
# Auslesen eines Varta Speicher über die integrierte XML-API der Batteroe.

if [[ "$usevartamodbus" != "1" ]]; then
speicherwatt=$(curl --connect-timeout 3 -s "$vartaspeicherip/cgi/ems_data.xml" | grep 'P' | sed 's/.*value=//' |tr -d "'/>")
# wenn WR aus bzw. im standby (keine Antwort) ersetze leeren Wert durch eine 0
ra='^-?[0-9]+$'
if [[ $speicherwatt =~ $ra ]] ; then
echo "$speicherwatt" > "$RAMDISKDIR/speicherleistung"
fi
speichersoc=$(curl --connect-timeout 3 -s "$vartaspeicherip/cgi/ems_data.xml" | grep 'SOC' | sed 's/.*value=//' |tr -d "'/>")
# if [[ $speichersoc -ge "101" ]]; then
speichersoc=$(echo "$speichersoc / 10" |bc)
# fi
if [[ $speichersoc =~ $ra ]] ; then
echo "$speichersoc" > "$RAMDISKDIR/speichersoc"
fi
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.varta.device" "bat_api" "${vartaspeicherip}">>"$MYLOGFILE" 2>&1
else
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "speicher_varta.varta" "${vartaspeicherip}" "${vartaspeicher2ip}" >>"$MYLOGFILE" 2>&1
ret=$?
openwbDebugLog ${DMOD} 2 "RET: ${ret}"
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.varta.device" "bat_modbus" "${vartaspeicherip}" "${vartaspeicher2ip}" >>"$MYLOGFILE" 2>&1
fi
ret=$?
openwbDebugLog ${DMOD} 2 "RET: ${ret}"
50 changes: 0 additions & 50 deletions modules/speicher_varta/varta.py

This file was deleted.

Empty file.
55 changes: 55 additions & 0 deletions packages/modules/devices/varta/bat_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
import xml.etree.ElementTree as ET

from dataclass_utils import dataclass_from_dict
from modules.common import req
from modules.common.component_state import BatState
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_bat_value_store
from modules.devices.varta.config import VartaBatApiSetup


class VartaBatApi:
def __init__(self, device_id: int, component_config: VartaBatApiSetup, device_address: str) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(VartaBatApiSetup, component_config)
self.__device_address = device_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.component_info = ComponentInfo.from_component_config(self.component_config)

def update(self) -> None:
def get_xml_text(attribute_value: str) -> float:
value = None
for element in root[0].iter("var"):
if element.attrib["name"] == attribute_value:
value = element.attrib["value"]
try:
return float(value)
except ValueError:
# Wenn Speicher aus bzw. im Standby (keine Antwort), ersetze leeren Wert durch eine 0.
return 0

response = req.get_http_session().get('http://'+self.__device_address+'/cgi/ems_data.xml',
timeout=5)
response.encoding = 'utf-8'
response = response.text.replace("\n", "")
root = ET.fromstring(response)

power = get_xml_text("P")
soc = get_xml_text("SOC") / 10

imported, exported = self.sim_counter.sim_count(power)

bat_state = BatState(
power=power,
soc=soc,
imported=imported,
exported=exported
)
self.store.set(bat_state)


component_descriptor = ComponentDescriptor(configuration_factory=VartaBatApiSetup)
36 changes: 36 additions & 0 deletions packages/modules/devices/varta/bat_modbus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
from dataclass_utils import dataclass_from_dict
from modules.common.component_state import BatState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_bat_value_store
from modules.devices.varta.config import VartaBatModbusSetup


class VartaBatModbus:
def __init__(self, device_id: int, component_config: VartaBatModbusSetup) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(VartaBatModbusSetup, component_config)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")
self.store = get_bat_value_store(self.component_config.id)
self.component_info = ComponentInfo.from_component_config(self.component_config)

def update(self, client: ModbusTcpClient_) -> None:
self.set_state(self.get_state(client))

def get_state(self, client: ModbusTcpClient_) -> BatState:
soc = client.read_holding_registers(1068, ModbusDataType.INT_16, unit=1)
power = client.read_holding_registers(1066, ModbusDataType.INT_16, unit=1)
return BatState(
power=power,
soc=soc,
)

def set_state(self, state: BatState) -> None:
state.imported, state.exported = self.sim_counter.sim_count(state.power)
self.store.set(state)


component_descriptor = ComponentDescriptor(configuration_factory=VartaBatModbusSetup)
70 changes: 70 additions & 0 deletions packages/modules/devices/varta/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Optional
from helpermodules.auto_str import auto_str
from modules.common.component_setup import ComponentSetup


@auto_str
class VartaConfiguration:
def __init__(self, ip_address: Optional[str] = None):
self.ip_address = ip_address


@auto_str
class Varta:
def __init__(self,
name: str = "Varta",
type: str = "varta",
id: int = 0,
configuration: VartaConfiguration = None) -> None:
self.name = name
self.type = type
self.id = id
self.configuration = configuration or VartaConfiguration()


@auto_str
class VartaBatApiConfiguration:
def __init__(self):
pass


@auto_str
class VartaBatApiSetup(ComponentSetup[VartaBatApiConfiguration]):
def __init__(self,
name: str = "Varta Speicher (Abfrage per API)",
type: str = "bat_api",
id: int = 0,
configuration: VartaBatApiConfiguration = None) -> None:
super().__init__(name, type, id, configuration or VartaBatApiConfiguration())


@auto_str
class VartaBatModbusConfiguration:
def __init__(self):
pass


@auto_str
class VartaBatModbusSetup(ComponentSetup[VartaBatModbusConfiguration]):
def __init__(self,
name: str = "Speicher Varta Pulse, Element, Neo, u.a. (Abfrage per Modbus)",
type: str = "bat_modbus",
id: int = 0,
configuration: VartaBatModbusConfiguration = None) -> None:
super().__init__(name, type, id, configuration or VartaBatModbusConfiguration())


@auto_str
class VartaCounterConfiguration:
def __init__(self):
pass


@auto_str
class VartaCounterSetup(ComponentSetup[VartaCounterConfiguration]):
def __init__(self,
name: str = "Varta Zähler",
type: str = "counter",
id: int = 0,
configuration: VartaCounterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or VartaCounterConfiguration())
34 changes: 34 additions & 0 deletions packages/modules/devices/varta/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
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.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_counter_value_store
from modules.devices.varta.config import VartaCounterSetup


class VartaCounter:
def __init__(self, device_id: int, component_config: VartaCounterSetup) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(VartaCounterSetup, component_config)
self.sim_counter = SimCounter(self.__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, client: ModbusTcpClient_):
power = client.read_holding_registers(1078, ModbusDataType.INT_16, unit=1) * -1
frequency = client.read_holding_registers(1082, ModbusDataType.UINT_16, unit=1) / 100
imported, exported = self.sim_counter.sim_count(power)

counter_state = CounterState(
imported=imported,
exported=exported,
power=power,
frequency=frequency
)
self.store.set(counter_state)


component_descriptor = ComponentDescriptor(configuration_factory=VartaCounterSetup)
Loading

0 comments on commit 40b3a02

Please sign in to comment.