From c4d2d7c317ea7b87598cf6584f7c413f4241b571 Mon Sep 17 00:00:00 2001 From: tcsenpai Date: Sun, 31 Mar 2024 15:19:21 +0200 Subject: [PATCH] fixed imports on key + added loader --- libs/emesh.py | 16 ++++++++++------ libs/loading.py | 30 ++++++++++++++++++++++++++++++ term.py | 13 +++++++++++-- 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 libs/loading.py diff --git a/libs/emesh.py b/libs/emesh.py index e192d61..fcd4404 100755 --- a/libs/emesh.py +++ b/libs/emesh.py @@ -4,7 +4,7 @@ from pubsub import pub import time # Helpers from hashlib import sha256 -import keys +import libs.keys as keys import json serial_port = None @@ -95,11 +95,15 @@ def connect(serialPort=None): keys.ensure() # Connecting to the radio serial_port = serialPort - pub.subscribe(onReceive, "meshtastic.receive") - pub.subscribe(onConnection, "meshtastic.connection.established") - interface = meshtastic.serial_interface.SerialInterface(serial_port) - print("[INITIALIZATION] Connection to radio established") - + try: + pub.subscribe(onReceive, "meshtastic.receive") + pub.subscribe(onConnection, "meshtastic.connection.established") + interface = meshtastic.serial_interface.SerialInterface(serial_port) + print("[INITIALIZATION] Connection to radio established") + except Exception as e: + print("[INITIALIZATION] Could not connect to radio") + print("[INITIALIZATION] " + str(e)) + return False def listSerials(): # TODO diff --git a/libs/loading.py b/libs/loading.py new file mode 100644 index 0000000..e965a8e --- /dev/null +++ b/libs/loading.py @@ -0,0 +1,30 @@ +import itertools +import threading +import time +import sys + +done = True +message = "" +#here is the animation +def animate(): + global done + global message + for c in itertools.cycle(['|', '/', '-', '\\']): + if done: + break + sys.stdout.write('\r' + message + ' ' + c) + sys.stdout.flush() + time.sleep(0.1) + sys.stdout.write('\rDone! \n') + +def start(msg="loading..."): + global done + global message + done = False + message = msg + t = threading.Thread(target=animate) + t.start() + +def stop(): + global done + done = True diff --git a/term.py b/term.py index e4db0ba..8943738 100755 --- a/term.py +++ b/term.py @@ -3,6 +3,7 @@ import builtins as __builtin__ import time import os from dotenv import load_dotenv +import libs.loading as loading # SECTION GUI Variables outputs = "" @@ -28,7 +29,12 @@ def print(*args, **kwargs): def init(): print("[SYSTEM] Starting EMesh...") vars = preparse() - emesh.connect(vars["port"]) + loading.start("Initializing EMesh...") + if not emesh.connect(vars["port"]): + print("[SYSTEM] Could not connect to the device. Exiting...") + loading.stop() + exit() + loading.stop() print("[LOADER] Initialized") @@ -53,6 +59,7 @@ def main(): print("[MAIN CYCLE] Starting watchdog...") was_connected = False cooldownHeader = False + loading.start("[ eMesh Main Cycle is Running ") while not ((os.getenv("FORCE_QUIT") == "True") or forceQuit): # This is just a way to check if we need to notify the gui are_connected = emesh.connected @@ -95,7 +102,9 @@ def main(): # print("[MAIN CYCLE] Sleeping for " + os.getenv('SLEEP_INTERVAL') + " seconds") time.sleep(int(os.getenv("SLEEP_INTERVAL"))) # print("[MAIN CYCLE] Sleeping complete. Proceeding to the next cycle...") - + print("[MAIN CYCLE] Exiting main cycle...") + print("[SYSTEM] Exiting...") + loading.stop() print("[SYSTEM] Ready to start.")