From 6ab4a23f60923818b914078269f9dfd77c45bdc0 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Sat, 29 Dec 2018 22:24:37 +0100 Subject: [PATCH] add loading local code --- main.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 44d7270..af07566 100755 --- a/main.py +++ b/main.py @@ -81,7 +81,7 @@ def ws_handler(): elif b'code' in data: data = ujson.loads(data) data['val'] = data['val'].replace(";;", "\n") - #print("code:", data['val']) + #print("main.py code=", data['val']) ast = compile(data['val'], "snippet", "exec") elif b'stop' in data: ast = "" @@ -90,12 +90,22 @@ def ws_handler(): sumorobot.terminate = True elif b'calibrate_line' in data: sumorobot.calibrate_line() - #print('calibrate') + #print('main.py: calibrate_line') elif b'Gone' in data: - print("server said 410 Gone, attempting to reconnect...") + print("main.py: server said 410 Gone, attempting to reconnect...") #conn = uwebsockets.connect(url) else: - print("unknown cmd:", data) + print("main.py: unknown cmd=", data) + +# Try to load the user code +try: + with open("code.py", "r") as code: + ast = compile(code.read(), "snippet", "exec") +except: + print("main.py: error loading code.py file") + +# Start the code processing thread +_thread.start_new_thread(step, ()) # Wait for WiFi to get connected while not wlan.isconnected(): @@ -116,7 +126,5 @@ has_wifi_connection = True # Indicate that the WebSocket is connected sumorobot.set_led(STATUS, True) -# Start the code processing thread -_thread.start_new_thread(step, ()) # Start the Websocket processing thread _thread.start_new_thread(ws_handler, ())