#!/bin/bash 
# Autor: Helmut Esau 
# Version 0.1 // Zipato URL als Variable
# Diese kleine Script liefertdie Werte der Netatmo aus, und schickt sie zur Zipabox 



# Hier werden die Werte von der NetAtmo API geholt
# (C) 2015 Michael Miklis (michaelmiklis.de)

listDevices() {
    # ------------------------------------------------------
    # Help
    # ------------------------------------------------------
    # usage: listdevices <USER> <PASSWORD>
    #
    # USER + PASSWORD -> your NetAtmo Website login
 
    # ------------------------------------------------------
    # Parsing Arguments
    # ------------------------------------------------------
    USER=$1
    PASS=$2
 
 
    # ------------------------------------------------------
    # Define some constants
    # ------------------------------------------------------
    URL_LOGIN="https://auth.netatmo.com/de-DE/access/login"
    API_GETMEASURECSV="https://my.netatmo.com/api/devicelist"
    SESSION_COOKIE="cookie_sess.txt"
    AUTH_COOKIE="cookie_auth.txt"
 
 
    # ------------------------------------------------------
    # URL encode the user entered parameters
    # ------------------------------------------------------
    USER="$(urlencode $USER)"
    PASS="$(urlencode $PASS)"
 
 
    # ------------------------------------------------------
    # Now let's fetch the data
    # ------------------------------------------------------
 
    # first we need to get a valid session cookie
    curl --silent -c $AUTH_COOKIE $URL_LOGIN  > /dev/null
 
    # then extract the ID from the authentication cookie
    SESS_ID="$(cat $AUTH_COOKIE | grep netatmocomci_csrf_cookie_na | cut -f7)"
 
    # and now we can login using cookie, id, user and password
    curl --silent -d "ci_csrf_netatmo=$SESS_ID&mail=$USER&pass=$PASS&log_submit=LOGIN" -b $AUTH_COOKIE -c $SESSION_COOKIE  $URL_LOGIN > /dev/null
 
    # next we extract the access_token from the session cookie
    ACCESS_TOKEN="$(cat $SESSION_COOKIE | grep netatmocomaccess_token | cut -f7)"
 
    # build the POST data
    PARAM="access_token=$ACCESS_TOKEN"
 
    # now download json data
    curl -d $PARAM $API_GETMEASURECSV
 
    # clean up
    rm $SESSION_COOKIE
    rm $AUTH_COOKIE
}
 
#____________________________________________________________________________________________________________________________________
 
urlencode() {
    # ------------------------------------------------------
    # urlencode function from mrubin
    # https://gist.github.com/mrubin
    #
    # usage: urlencode <string>
    # ------------------------------------------------------
    local length="${#1}"
 
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
 
        case $c in [a-zA-Z0-9.~_-])
            printf "$c" ;;
            *) printf '%%%02X' "'$c"
            esac
    done
}
 
#____________________________________________________________________________________________________________________________________
 
# In der folgenden Zeile "EMail Adresse" und "Password" mit den Login Daten fü # die Netatmo Seite ersetzten
net=$(listDevices "EMail Adresse" "Password" ); echo $net | /usr/bin/json_pp > /tmp/netatmo-werte

echo $net | /usr/bin/json_pp > /tmp/netatmo-werte


# Variablen Zipato URL

tempinnen=''
tempaussen=''
humidityaussen=''
humidityinnen=''
co2=''
presure=''



# Hier werden die Werte aufgesplitet

tempinnen1=`cat /tmp/netatmo-werte |grep '"Temperature" :' | awk '{print $3}' | sed 's/\,//g' | sed '1d'`
tempaussen1=`cat /tmp/netatmo-werte |grep '"Temperature" :' | awk '{print $3}' | sed 's/\,//g' | sed '2d'`
humidityaussen1=`cat /tmp/netatmo-werte |grep '"Humidity" :' | awk '{print $3}' | sed 's/\,//g' | sed '2d'`
humidityinnen1=`cat /tmp/netatmo-werte |grep '"Humidity" :' | awk '{print $3}' | sed 's/\,//g' | sed '1d'`
co21=`cat /tmp/netatmo-werte |grep '"CO2" :' | awk '{print $3}' | sed 's/\,//g'`
presure1=`cat /tmp/netatmo-werte |grep '"Pressure" :' | awk '{print $3}' | sed 's/\,//g'`
db1=`cat /tmp/netatmo-werte |grep '"Noise" :' | awk '{print $3}' | sed 's/\,//g'`



# Hier schicken wir die Daten an die Zipabox

curl $tempinnen$tempinnen1
curl $tempaussen$tempaussen1
curl $humidityaussen$humidityaussen1
curl $humidityinnen$humidityinnen1
curl $co2$co21
curl $presure$presure1
curl $db$db1

