diff --git a/__pycache__/app.cpython-310.pyc b/__pycache__/app.cpython-310.pyc new file mode 100644 index 0000000..26d4e5f Binary files /dev/null and b/__pycache__/app.cpython-310.pyc differ diff --git a/__pycache__/app_account.cpython-310.pyc b/__pycache__/app_account.cpython-310.pyc new file mode 100644 index 0000000..305bffa Binary files /dev/null and b/__pycache__/app_account.cpython-310.pyc differ diff --git a/__pycache__/app_car.cpython-310.pyc b/__pycache__/app_car.cpython-310.pyc new file mode 100644 index 0000000..6575be2 Binary files /dev/null and b/__pycache__/app_car.cpython-310.pyc differ diff --git a/__pycache__/app_events.cpython-310.pyc b/__pycache__/app_events.cpython-310.pyc new file mode 100644 index 0000000..f648fd7 Binary files /dev/null and b/__pycache__/app_events.cpython-310.pyc differ diff --git a/__pycache__/app_obd.cpython-310.pyc b/__pycache__/app_obd.cpython-310.pyc new file mode 100644 index 0000000..cac1e9c Binary files /dev/null and b/__pycache__/app_obd.cpython-310.pyc differ diff --git a/app.py b/app.py index 191dfc6..929fd0a 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,15 @@ from flask import Flask, render_template, make_response, json -import app_obd, app_car, app_account, time +import app_obd, app_car, app_account, time, app_events from app_events import socketio app = Flask(__name__) socketio.init_app(app) - -account = app_account.Account() -obd = app_obd.OBD() -car = app_car.Car() +app_obd.obd_connection() @app.route('/') def index(): - print("hello "+account.name) - return render_template('index.html', account = account) + print("hello") + return render_template('index.html') @app.route('/dtc') def dtc(): @@ -24,4 +21,15 @@ def data(): data = app_obd.speed response = make_response(json.dumps(data)) response.content_type = 'application/json' - return response \ No newline at end of file + return response + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Run the Flask application with a custom port." + ) + parser.add_argument( + "--port", type=int, default=5000, help="Port number (default is 5000)" + ) + args = parser.parse_args() + + app.run(debug=True, port=args.port) \ No newline at end of file diff --git a/app_account.py b/app_account.py index 8132564..0a507df 100644 --- a/app_account.py +++ b/app_account.py @@ -1,27 +1,18 @@ import app_car as Car +import threading, app_events +from threading import Thread -account_car = Car() -account_car.model = "YARIS" +socketio = app_events.SocketIO() +account_name = "quoije" account_pp_image = "static\img\image.png" -account_info = ["name","pp_image", "car_model"] +account_car = Car +account_car.model = "YARIS" +account_lock = threading.Lock() class Account: def __init__(self): - self.name = "quoije" - self.pp_image = None - self.car = account_car.model - -def account_info(): - obd_speed_lock.acquire() - cmd = obd.commands.GET_DTC # select an OBD command (sensor) - response = connection.query(cmd) # send the command and parse the response - try: - if response.is_null(): - error_txt = "No data received." - print(error_txt) - socketio.emit('obd_dtc', error_txt) - else: - print(response.value) - socketio.emit('obd_dtc', response.value) - finally: - obd_rpm_lock.release() \ No newline at end of file + self.name = account_name + self.pp_image = account_pp_image + self.car = account_car + def info(): + return [name, pp_image, car.model] \ No newline at end of file diff --git a/app_dash.py b/app_dash.py new file mode 100644 index 0000000..0c250fd --- /dev/null +++ b/app_dash.py @@ -0,0 +1,38 @@ +import os +import shutil +from tqdm import tqdm + +def mount_device(device, mount_point): + os.system(f"sudo mount {device} {mount_point}") + +def unmount_device(mount_point): + os.system(f"sudo umount {mount_point}") + +def copy_files(source_folder, destination_folder): + if not os.path.exists(destination_folder): + os.makedirs(destination_folder) + + for root, dirs, files in os.walk(source_folder): + for file in tqdm(files, desc="Copying files", unit="file"): + source_path = os.path.join(root, file) + destination_path = os.path.join(destination_folder, file) + + # Check if the file already exists in the destination folder + if not os.path.exists(destination_path): + shutil.copy2(source_path, destination_folder) + +if __name__ == "__main__": + source_device = "/dev/sda" + mount_point = "/mnt/dash" + destination_folder = "~/yaris-obd/dash/videos" + + try: + # Mount the device + mount_device(source_device, mount_point) + + # Copy files from source to destination + copy_files(os.path.join(mount_point, "DCIM"), os.path.expanduser(destination_folder)) + + finally: + # Unmount the device (even if an exception occurs) + unmount_device(mount_point) \ No newline at end of file diff --git a/app_events.py b/app_events.py index a5ee738..ac98017 100644 --- a/app_events.py +++ b/app_events.py @@ -1,6 +1,6 @@ from flask_socketio import SocketIO from threading import Thread -import app_obd, time, threading +import app_obd, app_account, time, threading socketio = SocketIO() uptime_lock = threading.Lock() @@ -9,7 +9,6 @@ uptime_lock = threading.Lock() def connect_event(): print('Client connected') print('Getting OBD status...') - app_obd.obd_connection() uptime() def uptime(): diff --git a/app_obd.py b/app_obd.py index 1798a52..23370de 100644 --- a/app_obd.py +++ b/app_obd.py @@ -5,7 +5,7 @@ from threading import Thread status = None speed = 0 -rpm = None +rpm = 0 class OBD: def __init__(self): @@ -15,7 +15,9 @@ class OBD: obd_status = ["Disconnected", "Connected", "Connection...", "wtf"] obd_speed_lock = threading.Lock() -obd_speed_rpm = threading.Lock() +obd_rpm_lock = threading.Lock() +obd_dtc_lock = threading.Lock() +obd_state_lock = threading.Lock() try: print(obd_status[2]) @@ -31,21 +33,28 @@ def get_speedtime(): time_now = str(date.strftime("%H:%M:%S")) return [str(time_now), speed] -@socketio.on('obd') -def obd_connection(): +def obd_connection(): if connection.is_connected(): print(obd_status[1]) - socketio.emit('obd_status', obd_status[1]) print("OBD connection established.") + obd_events(1) else: print("OBD connection failed.") print(obd_status[0]) + obd_events(2) - socketio.emit('obd_status', obd_status[0]) - socketio.emit('obd_speed', "-") - socketio.emit('obm_error', "-") - socketio.emit('obd_rpm', "-") +@socketio.on('obd') +def obd_events(status): + if status == 1: + thread_speed = Thread(target=obd_state(1)) + thread_speed.daemon = True + thread_speed.start() + + elif status == 2: + thread_speed = Thread(target=obd_state(2)) + thread_speed.daemon = True + thread_speed.start() # debug speed for testing thread_speed = Thread(target=obd_speed) @@ -56,7 +65,23 @@ def obd_connection(): thread_rpm = Thread(target=obd_rpm) thread_rpm.daemon = True thread_rpm.start() - + + else: + thread_speed = Thread(target=obd_state(0)) + thread_speed.daemon = True + thread_speed.start() + + +def obd_state(status): + obd_state_lock.acquire() + loop = True + try: + while loop == True: + socketio.emit('obd_status', obd_status[status]) + time.sleep(2) + finally: + obd_state_lock.release() + def obd_speed(): obd_speed_lock.acquire() cmd = obd.commands.SPEED # select an OBD command (sensor) @@ -64,6 +89,7 @@ def obd_speed(): loop = True try: while loop == True: + socketio.emit('obd_status', obd_status[3]) global speed speed = speed + 1 time.sleep(1) @@ -78,26 +104,26 @@ def obd_speed(): obd_speed_lock.release() def obd_rpm(): - obd_speed_lock.acquire() + obd_rpm_lock.acquire() cmd = obd.commands.RPM # select an OBD command (sensor) response = connection.query(cmd) # send the command and parse the response loop = True try: while loop == True: global rpm - speed = speed + 1 + rpm = rpm + 1 time.sleep(1) if response.is_null(): - print("No data received. #"+ str(get_speedtime())) - socketio.emit('obd_speed', get_speedtime()) + print("No data received. #"+"RPM :"+str(rpm)) + socketio.emit('obd_rpm', rpm) else: print("RPM:", response.value) - socketio.emit('obd_speed', response.value.to("kph")) + socketio.emit('obd_rpm', response.value.to("kph")) finally: obd_rpm_lock.release() def obd_dtc(): - obd_speed_lock.acquire() + obd_dtc_lock.acquire() cmd = obd.commands.GET_DTC # select an OBD command (sensor) response = connection.query(cmd) # send the command and parse the response try: @@ -109,7 +135,7 @@ def obd_dtc(): print(response.value) socketio.emit('obd_dtc', response.value) finally: - obd_rpm_lock.release() + obd_dtc_lock.release() def kill_obd(obd_type): if obd_type == "speed": diff --git a/static/js/yaris.js b/static/js/yaris.js index 133183f..be29997 100644 --- a/static/js/yaris.js +++ b/static/js/yaris.js @@ -32,6 +32,10 @@ chart = new Chart(ctx, { } }); +// account + + + // websockets socket.on('connect', function(data) { @@ -41,7 +45,7 @@ socket.on('connect', function(data) { socket.on('account_info', function(data) { console.log(data); - // car model +// car model const car_model = document.getElementById('index-car-model'); car_model.innerHTML = data; }); diff --git a/templates/dtc.html b/templates/dtc.html index a4abe0f..e0afd34 100644 --- a/templates/dtc.html +++ b/templates/dtc.html @@ -15,7 +15,7 @@