Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 61 additions & 65 deletions modules/soc_eq/auth.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,79 @@
#!/usr/bin/python3

import requests, json, sys, time, os, html

#call parameters
ChargePoint = str(sys.argv[1])
code = str(sys.argv[2])

#Debug = int(os.environ.get('debug'))
Debug = 0

moddir = '/var/www/html/openWB/modules/soc_eq/'
import requests
import json
import sys
import time
import html
from pathlib import Path

# call parameters
charge_point = sys.argv[1][-1] # get last Character to identify the Chargepoint
code = sys.argv[2]

# debug = int(os.environ.get('debug'))
debug = 0
base_dir = str(Path(__file__).resolve().parents[2])
module_dir = str(Path(__file__).resolve().parents[0])
client_id = ""
client_secret = ""
callback = ""


def printDebug(message, level):
htmlmsg = html.escape(message)
if level <= Debug:
print("<p>" + htmlmsg + "</p>")
if level <= debug:
print("<p>" + html.escape(message) + "</p>")


def printHtml(message):
htmlmsg = html.escape(message)
print("<p>" + htmlmsg + "</p>")

print("<html>")

client_id = ""
client_secret = ""
callback = ""

#get last Character to identify the Chargepoint
ChargePoint = ChargePoint[-1]

#get SoC module config from openWB cofig
fd = open('/var/www/html/openWB/openwb.conf','r')
for line in fd:
try:
printDebug("owb Conf: " + line,2 )
(key, val) = line.rstrip().split("=")
if key == "debug":
Debug = int(val)
if key == "soc_eq_client_id_lp" + str(ChargePoint):
printDebug("Found Client ID: " + val ,1)
client_id = val
if key == "soc_eq_client_secret_lp" + str(ChargePoint):
printDebug("Found Client Secret: " + val ,1)
client_secret = val
if key == "soc_eq_cb_lp" + str(ChargePoint):
printDebug("Found callback URL: " + val ,1)
callback = val.replace("'","")
except:

val = ""

fd.close()

tok_url = "https://id.mercedes-benz.com/as/token.oauth2"
print("<p>" + html.escape(message) + "</p>")


print("<html>\n<head>\n<meta charset=\"utf-8\" />\n</head>\n<body>\n")

# get SoC module config from openwb.conf
with open(base_dir + '/openwb.conf', 'r') as fd:
for line in fd:
try:
printDebug("owb Conf: " + line, 2)
(key, val) = line.rstrip().split("=")
if key == "debug":
debug = int(val)
if key == "soc_eq_client_id_lp" + str(charge_point):
printDebug("Found Client ID: " + val, 1)
client_id = val
if key == "soc_eq_client_secret_lp" + str(charge_point):
printDebug("Found Client Secret: " + val, 1)
client_secret = val
if key == "soc_eq_cb_lp" + str(charge_point):
printDebug("Found callback URL: " + val, 1)
callback = val.replace("'", "")
except Exception:
val = ""

# tok_url = "https://id.mercedes-benz.com/as/token.oauth2"
tok_url = "https://ssoalpha.dvb.corpinter.net/v1/token"

data = {'grant_type': 'authorization_code', 'code': str(code), 'redirect_uri': callback}
#call API to get Access/Refresh tokens
# call API to get Access/Refresh tokens
act = requests.post(tok_url, data=data, verify=True, allow_redirects=False, auth=(client_id, client_secret))

printDebug(act.url,1)
printDebug(act.url, 1)

if act.status_code == 200:
#valid Response
toks = json.loads(act.text)
access_token = toks['access_token']
refresh_token = toks['refresh_token']
expires_in = int(time.time())

#write tokens to files
# valid Response
tokens = json.loads(act.text)
access_token = tokens['access_token']
refresh_token = tokens['refresh_token']
expires_in = tokens['expires_in'] - 60 + int(time.time())

fd = open(moddir + 'soc_eq_acc_lp' + str(ChargePoint),'w')
json.dump({'expires_in' : expires_in, 'refresh_token' : refresh_token, 'access_token' : access_token}, fd)
fd.close()
# write tokens to files
with open(module_dir + '/' + 'soc_eq_acc_lp' + str(charge_point), 'w') as fd:
json.dump({'expires_in': expires_in, 'refresh_token': refresh_token, 'access_token': access_token}, fd)

if act.status_code == 200:
printHtml( "Anmeldung erfolgreich!" )
print( "<a href=""javascript:window.close()"">Sie k&ouml;nnen das Fenster schlie&szlig;en.</a>" )
else:
printHtml("Anmeldung erfolgreich!")
print("<a href=""javascript:window.close()"">Sie können das Fenster schließen.</a>")
else:
printHtml("Anmeldung Fehlgeschlagen Code: " + str(act.status_code) + " " + act.text)
print("</html>")
print("</body>\n</html>")
7 changes: 6 additions & 1 deletion modules/soc_eq/callback_lp.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php
//Callback procedure for mercedes SoC API LP1
if( $_GET["code"] ) {
system( "/var/www/html/openWB/modules/soc_eq/auth.py " . $_GET['state'] . " " . $_GET['code']) ;
$state= escapeshellarg($_GET['state']);
$code= escapeshellarg($_GET['code']);
$command = escapeshellcmd("/var/www/html/openWB/modules/soc_eq/auth.py " );
//system( "/var/www/html/openWB/modules/soc_eq/auth.py " . $_GET['state'] . " " . $_GET['code']) ;
system( $command . $state . " " . $code);

}
else {
echo "<html>";
Expand Down
10 changes: 9 additions & 1 deletion modules/soc_eq/callback_lp1.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php
//Callback procedure for mercedes SoC API LP1
if( $_GET["code"] ) {
system( "/var/www/html/openWB/modules/soc_eq/auth.py 1 " . $_GET['code']) ;
$code= escapeshellarg($_GET['code']);
$command = escapeshellcmd("/var/www/html/openWB/modules/soc_eq/auth.py 1 " );
system( $command . $code);
}
else {
echo "<html>";
echo "<p>" . $_GET["error"] . "</p>";
echo "<p>" . $_GET["error_description"] . "</p>";
echo "</html>";
}
?>
10 changes: 9 additions & 1 deletion modules/soc_eq/callback_lp2.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php
//Callback procedure for mercedes SoC API LP2
if( $_GET["code"] ) {
system( "/var/www/html/openWB/modules/soc_eq/auth.py 2 " . $_GET['code']) ;
$code= escapeshellarg($_GET['code']);
$command = escapeshellcmd("/var/www/html/openWB/modules/soc_eq/auth.py 2 " );
system( $command . $code);
}
else {
echo "<html>";
echo "<p>" . $_GET["error"] . "</p>";
echo "<p>" . $_GET["error_description"] . "</p>";
echo "</html>";
}
?>
116 changes: 57 additions & 59 deletions modules/soc_eq/main.sh
Original file line number Diff line number Diff line change
@@ -1,93 +1,91 @@
#!/bin/bash

OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
MODULEDIR=$(cd "$(dirname "$0")" && pwd)
DMOD="EVSOC"
MYLOGFILE="$RAMDISKDIR/soc.log"
CHARGEPOINT=$1
myPid=$$

# check if config file is already in env
if [[ -z "$debug" ]]; then
echo "soc_eq: Seems like openwb.conf is not loaded. Reading file."
# try to load config
. $OPENWBBASEDIR/loadconfig.sh
. "$OPENWBBASEDIR/loadconfig.sh"
# load helperFunctions
. $OPENWBBASEDIR/helperFunctions.sh
. "$OPENWBBASEDIR/helperFunctions.sh"
fi

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


case $CHARGEPOINT in
2)
# second charge point
ladeleistungfile="$RAMDISKDIR/llaktuells1"
soctimerfile="$RAMDISKDIR/soctimer1"
soc_file="$RAMDISKDIR/soc1"
soc_eq_client_id=$soc_eq_client_id_lp2
soc_eq_client_secret=$soc_eq_client_secret_lp2
soc_eq_vin=$soc_eq_vin_lp2
;;
*)
# defaults to first charge point for backward compatibility
# set CHARGEPOINT in case it is empty (needed for logging)
CHARGEPOINT=1
ladeleistungfile="$RAMDISKDIR/llaktuell"
soctimerfile="$RAMDISKDIR/soctimer"
soc_file="$RAMDISKDIR/soc"
soc_eq_client_id=$soc_eq_client_id_lp1
soc_eq_client_secret=$soc_eq_client_secret_lp1
soc_eq_vin=$soc_eq_vin_lp1
;;
esac

incrementTimer() {
case $dspeed in
1)
# Regelgeschwindigkeit 10 Sekunden
ticksize=1
;;
2)
# second charge point
ladeleistungfile="$RAMDISKDIR/llaktuells1"
soctimerfile="$RAMDISKDIR/soctimer1"
soc_file="$RAMDISKDIR/soc1"
soc_eq_client_id=$soc_eq_client_id_lp2
soc_eq_client_secret=$soc_eq_client_secret_lp2
soc_eq_vin=$soc_eq_vin_lp2
# Regelgeschwindigkeit 20 Sekunden
ticksize=2
;;
3)
# Regelgeschwindigkeit 60 Sekunden
ticksize=1
;;
*)
# defaults to first charge point for backward compatibility
# set CHARGEPOINT in case it is empty (needed for logging)
CHARGEPOINT=1
ladeleistungfile="$RAMDISKDIR/llaktuell"
soctimerfile="$RAMDISKDIR/soctimer"
soc_file="$RAMDISKDIR/soc"
soc_eq_client_id=$soc_eq_client_id_lp1
soc_eq_client_secret=$soc_eq_client_secret_lp1
soc_eq_vin=$soc_eq_vin_lp1
# Regelgeschwindigkeit unbekannt
ticksize=1
;;
esac

incrementTimer(){
case $dspeed in
1)
# Regelgeschwindigkeit 10 Sekunden
ticksize=1
;;
2)
# Regelgeschwindigkeit 20 Sekunden
ticksize=2
;;
3)
# Regelgeschwindigkeit 60 Sekunden
ticksize=1
;;
*)
# Regelgeschwindigkeit unbekannt
ticksize=1
;;
esac
soctimer=$((soctimer+$ticksize))
echo $soctimer > $soctimerfile
soctimer=$((soctimer + ticksize))
echo $soctimer >"$soctimerfile"
}

soctimer=$(<$soctimerfile)
ladeleistung=$(<$ladeleistungfile)
soctimer=$(<"$soctimerfile")
ladeleistung=$(<"$ladeleistungfile")

if (( ladeleistung > 500 ));then
tmpintervall=$(( 5 * 6 ))
if ((ladeleistung > 500)); then
tmpintervall=$((5 * 6))
else
tmpintervall=$(( 60 * 6 ))
tmpintervall=$((60 * 6))
fi
if (( socDebug > 0 )); then
tmpintervall=6
if ((socDebug > 0)); then
tmpintervall=12
fi

if (( soctimer < tmpintervall )); then
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: Nothing to do yet. Incrementing timer. ${soctimer} < ${tmpintervall}"
if ((soctimer < tmpintervall)); then
openwbDebugLog "$DMOD" 1 "Lp$CHARGEPOINT: Nothing to do yet. Incrementing timer. $soctimer < $tmpintervall"
incrementTimer
else
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: Requesting SoC"
echo 0 > $soctimerfile
$MODULEDIR/soc.py $soc_eq_client_id $soc_eq_client_secret $soc_eq_vin $soc_file $CHARGEPOINT >>$MYLOGFILE 2>&1
openwbDebugLog "$DMOD" 1 "Lp$CHARGEPOINT: Requesting SoC"
echo 0 >"$soctimerfile"
"$MODULEDIR/soc.py" "$soc_eq_client_id" "$soc_eq_client_secret" "$soc_eq_vin" "$soc_file" "$CHARGEPOINT" >>"$MYLOGFILE" 2>&1
ret=$?
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: Py Return: ${ret}"
openwbModulePublishState "EVSOC" "${ret}" "Code: ${ret}" "$CHARGEPOINT"
openwbDebugLog "$DMOD" 1 "Lp$CHARGEPOINT: Py Return: $ret"
openwbModulePublishState "EVSOC" "$ret" "Code: $ret" "$CHARGEPOINT"
fi
Loading