예제 #1
0
    def print_html(self, html, from_name, face='default'):
        from sirius.protocol import protocol_loop
        from sirius.protocol import messages
        from sirius.coding import image_encoding
        from sirius.coding import templating
        from sirius import stats
        from sirius.models import messages as model_messages

        pixels = image_encoding.default_pipeline(
            templating.default_template(html, from_name=from_name)
        )

        hardware_message = None
        if face == "noface":
            hardware_message = messages.SetDeliveryAndPrintNoFace(
                device_address=self.device_address,
                pixels=pixels,
            )
        else:
            hardware_message = messages.SetDeliveryAndPrint(
                device_address=self.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(
            self.device_address,
            hardware_message
        )

        if success:
            stats.inc('printer.print.ok')
        else:
            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_name=from_name,
            target_printer=self,
        )

        # 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)

        if not success:
            raise Printer.OfflineError()
예제 #2
0
    def test_set_delivery_and_print_no_face(self):
        command = messages.SetDeliveryAndPrintNoFace('some-device-address', self.bw_2x2())

        json = encoders.encode_bridge_command('some-bridge-address', command, 1, '0')

        expected_json = {
            'type': 'DeviceCommand',
            'bridge_address': 'some-bridge-address',
            'device_address': 'some-device-address',
            'command_id': 1,
            'timestamp': '0',
            'binary_payload': 'AQARAAEAAAAAAAAAJwAAACMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECAQ=='
        }

        self.assertDictEqual(expected_json, json)
예제 #3
0
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)
예제 #4
0
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,
    )