add try catch for code execution

This commit is contained in:
Silver Kuusik 2018-11-13 21:36:20 +01:00
parent 860335df8d
commit 283deea22c

12
main.py
View File

@ -23,7 +23,10 @@ def step():
battery_voltage = sumorobot.get_battery_voltage(), battery_voltage = sumorobot.get_battery_voltage(),
) )
# Execute code # Execute code
exec(ast) try:
exec(ast)
except:
pass
# When robot was stopped # When robot was stopped
if sumorobot.terminate: if sumorobot.terminate:
# Disable forceful termination of delays in code # Disable forceful termination of delays in code
@ -78,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("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 = ""
@ -87,6 +90,7 @@ 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')
elif b'Gone' in data: elif b'Gone' in data:
print("server said 410 Gone, attempting to reconnect...") print("server said 410 Gone, attempting to reconnect...")
#conn = uwebsockets.connect(url) #conn = uwebsockets.connect(url)
@ -98,8 +102,8 @@ while not wlan.isconnected():
sleep_ms(100) sleep_ms(100)
# Connect to the websocket # Connect to the websocket
url = "ws://%s/p2p/sumo-%s/browser/" % (config['sumo_server'], config['sumo_id']) uri = "ws://%s/p2p/sumo-%s/browser/" % (config['sumo_server'], config['sumo_id'])
conn = uwebsockets.connect(url) conn = uwebsockets.connect(uri)
# Set X seconds timeout for socket reads # Set X seconds timeout for socket reads
conn.settimeout(1) conn.settimeout(1)