diff --git a/loadvars.sh b/loadvars.sh index 4187863ad..4957233e8 100755 --- a/loadvars.sh +++ b/loadvars.sh @@ -30,6 +30,31 @@ run_soc_module() { fi } +run_inverter_module() { + module_dir="modules/$1" + if [ -f "$module_dir/main.sh" ] + then + openwbDebugLog "MAIN" 2 "Calling inverter module <$module_dir/main.sh>" + "$module_dir/main.sh" "$2" + elif [[ "$1" =~ ^wr([0-9]+)_(.*)$ ]] + then + # Historically if each Inverter-Module applied to a single inverter position, only. Thus if multiple inverters + # were to be supported the module was copied and got the prefix "wr2_" instead of "wr_" for the second inverter. + # With the new structure we call the script for inverter one with the actual inverter number as parameter. + module_dir_inverter1="modules/wr_${BASH_REMATCH[2]}" + inverter_num="${BASH_REMATCH[1]}" + if [ -d "$module_dir_inverter1" ] + then + openwbDebugLog "MAIN" 2 "Calling inverter module <$module_dir_inverter1/main.sh> for inverter $inverter_num" + "$module_dir_inverter1/main.sh" "$inverter_num" "$2" + else + openwbDebugLog "MAIN" 0 "Neither <$module_dir> nor <$module_dir_inverter1> exist!" + fi + else + openwbDebugLog "MAIN" 0 "inverter module <$module_dir> does not exist" + fi +} + loadvars(){ #reload mqtt vars renewmqtt=$( /var/www/html/openWB/ramdisk/pv1vorhanden - pvwatt=$("modules/$pvwattmodul/main.sh" || true) - if ! [[ $pvwatt =~ $re ]] ; then - openwbDebugLog "MAIN" 0 "ungültiger Wert für pvwatt: $pvwatt" - pvwatt="0" - fi - pv1watt=$pvwatt - echo "$pv1watt" > ramdisk/pv1watt - else - pv1vorhanden="0" - echo 0 > /var/www/html/openWB/ramdisk/pv1vorhanden - pvwatt=$( /var/www/html/openWB/ramdisk/pv2vorhanden - pv2watt=$("modules/$pv2wattmodul/main.sh" || true) - if ! [[ $pv2watt =~ $re ]] ; then - openwbDebugLog "MAIN" 0 "ungültiger Wert für pv2watt: $pv2watt" - pv2watt="0" - fi - echo $pv2watt > ramdisk/pv2watt - pvwatt=$((pvwatt + pv2watt)) - if ! [[ $pvwatt =~ $re ]] ; then - openwbDebugLog "MAIN" 0 "ungültiger Wert für PV Gesamtleistung: $pvwatt" - pvwatt="0" + pvwatt=0 + pvallwh=0 + for pv_num in 1 2 + do + ((pv_num == 1)) && pv_num_str="" || pv_num_str="$pv_num" + declare -n "pv_module=pv${pv_num_str}wattmodul" + declare -n pv_exists="pv${pv_num}vorhanden" + declare -n pv_n_watt="pv${pv_num}watt" + declare -n pv_n_energy="pv${pv_num_str}kwh" + if [[ "$pv_module" != "none" ]] + then + pv_exists=1 + pv_n_watt=$(run_inverter_module "$pv_module" "$pv_num") + if ! [[ "$pvwatt" =~ $re ]] + then + openwbDebugLog "MAIN" 0 "PV Module $pv_module reports illegal value for power: $pvwatt. Assuming 0W" + pv_n_watt=0 + fi + pv_n_energy=$(<"/var/www/html/openWB/ramdisk/pv${pv_num_str}kwh") + # Use `bc` for float-support + pvwatt=$(echo "$pvwatt+$pv_n_watt" | bc) + pvallwh=$(echo "$pvallwh+$pv_n_energy" | bc) + else + pv_exists=0 + pv_n_watt=0 + pv_n_energy=0 fi - echo "$pvwatt" > /var/www/html/openWB/ramdisk/pvallwatt - pvkwh=$( /var/www/html/openWB/ramdisk/pvallwh - else - pvkwh=$( /var/www/html/openWB/ramdisk/pv2vorhanden - echo "$pvkwh" > /var/www/html/openWB/ramdisk/pvallwh - echo "$pvwatt" > /var/www/html/openWB/ramdisk/pvallwatt - fi + echo "$pv_exists" > "/var/www/html/openWB/ramdisk/pv${pv_num}vorhanden" + echo "$pv_n_watt" > "/var/www/html/openWB/ramdisk/pv${pv_num}watt" + ((pv_num++)) + done + echo "$pvwatt" > /var/www/html/openWB/ramdisk/pvallwatt + echo "$pvallwh" > /var/www/html/openWB/ramdisk/pvallwh + pvallwatt="$pvwatt" + + openwbDebugLog "MAIN" 2 "pv1vorhanden=$pv1vorhanden pv2vorhanden=$pv2vorhanden pv1watt=$pv1watt pv2watt=$pv2watt pvwatt=$pvwatt pvallwatt=$pvallwatt pvkwh=$pvkwh pv2kwh=$pv2kwh pvallwh=$pvallwh" + #Speicher werte if [[ $speichermodul != "none" ]] ; then diff --git a/modules/wr2_json/main.sh b/modules/wr2_json/main.sh deleted file mode 100755 index 24cc210e5..000000000 --- a/modules/wr2_json/main.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd) -RAMDISKDIR="${OPENWBBASEDIR}/ramdisk" -DMOD="PV" -#DMOD="MAIN" -Debug=$debug - -#For Development only -#Debug=1 - -if [ ${DMOD} == "MAIN" ]; then - MYLOGFILE="${RAMDISKDIR}/openWB.log" -else - MYLOGFILE="${RAMDISKDIR}/nurpv.log" -fi - -openwbDebugLog ${DMOD} 2 "PV URL : ${wr2jsonurl}" -openwbDebugLog ${DMOD} 2 "PV Watt: ${wr2jsonwatt}" -openwbDebugLog ${DMOD} 2 "PV kWh : ${wr2jsonkwh}" - -bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.json.device" "inverter" "${wr2jsonurl}" "${wr2jsonwatt}" "${wr2jsonkwh}" "2" >>"$MYLOGFILE" 2>&1 -ret=$? - -openwbDebugLog ${DMOD} 2 "RET: ${ret}" - -cat "${RAMDISKDIR}/pv2watt" diff --git a/modules/wr_json/main.sh b/modules/wr_json/main.sh index bae0456ff..03153e312 100755 --- a/modules/wr_json/main.sh +++ b/modules/wr_json/main.sh @@ -15,11 +15,17 @@ else MYLOGFILE="${RAMDISKDIR}/nurpv.log" fi -openwbDebugLog ${DMOD} 2 "PV URL : ${wrjsonurl}" -openwbDebugLog ${DMOD} 2 "PV Watt: ${wrjsonwatt}" -openwbDebugLog ${DMOD} 2 "PV kWh : ${wrjsonkwh}" +inverter_num=${1:-1} +[[ "$inverter_num" -gt 1 ]] && config_prefix="wr$1" || config_prefix="wr" +declare -n "config_url=${config_prefix}jsonurl" +declare -n "config_watt=${config_prefix}jsonwatt" +declare -n "config_kwh=${config_prefix}jsonkwh" -bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.json.device" "inverter" "${wrjsonurl}" "${wrjsonwatt}" "${wrjsonkwh}" "1" >>"$MYLOGFILE" 2>&1 +openwbDebugLog ${DMOD} 2 "PV URL : ${config_url}" +openwbDebugLog ${DMOD} 2 "PV Watt: ${config_watt}" +openwbDebugLog ${DMOD} 2 "PV kWh : ${config_kwh}" + +bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.json.device" "inverter" "$config_url" "$config_watt" "$config_kwh" "$inverter_num" >>"$MYLOGFILE" 2>&1 ret=$? openwbDebugLog ${DMOD} 2 "RET: ${ret}"