#!/bin/bash
#
# RainerW 8th of April 2020
# Unfortunately Kostal has introduced the third version of interface.
# This time it is XML
# This is for Kostal_Piko_MP_plus and StecaGrid coolcept (single phase inverter)
# In fact Kostal is not developing own single phase inverter anymore but is souring them from Steca
# If you have the chance to test this module for three phase inverter from Kostal (Plenticore) or Steca (coocept3 or coolcept XL) let us know if it works

. /var/www/html/openWB/openwb.conf
# Kostal Piko MP is using XML

# call for XML file and parse it with xmlstarlet for current PV power
power_kostal_piko_MP=$(curl --connect-timeout 5 -s $wrkostalpikoip/measurements.xml | xmlstarlet sel -t -v '/Measurements/Measurement[@Type="AC_Power"]/@Value')

# cut the comma and the digit behind the comma
power_kostal_piko_MP=$(echo $power_kostal_piko_MP | sed 's/\..*$//')

# allow only numbers
	re='^-?[0-9]+$'
	if ! [[ $power_kostal_piko_MP =~ $re ]] ; then
	   power_kostal_piko_MP="0"
	fi

pcs_pv_total_power=$kostal_piko_MP

# call for XM file and parse it for total produced kw
pvkwh_kostal_piko_MP=$(curl --connect-timeout 5 -s $wrkostalpikoip/yields.xml | xmlstarlet sel -t -v '/root/Device/Yields/Yield/YieldValue/@Value')
pvkwh=$pvkwh_kostal_piko_MP


#
## Daten in Ramdisk schreiben
#
echo $pvkwh > /var/www/html/openWB/ramdisk/pvkwh
echo '-'$pcs_pv_total_power > /var/www/html/openWB/ramdisk/pvwatt
echo '-'$pcs_pv_total_power
