-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.py
132 lines (113 loc) · 4.71 KB
/
access.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from requests import ConnectionError
import requests
import json
from datetime import timedelta, date, datetime, time
import const
from zozo import Zoe
# todo: remove User Info (PW) from zoe.getPersonalInfo()
# noinspection SpellCheckingInspection
def ecGetWeatherForecast(forecast_time=14, days=1, JSON_File=False):
"""
Get weather forecast from weather service provider.
:param forecast_time: forecast hour in 24h format
:param days: days after calling this function
:param JSON_File: True: generate JSON file from weather API
:return: weather forecast for the selected day and time
"""
print('\nObtaining weather data ...')
description = ''
temperature = -9999
clouds = -1
weatherForecast = {'statusCode': 0, 'forecastDate': 0, 'clouds': 0, 'temp': -9999, 'weather': "?"}
localDate = date.today()
forecastDate = localDate + timedelta(days)
forecastTime = time(forecast_time, 0)
forecast_local = datetime.combine(forecastDate, forecastTime)
timestamp = forecast_local.timestamp()
url = const.C_WEATHER_URL + const.C_WEATHER_API_KEY
r = requests.get(url, timeout=20)
status_code = r.status_code
if status_code == 200:
jsondict = r.json()
clouds = -1
for i in jsondict['list']:
i_timestamp = i['dt']
if i_timestamp >= timestamp:
clouds = i['clouds']['all']
temperature = i['main']['temp']
description = i['weather'][0]['description'] # thanks https://stackoverflow.com/a/23306717
print('weather for:', forecast_local)
print('clouds:', clouds)
print('temperature:', temperature)
print('weather:', description)
break
if JSON_File:
with open("weather.json", 'w') as f:
json.dump(jsondict, f, indent=4)
weatherForecast['statusCode'] = status_code
weatherForecast['forecastDate'] = forecast_local
weatherForecast['clouds'] = clouds
weatherForecast['temp'] = temperature
weatherForecast['weather'] = description
return weatherForecast
# noinspection SpellCheckingInspection
def ec_GetCarData():
"""
Get car data from Renault cloud.
:rtype: dictionary
"""
print('\nObtaining car data ...')
carData = {'statusCode': 0, 'batteryLevel': -1, 'plugStatus': 0, 'clima': '---', 'mileage': 0, 'location': "---"}
try:
zoe = Zoe(const.C_RENAULT_USER, const.C_RENAULT_PASS)
zoe.getPersonalInfo()
# todo: ignore if error pos = zoe.googleLocation()
batt = zoe.batteryStatus()
cockpit = zoe.cockpit()
carData['statusCode'] = batt['status_code']
carData['batteryLevel'] = batt['data']['attributes']['batteryLevel']
carData['plugStatus'] = batt['data']['attributes']['plugStatus']
# carData['clima'] = hvac['data']['attributes']['hvacStatus']
carData['mileage'] = cockpit['data']['attributes']['totalMileage']
except ConnectionError:
carData['statusCode'] = -1
print('CONNECTION ERROR!')
except:
carData['statusCode'] = -2
print('JSON ERROR')
# carData['location'] = pos
# print('car Data:', carData)
return carData
# noinspection PyPep8
def ec_GetPVData(url=const.C_SOLAR_URL, tout=15):
"""
Get relevant data from PV inverter. API doc: https://www.solaredge.com/sites/default/files/se_monitoring_api.pdf
:param tout: timeout for url access
:param url: complete url including api key
:return: PV current flow
"""
print('\nObtaining PV data ...')
pvData = {'statusCode': 0, 'pvPower': 0.0, 'LoadPower': 0.0, 'PowerToGrid': -30}
try:
r = requests.get(url, tout)
pvData['statusCode'] = r.status_code
if pvData['statusCode'] == 200:
jsondata = r.json()
jsondata = jsondata['siteCurrentPowerFlow']
pvData['pvPower'] = jsondata['PV']['currentPower']
pvData['LoadPower'] = jsondata['LOAD']['currentPower']
# pvData['PowerToGrid'] = jsondata['GRID']['currentPower']
pvData['PowerToGrid'] = pvData['pvPower'] - pvData['LoadPower']
except ConnectionError:
pvData['statusCode'] = "-1"
print(pvData)
return pvData
# -------- test only -----------
if __name__ == "__main__":
# ecGetWeatherForecast(forecast_time=14, days=1, JSON_File=False)
ec_GetCarData()
# print("API Version =", const.C_CHARGER_API_VERSION)
# ecReadChargerData()
# ecSetChargerData("amp", "8")
# ecSetChargerData("alw", "0") # 1 : start charging
# ec_GetPVData()