From 7eda55e2b042566020ade34d7b2cdb9296f37f1d Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 4 Mar 2019 19:50:35 -0500 Subject: buildcommands: Use dictionaries to describe commands, responses, and output Avoid transmitting lists of message ids for commands and responses - gzip doesn't do a good job of compressing them. Signed-off-by: Kevin O'Connor --- klippy/msgproto.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'klippy/msgproto.py') diff --git a/klippy/msgproto.py b/klippy/msgproto.py index 5feb350d..6bb5c159 100644 --- a/klippy/msgproto.py +++ b/klippy/msgproto.py @@ -6,8 +6,8 @@ import json, zlib, logging DefaultMessages = { - 0: "identify_response offset=%u data=%.*s", - 1: "identify offset=%u count=%c", + "identify_response offset=%u data=%.*s": 0, + "identify offset=%u count=%c": 1, } MESSAGE_MIN = 5 @@ -190,7 +190,7 @@ class MessageParser: self.config = {} self.version = self.build_versions = "" self.raw_identify_data = "" - self._init_messages(DefaultMessages, DefaultMessages.keys()) + self._init_messages(DefaultMessages) def check_packet(self, s): if len(s) < MESSAGE_MIN: return 0 @@ -296,10 +296,10 @@ class MessageParser: #traceback.print_exc() raise error("Unable to encode: %s" % (msgname,)) return cmd - def _init_messages(self, messages, parsers): - for msgid, msgformat in messages.items(): + def _init_messages(self, messages, output_ids=[]): + for msgformat, msgid in messages.items(): msgid = int(msgid) - if msgid not in parsers: + if msgid in output_ids: self.messages_by_id[msgid] = OutputFormat(msgid, msgformat) continue msg = MessageFormat(msgid, msgformat) @@ -311,11 +311,14 @@ class MessageParser: data = zlib.decompress(data) self.raw_identify_data = data data = json.loads(data) - messages = data.get('messages') commands = data.get('commands') - self.command_ids = commands responses = data.get('responses') - self._init_messages(messages, commands+responses) + output = data.get('output', {}) + all_messages = dict(commands) + all_messages.update(responses) + all_messages.update(output) + self.command_ids = sorted(commands.values()) + self._init_messages(all_messages, output.values()) static_strings = data.get('static_strings', {}) self.static_strings = {int(k): v for k, v in static_strings.items()} self.config.update(data.get('config', {})) -- cgit v1.2.3-70-g09d2