websocket doesn't work

This commit is contained in:
Mathieu Moran
2023-11-18 14:02:49 -05:00
parent 7a90b3768a
commit f5bbbc00af
9 changed files with 106 additions and 35 deletions
+21 -13
View File
@@ -4,6 +4,7 @@ import app_obd, app_account, time, threading
socketio = SocketIO()
uptime_lock = threading.Lock()
os_type = ["Linux","Windows"]
@socketio.on('connect')
def connect_event():
@@ -12,25 +13,32 @@ def connect_event():
uptime()
def uptime():
thread = Thread(target=loop_uptime)
thread = Thread(target=loop_uptime(os_type[1]))
thread.daemon = True
thread.start()
def loop_uptime():
def loop_uptime(os):
loop = True
uptime_lock.acquire()
try:
loop = True
if os == "Linux":
try:
while loop == True:
boottime = time.clock_gettime(time.CLOCK_BOOTTIME)
# Convert to hours, minutes, and seconds
seconds = int(boottime)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
time.sleep(1)
uptime = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
socketio.emit("rpi_uptime", uptime)
finally:
uptime_lock.release()
elif os == "Windows":
while loop == True:
boottime = time.clock_gettime(time.CLOCK_BOOTTIME)
# Convert to hours, minutes, and seconds
seconds = int(boottime)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
time.sleep(1)
uptime = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
socketio.emit("rpi_uptime", uptime)
finally:
uptime_lock.release()
socketio.emit("rpi_uptime", "1")