Skip to content

Commit

Permalink
added debug logging to break up the data protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
bskrt committed Jan 30, 2016
1 parent 0180972 commit 596fc37
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
19 changes: 19 additions & 0 deletions sirius/coding/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
"""
import struct
import base64
import logging

from sirius.coding import image_encoding
from sirius.coding import claiming
from sirius.protocol import messages

logger = logging.getLogger(__name__)

__all__ = [
'encode_bridge_command',
]


def _encode_pixels(pixel_count, rle_image):
logger.debug("PIXEL COUNT: %s", pixel_count)

"""
:param pixel_count: total number of pixels
:param rle_image: Run-length encoded black and white image.
Expand All @@ -30,12 +35,21 @@ def _encode_pixels(pixel_count, rle_image):
n2, n1 = divmod(n3_remainder, 256)
printer_data = struct.pack("<8B", 0x1b, 0x2a, n1, n2, n3, 0, 0, 48)
header_region = struct.pack("<BI", 0, len(printer_control) + len(printer_data))
logger.debug("HEADER REGION: %s", header_region.encode('hex'))
logger.debug("PRINTER CONTROL: %s", printer_control.encode('hex'))
logger.debug("PRINTER DATA: %s", printer_data.encode('hex'))
header_region += printer_control + printer_data

# payload including the header
payload = struct.pack("<IB", len(header_region) + len(rle_image) + 1, 0)
logger.debug("PAYLOAD BASE: %s", payload.encode('hex'))
logger.debug("IMAGE: %s", rle_image.encode('hex'))

payload += header_region + rle_image

logger.debug("FULL PAYLOAD: %s", payload.encode('hex'))


return payload


Expand All @@ -60,6 +74,8 @@ def _encode_printer_message(command, payload, print_id):
)
entire_payload = command_header + struct.pack("<I", len(payload)) + payload

logger.debug("ENTIRE PAYLOAD: %s", entire_payload.encode('hex'))

return entire_payload


Expand Down Expand Up @@ -102,6 +118,9 @@ def make(extra):
})

elif type(command) == messages.SetDeliveryAndPrint:
# bin_payload = base64.b64encode(_encode_printer_message(0x1, _payload_from_pixels(command.pixels), command_id))
# logger.debug("payload base64: %s", bin_payload)

return make({
'binary_payload': base64.b64encode(_encode_printer_message(
0x1, _payload_from_pixels(command.pixels), command_id)),
Expand Down
10 changes: 10 additions & 0 deletions sirius/coding/image_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import struct
import io
import tempfile
import logging

logger = logging.getLogger(__name__)

WHITE = '\xff'
BLACK = '\x00'
Expand Down Expand Up @@ -74,6 +77,8 @@ def rle_from_bw(bw_image):
bw_image = bw_image.transpose(Image.ROTATE_180)
pixels = list(bw_image.getdata())

logger.debug("pixeldata: %s", pixels)

# Group each run length into lists each list is (result of
# identity function, [actual pixels])
grouped = groupby(pixels)
Expand All @@ -83,6 +88,7 @@ def rle_from_bw(bw_image):
for k, g in grouped:
groups.append((k, ilen(g)))

logger.debug("RLE list: %s", groups)
# the decoder assumes that the first run is white, so if the
# first pixel is black, add a zero run of white
if groups[0][0] == BLACK:
Expand All @@ -93,10 +99,14 @@ def rle_from_bw(bw_image):
x = bytearray(rle(lengths))
compressed_data = struct.pack("<%dB" % len(x), *x)

logger.debug("RLE compressed data: %s", compressed_data.encode('hex'))

# package up with length as header
# first byte is compressed type, always 1
output = struct.pack("<BL", 0x01, len(compressed_data)) + compressed_data

logger.debug("RLE final output: %s", compressed_data.encode('hex'))

# return (number of pixels, output)
return len(pixels), output

Expand Down
9 changes: 7 additions & 2 deletions sirius/web/printer_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import flask_wtf
import wtforms
import base64
import logging

from sirius.models.db import db
from sirius.models import hardware
Expand All @@ -15,6 +16,7 @@
from sirius.coding import templating
from sirius import stats

logger = logging.getLogger(__name__)

blueprint = flask.Blueprint('printer_print', __name__)

Expand All @@ -25,9 +27,12 @@ class PrintForm(flask_wtf.Form):
coerce=int,
validators=[wtforms.validators.DataRequired()],
)
# message = wtforms.TextAreaField(
# 'Message',
# validators=[wtforms.validators.DataRequired()],
# )
message = wtforms.TextAreaField(
'Message',
validators=[wtforms.validators.DataRequired()],
)


Expand Down Expand Up @@ -70,7 +75,7 @@ def printer_print(printer_id):
# TODO: use templating to avoid injection attacks
pixels = image_encoding.default_pipeline(
templating.default_template(form.message.data))
hardware_message = messages.SetDeliveryAndPrint(
hardware_message = messages.SetDeliveryAndPrintNoFace(
device_address=printer.device_address,
pixels=pixels,
)
Expand Down

0 comments on commit 596fc37

Please sign in to comment.