44import aiohttp
55import asyncio
66import config
7+ import serial
78
89import signal
910import sys
1718byte_length = 0
1819properties = []
1920frontend_data = {}
20- solar_car_connection = {'lte' : False , 'udp' : False }
21+ solar_car_connection = {'lte' : False , 'udp' : False , 'serial' : False }
2122# Convert dataformat to format string for struct conversion
2223# Docs: https://docs.python.org/3/library/struct.html
2324types = {'bool' : '?' , 'float' : 'f' , 'char' : 'c' , 'uint8' : 'B' , 'uint16' : 'H' , 'uint64' : 'Q' }
25+ serial_port = {"device" : "" , 'baud' : 115200 } # shared object with core_api for setting serial device from frontend
2426
2527def set_format (file_path : str ):
2628 global format_string , byte_length , properties
@@ -45,7 +47,7 @@ def unpack_data(data):
4547
4648
4749class Telemetry :
48- __tmp_data = {'tcp' : b'' , 'lte' : b'' , 'udp' : b'' , 'file_sync' : b'' }
50+ __tmp_data = {'tcp' : b'' , 'lte' : b'' , 'udp' : b'' , 'file_sync' : b'' , 'serial' : b'' }
4951 latest_tstamp = 0
5052
5153 def listen_udp (self , port : int ):
@@ -140,6 +142,56 @@ def listen_tcp(self, server_addr: str, port: int):
140142 solar_car_connection ['tcp' ] = False
141143 break
142144
145+ def serial_read (self ):
146+ global frontend_data , serial_port
147+ latest_tstamp = 0
148+ while True :
149+ curr_device = serial_port ['device' ]
150+ curr_baud = serial_port ['baud' ]
151+ print ('serial' , curr_device )
152+ if (curr_device ):
153+ # Establish a serial connection)
154+ ser = serial .Serial (curr_device , curr_baud )
155+ # if device has been updated then exit loop and connect to new device
156+ while curr_device == serial_port ['device' ]:
157+ print ('read serial' )
158+ # Read data from serial port
159+ try :
160+ data = b''
161+ if (ser .in_waiting > 0 ):
162+ data = ser .read (ser .in_waiting )
163+ else :
164+ time .sleep (0.1 )
165+ if not data :
166+ # No data received, continue listening
167+ continue
168+ print ('read data' )
169+ print ('data:' , data )
170+ packets = self .parse_packets (data , 'serial' )
171+ for packet in packets :
172+ if len (packet ) == byte_length :
173+ d = unpack_data (packet )
174+ latest_tstamp = time .time ()
175+ try :
176+ frontend_data = d .copy ()
177+ db .insert_data (d )
178+ except Exception as e :
179+ print (traceback .format_exc ())
180+ continue
181+ solar_car_connection ['serial' ] = True
182+ if time .time () - latest_tstamp / 1000 > 5 :
183+ solar_car_connection ['lte' ] = False
184+ break
185+ except Exception :
186+ print (traceback .format_exc ())
187+ solar_car_connection ['serial' ] = False
188+ serial_port ['device' ] = ""
189+ break
190+ else :
191+ solar_car_connection ['serial' ] = False
192+ # wait before retry
193+ time .sleep (1 )
194+
143195 async def fetch (self , session , url ):
144196 try :
145197 async with session .get (url , timeout = 2 ) as response :
@@ -257,11 +309,12 @@ def sigint_handler(signal, frame):
257309def start_comms ():
258310 # start file sync
259311 p .start ()
260-
261-
312+
262313 # Start two live comm channels
263314 vps_thread = threading .Thread (target = lambda : asyncio .run (telemetry .remote_db_fetch (config .VPS_URL )))
264315 vps_thread .start ()
265- socket_thread = threading .Thread (target = lambda : telemetry .listen_udp (config .UDP_PORT ))
316+ #socket_thread = threading.Thread(target=lambda: telemetry.listen_udp(config.UDP_PORT))
317+ #socket_thread.start()
318+ socket_thread = threading .Thread (target = lambda : telemetry .serial_read ())
266319 socket_thread .start ()
267320
0 commit comments