Skip to content

Commit

Permalink
fix coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
benderl committed Mar 24, 2022
1 parent ceb3d46 commit 075d03c
Show file tree
Hide file tree
Showing 18 changed files with 569 additions and 437 deletions.
97 changes: 53 additions & 44 deletions modules/et_awattarcap/awattarcapgetprices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@

# --- Imports ---

from datetime import datetime, timedelta, time
from datetime import datetime, time
import json
import requests
import os
import sys

import factors

# --- Globale Variablen ---

# --- Globale Variablen ---
debug = 0
priceFileToday = "/var/www/html/openWB/ramdisk/et_price_d0"
priceFileTomorrow = "/var/www/html/openWB/ramdisk/et_price_d1"
curveFile = "/var/www/html/openWB/ramdisk/etprovidergraphlist"
currentFile = "/var/www/html/openWB/ramdisk/etproviderprice"
priceUrl = "https://api.awattar.de/v1/marketdata?start="

# --- Helper-Funktionen ---

# --- Helper-Funktionen ---
def logDebug(msgLevel, msgText):
try:
if debug >= msgLevel:
Expand All @@ -38,20 +37,22 @@ def logDebug(msgLevel, msgText):
f.write(line)
except:
pass

return



def getWeekday(time):
# 0=Montag,...,6=Sonntag
dow = datetime.fromtimestamp(time).weekday()

return dow



def getSeason(time):
# 0=Sommer, 1=Winter, 2=Übergang
month = datetime.fromtimestamp(time).month
day = datetime.fromtimestamp(time).month

if ((month == 5) and (day >= 15)) or (month == 6) or (month == 7) or (month == 8) or ((month == 9) and (day < 15)):
season = 0
elif (month == 11) or (month == 12) or (month == 1) or (month == 2) or ((month == 3) and (day < 21)):
Expand All @@ -62,144 +63,152 @@ def getSeason(time):
season = 2
else:
raise ValueError

return season

# --- Funktionen ---


# --- Funktionen ---
def checkPrices(fileName, time):
try:
data = loadPrices(fileName)
fileTime = data['data'][0]['start_timestamp']
except:
return False

if fileTime == (time * 1000):
return True

return False


def downloadPrices(time):
try:
url = priceUrl + str(time) + "000"
jsond = requests.get(url, allow_redirects = True)
jsond = requests.get(url, allow_redirects=True)
data = json.loads(jsond.text)
except:
return 0

return data


def loadPrices(fileName):
try:
with open(fileName, 'r') as f:
data = json.loads(f.read())
except:
return 0

return data



def savePrices(fileName, data):
try:
with open(fileName, 'w') as f:
f.write(json.dumps(data))
except:
return

return

def calculateCurve(data, time):


def calculateCurve(data, time):
try:
season = getSeason(time)
weekday = getWeekday(time)
avgprice = 0
pricecurve = {}

for hour in range(24):
factor = factors.getFactor(season, weekday, hour)
hourprice = float(data['data'][hour]['marketprice'])
avgprice = avgprice + (factor * hourprice)

for hour in range(24):
hourprice = data['data'][hour]['marketprice']
pricecurve[hour] = round(((hourprice - avgprice) * 1.19) / 10, 2)
pricecurve[hour] = round(((hourprice - avgprice) * 1.19) / 10, 2)
except:
raise

return pricecurve


def savePriceCurve(curve1, curve2, time1, time2):
try:
with open(curveFile, 'w') as f:
try:
with open(curveFile, 'w') as f:
f.write("awattarcap_de\n")
for hour in range(datetime.now().hour, 24):
f.write(str(time1 + (hour*60*60)) + "," + str(curve1[hour]) + "\n")
f.write(str(time1 + (hour*60*60)) + "," + str(curve1[hour]) + "\n")
for hour in range(24):
f.write(str(time2 + (hour*60*60)) + "," + str(curve2[hour]) + "\n")
f.write(str(time2 + (hour*60*60)) + "," + str(curve2[hour]) + "\n")
except:
raise

return



def saveCurrentPrice(curve1, time1):
try:
with open(currentFile, 'w') as f:
f.write(str(curve1[datetime.now().hour]))
with open(currentFile, 'w') as f:
f.write(str(curve1[datetime.now().hour]))
except:
raise

return



def publishPrices():
os.system('mosquitto_pub -r -t openWB/global/awattar/pricelist -m "$(cat ' + curveFile + ')"')
os.system('mosquitto_pub -r -t openWB/global/awattar/ActualPriceForCharging -m "$(cat ' + currentFile + ')"')
return

# --- Hauptprogramm --


# --- Hauptprogramm --
def main():
try:
debug = int(sys.argv[1])
timeToday = int(datetime.combine(datetime.today(), time.min).timestamp())
timeTomorrow = int(timeToday + (24*60*60))
except:
raise

try:
if checkPrices(priceFileToday, timeToday):
dataToday = loadPrices(priceFileToday)
else:
dataToday = downloadPrices(timeToday)
savePrices(priceFileToday, dataToday)

curveToday = calculateCurve(dataToday, timeToday)
except:
curveToday = {}
for hour in range(24):
curveToday[hour] = 0
pass

try:
if checkPrices(priceFileTomorrow, timeTomorrow):
dataTomorrow = loadPrices(priceFileTomorrow)
else:
dataTomorrow = downloadPrices(timeTomorrow)
savePrices(priceFileTomorrow, dataTomorrow)

curveTomorrow = calculateCurve(dataTomorrow, timeTomorrow)
except:
curveTomorrow = {}
for hour in range(24):
curveTomorrow[hour] = 0
pass

try:
savePriceCurve(curveToday, curveTomorrow, timeToday, timeTomorrow)
saveCurrentPrice(curveToday, timeToday)
publishPrices()
except:
raise

exit(0)


if __name__ == '__main__':
main()
main()
3 changes: 2 additions & 1 deletion modules/et_awattarcap/factors.py

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions modules/et_awattarcap/main.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/bin/bash
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd)
MODULEDIR=$(cd "$(dirname "$0")" && pwd)

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

# abort if we try to use unset variables
set -o nounset

# call module
sudo python3 $OPENWBBASEDIR/modules/et_awattarcap/awattarcapgetprices.py $debug
sudo python3 "$MODULEDIR/awattarcapgetprices.py" "$debug"
28 changes: 14 additions & 14 deletions modules/soc_carnet/main.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/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"
CHARGEPOINT=$1

# check if config file is already in env
if [[ -z "$debug" ]]; then
echo "soc_carnet: 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

case $CHARGEPOINT in
Expand Down Expand Up @@ -57,32 +57,32 @@ incrementTimer(){
ticksize=1
;;
esac
soctimer=$((soctimer+$ticksize))
echo $soctimer > $soctimerfile
soctimer=$((soctimer + ticksize))
echo $soctimer > "$soctimerfile"
}

soctimer=$(<$soctimerfile)
soctimer=$(<"$soctimerfile")
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: timer = $soctimer"
if (( soctimer < 60 )); then
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: Nothing to do yet. Incrementing timer."
openwbDebugLog ${DMOD} 2 "Lp$CHARGEPOINT: Nothing to do yet. Incrementing timer."
incrementTimer
else
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: Requesting SoC"
#Abfrage Ladung aktiv. Hochsetzen des soctimers, um das Intervall zu verkürzen.
if (( ladeleistung > 800 )) ; then
soctimer=$((60 * (10 - $intervall) / 10))
echo $soctimer > $soctimerfile
soctimer=$((60 * (10 - intervall) / 10))
echo $soctimer > "$soctimerfile"
else
echo 0 > $soctimerfile
echo 0 > "$soctimerfile"
fi

response=$(sudo PYTHONIOENCODING=UTF-8 python $MODULEDIR/we_connect_client.py --user="$username" --password="$password")
response=$(sudo PYTHONIOENCODING=UTF-8 python "$MODULEDIR/we_connect_client.py" --user="$username" --password="$password")
soclevel=$(echo "$response" | grep batteryPercentage | jq -r .EManager.rbc.status.batteryPercentage)
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: Filtered SoC from Server: $soclevel"
if [[ $soclevel =~ $reValidSoc ]] ; then
if (( $soclevel != 0 )) ; then
if (( soclevel != 0 )) ; then
openwbDebugLog ${DMOD} 1 "Lp$CHARGEPOINT: SoC is valid"
echo $soclevel > $socfile
echo "$soclevel" > "$socfile"
fi
else
openwbDebugLog ${DMOD} 0 "Lp$CHARGEPOINT: SoC is not valid."
Expand Down
Loading

0 comments on commit 075d03c

Please sign in to comment.