From 9c640e59daf503e5fe6b8ae3e72a577125b3af80 Mon Sep 17 00:00:00 2001 From: Matt Webb Date: Mon, 22 Sep 2014 11:20:40 +0100 Subject: [PATCH] some possible fixes to device command encoding --- app/__init__.py | 23 ++++++++++++++--------- app/commands.py | 24 ++++++++++++------------ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 7fa4701..e800423 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -39,6 +39,7 @@ def create_app(config_name): def echo_socket(ws): print "here" done_once = False + counting = 0 while True: message = ws.receive() pprint(message) @@ -51,15 +52,19 @@ def create_app(config_name): print "==> encryption_key_required" j = commands.add_device_encryption_key().to_json() ws.send(j) - gevent.sleep(0.5) - print "===> %s" % j - j = commands.set_delivery_and_print().to_json() - ws.send(j) - if event['type'] == 'BridgeEvent' and done_once == False: - j = commands.set_delivery_and_print().to_json() - ws.send(j) - print "===> tried to send image" - done_once = True + #gevent.sleep(0.5) + #print "===> %s" % j + #j = commands.set_delivery_and_print().to_json() + #ws.send(j) + if event['type'] == 'DeviceEvent': + counting += 1 + if counting == 10: + gevent.sleep(0.5) + j = commands.set_delivery_and_print().to_json() + ws.send(j) + print "===> tried to send image" + pprint(j) + done_once = True except: pass diff --git a/app/commands.py b/app/commands.py index c176262..b2b53e7 100644 --- a/app/commands.py +++ b/app/commands.py @@ -54,7 +54,7 @@ def set_delivery_and_print_payload(file_id, png_fn): # file ID is a 32 bit ID which is returned as a did_print event # three parts: # - command header - # - >cxHL + # - >cxHLL # - payload header # - length >L # - payload @@ -68,31 +68,31 @@ def set_delivery_and_print_payload(file_id, png_fn): # - encode_image_region # device type >c, reserved byte >c, - # command_name >H (short), file_id >L (long) - command_header = struct.pack(">cxHL", '\x01', 1, 1) + # command_name >H (short), file_id >L (long), + # unimplemented CRC (long is zero) + command_header = struct.pack(">BBHLL", 1, 0, 1, 1, 0) # get the encoded image now, because we'll need the data later pixel_count, encoded_image = rle_image(png_fn) # payload header region - printer_control = struct.pack("xL", len(printer_control) + len(printer_data)) + printer_data = struct.pack("<8B", 0x1b, 0x2a, n1, n2, n3, 0, 0, 48) + header_region = struct.pack(">BL", 0, len(printer_control) + len(printer_data)) header_region += printer_control + printer_data # payload including the header - payload = struct.pack(">Lx", len(header_region) + len(encoded_image) + 1) + payload = struct.pack(">LB", len(header_region) + len(encoded_image) + 1, 0) payload += header_region + encoded_image entire_payload = command_header + struct.pack(">L", len(payload)) + payload -- 2.47.3