reading tags is done, tag is read and contents are saved to json with lap times
This commit is contained in:
parent
e123b57165
commit
a2ddbb575e
@ -1,4 +1,59 @@
|
|||||||
import serial
|
import serial
|
||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
JSON_PATH = './Alkator.json'
|
||||||
|
|
||||||
|
data_dict = ''
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(JSON_PATH, 'r') as file:
|
||||||
|
file_content = file.read()
|
||||||
|
data_dict = json.loads(file_content)
|
||||||
|
print('json_exists')
|
||||||
|
except:
|
||||||
|
data_dict = {}
|
||||||
|
|
||||||
|
def get_seconds():
|
||||||
|
current_time = datetime.now()
|
||||||
|
hours = current_time.hour
|
||||||
|
mins = current_time.minute
|
||||||
|
secs = current_time.second
|
||||||
|
us = current_time.microsecond
|
||||||
|
seconds = hours*3600 + mins*60 + secs
|
||||||
|
seconds = float(str(seconds) + '.' + str(us))
|
||||||
|
return seconds
|
||||||
|
|
||||||
|
def read(ser):
|
||||||
|
while True:
|
||||||
|
mes = ser.read_until(b'\r')
|
||||||
|
if b'Reading ntag' in mes:
|
||||||
|
print('reading')
|
||||||
|
if b'Data' in mes:
|
||||||
|
data = mes.decode('utf-8')
|
||||||
|
data = data[6:].replace('\x00', '').replace('\r', '').split(' ')
|
||||||
|
print('data read done')
|
||||||
|
try:
|
||||||
|
lap_secs = get_seconds() - data_dict[data[0]]['time']
|
||||||
|
print(lap_secs)
|
||||||
|
lap = data_dict[data[0]]['laps']
|
||||||
|
try:
|
||||||
|
data_dict[data[0]]['lap_time'][lap] = lap_secs
|
||||||
|
except:
|
||||||
|
data_dict[data[0]]['lap_time'] = {lap: lap_secs}
|
||||||
|
lap += 1
|
||||||
|
data_dict[data[0]]['laps'] = lap
|
||||||
|
data_dict[data[0]]['time'] = get_seconds()
|
||||||
|
|
||||||
|
except:
|
||||||
|
data_dict[data[0]] = {'name': data[1]}
|
||||||
|
data_dict[data[0]]['time'] = get_seconds()
|
||||||
|
data_dict[data[0]]['laps'] = 0
|
||||||
|
|
||||||
|
data_json = json.dumps(data_dict, indent=4)
|
||||||
|
print(data_json)
|
||||||
|
with open(JSON_PATH, 'w') as file:
|
||||||
|
file.write(data_json)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
s = serial.Serial(port="/dev/ttyACM0", parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1)
|
s = serial.Serial(port="/dev/ttyACM0", parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1)
|
||||||
@ -14,15 +69,11 @@ def main():
|
|||||||
s.write(data.encode())
|
s.write(data.encode())
|
||||||
elif rw == 'r':
|
elif rw == 'r':
|
||||||
s.write('r\r'.encode())
|
s.write('r\r'.encode())
|
||||||
while True:
|
read(s)
|
||||||
mes = s.read_until(b'\r')
|
|
||||||
print(mes.decode())
|
|
||||||
elif rw == '':
|
elif rw == '':
|
||||||
s.write('r\r'.encode())
|
s.write('r\r'.encode())
|
||||||
while True:
|
read(s)
|
||||||
mes = s.read_until(b'\r')
|
|
||||||
if mes != '':
|
|
||||||
print(mes.decode())
|
|
||||||
else:
|
else:
|
||||||
print('Invalid option')
|
print('Invalid option')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user