Esempio n. 1
0
def printer():
    device_address = os.urandom(8).encode('hex')
    xor = bitshuffle.hardware_xor_from_device_address(device_address)
    secret = os.urandom(5).encode('hex')

    cc = claiming.encode(xor, int(secret, 16))
    printer = hardware.Printer(
        device_address=device_address,
        hardware_xor=xor,
    )
    db.session.add(printer)
    db.session.commit()

    def out(x):
        x.write('     address: {}\n'.format(device_address))
        x.write('       DB id: {}\n'.format(printer.id))
        x.write('      secret: {}\n'.format(secret))
        x.write('         xor: {}\n'.format(xor))
        x.write('  claim code: {}\n'.format(cc))

    path = device_address + '.printer'
    with open(path, 'w') as f:
        out(f)

    out(sys.stdout)
    print('\nCreated printer and saved as {}'.format(path))
Esempio n. 2
0
def _decode_bridge_event(data):
    """Decodes a single bridge event from data.

    :param data: A python dictionary, as decoded from json.
    """
    try:
        name = data['json_payload']['name']
        payload = data['json_payload']
    except KeyError as e:
        return messages.MalformedEvent(data, 'Missing field {}'.format(e))

    if name == 'power_on':
        return messages.PowerOn(
            bridge_address=data['bridge_address'],
            model=payload['model'],
            firmware_version=payload['firmware_version'],
            ncp_version=payload['ncp_version'],
            local_ip_address=payload['local_ip_address'],
            mac_address=payload['mac_address'],
            uptime=payload['uptime'],
            uboot_environment=payload['uboot_environment'],
            network_info=payload['network_info'],
        )
    elif name == 'device_connect':
        return messages.DeviceConnect(
            bridge_address=data['bridge_address'],
            device_address=payload['device_address'],
        )
    elif name == 'device_disconnect':
        return messages.DeviceDisconnect(
            bridge_address=data['bridge_address'],
            device_address=payload['device_address'],
        )
    elif name == 'encryption_key_required':
        return messages.EncryptionKeyRequired(
            bridge_address=data['bridge_address'],
            device_address=payload['device_address'],
            hardware_xor=bitshuffle.hardware_xor_from_device_address(
                payload['device_address']),
        )
    else:
        return messages.UnknownEvent(data)

    assert False, "never reached"
Esempio n. 3
0
    def phone_home(cls, device_address):
        """This gets called every time the in-memory machinery thinks it has
        seen a printer for the first time round, e.g. after a
        re-connect of the websocket.
        """
        printer = cls.query.filter_by(device_address=device_address).first()
        hardware_xor = bitshuffle.hardware_xor_from_device_address(
            device_address)

        if printer is not None:
            return

        printer = cls(
            device_address=device_address,
            hardware_xor=hardware_xor,
            owner_id=None,
            used_claim_code=None,
        )
        db.session.add(printer)
        db.session.commit()

        # Connect hardware xor and printer if there is a claim code
        # waiting.
        #
        # Printers always generate the same XOR. I.e. we can have more
        # than one claim code with the same XOR. We always pick the
        # newest claim code.
        claim_code_query = ClaimCode.query.filter_by(
            hardware_xor=hardware_xor).order_by(desc('created'), desc('id'))
        claim_code = claim_code_query.first()
        if claim_code is None:
            return

        printer.owner_id = claim_code.by_id
        printer.name = claim_code.name
        printer.used_claim_code = claim_code.claim_code
        db.session.add(printer)
        db.session.commit()
Esempio n. 4
0
    def phone_home(cls, device_address):
        """This gets called every time the in-memory machinery thinks it has
        seen a printer for the first time round, e.g. after a
        re-connect of the websocket.
        """
        printer = cls.query.filter_by(device_address=device_address).first()
        hardware_xor = bitshuffle.hardware_xor_from_device_address(device_address)

        if printer is not None:
            return

        printer = cls(
            device_address=device_address,
            hardware_xor=hardware_xor,
            owner_id=None,
            used_claim_code=None,
        )
        db.session.add(printer)
        db.session.commit()

        # Connect hardware xor and printer if there is a claim code
        # waiting.
        #
        # Printers always generate the same XOR. I.e. we can have more
        # than one claim code with the same XOR. We always pick the
        # newest claim code.
        claim_code_query = ClaimCode.query.filter_by(
            hardware_xor=hardware_xor).order_by(desc('created'), desc('id'))
        claim_code = claim_code_query.first()
        if claim_code is None:
            return

        printer.owner_id = claim_code.by_id
        printer.name = claim_code.name
        printer.used_claim_code = claim_code.claim_code
        db.session.add(printer)
        db.session.commit()