From bd2ab3210ac461e6067a32d91f02bc4f9b0ebfc6 Mon Sep 17 00:00:00 2001 From: Tim Brewis Date: Tue, 7 Feb 2023 21:59:02 +0000 Subject: [PATCH] feat: sort PDUs by CAN message priority --- tsgen/__main__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tsgen/__main__.py b/tsgen/__main__.py index 74eea71..0b950c0 100644 --- a/tsgen/__main__.py +++ b/tsgen/__main__.py @@ -220,7 +220,7 @@ def generate_intermediate_server(self): self._sensor_schema = {} pdus = {} - for id, message in enumerate(self._dbc.messages): + for id, message in enumerate(self.sort_dbc_messages(self._dbc.messages)): pdus[message.name] = self.create_pdu(message, id) @@ -241,6 +241,13 @@ def save_output(file_name, json_data): save_output(self.SENSORS_FILE_NAME, self._sensor_schema) save_output(self.SCHEMA_FILE_NAME, self._telemetry_schema) + def sort_dbc_messages(self, + messages: list[cantools.database.can.Message]): + """Returns a the messages in a DBC, sorted by CAN ID with lowest CAN IDs + first + """ + return sorted(messages, key=lambda x: x.frame_id) + def create_pdu(self, message: cantools.database.can.Message, id: int) -> dict: