Ejemplo n.º 1
0
def get_wifi_info_from_user():
    camera = CameraRGB()

    for i, frame in enumerate(camera.stream()):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        stream_frame(frame)
        qrcodes = myzbarlight.qr_scan(frame)

        if len(qrcodes) > 0:
            qr_data = qrcodes[0]
            try:
                qr_data = json.loads(qr_data.decode('utf-8'))
                ssid = qr_data['s']
                password = qr_data['p']
                break
            except:
                # We got invalid data from the QR code, which is
                # fine, we'll just move on with life and try again.
                pass

        if (i % 100) == 99:
            # Every 100 frames, we'll check to see if WiFi magically came back.
            if wireless.current() is not None:
                ssid = None
                password = None
                break

    camera.close()

    return ssid, password
Ejemplo n.º 2
0
def ensure_token():
    token = STORE.get('DEVICE_TOKEN', None)

    if token is not None:
        # We have a token. All is well.
        return

    console.big_image('images/token_error.png')
    console.big_status('Ready to receive login token.')

    camera = CameraRGB()

    system_password = None

    for i, frame in enumerate(camera.stream()):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        stream_frame(frame)
        qrcodes = myzbarlight.qr_scan(frame)

        if len(qrcodes) > 0:
            qr_data = qrcodes[0]
            try:
                qr_data = json.loads(qr_data.decode('utf-8'))
                token = qr_data['t']
                if 'p' in qr_data:
                    # This allows us to override the default system_password for special-purpose devices.
                    # The default is just... a default. No matter what is set here, it can be changed later.
                    system_password = qr_data['p']
                break
            except:
                pass

    console.big_image('images/token_success.png')
    console.big_status('Success. Token: {}...'.format(token[:5]))

    camera.close()

    STORE.put('DEVICE_TOKEN', token)
    print_all("Stored Device token: {}...".format(token[:5]))

    jupyter_password = util.token_to_jupyter_password(token)
    STORE.put('DEVICE_JUPYTER_PASSWORD', jupyter_password)
    print_all("Stored Jupyter password: {}...".format(jupyter_password[:2]))

    if system_password is None:
        # If a particular default system_password was not specified, we will generate a good
        # default system_password from the token. This is a good thing, since it ensures that
        # each device is given a strong, unique default system_password.
        system_password = util.token_to_system_password(token)

    util.change_system_password(system_priv_user, system_password)
    print_all("Successfully changed {}'s password!".format(system_priv_user))

    time.sleep(5)

    console.big_clear()
    console.clear_image()