Skip to content

Commit

Permalink
auth based on tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
benderl committed Jul 13, 2020
1 parent edd0145 commit ad94b16
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ ramdisk/
web/settings/.htaccess
web/settings/.passwd
web/lastcommit
modules/soc_tesla/tokens.*
125 changes: 101 additions & 24 deletions modules/soc_tesla/main.sh
Original file line number Diff line number Diff line change
@@ -1,68 +1,145 @@
#!/bin/bash

# echo $#
TOKENPASSWORD='#TokenInUse#'
response=''

OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
# echo $OPENWBBASEDIR
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
CONFIGFILE="$OPENWBBASEDIR/openwb.conf"
LOGFILE="$RAMDISKDIR/soc-tesla.log"

socTeslaDebug=$debug
# for developement only
#socTeslaDebug=1

case $1 in
2)
# second charge point
tintervallladen=$(( soc_teslalp2_intervallladen * 6 ))
tintervall=$(( soc_teslalp2_intervall * 6 ))
teslatimer=$(<$OPENWBBASEDIR/ramdisk/soctimer1)
ladeleistung=$(<$OPENWBBASEDIR/ramdisk/llaktuells1)
soctimerfile="$OPENWBBASEDIR/ramdisk/soctimer1"
socfile="$OPENWBBASEDIR/ramdisk/soc1"
socintervallladen=$(( soc_teslalp2_intervallladen * 6 ))
socintervall=$(( soc_teslalp2_intervall * 6 ))
ladeleistung=$(<$RAMDISKDIR/llaktuells1)
soctimerfile="$RAMDISKDIR/soctimer1"
socfile="$RAMDISKDIR/soc1"
username=$soc_teslalp2_username
password=$soc_teslalp2_password
carnumber=$soc_teslalp2_carnumber
tokensfile="$MODULEDIR/tokens.lp2"
;;
*)
# defaults to first charge point for backward compatibility
tintervallladen=$(( soc_tesla_intervallladen * 6 ))
tintervall=$(( soc_tesla_intervall * 6 ))
teslatimer=$(<$OPENWBBASEDIR/ramdisk/soctimer)
ladeleistung=$(<$OPENWBBASEDIR/ramdisk/llaktuell)
soctimerfile="$OPENWBBASEDIR/ramdisk/soctimer"
socfile="$OPENWBBASEDIR/ramdisk/soc"
socintervallladen=$(( soc_tesla_intervallladen * 6 ))
socintervall=$(( soc_tesla_intervall * 6 ))
ladeleistung=$(<$RAMDISKDIR/llaktuell)
soctimerfile="$RAMDISKDIR/soctimer"
socfile="$RAMDISKDIR/soc"
username=$soc_tesla_username
password=$soc_tesla_password
carnumber=$soc_tesla_carnumber
tokensfile="$MODULEDIR/tokens.lp1"
;;
esac

socTeslaLog(){
if (( $socTeslaDebug > 0 )); then
timestamp=`date --rfc-3339=seconds`
echo "$timestamp: $@" >> $LOGFILE
fi
}

getAndWriteSoc(){
re='^-?[0-9]+$'
soclevel=$(sudo python $OPENWBBASEDIR/modules/soc_tesla/tsoc.py $username $password $carnumber | jq .battery_level)
response=$(python $MODULEDIR/teslajson.py --email $username --tokens_file $tokensfile --vid $carnumber --json get data)
# current state of car
state=$(echo $response | jq .response.state)
socTeslaLog "State: $state"
soclevel=$(echo $response | jq .response.charge_state.battery_level)
socTeslaLog "SoC: $soclevel"

if [[ $soclevel =~ $re ]] ; then
if (( $soclevel != 0 )) ; then
echo $soclevel > $socfile
# echo "$soclevel > $socfile"
fi
fi
echo 0 > $soctimerfile
# echo "0 > $soctimerfile"
}

incrementTimer(){
teslatimer=$((teslatimer+1))
echo $teslatimer > $soctimerfile
# echo "$teslatimer > $soctimerfile"
soctimer=$((soctimer+1))
echo $soctimer > $soctimerfile
}

clearPassword(){
socTeslaLog "Removing password from config."
sed -i "s/soc_tesla_password=.*/soc_tesla_password=''/" $CONFIGFILE
}

setTokenPassword(){
socTeslaLog "Writing token password to config."
sed -i "s/soc_tesla_password=.*/soc_tesla_password='$TOKENPASSWORD'/" $CONFIGFILE
}

checkToken(){
case $password in
'')
# empty password tells us to remove a possible saved token
if [ -f $tokensfile ]; then
socTeslaLog "Empty password set: removing tokensfile."
rm $tokensfile
fi
;;
$TOKENPASSWORD)
# check if token is present
if [ ! -f $tokensfile ]; then
socTeslaLog "Tokenpassword set but no token found: clearing password in config."
clearPassword
fi
;;
*)
# new password entered
if [ -f $tokensfile ]; then
socTeslaLog "New password set: removing tokensfile."
rm $tokensfile
fi
# Request new token with user/pass.
socTeslaLog "Requesting new token..."
response=$(python $MODULEDIR/teslajson.py --email $username --password $password --tokens_file $tokensfile --json)
if [ -f $tokensfile ]; then
socTeslaLog "...all done, removing password from config file."
setTokenPassword
else
socTeslaLog "ERROR: Auth with user/pass failed!"
fi
;;
esac
}

wakeUpCar(){
socTeslaLog "Waking up car."
response=$(python $MODULEDIR/teslajson.py --email $username --tokens_file $tokensfile --vid $carnumber --json do wake_up)
state=$(echo $response | jq .response.state)
socTeslaLog "Car state after wakeup: $state"
}

checkToken
soctimer=$(<$soctimerfile)
if (( ladeleistung > 1000 )); then
if (( teslatimer < tintervallladen )); then
# echo "$teslatimer < $tintervallladen"
# car is charging
if (( soctimer < socintervallladen )); then
# waiting
incrementTimer
else
# car cannot be asleep while charging
getAndWriteSoc
fi
else
if (( teslatimer < tintervall )); then
# echo "$teslatimer < $tintervall"
# car is not charging
if (( soctimer < socintervall )); then
# waiting
incrementTimer
else
# todo: do not always wake car
wakeUpCar
getAndWriteSoc
fi
fi
Loading

0 comments on commit ad94b16

Please sign in to comment.