@@ -157,15 +157,29 @@ async def handle_client(self, reader, writer):
157157 if tcu_info .get ("tcu_id" , None ) != car .tcu_model :
158158 writer .write (create_charge_status_response (False ))
159159 await writer .drain ()
160- raise CommandError ("TCU ID Mismatch" )
161- if tcu_info .get ("unit_id" , None ) != car .tcu_serial :
160+ new_alert = AlertHistory ()
161+ new_alert .type = 99
162+ new_alert .additional_data = _ ("TCU ID does not match with specified ID, please double check!" )
163+ new_alert .car = car
164+ new_alert .command_id = car .command_id
165+ await sync_to_async (new_alert .save )()
166+ elif tcu_info .get ("unit_id" , None ) != car .tcu_serial :
162167 writer .write (create_charge_status_response (False ))
163168 await writer .drain ()
164- raise CommandError ("TCU Unit ID Mismatch" )
165- if tcu_info .get ("iccid" , None ) != car .iccid :
169+ new_alert = AlertHistory ()
170+ new_alert .type = 99
171+ new_alert .additional_data = _ ("Navi ID does not match with specified ID, please double check!" )
172+ new_alert .car = car
173+ new_alert .command_id = car .command_id
174+ await sync_to_async (new_alert .save )()
175+ elif tcu_info .get ("iccid" , None ) != car .iccid :
166176 writer .write (create_charge_status_response (False ))
167177 await writer .drain ()
168- raise CommandError ("TCU ICCID Mismatch" )
178+ new_alert = AlertHistory ()
179+ new_alert .type = 99
180+ new_alert .additional_data = _ ("Sim ID does not match with specified ID, please double check!" )
181+ new_alert .car = car
182+ new_alert .command_id = car .command_id
169183
170184 # skip auth and set as authenticated if check is disabled
171185 authenticated = car .disable_auth
@@ -175,10 +189,15 @@ async def handle_client(self, reader, writer):
175189 auth_data = parsed_data .get ("auth" , None )
176190
177191 if auth_data is None :
178- authenticated = False
179192 writer .write (create_charge_status_response (False ))
180193 await writer .drain ()
181- raise CommandError ("TCU auth missing" )
194+ new_alert = AlertHistory ()
195+ new_alert .type = 99
196+ new_alert .additional_data = _ (
197+ "Authentication failed, username or password is missing! Please sign in using navigation unit." )
198+ new_alert .car = car
199+ new_alert .command_id = car .command_id
200+ await sync_to_async (new_alert .save )()
182201
183202 username = auth_data ["user" ]
184203 password_hash = auth_data ["pass" ]
@@ -188,10 +207,15 @@ async def handle_client(self, reader, writer):
188207 if username == car_owner .username or password_hash == car_owner .tcu_pass_hash :
189208 authenticated = True
190209 else :
191- authenticated = False
192210 writer .write (create_charge_status_response (False ))
193211 await writer .drain ()
194- raise CommandError ("TCU auth mismatch" )
212+ new_alert = AlertHistory ()
213+ new_alert .type = 99
214+ new_alert .additional_data = _ (
215+ "Authentication failed, username or password is incorrect! Please sign in using navigation unit." )
216+ new_alert .car = car
217+ new_alert .command_id = car .command_id
218+ await sync_to_async (new_alert .save )()
195219
196220 car .last_connection = timezone .now ()
197221
@@ -205,14 +229,19 @@ async def handle_client(self, reader, writer):
205229 await writer .drain ()
206230 raise CommandError ("No car found" )
207231
232+ if not authenticated :
233+ car .command_result = 1
234+ car .command_requested = False
235+ await sync_to_async (car .save )()
236+ await writer .drain ()
237+ return
238+
208239 if parsed_data .get ("gps" , None ) is not None :
209240 logger .info (f"GPS Data: { parsed_data ['gps' ]} " )
210241 await set_gpsinfo (car , parsed_data ["gps" ])
211242
212243 if parsed_data ["message_type" ][0 ] == 1 :
213244 logger .info (f"Auth Data: { parsed_data ['auth' ]} " )
214- with open (f"datalog-msgtype1-{ car .command_id } .bin" , "wb" ) as file :
215- file .write (data )
216245 if car .command_requested and car .command_result == - 1 :
217246 logger .info (f"Command found: { car .command_id } { car .command_requested } { car .command_type } { car .command_payload } { car .command_request_time } " )
218247 car .command_result = 3
@@ -241,9 +270,6 @@ async def handle_client(self, reader, writer):
241270 writer .write (create_charge_status_response (False ))
242271 elif parsed_data ["message_type" ][0 ] == 3 :
243272 logger .info (f"Auth Data: { parsed_data ['auth' ]} " )
244- dtnow = timezone .now ().strftime ("%Y-%m-%dT%H:%M:%S" )
245- with open (f"datalog-msgtype3-{ car .command_id } -{ dtnow } .bin" , "wb" ) as file :
246- file .write (data )
247273 body_type = parsed_data ["body_type" ]
248274 logger .info (f"Body Type: { body_type } " )
249275
@@ -335,20 +361,16 @@ async def handle_client(self, reader, writer):
335361 # TODO: capture resultstate to determine battery heater status
336362 logger .warning ("Battery heat! Resultstate: %d, alertstate: %d" , req_body ["resultstate" ], req_body ["alertstate" ])
337363 new_alert = AlertHistory ()
338- new_alert .type = 9
364+ new_alert .type = 9 if req_body . get ( 'batt_heat_active' , False ) else 10
339365 new_alert .car = car
340366 new_alert .command_id = car .command_id
341367 await sync_to_async (new_alert .save )()
342368 await send_vehicle_alert_notification (
343369 car ,
344370 _ ("Battery heater notification" ),
345- _ ("Battery heater notification " )
371+ _ ("Battery heater has turned on" ) if req_body . get ( 'batt_heat_active' , False ) else _ ( "Battery heater has turned off " )
346372 )
347373 elif parsed_data ["message_type" ][0 ] == 5 :
348- if not authenticated :
349- break
350- with open (f"datalog-msgtype5-{ car .command_id } .bin" , "wb" ) as file :
351- file .write (data )
352374 car .command_result = 0
353375
354376 new_alert = AlertHistory ()
0 commit comments