add app_dash.py and some files

This commit is contained in:
Mathieu Moran
2023-11-18 00:23:00 -05:00
parent 37f7d99883
commit 7a90b3768a
12 changed files with 116 additions and 50 deletions
+43 -17
View File
@@ -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":