Exemple #1
0
def save_token_and_userid():
    """Save token and user id in config file

    Returns:
        status (str): json status code
            error='format' or error='arguments" - if an error occured
            status='ok' - if everything was fine
    """
    config.read(CONFIG_FILE)

    regex_userid = re.compile(r'^[a-z,0-9]{24}$')
    regex_token = re.compile(r'^[A-Z,a-z,0-9]{64}$')

    if 'token' not in request.json and 'userid' not in request.json:
        log_error('missing arguments in post request')
        return jsonify(error='arguments')

    if 'token' in request.json:
        if regex_token.match(request.json['token']) is None:
            log_error('wrong format of token string')
            return jsonify(error='format')
        config.set('AUTH', 'token', request.json['token'])
        log_info('token set in config file')

    if 'userid' in request.json:
        if regex_userid.match(request.json['userid']) is None:
            log_error('wrong format of userid string')
            return jsonify(error='format')
        config.set('AUTH', 'userid', request.json['userid'])
        log_info('userid set in config file')

    with open(CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
    set_status_led(LedStatusTypes.pairing_succeeded.name)
    return jsonify(status='ok')
Exemple #2
0
def save_token_and_userid():
    """Save token and user id in config file

    Returns:
        status (str): json status code
            error='format' or error='arguments" - if an error occured
            status='ok' - if everything was fine
    """
    config.read(CONFIG_FILE)

    regex_userid = re.compile(r'^[a-z,0-9]{24}$')
    regex_token = re.compile(r'^[A-Z,a-z,0-9]{64}$')

    if 'token' not in request.json and 'userid' not in request.json:
        log_error('missing arguments in post request')
        return jsonify(error='arguments')

    if 'token' in request.json:
        if regex_token.match(request.json['token']) is None:
            log_error('wrong format of token string')
            return jsonify(error='format')
        config.set('AUTH', 'token', request.json['token'])
        log_info('token set in config file')

    if 'userid' in request.json:
        if regex_userid.match(request.json['userid']) is None:
            log_error('wrong format of userid string')
            return jsonify(error='format')
        config.set('AUTH', 'userid', request.json['userid'])
        log_info('userid set in config file')

    with open(CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
    set_status_led(LedStatusTypes.pairing_succeeded.name)
    return jsonify(status='ok')
def send_pictures():
    """Send pictures to web server
       {num_pics} pics will be sent, one pic every {time_between_pics}
       seconds
    """
    # create container
    container_name = str(int(time()))
    payload = {"name": container_name}
    headers = {"Content-Type": "application/json"}
    try:
        r = requests.post(containers_url, data=json.dumps(payload), headers=headers)
        print "Container created"
    except requests.exceptions.ConnectionError as ce:
        # catch and treat requests connection errors
        log_error("requests failure: " + str(ce))
        return
    except requests.exceptions.RequestException as re:
        # catch and treat requests exceptions
        log_error("requests failure: " + str(re))
        return
    else:
        # send image files to container
        i = 0
        while i < int(num_pics):
            # build file name
            filename = "pic" + str(int(time())) + ".jpg"
            files = {"file": (filename, open(PICS_PATH, "rb"))}
            pics_url = containers_url + container_name + "/upload"
            try:
                r = requests.post(pics_url, files=files)
                print "File sent"
                i = i + 1
                sleep(float(time_between_pics))
            except requests.exceptions.ConnectionError as ce:
                # catch and treat requests connection errors
                log_error("requests failure: " + str(ce))
                return
            except requests.exceptions.RequestException as re:
                # catch and treat requests exceptions
                log_error("requests failure: " + str(re))
                return