Handle exception when the data is not correctly formatted or partial (ie : first start at boot time)

This commit is contained in:
Pierrick C 2018-08-04 22:20:19 +02:00
parent 311a36fbdf
commit dda9f3ed7d

View File

@ -68,12 +68,13 @@ mqtt_topic = "feather0"
with serial.Serial(uart_name, uart_baud_rate, timeout=2) as uart: with serial.Serial(uart_name, uart_baud_rate, timeout=2) as uart:
print(uart.name) print(uart.name)
while True: while True:
line = uart.readline() line = uart.readline()
if line != b'': if line != b'':
# If the line is not empty, it's data and they should be formatted as # If the line is not empty, it's data and they should be formatted as
# compact JSON as bytes. Lets decode and load everything ! # compact JSON as bytes. Lets decode and load everything !
try:
data = json.loads(line.decode('utf8')) data = json.loads(line.decode('utf8'))
reception_date = datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S ") reception_date = datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S ")
#print("{} :\n{}".format(reception_date, data)) #print("{} :\n{}".format(reception_date, data))
@ -94,3 +95,5 @@ with serial.Serial(uart_name, uart_baud_rate, timeout=2) as uart:
port= mqtt_port , port= mqtt_port ,
client_id= mqtt_client_id , client_id= mqtt_client_id ,
auth= mqtt_auth ) auth= mqtt_auth )
except:
print("Bad data:\n{}".format(line))