def randomly_change_personality(): # Restrict to debug for now until we have a proper user management # system. assert flask.current_app.config['DEBUG'], "Debug not enabled" im = image_encoding.threshold(Image.open('./tests/normalface.png')) msg = messages.SetPersonality( device_address='000d6f000273ce0b', face_pixels=im, nothing_to_print_pixels=image_encoding.default_pipeline('Nothing to print'), cannot_see_bridge_pixels=image_encoding.default_pipeline('Cannot see bridge'), cannot_see_internet_pixels=image_encoding.default_pipeline('Cannot see internet'), ) success, next_print_id = protocol_loop.send_message( '000d6f000273ce0b', msg) if success: flask.flash('Sent your message to the printer!') else: flask.flash(("Could not send message because the " "printer {} is offline.").format('000d6f000273ce0b'), 'error') return flask.redirect('/admin')
def printer_print_api(): printer = hardware.Printer.query.get(1) pixels = image_encoding.default_pipeline( templating.default_template(flask.request.data)) hardware_message = messages.SetDeliveryAndPrint( device_address=printer.device_address, pixels=pixels, ) # If a printer is "offline" then we won't find the printer # connected and success will be false. success, next_print_id = protocol_loop.send_message( printer.device_address, hardware_message) if success: flask.flash('Sent your message to the printer!') stats.inc('printer.print.ok') else: flask.flash(("Could not send message because the " "printer {} is offline.").format(printer.name), 'error') stats.inc('printer.print.offline') # We know immediately if the printer wasn't online. if not success: model_message.failure_message = 'Printer offline' model_message.response_timestamp = datetime.datetime.utcnow() db.session.add(model_message) return 'ok'
def raw_printer_print_api(action): # SetDeliveryAndPrint # SetDelivery # SetDeliveryAndPrintNoFace # SetDeliveryNoFace printer = hardware.Printer.query.get(1) pixels = image_encoding.raw_image_pipeline(BytesIO(base64.b64decode(flask.request.data))) print action if action == 'deliveryandface': hardware_message = messages.SetDelivery( device_address=printer.device_address, pixels=pixels, ) elif action == 'printandface': hardware_message = messages.SetDeliveryAndPrint( device_address=printer.device_address, pixels=pixels, ) elif action == 'delivery': hardware_message = messages.SetDeliveryNoFace( device_address=printer.device_address, pixels=pixels, ) elif action == 'print': hardware_message = messages.SetDeliveryAndPrintNoFace( device_address=printer.device_address, pixels=pixels, ) elif action == 'print': hardware_message = messages.SetDeliveryAndPrintNoFace( device_address=printer.device_address, pixels=pixels, ) elif action == 'personalityandmessage': hardware_message = messages.SetPersonalityWithMessage( device_address=printer.device_address, face_pixels=pixels, nothing_to_print_pixels=image_encoding.default_pipeline(gentemplate('Nothing to print')), cannot_see_bridge_pixels=image_encoding.default_pipeline(gentemplate('Cannot see bridge')), cannot_see_internet_pixels=image_encoding.default_pipeline(gentemplate('Cannot see internet')), message_pixels=image_encoding.default_pipeline(gentemplate('Hi there!')), ) elif action == 'personality': hardware_message = messages.SetPersonality( device_address=printer.device_address, face_pixels=pixels, nothing_to_print_pixels=image_encoding.default_pipeline(gentemplate('Nothing to print')), cannot_see_bridge_pixels=image_encoding.default_pipeline(gentemplate('Cannot see bridge')), cannot_see_internet_pixels=image_encoding.default_pipeline(gentemplate('Cannot see internet')) ) else: assert False, "wtf print route {}".format(x) # If a printer is "offline" then we won't find the printer # connected and success will be false. success, next_print_id = protocol_loop.send_message( printer.device_address, hardware_message) return 'ok'
def preview_api(): message = flask.request.data.decode('utf-8') pixels = image_encoding.default_pipeline( templating.default_template(message)) png = io.BytesIO() pixels.save(png, "PNG") stats.inc('printer.preview') return '<img style="width: 100%;" src="data:image/png;base64,{}">'.format(base64.b64encode(png.getvalue()))
def preview(user_id, username, printer_id): assert user_id == login.current_user.id assert username == login.current_user.username message = flask.request.data.decode('utf-8') pixels = image_encoding.default_pipeline( templating.default_template(message)) png = io.BytesIO() pixels.save(png, "PNG") stats.inc('printer.preview') return '<img style="width: 100%;" src="data:image/png;base64,{}">'.format(base64.b64encode(png.getvalue()))
def printer_print(printer_id): printer = hardware.Printer.query.get(printer_id) if printer is None: flask.abort(404) # PERMISSIONS # the printer must either belong to this user, or be # owned by a friend if printer.owner.id == login.current_user.id: # fine pass elif printer.id in [p.id for p in login.current_user.friends_printers()]: # fine pass else: flask.abort(404) form = PrintForm() # Note that the form enforces access permissions: People can't # submit a valid printer-id that's not owned by the user or one of # the user's friends. choices = [ (x.id, x.name) for x in login.current_user.printers ] + [ (x.id, x.name) for x in login.current_user.friends_printers() ] form.target_printer.choices = choices # Set default printer on get if flask.request.method != 'POST': form.target_printer.data = printer.id if form.validate_on_submit(): # TODO: move image encoding into a pthread. # TODO: use templating to avoid injection attacks pixels = image_encoding.default_pipeline( templating.default_template(form.message.data)) hardware_message = messages.SetDeliveryAndPrint( device_address=printer.device_address, pixels=pixels, ) # If a printer is "offline" then we won't find the printer # connected and success will be false. success, next_print_id = protocol_loop.send_message( printer.device_address, hardware_message) if success: flask.flash('Sent your message to the printer!') stats.inc('printer.print.ok') else: flask.flash(("Could not send message because the " "printer {} is offline.").format(printer.name), 'error') stats.inc('printer.print.offline') # Store the same message in the database. png = io.BytesIO() pixels.save(png, "PNG") model_message = model_messages.Message( print_id=next_print_id, pixels=bytearray(png.getvalue()), sender_id=login.current_user.id, target_printer=printer, ) # We know immediately if the printer wasn't online. if not success: model_message.failure_message = 'Printer offline' model_message.response_timestamp = datetime.datetime.utcnow() db.session.add(model_message) return flask.redirect(flask.url_for( 'printer_overview.printer_overview', printer_id=printer.id)) return flask.render_template( 'printer_print.html', printer=printer, form=form, )
def test_default_pipeline(self): n_bytes, _ = image_encoding.rle_from_bw( image_encoding.default_pipeline('') ) self.assertEquals(n_bytes, 3072)
def print_html(printer_id): printer = hardware.Printer.query.get(printer_id) if printer is None: flask.abort(404) # PERMISSIONS # the printer must either belong to this user, or be # owned by a friend if printer.owner.id == login.current_user.id: # fine pass elif printer.id in [p.id for p in login.current_user.friends_printers()]: # fine pass else: flask.abort(404) request.get_data() task = json.loads(request.data) if not task['message'] or not task['face']: flask.abort(500) pixels = image_encoding.default_pipeline( templating.default_template(task['message'], from_name=login.current_user.username)) hardware_message = None if task['face'] == "noface": hardware_message = messages.SetDeliveryAndPrintNoFace( device_address=printer.device_address, pixels=pixels, ) else: hardware_message = messages.SetDeliveryAndPrint( device_address=printer.device_address, pixels=pixels, ) # If a printer is "offline" then we won't find the printer # connected and success will be false. success, next_print_id = protocol_loop.send_message( printer.device_address, hardware_message) # Store the same message in the database. png = io.BytesIO() pixels.save(png, "PNG") model_message = model_messages.Message( print_id=next_print_id, pixels=bytearray(png.getvalue()), sender_id=login.current_user.id, target_printer=printer, ) # We know immediately if the printer wasn't online. if not success: model_message.failure_message = 'Printer offline' model_message.response_timestamp = datetime.datetime.utcnow() db.session.add(model_message) response = {} if success: response['status'] = 'Sent your message to the printer!' else: response['status'] = ("Could not send message because the " "printer {} is offline.").format(printer.name) return json.dumps(response)
def printer_print(printer_id): printer = hardware.Printer.query.get(printer_id) if printer is None: flask.abort(404) # PERMISSIONS # the printer must either belong to this user, or be # owned by a friend if printer.owner.id == login.current_user.id: # fine pass elif printer.id in [p.id for p in login.current_user.friends_printers()]: # fine pass else: flask.abort(404) form = PrintForm() # Note that the form enforces access permissions: People can't # submit a valid printer-id that's not owned by the user or one of # the user's friends. choices = [(x.id, x.name) for x in login.current_user.printers] + [ (x.id, x.name) for x in login.current_user.friends_printers() ] form.target_printer.choices = choices form.face.choices = [("default", "Default face"), ("noface", "No face")] # Set default printer on get if flask.request.method != 'POST': form.target_printer.data = printer.id form.face.data = "default" if form.validate_on_submit(): # TODO: move image encoding into a pthread. # TODO: use templating to avoid injection attacks pixels = image_encoding.default_pipeline( templating.default_template(form.message.data)) hardware_message = None if form.face.data == "noface": hardware_message = messages.SetDeliveryAndPrintNoFace( device_address=printer.device_address, pixels=pixels, ) else: hardware_message = messages.SetDeliveryAndPrint( device_address=printer.device_address, pixels=pixels, ) # If a printer is "offline" then we won't find the printer # connected and success will be false. success, next_print_id = protocol_loop.send_message( printer.device_address, hardware_message) if success: flask.flash('Sent your message to the printer!') stats.inc('printer.print.ok') else: flask.flash(("Could not send message because the " "printer {} is offline.").format(printer.name), 'error') stats.inc('printer.print.offline') # Store the same message in the database. png = io.BytesIO() pixels.save(png, "PNG") model_message = model_messages.Message( print_id=next_print_id, pixels=bytearray(png.getvalue()), sender_id=login.current_user.id, target_printer=printer, ) # We know immediately if the printer wasn't online. if not success: model_message.failure_message = 'Printer offline' model_message.response_timestamp = datetime.datetime.utcnow() db.session.add(model_message) return flask.redirect( flask.url_for('printer_overview.printer_overview', printer_id=printer.id)) return flask.render_template( 'printer_print.html', printer=printer, form=form, )
def test_default_pipeline(self): n_bytes, _ = image_encoding.rle_from_bw( image_encoding.default_pipeline( '<html><body style="margin: 0px; height: 10px;"></body></html>' )) self.assertEquals(n_bytes, 3840)