-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
275 lines (236 loc) · 11.6 KB
/
Copy pathserver.py
File metadata and controls
275 lines (236 loc) · 11.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
import os
import requests
from datetime import datetime, timedelta, timezone
from typing import List
from fastapi import FastAPI, BackgroundTasks, Request
from pydantic import BaseModel
import uvicorn
# 🟢 FIX: Use a global session to pool connections and prevent socket exhaustion during rapid bulk uploads!
session = requests.Session()
app = FastAPI(title="OBD ML Server")
# Your Firebase Database URL
FIREBASE_DB_URL = "https://arapp-feb0f-default-rtdb.firebaseio.com/"
# A simple model representing the incoming data from Unity
class VehicleData(BaseModel):
device_id: str
phone_id: str = "legacy_phone"
timestamp: str
RPM: float
Speed: int
CoolantTemp: int
EngineLoad: float
IntakeTemp: int = 0
MAF: float = 0
ThrottlePos: float = 0
Voltage: float = 0
OilTemp: int = 0
MAP: int = 0
FuelLevel: float = 0
STFT: float = 0
LTFT: float = 0
O2Voltage: float = 0
ml_prediction: str = "Healthy"
ml_future_status: str = "Healthy"
ml_future_component: str = "None"
ml_future_hours: float = 0.0
def process_and_upload(data: VehicleData):
"""
Background task to run ML predictions and upload to Firebase.
"""
# ---------------------------------------------------------
# 🧠 FUTURE ML LOGIC GOES HERE
# ---------------------------------------------------------
alert_msg = "None"
status = "Healthy"
# Very basic threshold examples based on your 9-class architecture:
if data.Voltage > 0 and data.Voltage < 11.5 and data.RPM == 0:
alert_msg = "Warning: Weak Battery Detected"
status = "Warning"
elif data.CoolantTemp > 95:
alert_msg = "CRITICAL: Engine Overheating"
status = "Critical"
elif data.ThrottlePos > 80 and data.MAP < 30: # Example logic
alert_msg = "Warning: Possible Clogged Air Filter"
status = "Warning"
# ---------------------------------------------------------
# ☁️ FIREBASE UPLOAD
# ---------------------------------------------------------
try:
# 1. Update the "Live" state for the dashboard speedometers
live_payload = data.dict()
live_payload["ml_status"] = status
live_payload["ml_alert"] = alert_msg
live_url = f"{FIREBASE_DB_URL}live/{data.device_id}.json"
session.put(live_url, json=live_payload, timeout=5)
# 2. Append to "History" for the dashboard graphs
# 🟢 FIX: Use the actual timestamp from the app so delayed uploads stay in perfectly sorted order!
try:
dt = datetime.strptime(data.timestamp, "%Y-%m-%d %H:%M:%S")
time_key = dt.strftime("%Y%m%d_%H%M%S")
except:
time_key = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
history_url = f"{FIREBASE_DB_URL}history/{data.device_id}/{time_key}.json"
# 🟢 FIX: Save ALL data so the raw table doesn't show 0s!
history_payload = {
"timestamp": live_payload["timestamp"],
"RPM": data.RPM,
"Speed": data.Speed,
"CoolantTemp": data.CoolantTemp,
"EngineLoad": data.EngineLoad,
"Voltage": data.Voltage,
"IntakeTemp": data.IntakeTemp,
"MAF": data.MAF,
"ThrottlePos": data.ThrottlePos,
"OilTemp": data.OilTemp,
"MAP": data.MAP,
"FuelLevel": data.FuelLevel,
"STFT": data.STFT,
"LTFT": data.LTFT,
"O2Voltage": data.O2Voltage
}
session.put(history_url, json=history_payload, timeout=5)
# 3. Trim History
trim_history(data.device_id)
except Exception as e:
print(f"Firebase Upload Error: {e}")
def trim_history(device_id: str):
""" Deletes old records if the history gets too large """
try:
# Fetch only the keys (shallow=true) to save bandwidth
history_url = f"{FIREBASE_DB_URL}history/{device_id}.json?shallow=true"
response = session.get(history_url, timeout=5)
if response.status_code == 200 and response.json():
keys = sorted(list(response.json().keys()))
if len(keys) > 2000:
# Delete the oldest keys
keys_to_delete = keys[:-2000]
for key in keys_to_delete:
del_url = f"{FIREBASE_DB_URL}history/{device_id}/{key}.json"
session.delete(del_url, timeout=5)
except Exception as e:
print("Trim error:", e)
# 🟢 NEW: Track active phone sessions to detect ID collisions!
active_devices = {}
def process_bulk_upload(data_list: List[VehicleData], background_tasks: BackgroundTasks = None):
""" Bulk uploads an entire queue to Firebase in a single blazing fast request """
if not data_list:
return False
try:
latest = data_list[-1]
# 🟢 NEW: Check for Phone ID Collisions
is_collision = False
if latest.device_id in active_devices:
if active_devices[latest.device_id] != latest.phone_id:
is_collision = True
else:
# First time this device_id is seen, claim it for this phone_id
active_devices[latest.device_id] = latest.phone_id
# 🟢 FIX: ALWAYS Update Live Node!
# Unity was sending cache batches (> 5) when lagging. Because of the previous `is_live` check,
# the Live node was completely ignored, causing it to freeze on 19-hour old packets!
# This forced the dashboard to incorrectly report "19 hours offline" and wait 8-10 seconds
# for the History node to refresh.
live_payload = latest.dict()
prediction = latest.ml_prediction
if "Healthy" in prediction:
live_payload["ml_status"] = "Healthy"
live_payload["ml_alert"] = "None"
else:
warnings = ["Weak_Dying_Battery", "Clogged_Air_Filter", "Engine_Overheating", "Vacuum_Leak", "Lazy_Oxygen_Sensor", "Minor_Spark_Plug_Misfire"]
live_payload["ml_status"] = "Warning" if prediction in warnings else "Critical"
live_payload["ml_alert"] = f"ML DETECTION: {prediction.replace('_', ' ')}"
# 🟢 FIX: Only inject UTC Server Time if the packet is actually from this session.
# If Unity pushes a 19-hour old offline cache, we DO NOT want to inject current UTC,
# otherwise the dashboard thinks a 19-hour old packet just arrived "live"!
try:
dt = datetime.strptime(latest.timestamp, "%Y-%m-%d %H:%M:%S")
# Convert packet to UTC (Assuming UTC+5 based on system context)
packet_utc = dt - timedelta(hours=5)
age_seconds = (datetime.now(timezone.utc).replace(tzinfo=None) - packet_utc).total_seconds()
except:
age_seconds = 0
# If packet is newer than 1 hour, it's from the current drive cycle. Inject perfect server time.
if -3600 < age_seconds < 3600:
live_payload["server_timestamp_utc"] = datetime.now(timezone.utc).isoformat()
# raise_for_status() ensures we fail loudly if Firebase rejects it!
session.put(f"{FIREBASE_DB_URL}live/{latest.device_id}.json", json=live_payload, timeout=5).raise_for_status()
# 2. Update History in Bulk (ALL packets at once using PATCH)
history_updates = {}
for i, d in enumerate(data_list):
# 🟢 CRITICAL FIX: THE TIMEZONE INDEX BUG!
# Render is in UTC, but the old broken keys were saved in your Local Time (+5 hours).
# Because Local Time is 'numerically higher' than UTC, Firebase's alphabetical index
# buried all the new perfect UTC keys underneath the old broken Local keys!
# We now construct the key using your exact mobile time, but manually append the array index
# as a 'pseudo-microsecond' to guarantee 100% uniqueness and prevent the overwrite bug!
dt = datetime.strptime(d.timestamp, "%Y-%m-%d %H:%M:%S")
time_key = dt.strftime("%Y%m%d_%H%M%S") + f"_{i:06d}"
# Ensure no missing fields
payload = d.dict()
history_updates[time_key] = payload
patch_url = f"{FIREBASE_DB_URL}history/{latest.device_id}.json"
# 🟢 FIX: CLOSED LOOP CONFIRMATION
# raise_for_status() will instantly crash this function if Firebase fails.
# This prevents Unity from deleting its cache if Firebase didn't actually save the data!
session.patch(patch_url, json=history_updates, timeout=15).raise_for_status()
# 3. Trim History (IN BACKGROUND)
# 🟢 FIX: Trimming history requires a GET request that takes 500ms!
# Doing this synchronously was slowing down the Unity response loop and causing 8-9 second delays.
if background_tasks:
background_tasks.add_task(trim_history, latest.device_id)
else:
trim_history(latest.device_id)
return is_collision
except Exception as e:
print(f"Bulk Upload Error: {e}")
# Re-raise the exception so FastAPI returns a 500 Server Error to Unity,
# forcing Unity to keep the data in its offline cache for a retry!
raise e
@app.post("/api/upload")
def upload_data(data: List[VehicleData], background_tasks: BackgroundTasks):
"""
Unity sends data here as a JSON array (bulk upload).
🟢 FIX: We wait for Firebase to successfully save the data BEFORE returning 200 OK.
However, we offload the heavy 'trim_history' to a background task so Unity gets an instant response!
"""
is_collision = process_bulk_upload(data, background_tasks)
return {"status": "success", "message": f"Bulk processing {len(data)} items", "collision": is_collision}
class RenameRequest(BaseModel):
old_id: str
new_id: str
phone_id: str
@app.post("/api/rename")
def rename_device(req: RenameRequest):
""" Renames a device in Firebase by moving all history to the new ID """
try:
# Update ownership in memory
if req.old_id in active_devices:
del active_devices[req.old_id]
active_devices[req.new_id] = req.phone_id
# Move Live Data
live_res = session.get(f"{FIREBASE_DB_URL}live/{req.old_id}.json", timeout=10)
if live_res.status_code == 200 and live_res.json():
live_data = live_res.json()
live_data["device_id"] = req.new_id
session.put(f"{FIREBASE_DB_URL}live/{req.new_id}.json", json=live_data)
session.delete(f"{FIREBASE_DB_URL}live/{req.old_id}.json")
# Move History Data (This copies the whole tree)
hist_res = session.get(f"{FIREBASE_DB_URL}history/{req.old_id}.json", timeout=20)
if hist_res.status_code == 200 and hist_res.json():
hist_data = hist_res.json()
for key in hist_data:
hist_data[key]["device_id"] = req.new_id
session.put(f"{FIREBASE_DB_URL}history/{req.new_id}.json", json=hist_data)
session.delete(f"{FIREBASE_DB_URL}history/{req.old_id}.json")
return {"status": "success", "message": f"Renamed from {req.old_id} to {req.new_id}"}
except Exception as e:
print("Rename error:", e)
return {"status": "error", "message": str(e)}
@app.get("/")
def health_check():
""" Render.com needs this to know the server is alive """
return {"status": "online", "message": "OBD FastAPI Server is Running!"}
if __name__ == "__main__":
# Render.com provides the PORT environment variable
port = int(os.environ.get("PORT", 10000))
uvicorn.run(app, host="0.0.0.0", port=port)