-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase_adapter.py
More file actions
197 lines (164 loc) · 7.65 KB
/
Copy pathfirebase_adapter.py
File metadata and controls
197 lines (164 loc) · 7.65 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
import json
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime, timezone
from config import FIREBASE_CONFIG
def firebase_is_configured():
return (
FIREBASE_CONFIG["enabled"]
and FIREBASE_CONFIG["mode"] == "realtime_db"
and bool(FIREBASE_CONFIG["database_url"])
and bool(FIREBASE_CONFIG["database_secret"])
)
def firebase_status():
return {
"enabled": FIREBASE_CONFIG["enabled"],
"mode": FIREBASE_CONFIG["mode"],
"project_id_configured": bool(FIREBASE_CONFIG["project_id"]),
"database_url_configured": bool(FIREBASE_CONFIG["database_url"]),
"database_secret_configured": bool(FIREBASE_CONFIG["database_secret"]),
"realtime_database_target": firebase_is_configured(),
"root": FIREBASE_CONFIG["root"],
}
def firebase_error_hint(error):
message = str(error)
if "401" in message or "Permission denied" in message:
return "Realtime Database rejected the legacy database secret. Check AIRHUB_FIREBASE_DATABASE_SECRET in .env."
if "404" in message:
return "Realtime Database URL/path was not found. Check AIRHUB_FIREBASE_DATABASE_URL in .env."
return None
def rtdb_safe(value):
if isinstance(value, datetime):
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value.isoformat()
return value
def rtdb_rest_url(path):
base_url = FIREBASE_CONFIG["database_url"].rstrip("/")
clean_path = path.strip("/")
query = urllib.parse.urlencode({"auth": FIREBASE_CONFIG["database_secret"]})
return f"{base_url}/{clean_path}.json?{query}"
def rtdb_request(path, payload, method, timeout):
data = None if method == "DELETE" else json.dumps(payload, separators=(",", ":"), default=str).encode("utf-8")
request = urllib.request.Request(
rtdb_rest_url(path),
data=data,
method=method,
headers={"Content-Type": "application/json"},
)
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
response.read()
except urllib.error.HTTPError as exc:
body = exc.read().decode("utf-8", errors="ignore")
raise RuntimeError(f"Realtime Database REST error {exc.code}: {body}") from exc
def write_rtdb(path, payload):
rtdb_request(path, payload, "PUT", timeout=10)
def delete_user_from_firebase(user_id):
if not firebase_is_configured():
return {"synced": False, "reason": "Realtime Database is not enabled/configured."}
try:
rtdb_request(f"{FIREBASE_CONFIG['root']}/users/{user_id}", None, "DELETE", timeout=10)
return {"synced": True}
except Exception as exc:
return {"synced": False, "reason": str(exc)}
def update_rtdb(updates):
rtdb_request("/", updates, "PATCH", timeout=20)
def public_log_payload(log_record):
status = log_record.get("status") or "GUEST_PENDING"
return {
"local_id": log_record.get("id"),
"status": status,
"event_type": log_record.get("event_type"),
"tap_type": status if status in ("TAP_IN", "TAP_OUT") else None,
"date_logged": rtdb_safe(log_record.get("date_logged")),
"time_entered": log_record.get("time_entered"),
"time_left": log_record.get("time_left"),
"duration_seconds": log_record.get("duration_seconds"),
"duration_label": log_record.get("duration_label"),
"source": "raspberry_pi",
"updated_at": datetime.now(timezone.utc).isoformat(),
}
def user_payload(user_record):
return {
"local_id": user_record.get("id"),
"student_no": user_record.get("student_no"),
"lastname": user_record.get("lastname"),
"firstname": user_record.get("firstname"),
"middlename": user_record.get("middlename"),
"fullname": user_record.get("fullname"),
"course": user_record.get("course"),
"project_type": user_record.get("project_type"),
"room": user_record.get("room"),
"created_at": rtdb_safe(user_record.get("created_at")),
"updated_at": rtdb_safe(user_record.get("updated_at")),
"synced_at": datetime.now(timezone.utc).isoformat(),
}
def reservation_payload(record):
return {
"local_id": record.get("id"),
"service": record.get("service"),
"fullname": record.get("fullname"),
"student_no": record.get("student_no"),
"course": record.get("course"),
"reservation_date": rtdb_safe(record.get("reservation_date")),
"schedule_time": rtdb_safe(record.get("schedule_time")),
"duration_minutes": record.get("duration_minutes"),
"queue_position": record.get("queue_position"),
"teacher_name": record.get("teacher_name"),
"project_name": record.get("project_name"),
"purpose": record.get("purpose"),
"notes": record.get("notes"),
"model_file_name": record.get("model_file_name"),
"status": record.get("status"),
"created_at": rtdb_safe(record.get("created_at")),
"updated_at": rtdb_safe(record.get("updated_at")),
"synced_at": datetime.now(timezone.utc).isoformat(),
}
def sync_log(log_record):
try:
if not firebase_is_configured() or not log_record:
return {"synced": False, "reason": "Realtime Database is not enabled/configured."}
doc_id = str(log_record.get("id"))
root = FIREBASE_CONFIG["root"]
write_rtdb(f"{root}/logs/{doc_id}", public_log_payload(log_record))
return {"synced": True, "paths": [f"rtdb:{root}/logs/{doc_id}"], "id": doc_id}
except Exception as exc:
return {"synced": False, "reason": str(exc), "hint": firebase_error_hint(exc)}
def sync_user(user_record):
try:
if not firebase_is_configured() or not user_record:
return {"synced": False, "reason": "Realtime Database is not enabled/configured."}
doc_id = str(user_record.get("id"))
root = FIREBASE_CONFIG["root"]
write_rtdb(f"{root}/users/{doc_id}", user_payload(user_record))
return {"synced": True, "paths": [f"rtdb:{root}/users/{doc_id}"], "id": doc_id}
except Exception as exc:
return {"synced": False, "reason": str(exc), "hint": firebase_error_hint(exc)}
def sync_reservation(record):
try:
if not firebase_is_configured() or not record:
return {"synced": False, "reason": "Realtime Database is not enabled/configured."}
doc_id = str(record.get("id"))
root = FIREBASE_CONFIG["root"]
write_rtdb(f"{root}/reservations/{doc_id}", reservation_payload(record))
return {"synced": True, "paths": [f"rtdb:{root}/reservations/{doc_id}"], "id": doc_id}
except Exception as exc:
return {"synced": False, "reason": str(exc), "hint": firebase_error_hint(exc)}
def sync_all(users, logs, reservations=None):
try:
if not firebase_is_configured():
return {"synced": False, "reason": "Realtime Database is not enabled/configured."}
updates = {}
for user in users:
updates[f"{FIREBASE_CONFIG['root']}/users/{user.get('id')}"] = user_payload(user)
for log in logs:
updates[f"{FIREBASE_CONFIG['root']}/logs/{log.get('id')}"] = public_log_payload(log)
for reservation in reservations or []:
updates[f"{FIREBASE_CONFIG['root']}/reservations/{reservation.get('id')}"] = reservation_payload(reservation)
if updates:
update_rtdb(updates)
return {"synced": True, "users": len(users), "logs": len(logs), "reservations": len(reservations or []), "targets": ["realtime_database"]}
except Exception as exc:
return {"synced": False, "reason": str(exc), "hint": firebase_error_hint(exc)}