from app import db
from app.models.core import BridgeCommand, DeviceCommand, Bridge, Device
-from ..image_encoding import rle_image
-
+from ..image_encoding import rle_image, rle_html
from .claiming import process_claim_code
import struct
return command
-def set_delivery_and_print_payload(file_id, png_fn):
+def set_delivery_and_print_payload(file_id, png_fn, html=None):
# file ID is a 32 bit ID which is returned as a did_print event
# three parts:
# - command header
command_header = struct.pack("<BBHII", 1, 0, 1, 123, 0)
# get the encoded image now, because we'll need the data later
- pixel_count, encoded_image = rle_image(png_fn)
+ if html:
+ pixel_count, encoded_image = rle_html(html)
+ else:
+ pixel_count, encoded_image = rle_image(png_fn)
# payload header region
printer_control = struct.pack("<13B",
return entire_payload
-def set_delivery_and_print(device_address):
+def set_delivery_and_print(device_address, html=None):
command = DeviceCommand(
device_address=device_address,
- binary_payload=base64.b64encode(set_delivery_and_print_payload(TEST_FILE_ID, TEST_PNG_FN)),
+ binary_payload=base64.b64encode(set_delivery_and_print_payload(TEST_FILE_ID, TEST_PNG_FN, html=html)),
state=u'ready',
deliver_at=datetime.utcnow()
)
from PIL import Image
from itertools import groupby
import struct
+import io
WHITE = 0
BLACK = 1
if remainder > 0:
yield 0
yield remainder
-
+
def rle_image(image_fn):
- # get image pixels
im = Image.open(image_fn)
+ return _rle_image(im)
+
+def _rle_image(im):
+ # get image pixels
im = im.convert('RGBA')
pixels = im.getdata()
# return (number of pixels, output)
return len(pixels), output
+def rle_html(html):
+ from selenium import webdriver
+ driver = webdriver.PhantomJS('phantomjs')
+ driver.set_window_size(384, 800)
+ #driver.get("http://www.google.com")
+
+ driver.get("about:black")
+ html = html.replace('"', '\\"')
+ driver.execute_script("""document.write("%s")""" % html)
+
+ p = driver.get_screenshot_as_png()
+ im = Image.open(io.BytesIO(p))
+ return _rle_image(im)
+
# ~~~~
# Test
# ~~~~
sender = current_app.sender
if form.validate_on_submit():
+ html = """<html><body><h1>Hello, World!</h1><p>My name is Little Printer</p></html>"""
flash('Form submitted')
q = sender.for_device(device)
if q is not None:
- dc = set_delivery_and_print(device.device_address)
+ dc = set_delivery_and_print(device.device_address, html=html)
q.queue_device_command(dc)
return render_template('dump.html', dump=pprint.pformat(device), form=form)