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/') 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()