-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolarprognose_WeatherData.py
84 lines (76 loc) · 3.8 KB
/
Solarprognose_WeatherData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import requests
import json
import time
from datetime import datetime, timedelta
import FUNCTIONS.functions
def loadLatestWeatherData():
url = 'http://www.solarprognose.de/web/solarprediction/api/v1?access-token={}&item={}&id={}&type={}&algorithm={}'.format(accesstoken, item, id, type, algorithm)
# Hier wieder ABHOLEN EIN
try:
apiResponse = requests.get(url, timeout=99)
#apiResponse.raise_for_status()
if apiResponse.status_code != 204:
json_data1 = dict(json.loads(apiResponse.text))
else:
print("### ERROR: Keine forecasts-Daten von www.solarprognose.de")
exit()
except requests.exceptions.Timeout:
print("### ERROR: Timeout von www.solarprognose.de")
exit()
dict_watts = {}
dict_watts['result'] = {}
dict_watts['result']['watts'] = {}
for key, value in json_data1.get('data',{}).items():
key_neu = str(datetime.fromtimestamp(value[0]) + timedelta(hours=Zeitversatz))
dict_watts['result']['watts'][key_neu] = int(value[1]*1000*KW_Faktor)
return(dict_watts, json_data1)
if __name__ == '__main__':
basics = FUNCTIONS.functions.basics()
config = basics.loadConfig(['default', 'weather'])
# Benoetigte Variablen definieren und prüfen
id = basics.getVarConf('solarprognose', 'id', 'eval')
KW_Faktor = basics.getVarConf('solarprognose', 'KW_Faktor', 'eval')
dataAgeMaxInMinutes = basics.getVarConf('solarprognose', 'dataAgeMaxInMinutes', 'eval')
WaitSec = basics.getVarConf('solarprognose', 'WaitSec', 'eval')
weatherfile = basics.getVarConf('solarprognose', 'weatherfile', 'str')
accesstoken = basics.getVarConf('solarprognose', 'accesstoken', 'str')
item = basics.getVarConf('solarprognose', 'item', 'str')
type = basics.getVarConf('solarprognose', 'type', 'str')
Zeitversatz = int(basics.getVarConf('solarprognose', 'Zeitversatz', 'eval'))
algorithm = basics.getVarConf('solarprognose', 'algorithm', 'str')
time.sleep(WaitSec)
format = "%Y-%m-%d %H:%M:%S"
now = datetime.now()
data = basics.loadWeatherData(weatherfile)
dataIsExpired = True
if (data):
dateCreated = None
if (data['messageCreated']):
dateCreated = datetime.strptime(data['messageCreated'], format)
if (dateCreated):
diff = now - dateCreated
dataAgeInMinutes = diff.total_seconds() / 60
if (dataAgeInMinutes < dataAgeMaxInMinutes):
print_level = basics.getVarConf('env','print_level','eval')
if ( print_level != 0 ):
print('solarprognose.de ERROR: Die Minuten aus "dataAgeMaxInMinutes" ', dataAgeMaxInMinutes ,' Minuten sind noch nicht abgelaufen!!')
print(f'[Now: {now}] [Data created: {dateCreated}] -> age in min: {dataAgeInMinutes}\n')
dataIsExpired = False
if (dataIsExpired):
data_all = loadLatestWeatherData()
data = data_all[0]
data_err = data_all[1]
if(data['result']['watts'] != {}):
if not data == "False":
# hier evtl Begrenzungen der Prognose anbringen
MaximalPrognosebegrenzung = basics.getVarConf('env','MaximalPrognosebegrenzung','eval')
if (MaximalPrognosebegrenzung == 1):
data = basics.checkMaxPrognose(data)
if (MaximalPrognosebegrenzung == 2):
data = basics.Prognoseoptimierung(data)
basics.storeWeatherData(weatherfile, data, now, 'solarprognose.de')
dateCreated_new = data['messageCreated']
print(f'solarprognose.de OK: Prognosedaten vom {dateCreated_new} gespeichert.\n')
else:
print("Fehler bei Datenanforderung solarprognose.de:")
print(data_err)