예제 #1
0
파일: admin.py 프로젝트: bentimms/sirius
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')
예제 #2
0
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.convert_to_1bit(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')
예제 #3
0
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'
예제 #4
0
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'
예제 #5
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()
예제 #6
0
def imageprint(printer_id):
    printer = hardware.Printer.query.get(printer_id)
    if printer is None:
        flask.abort(404)
    form = PrintForm()
    if flask.request.method == 'POST':
        pixels = image_encoding.image_pipeline(max(glob.iglob('/var/upload/*'), key=os.path.getctime))
        ##################
        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_file.html',
        printer=printer,
        form=form
    )
예제 #7
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

    # 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,
    )
예제 #8
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)
예제 #9
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

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