Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/snaptec/openWB
Browse files Browse the repository at this point in the history
  • Loading branch information
snaptec committed Dec 16, 2020
2 parents 66b9068 + bc18639 commit db1c872
Show file tree
Hide file tree
Showing 23 changed files with 653 additions and 373 deletions.
19 changes: 14 additions & 5 deletions modules/smarthome/shelly/watt.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@
import codecs
import binascii
import urllib.request

def totalPowerFromShellyJson(json):
if 'meters' in answer:
meters = answer['meters'] # shelly
else:
meters = answer['emeters'] # shellyEM & shelly3EM
total = 0
# shellyEM has one meter, shelly3EM has three meters:
for meter in meters:
total = total + meter['power']
return int(total)

named_tuple = time.localtime() # getstruct_time
time_string = time.strftime("%m/%d/%Y, %H:%M:%S shelly watty.py", named_tuple)
devicenumber=str(sys.argv[1])
ipadr=str(sys.argv[2])
uberschuss=int(sys.argv[3])
answer = json.loads(str(urllib.request.urlopen("http://"+str(ipadr)+"/status", timeout=3).read().decode("utf-8")))
try:
aktpower = int(answer['meters'][0]['power']) # Abfrage shelly
except:
aktpower = int(answer['emeters'][0]['power']) # Abfrage shellyEM
aktpower = totalPowerFromShellyJson(answer)
relais = int(answer['relays'][0]['ison'])
powerc = 0
answer = '{"power":' + str(aktpower) + ',"powerc":' + str(powerc) + ',"on":' + str(relais) + '} '
f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w')
json.dump(answer,f1)
f1.close()
f1.close()
62 changes: 51 additions & 11 deletions modules/soc_bluelink/main.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
#!/bin/bash
soctimer=$(</var/www/html/openWB/ramdisk/soctimer)

#soc_bluelink_email='[email protected]'
#soc_bluelink_password='xxx'
#soc_bluelink_pin='xxx'
#soc_bluelink_interval='5'
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
LOGFILE="$RAMDISKDIR/soc.log"
CHARGEPOINT=$1

tmpintervall=$(( soc_bluelink_interval * 6 ))
socDebug=$debug
# for developement only
socDebug=1

if (( soctimer < tmpintervall )); then
soctimer=$((soctimer+1))
echo $soctimer > /var/www/html/openWB/ramdisk/soctimer
case $CHARGEPOINT in
2)
# second charge point
soctimerfile="$RAMDISKDIR/soctimer1"
socfile="$RAMDISKDIR/soc1"
bluelink_email=$soc2user
bluelink_password=$soc2pass
bluelink_pin=$soc2pin
bluelink_intervall=20
;;
*)
# defaults to first charge point for backward compatibility
# set CHARGEPOINT in case it is empty (needed for logging)
CHARGEPOINT=1
soctimerfile="$RAMDISKDIR/soctimer"
socfile="$RAMDISKDIR/soc"
bluelink_email=$soc_bluelink_email
bluelink_password=$soc_bluelink_password
bluelink_pin=$soc_bluelink_pin
bluelink_intervall=$soc_bluelink_interval
;;
esac

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

soctimervalue=$(<$soctimerfile)

tmpintervall=$(( bluelink_intervall * 6 ))

socDebugLog "SoCtimer: $soctimervalue, SoCIntervall: $tmpintervall"

if (( soctimervalue < tmpintervall )); then
socDebugLog "Nothing to do yet. Incrementing timer."
soctimervalue=$((soctimervalue+1))
echo $soctimervalue > $soctimerfile
else
/var/www/html/openWB/modules/evcc-soc hyundai --user $soc_bluelink_email --password $soc_bluelink_password --pin $soc_bluelink_pin > /var/www/html/openWB/ramdisk/soc &
echo 0 > /var/www/html/openWB/ramdisk/soctimer
socDebugLog "Requesting SoC"
$MODULEDIR/../evcc-soc hyundai --user $soc_bluelink_email --password $soc_bluelink_password --pin $soc_bluelink_pin > $socfile &
echo 0 > $soctimerfile
fi
14 changes: 5 additions & 9 deletions modules/soc_bluelinklp2/main.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#!/bin/bash
soctimer=$(</var/www/html/openWB/ramdisk/soctimer1)

tmpintervall=120
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)

if (( soctimer < tmpintervall )); then
soctimer=$((soctimer+1))
echo $soctimer > /var/www/html/openWB/ramdisk/soctimer1
else
/var/www/html/openWB/modules/evcc-soc hyundai --user $soc2user --password $soc2pass --pin $soc2pin > /var/www/html/openWB/ramdisk/soc1 &
echo 0 > /var/www/html/openWB/ramdisk/soctimer1
fi
# for backward compatibility only
# functionality is in soc_bluelink
$OPENWBBASEDIR/modules/soc_bluelink/main.sh 2
exit 0
2 changes: 1 addition & 1 deletion modules/soc_kia/kiasoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def main():
cookies = session.cookies.get_dict()
#print(cookies)
else:
print('NOK cookie für login')
print('NOK cookie for login')
return

url = 'https://prd.eu-ccapi.kia.com:8080/api/v1/user/language'
Expand Down
28 changes: 22 additions & 6 deletions modules/soc_kia/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
LOGFILE="$RAMDISKDIR/soc.log"
CHARGEPOINT=$1

socDebug=$debug
# for developement only
socDebug=1

case $CHARGEPOINT in
2)
# second charge point
Expand All @@ -30,13 +35,24 @@ case $CHARGEPOINT in
;;
esac

soctimer=$(<$soctimerfile)
socDebugLog(){
if (( socDebug > 0 )); then
timestamp=`date --rfc-3339=seconds`
echo "$timestamp: Lp$CHARGEPOINT: $@" >> $LOGFILE
fi
}

soctimervalue=$(<$soctimerfile)
tmpintervall=$(( kia_intervall * 6 ))

if (( soctimer < tmpintervall )); then
soctimer=$((soctimer+1))
echo $soctimer > soctimerfile
socDebugLog "SoCtimer: $soctimervalue, SoCIntervall: $tmpintervall"

if (( soctimervalue < tmpintervall )); then
socDebugLog "Nothing to do yet. Incrementing timer."
soctimervalue=$((soctimervalue+1))
echo $soctimervalue > $soctimerfile
else
sudo python3 $MODULEDIR/kiasoc.py $kia_email $kia_password $kia_pin $kia_vin $socfile &
echo 0 > soctimerfile
socDebugLog "Requesting SoC"
sudo python3 $MODULEDIR/kiasoc.py $kia_email $kia_password $kia_pin $kia_vin $socfile >> $LOGFILE &
echo 0 > $soctimerfile
fi
55 changes: 26 additions & 29 deletions modules/soc_tesla/main.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/bin/bash

TOKENPASSWORD='#TokenInUse#'
response=''

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

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

case $CHARGEPOINT in
2)
Expand Down Expand Up @@ -45,23 +44,21 @@ esac

password="${!passwordConfigText}"

socTeslaLog(){
if (( $socTeslaDebug > 0 )); then
socDebugLog(){
if (( socDebug > 0 )); then
timestamp=`date --rfc-3339=seconds`
echo "$timestamp: Lp$CHARGEPOINT: $@" >> $LOGFILE
fi
# limit logfile to 500 lines
mv $LOGFILE $LOGFILE.old; tail -n 500 $LOGFILE.old > $LOGFILE; rm $LOGFILE.old
}

getAndWriteSoc(){
re='^-?[0-9]+$'
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"
socDebugLog "State: $state"
soclevel=$(echo $response | jq .response.charge_state.battery_level)
socTeslaLog "SoC: $soclevel"
socDebugLog "SoC: $soclevel"

if [[ $soclevel =~ $re ]] ; then
if (( $soclevel != 0 )) ; then
Expand All @@ -76,12 +73,12 @@ incrementTimer(){
}

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

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

Expand All @@ -91,60 +88,60 @@ checkToken(){
'')
# empty password tells us to remove a possible saved token
if [ -f $tokensfile ]; then
socTeslaLog "Empty password set: removing tokensfile."
socDebugLog "Empty password set: removing tokensfile."
rm $tokensfile
fi
socTeslaLog "Empty Password - nothing to do."
socDebugLog "Empty Password - nothing to do."
returnValue=1
;;
$TOKENPASSWORD)
# check if token is present
if [ ! -f $tokensfile ]; then
socTeslaLog "Tokenpassword set but no token found: clearing password in config."
socDebugLog "Tokenpassword set but no token found: clearing password in config."
clearPassword
socTeslaLog "Tokenpassword without token - nothing to do."
socDebugLog "Tokenpassword without token - nothing to do."
returnValue=2
fi
;;
*)
# new password entered
if [ -f $tokensfile ]; then
socTeslaLog "New password set: removing tokensfile."
socDebugLog "New password set: removing tokensfile."
rm $tokensfile
fi
# Request new token with user/pass.
socTeslaLog "Requesting new token..."
socDebugLog "Requesting new token..."
response=$(python $MODULEDIR/teslajson.py --email="$username" --password="$password" --tokens_file="$tokensfile" --json)
# password in response, so do not log it!
if [ -f $tokensfile ]; then
socTeslaLog "...all done, removing password from config file."
socDebugLog "...all done, removing password from config file."
setTokenPassword
else
socTeslaLog "ERROR: Auth with user/pass failed!"
socDebugLog "ERROR: Auth with user/pass failed!"
echo "Fehler: Anmeldung bei Tesla gescheitert!" > $RAMDISKDIR/lastregelungaktiv
returnValue=3
fi
;;
esac
socTeslaLog "CheckToken returnValue: $returnValue"
socDebugLog "CheckToken returnValue: $returnValue"
return "$returnValue"
}

wakeUpCar(){
socTeslaLog "Waking up car."
socDebugLog "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"
socDebugLog "Car state after wakeup: $state"
}

soctimer=$(<$soctimerfile)
if (( ladeleistung > 1000 )); then
# car is charging
# socDebugLog "Car is charging"
if (( soctimer < socintervallladen )); then
# waiting
# socDebugLog "Nothing to do yet. Incrementing timer."
incrementTimer
else
# reset timer
socDebugLog "Requesting SoC"
echo 0 > $soctimerfile
checkToken
checkResult=$?
Expand All @@ -154,12 +151,12 @@ if (( ladeleistung > 1000 )); then
fi
fi
else
# car is not charging
# socDebugLog "Car is not charging"
if (( soctimer < socintervall )); then
# waiting
# socDebugLog "Nothing to do yet. Incrementing timer."
incrementTimer
else
# reset timer
socDebugLog "Requesting SoC"
echo 0 > $soctimerfile
checkToken
checkResult=$?
Expand Down
2 changes: 0 additions & 2 deletions runs/atreboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ echo 0 > /var/www/html/openWB/ramdisk/device6_wh
echo 0 > /var/www/html/openWB/ramdisk/device7_wh
echo 0 > /var/www/html/openWB/ramdisk/device8_wh
echo 0 > /var/www/html/openWB/ramdisk/device9_wh
echo 0 > /var/www/html/openWB/ramdisk/device10_wh
echo 0 > /var/www/html/openWB/ramdisk/device1_temp0
echo 0 > /var/www/html/openWB/ramdisk/device1_temp1
echo 0 > /var/www/html/openWB/ramdisk/device1_temp2
Expand Down Expand Up @@ -209,7 +208,6 @@ echo 0 > /var/www/html/openWB/ramdisk/smarthome_device_manual_6
echo 0 > /var/www/html/openWB/ramdisk/smarthome_device_manual_7
echo 0 > /var/www/html/openWB/ramdisk/smarthome_device_manual_8
echo 0 > /var/www/html/openWB/ramdisk/smarthome_device_manual_9
echo 0 > /var/www/html/openWB/ramdisk/smarthome_device_manual_10
echo 0 > /var/www/html/openWB/ramdisk/devicetotal_watt
touch /var/www/html/openWB/ramdisk/wattbezug
echo 10 > /var/www/html/openWB/ramdisk/lp1sofortll
Expand Down
2 changes: 1 addition & 1 deletion runs/cron5min.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ d6=$(</var/www/html/openWB/ramdisk/device6_wh)
d7=$(</var/www/html/openWB/ramdisk/device7_wh)
d8=$(</var/www/html/openWB/ramdisk/device8_wh)
d9=$(</var/www/html/openWB/ramdisk/device9_wh)
d10=$(</var/www/html/openWB/ramdisk/device10_wh)
d10="0"



Expand Down
2 changes: 1 addition & 1 deletion runs/cronnightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ d6=$(</var/www/html/openWB/ramdisk/device6_wh)
d7=$(</var/www/html/openWB/ramdisk/device7_wh)
d8=$(</var/www/html/openWB/ramdisk/device8_wh)
d9=$(</var/www/html/openWB/ramdisk/device9_wh)
d10=$(</var/www/html/openWB/ramdisk/device10_wh)
d10="0"

ll1=$(echo "$ll1 * 1000" | bc)
ll2=$(echo "$ll2 * 1000" | bc)
Expand Down
Loading

0 comments on commit db1c872

Please sign in to comment.