#!/bin/ash

echo >/tmp/api

echo $@ >>/tmp/api

RETURN=0;
if [ "$1" == "" ]
then
    echo "-2"
    exit 2
fi

#decode data

j=0
for i in "$@"
do
    case $j in
    0)
        thingsNetworkAPIKey="KEY?????"
        nodeid=$i
        wundergoundID="ID????"
        wundergoundPassword="pass???"
        # case i match LoRa End Node ID
        case $i in
            0)
                #ruche gauche
                thingsNetworkAPIKey="KEY?????"
                wundergoundID="ID????"
                wundergoundPassword="pass???"
                name="Ruche gauche"
                ;;
            1)
                #ruche droite
                thingsNetworkAPIKey="KEY?????"
                wundergoundID="ID????"
                wundergoundPassword="pass???"
                name="Ruche droite"
                ;;
            2)
                #Serre 1
                thingsNetworkAPIKey="KEY?????"
                wundergoundID="ID????"
                wundergoundPassword="pass???"
                name="Serre 1"
                ;;
            3)
                #Serre 2
                thingsNetworkAPIKey="KEY?????"
                wundergoundID="ID????"
                wundergoundPassword="pass???"
                name="Serre 2"
                ;;
            4)
                #Serre 3
                thingsNetworkAPIKey="KEY?????"
                wundergoundID="ID????"
                wundergoundPassword="pass???"
                name="Serre 3"
                ;;
            *)
                thingsNetworkAPIKey="KEY?????"
                ;;
        esac
        ;;
    1)
        if [ $i -gt 127 ]
        then
            # unsigned int to int
            let celsius=256-$i
            celsius=-$celsius
        else
            celsius=$i
        fi
        ;;
    2)
        celsius=$celsius.$(printf "%02d" $i)
        fahrenheit=`awk "BEGIN {print $celsius*9/5+32}"`
        ;;
    3)
        humidity=$i
        ;;
    4)
        humidity=$humidity.$(printf "%02d" $i)
        ;;
    5)
        case $nodeid in
        0)
            #ruche gauche - lux
            luxbit=`echo $i | awk '{r="";a=$1;while(a){r=a%2r;a=int(a/2)}printf"%08d\n",r,$1}'`
            ;;
        1)
            #ruche droite - barometer
            let barometer=$i+926 #hPa correction delta 6 hPa
            #waterInches=`awk "BEGIN {print $barometer*0.029529983071445}" | sed -e 's/ //g'`
            ;;
        *)
            ;;
        esac
        ;;
    6)
        case $nodeid in
        0)
            #ruche gauche - lux
            luxbit=0b$luxbit`echo $i | awk '{r="";a=$1;while(a){r=a%2r;a=int(a/2)}printf"%08d\n",r,$1}'`
            luxExponent=`python -c "print(($luxbit&0xF000)>>12)"`
            luxMantissa=`python -c "print($luxbit&0x0FFF)"`
            echo luxbit $luxbit
            lux=`awk "BEGIN {print $luxMantissa * (0.01 * 2^$luxExponent)}"`
            lumwm2=`awk "BEGIN {print $luxMantissa * (0.01 * 2^$luxExponent)*0.0079}"`
            ;;
        1)                                                                                     
            #ruche droite - barometer decimal
            barometer=$barometer.$(printf "%02d" $i)
            waterInches=`awk "BEGIN {print $barometer*0.029529983071445}" | sed -e 's/ //g'`   
            ;;
        *)
            ;;
        esac
        ;;
    7)
        case $nodeid in
        0)
            #ruche gauche - poids
            poids=$i
            ;;
        *)
            ;;
        esac
        ;;
    8)
        case $nodeid in
        0)
            #ruche gauche - poids
            poids=$poids.$(printf "%02d" $i)
            ;;
        *)
            ;;
        esac
        ;;
    esac
    let j=$j+1
done

echo nodeid $nodeid >>/tmp/api
echo celsius $celsius >>/tmp/api
echo humidity $humidity >>/tmp/api
echo barometer $barometer >>/tmp/api
echo Pressure water inches $waterInches >>/tmp/api
echo lux $lux >>/tmp/api
echo Lum W\/m2 $lumwm2 >>/tmp/api
echo Poids Kg $poids >>/tmp/api

if [ $nodeid -eq 0 ] || [ $nodeid -eq 1 ]
then
    if [ "$lux" == "" ]                                                                                     
    then                                                                                                    
            if [ -f /tmp/lux ]                                                                              
            then                                                                                            
                    lux=`cat /tmp/lux `                                                                     
                    lumwm2=`awk "BEGIN {print $lux*0.0079}"`                                       
                    rm -f /tmp/lux                                                                          
            fi                                                                                              
    else                                                                                                    
            echo $lux >/tmp/lux                                                                                                            
    fi
    if [ "$barometer" == "" ]                                                                                     
    then                                                                                                    
            if [ -f /tmp/barometer ]                                                                              
            then                                                                                            
                    barometer=`cat /tmp/barometer `                                                                     
                    waterInches=`awk "BEGIN {print $barometer*0.029529983071445}" | sed -e 's/ //g'`                                      
                    rm -f /tmp/barometer                                                                          
            fi                                                                                              
    else                                                                                                    
            echo $barometer >/tmp/barometer                                                                                                            
    fi
fi

#thinkspeak

url="https://api.thingspeak.com/update?api_key=$url$thingsNetworkAPIKey&field1=$celsius&field2=$humidity&field3=$barometer&field4=$lux&field5=$poids"

date >>/tmp/api
echo curl -k $url >>/tmp/api
curl -k $url | tee -a /tmp/api
if [ $? -ne 0 ]
then
    echo "-1"
    RETURN=1;
fi
echo 

#Wundergound

url="https://rtupdate.wunderground.com/weatherstation/updateweatherstation.php?ID=$wundergoundID&PASSWORD=$wundergoundPassword&dateutc=now&tempf=$fahrenheit&humidity=$humidity&baromin=$waterInches&solarradiation=$lumwm2&dewptf=32"

date >>/tmp/api
echo curl -k $url >>/tmp/api
curl -k $url >> /tmp/api
if [ $? -ne 0 ]
then
    echo "-1"
    RETURN=1;
fi


exit $RETURN
