-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
executable file
·288 lines (265 loc) · 11.9 KB
/
query.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python3
import sys
import signal
import teslajson
import time
import datetime
import pprint
import json
import requests
import urllib3
import os
from geopy.distance import geodesic
#ToDo: research python method to execute function on failure just before exit...
# -- call lumen with "alarm" animation.
#ToDo: send animation to lumen on soft state interval.
urllib3.disable_warnings()
getStateIntervalDefault = 3600
getStateIntervalActive = 5
getStateIntervalOnline = 900
getStateIntervalOnline = 1200
getStateIntervalOnline = getStateIntervalDefault
loginfailloop = 90
debugEnabled = True
def signal_handler(sig, frame):
pdebug('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def pdebug(message):
if debugEnabled == True:
print(str(datetime.datetime.now()) + ' ' + message)
def initializeState():
state = {}
state['data_state'] = {}
state['data_state']['timestamp'] = 0
state['data_state']['isGood'] = False
return state
def lumenPUT(data):
try:
lumenPUT = requests.put('http://'+lumen+':9000/lumen', data=data)
pdebug('response code: ' + str(lumenPUT.status_code) + ', response json: ' + lumenPUT.text + ', data: ' + data)
except:
pdebug("failed to PUT animation to lumen")
def configGET(key):
try:
response = requests.get('http://'+config+':8443/badger/'+key, verify=False)
return response.text
except:
return ""
def secretGET(key):
try:
response = requests.get('http://'+config+':8443/secret/'+key, verify=False)
pdebug('/secret/'+key+' retrieved.')
return response.text
except:
pdebug('/secret/'+key+' failed.')
return ""
def getVehicle(vehicle):
pdebug('start getVehicle')
try:
tToken = secretGET('tToken')
access_token = json.loads(tToken)['access_token']
connection = teslajson.Connection(access_token=access_token, tesla_client='{"v1": {"id": "e4a9949fcfa04068f59abb5a658f2bac0a3428e4652315490b659d5ab3f35a9e", "secret": "c75f14bbadc8bee3a7594412c31416f8300256d7668ea7e6e7f06727bfb9d220", "baseurl": "https://owner-api.teslamotors.com", "api": "/api/1/"}}')
vehicle = connection.vehicles[vehicle]
#pprint.pprint(vehicle, indent=2)
return vehicle
except:
return {'state' : 'failed'}
def getState(vehicle):
pdebug('start getState')
state = initializeState()
vehicleState = vehicle['state']
while True:
pdebug(vehicleState)
if vehicleState != 'online':
time.sleep(5)
try:
vehicleState = vehicle.wake_up()['response']['state']
except:
return state
else:
break
try:
state['vehicle'] = vehicle
state['charge_state'] = vehicle.data_request('charge_state')
state['climate_state'] = vehicle.data_request('climate_state')
state['drive_state'] = vehicle.data_request('drive_state')
state['gui_settings'] = vehicle.data_request('gui_settings')
state['vehicle_state'] = vehicle.data_request('vehicle_state')
state['vehicle_config'] = vehicle.data_request('vehicle_config')
#state['mobile_enabled'] = vehicle.data_request('mobile_enabled')
#state['nearby_charging_sites'] = vehicle.data_request('nearby_charging_sites')
except:
return state
state['state'] = {}
state['state']['distanceFromHome'] = geodesic((state['drive_state']['latitude'],state['drive_state']['longitude']),tHome).ft
if int(state['state']['distanceFromHome']) < int(tHomeRadiusFt):
state['state']['isHome'] = True
else:
state['state']['isHome'] = False
if int(state['charge_state']['ideal_battery_range']) >= int(tChargeRangeFull)-10:
state['state']['isCharged'] = True
else:
state['state']['isCharged'] = False
if int(state['charge_state']['ideal_battery_range']) <= int(tChargeRangeMedium):
state['state']['shouldCharge'] = True
else:
state['state']['shouldCharge'] = False
if int(state['charge_state']['ideal_battery_range']) <= int(tChargeRangeLow):
state['state']['isLow'] = True
else:
state['state']['isLow'] = False
state['data_state']['timestamp'] = int(time.time())
state['data_state']['isGood'] = True
return state
if os.environ.get('LUMEN') != None:
lumen = os.environ.get('LUMEN')
else:
lumen = 'lumen'
if os.environ.get('CONFIG') != None:
config = os.environ.get('CONFIG')
else:
config = 'config'
pdebug('lumen server: ' + lumen)
pdebug('config server: ' + config)
def getConfig(key, defaultVal):
try:
val = open('/var/run/config/' + key, 'r').read().strip()
pdebug('note: ' + key + ' = ' + val)
return val
except:
val = configGET(key)
pdebug('from configGET: ' + key + ' = ' + val)
if val == "":
val = defaultVal
pdebug('defaulted: ' + key + ' = ' + val)
return val
def query(vehicle):
if vehicle['state'] != 'failed':
state = getState(vehicle)
if state['data_state']['isGood'] == True:
#pprint.pprint(state, indent=2)
pdebug('shift_state: ' + str(state['drive_state']['shift_state']) + ', speed: ' + str(state['drive_state']['speed']) + ', distance from home: ' + str(state['state']['distanceFromHome']) + ', range: ' + str(state['charge_state']['battery_range']) + ', charge rate: ' + str(state['charge_state']['charge_rate']))
rangePercent = int(state['charge_state']['battery_range']) / int(tChargeRangeFull) * 100
if str(state['drive_state']['speed']) == "0" or state['drive_state']['speed'] == None:
velocity = "1"
else:
velocity = str(state['drive_state']['speed'])
if state['state']['isHome'] == True and state['charge_state']['charging_state'] != 'Disconnected':
pdebug('Car is home and plugged in!')
lumenPUT('{"animation":"'+getConfig('eIHIP','fill')+'","rgbw":"'+getConfig('cIHIP','')+'","percent":'+str(rangePercent)+'}')
elif state['state']['isHome'] == True and state['charge_state']['charging_state'] == 'Disconnected' and int(state['charge_state']['battery_range']) < int(tChargeRangeMedium):
pdebug('car is home, not plugged in and below ' + tChargeRangeMedium + ' miles range')
lumenPUT('{"animation":"'+getConfig('eIHNPBCRM','fill')+'","rgbw":"'+getConfig('cIHNPBCRM','')+'","percent":'+str(rangePercent)+'}')
elif state['state']['isHome'] == True and state['charge_state']['charging_state'] == 'Disconnected':
pdebug('Warning car is home but not plugged in!')
lumenPUT('{"animation":"'+getConfig('eIHNP','fill')+'","rgbw":"'+getConfig('cIHNP','')+'","percent":'+str(rangePercent)+'}')
elif state['state']['isHome'] == False:
pdebug('Car is not at home')
lumenPUT('{"animation":"'+getConfig('eNH','rainbow')+'","rgbw":"'+getConfig('cNH','')+'","percent":'+str(rangePercent)+',"velocity":'+velocity+'}')
elif state['drive_state']['shift_state'] != None:
lumenPUT('{"animation":"rainbow","percent":'+str(rangePercent)+',"velocity":'+velocity+'}')
currentStateKey = 'ts_'+str(int(time.time()))
try:
# ['data_state']['isGood']
# ['drive_state']['speed']
# ['state']['distanceFromHome']
#['climate_state']['outside_temp']
# ['charge_state']['battery_range']
# ['charge_state']['charge_rate']
# ['charge_state']['battery_level']
graphState = {}
graphState['data_state'] = {}
graphState['data_state']['isGood'] = state['data_state']['isGood']
graphState['drive_state'] = {}
graphState['drive_state']['speed'] = state['drive_state']['speed']
graphState['state'] = {}
graphState['state']['distanceFromHome'] = state['state']['distanceFromHome']
graphState['climate_state'] = {}
graphState['climate_state']['outside_temp'] = state['climate_state']['outside_temp']
graphState['charge_state'] = {}
graphState['charge_state']['battery_range'] = state['charge_state']['battery_range']
graphState['charge_state']['charge_rate'] = state['charge_state']['charge_rate']
graphState['charge_state']['battery_level'] = state['charge_state']['battery_level']
graphPUT = requests.put('http://'+config+':8443/badger/tsg_'+str(int(time.time())), data=json.dumps(graphState), verify=False)
pdebug("PUT graphState response code: " + str(graphPUT.status_code))
except:
pdebug("failed to PUT graph optimized state ")
try:
dataPUT = requests.put('http://'+config+':8443/badger/'+currentStateKey, data=json.dumps(state), verify=False)
pdebug("PUT currentState response code: " + str(dataPUT.status_code))
dataPUT = requests.put('http://'+config+':8443/badger/currentStateKey',currentStateKey,verify=False)
pdebug("PUT currentStateKey response code: " + str(dataPUT.status_code))
except:
pdebug("failed to PUT currentState and/or currentStateKey")
else:
pdebug('login failed, sleeping for a while')
for i in range(loginfailloop):
lumenPUT('{"animation":"fill","r":255}')
time.sleep(1)
lumenPUT('{"animation":"fill"}')
time.sleep(1)
return state
tHome = getConfig('tHome','37.4919392,-121.9469367')
tHomeRadiusFt = getConfig('tHomeRadiusFt','100')
tWork = getConfig('tWork','37.4919392,-121.9469367')
tWorkRadiusFt = getConfig('tWorkRadiusFt','1000')
tEmailAdr = getConfig('tEmailAdr',"")
tChargeRangeFull = getConfig('tChargeRangeFull','270')
tChargeRangeMedium = getConfig('tChargeRangeMedium','100')
tChargeRangeLow = getConfig('tChargeRangeLow','30')
tVehicle = int(getConfig('tVehicle','0'))
getStateInterval = int(getConfig('tGetStateInterval','3600'))
softStateInterval = int(getConfig('tSoftStateInterval','300'))
lastSoftStateInterval = float(getConfig('lastSoftStateInterval','0'))
if lastSoftStateInterval == 0:
lastSoftStateInterval = time.time()
pdebug('lastSoftStateInterval set to now:' + str(lastSoftStateInterval))
else:
pdebug('lastSoftStateInterval restore from badger:'+str(lastSoftStateInterval))
try:
state = json.loads(getConfig(getConfig('currentStateKey',''),''))
pdebug('state recovered... ')
except:
state = initializeState()
try:
tPassword = open('/var/run/secrets/tPassword', 'r').read().strip()
pdebug('note: tPassword = [redacted]')
except:
pdebug('warning: tPassword = None')
tPassword = None
#Todo: keep check interval short for 1 full cycle before going back to default.
#Todo: current (5 minute) default for soft check may be too short.. ?10 mintes? -- many be dynamic depending on battery %.
queryNext = False
while True:
if int(time.time()) - int(lastSoftStateInterval) > int(softStateInterval):
softStateInterval = int(getConfig('tSoftStateInterval','300'))
lastSoftStateInterval = time.time()
try:
dataPUT = requests.put('http://'+config+':8443/badger/lastSoftStateInterval',str(time.time()),verify=False)
pdebug("PUT lastSoftStateInterval response code: " + str(dataPUT.status_code))
except:
pdebug('failed to store lastSoftStateInterval')
vehicle = getVehicle(tVehicle)
pdebug('soft check state: ' + vehicle['state'])
if vehicle['state'] == 'online':
queryNext = True
else:
queryNext = False
pdebug(' next soft state check: ' + str(datetime.datetime.now() + datetime.timedelta(seconds=softStateInterval)) + ' (' + str(softStateInterval) + ')')
if int(time.time()) - int(state['data_state']['timestamp']) > getStateInterval:
vehicle = getVehicle(tVehicle)
queryNext = True
if queryNext == True:
state = query(vehicle)
if state['drive_state']['shift_state'] != None:
getStateInterval = int(getConfig('tGetStateIntervalDriving','30'))
elif state['charge_state']['charge_rate'] > 0:
getStateInterval = int(getConfig('tGetStateIntervalCharging','60'))
else:
getStateInterval = int(getConfig('tGetStateInterval','3600'))
queryNext = False
lastSoftStateInterval = time.time()
pdebug('next hard vehicle query: ' + str(datetime.datetime.now() + datetime.timedelta(seconds=getStateInterval)) + ' (' + str(getStateInterval) + ')')
pdebug(' next soft state check: ' + str(datetime.datetime.now() + datetime.timedelta(seconds=softStateInterval)) + ' (' + str(softStateInterval) + ')')
time.sleep(.5)