aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/msgproto.py
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-03-04 19:50:35 -0500
committerKevinOConnor <kevin@koconnor.net>2019-03-17 19:38:18 -0400
commit7eda55e2b042566020ade34d7b2cdb9296f37f1d (patch)
tree3fe33547e2d8d385effbb4ce33f9426459fd91e3 /klippy/msgproto.py
parentb9b03dd0826f7212e78034bf522eca64de4a258e (diff)
downloadkutter-7eda55e2b042566020ade34d7b2cdb9296f37f1d.tar.gz
kutter-7eda55e2b042566020ade34d7b2cdb9296f37f1d.tar.xz
kutter-7eda55e2b042566020ade34d7b2cdb9296f37f1d.zip
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 <kevin@koconnor.net>
Diffstat (limited to 'klippy/msgproto.py')
-rw-r--r--klippy/msgproto.py21
1 files changed, 12 insertions, 9 deletions
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', {}))