#!/bin/sh
cd /www/Bosch
out=$(awk -f setspot.awk spot.out)
new=`echo $out | awk '{print $1}'`
new_hw=`echo $out | awk '{print $2}'`
num_spot=`echo $out | awk '{print $4}'`
if [ ! $new ] || [ $new -eq -1 ] || [ $num_spot -lt 10 ]
then
    ./getspot.sh
    out=$(awk -f setspot.awk spot.out)
    new=`echo $out | awk '{print $1}'`
    new_hw=`echo $out | awk '{print $2}'`
    echo $new, $new_hw
    if [ $new -eq -1 ]
    then
	# Normal indoor and EcoPlus, if no spot price
	new='210'
	new_hw='5' 
    fi
fi
echo $new, $new_hw

# Turn off heating, if outdoor is more than 16 C
t_out=` wget -q http://husdata/api/alldata -O -|jsonfilter -e '@["0007"]'`

if [ $t_out -gt 160 ]
then
   new='150'
fi
echo $new

old=` wget -q http://husdata/api/alldata -O -|jsonfilter -e '@["0203"]'`
if [ $new -ne $old  ] && [ $new -lt 250 ] && [ $new -gt 100 ]
then
    wget -q http://husdata/api/set?idx=0203\&val=$new -O -
fi

old_hw=` wget -q http://husdata/api/alldata -O -|jsonfilter -e '@["2213"]'`
if [ $new_hw -ne $old_hw  ] && [ $new_hw -lt 6 ] && [ $new_hw -gt 0 ]
then
    wget -q http://husdata/api/set?idx=2213\&val=$new_hw -O -
fi

echo $out|awk '{print $3}' > price.txt


