Skip to content

Commit d072981

Browse files
author
Thomas Salm
committed
to version 1.6.3
1 parent 70680d6 commit d072981

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "saic_ismart_client"
7-
version = "1.6.2"
7+
version = "1.6.3"
88
authors = [
99
{ name = "Thomas Salm", email="[email protected]"},
1010
]

src/saic_ismart_client/common_model.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def init_from_dict(self, data: dict):
196196
if FIELD_RESULT in data:
197197
self.result = data.get(FIELD_RESULT)
198198
if FIELD_ERROR_MESSAGE in data:
199-
self.error_message = data.get(FIELD_ERROR_MESSAGE)
199+
self.error_message = data.get(FIELD_ERROR_MESSAGE).decode()
200200

201201

202202
class MessageBodyV1(AbstractMessageBody):
@@ -338,6 +338,20 @@ def get_application_data_bytes(self, application_data: ApplicationData, asn1_too
338338
application_data_bytes = bytes()
339339
return application_data_bytes
340340

341+
@staticmethod
342+
def validate_dispatcher_message_size(dispatcher_message_size, netto_message_size):
343+
# The API sometimes provides a wrong dispatcher_message_length value
344+
# Normally the body size is about 120 bytes
345+
# A value below 50 is very unlikely.
346+
if dispatcher_message_size > 50:
347+
dispatcher_message_bytes_to_read = dispatcher_message_size
348+
else:
349+
LOG.debug(f'Calculated message size {int(netto_message_size)} does not match '
350+
+ f'with header size information {dispatcher_message_size}. Using calculated size.')
351+
# This will fail if the message contains application data. In this case we cannot tell the body size
352+
dispatcher_message_bytes_to_read = int(netto_message_size)
353+
return dispatcher_message_bytes_to_read
354+
341355

342356
class MessageCoderV1(AbstractMessageCoder):
343357
def __init__(self, asn_files_dir: str):
@@ -378,16 +392,28 @@ def encode_request(self, message: MessageV1) -> str:
378392
return result.upper()
379393

380394
def decode_response(self, message: str, decoded_message: MessageV1) -> None:
395+
LOG.debug(f'Message length in bytes: {len(message[5:]) / 2}')
381396
buffered_message_bytes = io.BytesIO(bytes.fromhex(message[5:]))
382397

398+
header = decoded_message.header
383399
header_bytes = buffered_message_bytes.read(self.header_length)
384-
decoded_message.header.protocol_version = int(header_bytes[0])
385-
decoded_message.header.security_context = int(header_bytes[1])
386-
decoded_message.header.dispatcher_message_length = int(header_bytes[2])
387-
decoded_message.header.dispatcher_body_encoding = int(header_bytes[3])
400+
header.protocol_version = int(header_bytes[0])
401+
LOG.debug(f'Protocol version: {header.protocol_version}')
402+
header.security_context = int(header_bytes[1])
403+
header.dispatcher_message_length = int(header_bytes[2])
404+
LOG.debug(f'Dispatcher message length: {header.dispatcher_message_length}')
405+
header.dispatcher_body_encoding = int(header_bytes[3])
388406

389-
dispatcher_message_bytes = buffered_message_bytes.read(
390-
decoded_message.header.dispatcher_message_length - self.header_length)
407+
netto_message_size = len(message[5:]) / 2 - self.header_length
408+
LOG.debug(f'Message size without header: {netto_message_size}')
409+
410+
dispatcher_message_size = header.dispatcher_message_length -self.header_length
411+
LOG.debug(f'Dispatcher message bytes: {dispatcher_message_size}')
412+
413+
dispatcher_message_bytes_to_read = AbstractMessageCoder.validate_dispatcher_message_size(
414+
dispatcher_message_size, netto_message_size)
415+
416+
dispatcher_message_bytes = buffered_message_bytes.read(dispatcher_message_bytes_to_read)
391417
message_body = decoded_message.body
392418
message_body_dict = self.asn1_tool_uper.decode(message_body.asn_type, dispatcher_message_bytes)
393419
message_body.init_from_dict(message_body_dict)
@@ -477,16 +503,8 @@ def decode_response(self, message: str, decoded_message: MessageV2) -> None:
477503
dispatcher_message_size = header.dispatcher_message_length - self.header_length
478504
LOG.debug(f'Dispatcher message bytes: {dispatcher_message_size}')
479505

480-
# The API sometimes provides a wrong dispatcher_message_length value
481-
# Normally the body size is about 120 bytes
482-
# A value below 50 is very unlikely.
483-
if dispatcher_message_size > 50:
484-
dispatcher_message_bytes_to_read = dispatcher_message_size
485-
else:
486-
LOG.debug(f'Calculated message size {int(netto_message_size)} does not match '
487-
+ f'with header size information {dispatcher_message_size}. Using calculated size.')
488-
# This will fail if the message contains application data. In this case we cannot tell the body size
489-
dispatcher_message_bytes_to_read = int(netto_message_size)
506+
dispatcher_message_bytes_to_read = AbstractMessageCoder.validate_dispatcher_message_size(
507+
dispatcher_message_size, netto_message_size)
490508

491509
dispatcher_message_bytes = buffered_message_bytes.read(dispatcher_message_bytes_to_read)
492510
message_body_dict = self.asn1_tool_uper.decode('MPDispatcherBody', dispatcher_message_bytes)

src/saic_ismart_client/saic_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def login(self) -> MessageV11:
119119
self.message_v1_1_coder.decode_response(login_response_hex, login_response_message)
120120
self.publish_json_response(application_id, application_data_protocol_version, login_response_message.get_data())
121121
if login_response_message.body.error_message is not None:
122-
raise SaicApiException(login_response_message.body.error_message.decode(),
122+
raise SaicApiException(login_response_message.body.error_message,
123123
login_response_message.body.result)
124124
else:
125125
self.uid = login_response_message.body.uid
@@ -164,7 +164,7 @@ def set_alarm_switches(self, alarm_switches: list, pin: str = None) -> None:
164164
alarm_switch_response_message.get_data())
165165

166166
if alarm_switch_response_message.body.error_message is not None:
167-
raise SaicApiException(alarm_switch_response_message.body.error_message.decode(),
167+
raise SaicApiException(alarm_switch_response_message.body.error_message,
168168
alarm_switch_response_message.body.result)
169169

170170
def get_vehicle_status(self, vin_info: VinInfo, event_id: str = None) -> MessageV2:
@@ -479,7 +479,7 @@ def __handle_retry_without_app_data(self, func, vin_info: VinInfo, max_retries:
479479

480480
retry += 1
481481
if rsp_msg.body.error_message is not None:
482-
raise SaicApiException(rsp_msg.body.error_message.decode(),
482+
raise SaicApiException(rsp_msg.body.error_message,
483483
rsp_msg.body.result)
484484
return rsp_msg
485485

@@ -783,7 +783,7 @@ def __change_message_status(self, message_id: int | None, action_type: str, even
783783
self.message_v1_1_coder.decode_response(message_delete_rsp_hex, message_delete_rsp_msg)
784784
self.publish_json_response(application_id, application_protocol_version, message_delete_rsp_msg.get_data())
785785
if message_delete_rsp_msg.body.error_message is not None:
786-
raise SaicApiException(message_delete_rsp_msg.body.error_message.decode(),
786+
raise SaicApiException(message_delete_rsp_msg.body.error_message,
787787
message_delete_rsp_msg.body.result)
788788

789789
def publish_raw_value(self, key: str, raw: str):
@@ -854,7 +854,7 @@ def handle_error(self, message_body: AbstractMessageBody, iteration: int):
854854
waiting_time = AVG_SMS_DELIVERY_TIME
855855
message = f'application ID: {message_body.application_id},' \
856856
+ f' protocol version: {message_body.application_data_protocol_version},' \
857-
+ f' message: {message_body.error_message.decode()}' \
857+
+ f' message: {message_body.error_message}' \
858858
+ f' result code: {message_body.result}'
859859

860860
if message_body.result == 2:
@@ -877,7 +877,7 @@ def handle_error(self, message_body: AbstractMessageBody, iteration: int):
877877
LOG.warning(message)
878878
else:
879879
LOG.error(message)
880-
raise SaicApiException(message_body.error_message.decode(), message_body.result)
880+
raise SaicApiException(message_body.error_message, message_body.result)
881881

882882

883883
def bool_to_bit(flag):

0 commit comments

Comments
 (0)