add loading local code

This commit is contained in:
Silver Kuusik 2018-12-29 22:24:37 +01:00
parent 38d8d9f85e
commit 6ab4a23f60

20
main.py
View File

@ -81,7 +81,7 @@ def ws_handler():
elif b'code' in data: elif b'code' in data:
data = ujson.loads(data) data = ujson.loads(data)
data['val'] = data['val'].replace(";;", "\n") data['val'] = data['val'].replace(";;", "\n")
#print("code:", data['val']) #print("main.py code=", data['val'])
ast = compile(data['val'], "snippet", "exec") ast = compile(data['val'], "snippet", "exec")
elif b'stop' in data: elif b'stop' in data:
ast = "" ast = ""
@ -90,12 +90,22 @@ def ws_handler():
sumorobot.terminate = True sumorobot.terminate = True
elif b'calibrate_line' in data: elif b'calibrate_line' in data:
sumorobot.calibrate_line() sumorobot.calibrate_line()
#print('calibrate') #print('main.py: calibrate_line')
elif b'Gone' in data: 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) #conn = uwebsockets.connect(url)
else: 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 # Wait for WiFi to get connected
while not wlan.isconnected(): while not wlan.isconnected():
@ -116,7 +126,5 @@ has_wifi_connection = True
# Indicate that the WebSocket is connected # Indicate that the WebSocket is connected
sumorobot.set_led(STATUS, True) sumorobot.set_led(STATUS, True)
# Start the code processing thread
_thread.start_new_thread(step, ())
# Start the Websocket processing thread # Start the Websocket processing thread
_thread.start_new_thread(ws_handler, ()) _thread.start_new_thread(ws_handler, ())