from flask import Flask, render_template, make_response, json, jsonify, request 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.start_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/dash/files') def dash_files(): summary = app_dash.list_dashcam_files() return jsonify(summary) @app.route('/api/dash/files', methods=['DELETE']) def dash_delete_all(): summary = app_dash.delete_dashcam_files(delete_all=True) status_code = 200 if summary["status"] in {"ok", "partial"} else 409 return jsonify(summary), status_code @app.route('/api/dash/file', methods=['DELETE']) def dash_delete_file(): body = request.get_json(silent=True) or {} summary = app_dash.delete_dashcam_files(relative_path=body.get("path")) status_codes = { "ok": 200, "partial": 409, "invalid_path": 400, "not_found": 404, } return jsonify(summary), status_codes.get(summary["status"], 409) @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()) # 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()