Repository navigation
Expand file tree
/
Copy pathevolute_api.py
More file actions
403 lines (340 loc) · 12.6 KB
/
Copy pathevolute_api.py
File metadata and controls
403 lines (340 loc) · 12.6 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import os
import json
import time
import logging
import threading
import sys
import requests
from flask import Flask, request, jsonify, abort
from datetime import datetime
from flask import Response
from urllib.parse import urljoin
LISTEN = os.getenv("LISTEN", "127.0.0.1")
PORT = int(os.getenv("PORT", 12321))
API_KEY = os.getenv("API_KEY", "change_me")
API_KEY_RW = os.getenv("API_KEY_RW", "change_me_rw")
TIMEOUT = int(os.getenv("TIMEOUT", 60))
REFRESH_INTERVAL = int(os.getenv("REFRESH_INTERVAL", 600))
SENSORS_REFRESH_INTERVAL = int(os.getenv("SENSORS_REFRESH_INTERVAL", 120))
JSON_SUB = os.getenv("JSON_SUB", ".sensors")
EVOLUTE_TOKEN_FILENAME = os.getenv("EVOLUTE_TOKEN_FILENAME", "evy-platform-access.txt")
EVOLUTE_REFRESH_TOKEN_FILENAME = os.getenv("EVOLUTE_REFRESH_TOKEN_FILENAME", "evy-platform-refresh.txt")
CAR_ID = os.getenv("CAR_ID", "SOME_CAR_ID_HASH_CHANGE_ME")
current_refresh_interval = REFRESH_INTERVAL
USER_AGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/144.0.0.0 Safari/537.36"
)
DUMP_FILE = "dump.json"
STATUS_FILE = "status.json"
EVOLUTE_REFRESH_URL = "https://app.evassist.ru/id-service/auth/refresh-token"
EVOLUTE_SENSOR_URL = f"https://app.evassist.ru/car-service/tbox/{CAR_ID}/info"
INTELLIGENT_ACTIONS = {
"lock_close": ("centralLockingToggle", "centralLockingStatus", 1),
"lock_open": ("centralLockingToggle", "centralLockingStatus", 0),
"heating_off": ("heating", "climateStatus", 0),
"heating_on": ("heating", "climateStatus", 1),
"cooling_off": ("cooling", "climateStatus", 0),
"cooling_on": ("cooling", "climateStatus", 1),
"trunk_close": ("trunkOpen", "trunkStatus", 0),
"trunk_open": ("trunkOpen", "trunkStatus", 1),
"prepare_on": ("PREPARE", "ignitionStatus", 1),
"prepare_off": ("CANCEL", "ignitionStatus", 0),
"blink": ("blink", "ready", 1),
}
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] %(asctime)s %(message)s"
)
logger = logging.getLogger(__name__)
app = Flask(__name__)
sensors_data = {}
status_info = {
"start_time": datetime.utcnow().isoformat(),
"last_token_update": None,
"last_sensor_update": None,
}
tokens_ok = False
start_timestamp = time.time()
def read_json_file(filename, default=None):
try:
with open(filename, "r") as f:
return json.load(f)
except Exception:
return default or {}
def write_json_file(filename, data):
try:
with open(filename, "w") as f:
json.dump(data, f, indent=2)
except Exception as e:
logger.error(f"Failed to write {filename}: {e}")
def load_token(filename):
try:
with open(filename, "r") as f:
return f.read().strip()
except FileNotFoundError:
return ""
def save_token(filename, token):
with open(filename, "w") as f:
f.write(token.strip())
def get_tokens():
return {
"access": load_token(EVOLUTE_TOKEN_FILENAME),
"refresh": load_token(EVOLUTE_REFRESH_TOKEN_FILENAME),
}
def update_status(key):
status_info[key] = datetime.utcnow().isoformat()
write_json_file(STATUS_FILE, status_info)
def refresh_tokens():
global tokens_ok, current_refresh_interval
try:
tokens = get_tokens()
payload = {"refreshToken": tokens["refresh"]}
response = requests.post(EVOLUTE_REFRESH_URL, json=payload, timeout=TIMEOUT)
response.raise_for_status()
data = response.json()
save_token(EVOLUTE_TOKEN_FILENAME, data["accessToken"])
save_token(EVOLUTE_REFRESH_TOKEN_FILENAME, data["refreshToken"])
update_status("last_token_update")
tokens_ok = True
if current_refresh_interval != REFRESH_INTERVAL:
logger.info(f"Token refresh successful. Resetting interval to {REFRESH_INTERVAL}s")
current_refresh_interval = REFRESH_INTERVAL
else:
logger.info("Tokens refreshed successfully")
except requests.exceptions.HTTPError as e:
tokens_ok = False
if e.response is not None and e.response.status_code == 403:
new_interval = min(current_refresh_interval * 2, 3600)
logger.warning(f"403 Forbidden during token refresh. Increasing cooldown from {current_refresh_interval}s to {new_interval}s")
current_refresh_interval = new_interval
else:
logger.error(f"HTTP error refreshing tokens: {e}")
except Exception as e:
tokens_ok = False
logger.error(f"Failed to refresh tokens: {e}")
def fetch_sensor_data():
global sensors_data
if not tokens_ok:
logger.warning("Sensor data fetch skipped: tokens are not active")
return
try:
tokens = get_tokens()
cookies = {
"evy-platform-access": tokens["access"],
"evy-platform-refresh": tokens["refresh"]
}
headers = {
"User-Agent": USER_AGENT
}
response = requests.get(EVOLUTE_SENSOR_URL, headers=headers, cookies=cookies, timeout=TIMEOUT)
response.raise_for_status()
data = response.json()
keys = JSON_SUB.strip(".").split(".")
for k in keys:
data = data.get(k, {})
sensors_data = data
update_status("last_sensor_update")
write_json_file(DUMP_FILE, sensors_data)
logger.info("Sensor data updated")
except Exception as e:
logger.error(f"Failed to fetch sensor data: {e}")
def periodic_refresh():
refresh_tokens()
t = threading.Timer(current_refresh_interval, periodic_refresh)
t.daemon = True
t.start()
def periodic_fetch():
fetch_sensor_data()
t = threading.Timer(SENSORS_REFRESH_INTERVAL, periodic_fetch)
t.daemon = True
t.start()
def check_auth(req):
key = req.headers.get("X-API-Key") or req.args.get("api_key")
if key != API_KEY:
abort(jsonify({"error": "Unauthorized"}), 401)
def check_auth_rw(req):
key = req.headers.get("X-API-Key") or req.args.get("api_key")
if key != API_KEY_RW:
abort(jsonify({"error": "Unauthorized"}), 401)
@app.route("/ping", methods=["GET"])
def ping():
return jsonify({"status": "ok"}), 200
@app.route("/status", methods=["GET"])
def status():
uptime_seconds = time.time() - start_timestamp
return jsonify({
"alive": True,
"uptime": uptime_seconds,
"start_time": status_info["start_time"],
"last_token_update": status_info["last_token_update"],
"last_sensor_update": status_info["last_sensor_update"],
"tokens_active": tokens_ok,
"current_refresh_interval": current_refresh_interval
})
@app.route("/set_tokens", methods=["POST"])
def set_tokens():
global current_refresh_interval
check_auth(request)
data = request.get_json(force=True)
access = data.get("access")
refresh = data.get("refresh")
if access:
save_token(EVOLUTE_TOKEN_FILENAME, access)
if refresh:
save_token(EVOLUTE_REFRESH_TOKEN_FILENAME, refresh)
current_refresh_interval = REFRESH_INTERVAL
return jsonify({"status": "tokens updated"})
@app.route("/manual_refresh", methods=["POST"])
def manual_refresh():
check_auth(request)
refresh_tokens()
return jsonify({"status": "refreshed", "interval": current_refresh_interval})
@app.route("/sensors/all", methods=["GET"])
def get_all_sensors():
check_auth(request)
sensors = sensors_data.get("sensorsData")
if sensors:
return jsonify(sensors)
else:
return jsonify({"error": "No sensors data available"}), 404
@app.route("/position/all", methods=["GET"])
def get_all_positions():
check_auth(request)
position = sensors_data.get("positionData")
if position:
return jsonify(position)
else:
return jsonify({"error": "No position data available"}), 404
@app.route("/sensors/<string:sensor_name>", methods=["GET"])
def get_single_sensor(sensor_name):
check_auth_rw(request)
value = sensors_data.get(sensor_name)
if value is None:
return jsonify({"error": "sensor not found"}), 404
return jsonify({sensor_name: value})
@app.route("/proxy/<path:subpath>", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
def proxy(subpath):
check_auth_rw(request)
tokens = get_tokens()
base_url = "https://app.evassist.ru/"
target_url = urljoin(base_url, subpath)
method = request.method
headers = {
"User-Agent": USER_AGENT
}
headers.update({
k: v for k, v in request.headers.items()
if k.lower() not in ["host", "content-length", "content-type", "x-api-key"]
})
if request.content_type:
headers["Content-Type"] = request.content_type
cookies = {
"evy-platform-access": tokens["access"],
"evy-platform-refresh": tokens["refresh"]
}
try:
resp = requests.request(
method,
target_url,
headers=headers,
params=request.args,
data=request.get_data(),
cookies=cookies,
timeout=TIMEOUT,
allow_redirects=False
)
excluded_headers = ["content-encoding", "transfer-encoding", "connection"]
response_headers = [
(name, value) for (name, value) in resp.raw.headers.items()
if name.lower() not in excluded_headers
]
return Response(resp.content, resp.status_code, response_headers)
except Exception as e:
logger.error(f"Proxy request failed: {e}")
return jsonify({"error": "Proxy failed"}), 500
@app.route("/tbox/<string:action>", methods=["POST"])
def tbox_action(action):
check_auth_rw(request)
tokens = get_tokens()
target_url = f"https://app.evassist.ru/car-service/tbox/{CAR_ID}/{action}"
headers = {
"User-Agent": USER_AGENT,
"Content-Type": "application/json"
}
cookies = {
"evy-platform-access": tokens["access"],
"evy-platform-refresh": tokens["refresh"]
}
try:
resp = requests.post(
target_url,
headers=headers,
data=request.get_data(),
cookies=cookies,
timeout=TIMEOUT
)
resp.raise_for_status()
return jsonify({"status": "success"})
except Exception as e:
logger.error(f"TBox action request failed: {e}")
return jsonify({"status": "error", "error": str(e)}), 500
@app.route("/tbox-i/<string:action>", methods=["POST"])
def tbox_i_action(action):
check_auth_rw(request)
if action not in INTELLIGENT_ACTIONS:
return jsonify({"status": "error", "error": f"Unknown intelligent action: {action}"}), 400
endpoint, status_key, skip_if_value = INTELLIGENT_ACTIONS[action]
try:
fetch_sensor_data()
current_value = sensors_data.get("sensorsData", {}).get(status_key)
if current_value == skip_if_value:
logger.info(f"Intelligent action '{action}' skipped: already in desired state")
return jsonify({"status": "already_ok"})
target_url = f"https://app.evassist.ru/car-service/tbox/{CAR_ID}/{endpoint}"
tokens = get_tokens()
headers = {
"User-Agent": USER_AGENT,
"Content-Type": "application/json"
}
cookies = {
"evy-platform-access": tokens["access"],
"evy-platform-refresh": tokens["refresh"]
}
resp = requests.post(
target_url,
headers=headers,
data=request.get_data(),
cookies=cookies,
timeout=TIMEOUT
)
resp.raise_for_status()
logger.info(f"Intelligent action '{action}' executed successfully")
fetch_sensor_data()
return jsonify({"status": "success"})
except Exception as e:
logger.error(f"Intelligent action '{action}' failed: {e}")
return jsonify({"status": "error", "error": str(e)}), 500
@app.errorhandler(404)
def not_found(e):
return jsonify({"error": "Not found"}), 404
@app.errorhandler(405)
def method_not_allowed(e):
return jsonify({"error": "Method Not Allowed"}), 405
@app.errorhandler(Exception)
def handle_exception(e):
logger.error(f"Unhandled error: {e}")
return jsonify({"error": "Internal Server Error"}), 500
if __name__ == "__main__":
if CAR_ID == "SOME_CAR_ID_HASH_CHANGE_ME":
logger.error("Critical environment variable CAR_ID is not set. Exiting.")
sys.exit(1)
start_timestamp = time.time()
sensors_data = read_json_file(DUMP_FILE, default={})
loaded_status = read_json_file(STATUS_FILE, default={})
status_info.update({k: v for k, v in loaded_status.items() if k in status_info})
periodic_refresh()
periodic_fetch()
logger.info("App started")
app.run(host=LISTEN, port=PORT)