-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
81 lines (70 loc) · 2.55 KB
/
api.py
File metadata and controls
81 lines (70 loc) · 2.55 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
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
import os
from db_config import get_db_connection
app = Flask(__name__)
CORS(app)
@app.route("/captures/<path:filename>")
def serve_captures(filename):
captures_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "captures")
return send_from_directory(captures_dir, filename)
@app.route("/alerts", methods=["GET"])
def get_alerts():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT alert_id, driver_id, status, location, timestamp, pir_status, image_path FROM alerts ORDER BY timestamp DESC LIMIT 50;")
rows = cur.fetchall()
cur.close()
conn.close()
alerts = [
{
"alert_id": r[0],
"driver_id": r[1],
"status": r[2],
"location": r[3],
"timestamp": r[4].isoformat(),
"pir_status": r[5],
"image_path": r[6]
}
for r in rows
]
return jsonify(alerts)
@app.route("/latest_status/<int:driver_id>", methods=["GET"])
def latest_status(driver_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT status, timestamp FROM alerts WHERE driver_id=%s ORDER BY timestamp DESC LIMIT 1;", (driver_id,))
row = cur.fetchone()
cur.close()
conn.close()
if row:
return jsonify({"driver_id": driver_id, "status": row[0], "timestamp": row[1].isoformat()})
else:
return jsonify({"driver_id": driver_id, "status": "Unknown"}), 404
@app.route("/drivers", methods=["GET"])
def get_drivers():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT driver_id, name, phone, email FROM drivers;")
rows = cur.fetchall()
cur.close()
conn.close()
drivers = [{"driver_id": r[0], "name": r[1], "phone": r[2], "email": r[3]} for r in rows]
return jsonify(drivers)
@app.route("/add_alert", methods=["POST"])
def add_alert():
data = request.json
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
INSERT INTO alerts (driver_id, status, location, image_path, pir_status, notified)
VALUES (%s, %s, %s, %s, %s, %s)
RETURNING alert_id, timestamp;
""", (data["driver_id"], data["status"], data.get("location"), data.get("image_path"), data.get("pir_status"), data.get("notified", True)))
alert_id, ts = cur.fetchone()
conn.commit()
cur.close()
conn.close()
return jsonify({"alert_id": alert_id, "timestamp": ts.isoformat()}), 201
if __name__ == "__main__":
app.run(debug=True, port=5000)