Root-caused the slow-upload complaint to the Pi's own congested 2.4GHz WiFi radio (confirmed via iperf3 - same degraded throughput even on pure local LAN traffic, ruling out Cloudflare/ISP). Since the Pi 3 B has no 5GHz radio, add a way to bypass it for the internet-bound leg: a phone opens /dash and manually relays pending dashcam files straight to the remote server over its own network, reusing the exact same chunked upload protocol the Pi's client already uses. - server/app.py: add CORS headers (after_request) so the phone's browser can call the remote server cross-origin from a page served by the Pi. - app.py: new endpoints - GET /api/dash/pending (list not-yet-uploaded files), GET /api/dash/file/<path> (Range-request-capable download, path-traversal safe), GET /api/dash/relay/config (hands the phone the device credentials), POST /api/dash/relay/heartbeat|stop (lease- based pause of the Pi's own background uploader, self-heals if the phone's tab closes uncleanly), POST /api/dash/relay/mark_uploaded. - modules/app_upload.py: list_pending_dashcam_files(), relay lease helpers, _upload_dashcam_files() now pauses while a relay lease is active. - modules/app_sql.py: new dashcam_hash_cache table so a file's sha256 is only computed once (keyed by path+size+mtime) instead of being recomputed on every loop iteration/pending-list call - benefits the existing background uploader too. - templates/dash.html / static/js/dash.js: new "Relay via phone" card - per-file and "relay all" buttons (manual trigger only, per user preference), downloads+uploads one 40MB slice at a time so the phone never holds a whole file in memory, retries chunks up to 3x. - PLAN.md/readme.md/server/README.md updated (Phase 11). Tested via a full local end-to-end simulation driving the real Pi and server Flask apps side by side (Postgres mocked only at the db module boundary - all HTTP/chunking/auth/CORS logic is real): verified Range downloads (206), byte-for-byte reassembly via SHA256, path-traversal rejection, and the relay lease correctly pausing/resuming the background uploader. Not yet tested against a real phone/live network.
125 lines
4.5 KiB
Python
125 lines
4.5 KiB
Python
import os
|
|
|
|
from flask import Flask, render_template, make_response, json, jsonify, request, send_file, abort
|
|
from werkzeug.utils import safe_join
|
|
from modules import app_gps, app_events, app_car, app_account, app_gps,app_obd, app_sql, app_dash, app_upload
|
|
import time
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
|
|
app = Flask(__name__)
|
|
app_events.socketio.init_app(app)
|
|
|
|
account = app_account.Account()
|
|
obd = app_obd.OBD()
|
|
car = app_car.Car()
|
|
gps = app_gps.start_gps()
|
|
app_sql.start_logging()
|
|
app_upload.start_uploading()
|
|
|
|
@app.route('/')
|
|
def index():
|
|
print("hello "+ account.name)
|
|
print("you're car model is:" + car.model)
|
|
return render_template('index.html', account = account, car = car)
|
|
|
|
@app.route('/dtc')
|
|
def dtc():
|
|
return render_template('dtc.html', account = account, car = car)
|
|
|
|
@app.route('/dash')
|
|
def dash():
|
|
return render_template('dash.html', account = account, car = car)
|
|
|
|
@app.route('/api/dash/devices')
|
|
def dash_devices():
|
|
devices = app_dash.list_usb_storage_partitions()
|
|
return jsonify({"devices": devices})
|
|
|
|
@app.route('/api/dash/import', methods=['POST'])
|
|
def dash_import():
|
|
summary = app_dash.import_dashcam()
|
|
return jsonify(summary)
|
|
|
|
@app.route('/api/upload/status')
|
|
def upload_status():
|
|
return jsonify(app_upload.get_status())
|
|
|
|
@app.route('/api/upload/dashcam_toggle', methods=['POST'])
|
|
def upload_dashcam_toggle():
|
|
body = request.get_json(silent=True) or {}
|
|
if "enabled" not in body:
|
|
return jsonify(error="expected JSON body {'enabled': true|false}"), 400
|
|
app_upload.set_dashcam_upload_enabled(bool(body["enabled"]))
|
|
return jsonify(app_upload.get_status())
|
|
|
|
# --- Phone-relay: lets a phone on the Pi's own network list, download, and
|
|
# directly re-upload dashcam files to the remote server itself (bypassing
|
|
# the Pi's own often-congested WiFi radio for the internet-bound leg). See
|
|
# static/js/dash.js for the client side of this. ---
|
|
|
|
@app.route('/api/dash/pending')
|
|
def dash_pending():
|
|
return jsonify({"files": app_upload.list_pending_dashcam_files()})
|
|
|
|
@app.route('/api/dash/file/<path:relpath>')
|
|
def dash_file(relpath):
|
|
abs_path = safe_join(app_dash.DEFAULT_DESTINATION, relpath)
|
|
if abs_path is None or not os.path.isfile(abs_path):
|
|
abort(404)
|
|
# conditional=True (Flask's default) makes this support Range requests,
|
|
# so the phone can fetch/re-upload one chunk at a time instead of ever
|
|
# holding the whole (300+MB) file in the browser's memory at once.
|
|
return send_file(abs_path, mimetype='application/octet-stream', conditional=True)
|
|
|
|
@app.route('/api/dash/relay/config')
|
|
def dash_relay_config():
|
|
# Hands the phone the same device credentials the Pi's own upload client
|
|
# uses, so its browser JS can call the remote server's existing chunked
|
|
# upload endpoints directly. Same trust boundary as the rest of this
|
|
# unauthenticated local UI - anyone on the Pi's hotspot/LAN is already
|
|
# implicitly trusted.
|
|
return jsonify({
|
|
"server_url": app_upload.SERVER_URL,
|
|
"device_name": app_upload.DEVICE_NAME,
|
|
"api_key": app_upload.API_KEY,
|
|
"chunk_size": app_upload.DASHCAM_CHUNK_SIZE,
|
|
"configured": app_upload.is_configured(),
|
|
})
|
|
|
|
@app.route('/api/dash/relay/heartbeat', methods=['POST'])
|
|
def dash_relay_heartbeat():
|
|
app_upload.relay_heartbeat()
|
|
return jsonify(app_upload.get_status())
|
|
|
|
@app.route('/api/dash/relay/stop', methods=['POST'])
|
|
def dash_relay_stop():
|
|
app_upload.relay_stop()
|
|
return jsonify(app_upload.get_status())
|
|
|
|
@app.route('/api/dash/relay/mark_uploaded', methods=['POST'])
|
|
def dash_relay_mark_uploaded():
|
|
body = request.get_json(silent=True) or {}
|
|
relative_path = (body.get("relative_path") or "").strip()
|
|
sha256 = (body.get("sha256") or "").strip().lower()
|
|
if not relative_path or not sha256:
|
|
return jsonify(error="expected JSON body {'relative_path', 'sha256'}"), 400
|
|
app_sql.mark_dashcam_file_uploaded(relative_path, sha256)
|
|
return jsonify(status="ok")
|
|
|
|
# not sure if im gonna use this
|
|
@app.route('/api/obd_speed/')
|
|
def data():
|
|
data = app_obd.speed
|
|
response = make_response(json.dumps(data))
|
|
response.content_type = 'application/json'
|
|
return response
|
|
|
|
if __name__ == '__main__':
|
|
# Debug/Development
|
|
# app.run(debug=True, host="0.0.0.0", port="5000")
|
|
# Production
|
|
http_server = WSGIServer(('', 5000), app)
|
|
print("server on http://localhost:5000")
|
|
http_server.serve_forever()
|
|
|