Beispiel #1
0
def init_config():
    """Read existing config file or create a default one"""
    # If config file not exists create a default one
    if not path.isfile(CONFIG_FILE):
        log_info('No config file found, create default one')
        config.add_section('COMMON')
        config.add_section('AUTH')
        config.add_section('SENSORS')
        config.add_section('API')
        config.set('AUTH', 'token', '')
        config.set('AUTH', 'userid', '')
        config.set('API', 'url', 'http://loggr.stkn.org/api/')
        config.set('COMMON', 'scripts_path', 'sensors/')
        with open(CONFIG_FILE, 'w') as configfile:
            config.write(configfile)
        log_info('Created default config file')
        return

    config.read(CONFIG_FILE)

    if not config.has_section('AUTH'):
        config.add_section('AUTH')

    if not config.has_section('SENSORS'):
        config.add_section('SENSORS')

    if not config.has_option('AUTH', 'token'):
        config.set('AUTH', 'token', '')

    if not config.has_option('AUTH', 'userid'):
        config.set('AUTH', 'userid', '')

    with open(CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Beispiel #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')
Beispiel #3
0
def init_config():
    """Read existing config file or create a default one"""
    # If config file not exists create a default one
    if not path.isfile(CONFIG_FILE):
        log_info('No config file found, create default one')
        config.add_section('COMMON')
        config.add_section('AUTH')
        config.add_section('SENSORS')
        config.add_section('API')
        config.set('AUTH', 'token', '')
        config.set('AUTH', 'userid', '')
        config.set('API', 'url', 'http://loggr.stkn.org/api/')
        config.set('COMMON', 'scripts_path', 'sensors/')
        with open(CONFIG_FILE, 'w') as configfile:
            config.write(configfile)
        log_info('Created default config file')
        return

    config.read(CONFIG_FILE)

    if not config.has_section('AUTH'):
        config.add_section('AUTH')

    if not config.has_section('SENSORS'):
        config.add_section('SENSORS')

    if not config.has_option('AUTH', 'token'):
        config.set('AUTH', 'token', '')

    if not config.has_option('AUTH', 'userid'):
        config.set('AUTH', 'userid', '')

    with open(CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Beispiel #4
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 motion(PIR_PIN):
    """Callback function of event listener on pin 26
       called on rising edge (motion detected by PIR module)

    Args:
        PIR_PIN (int): pin of detected event (rising edge)
    """
    log_info("PIR: Motion detected")
    # take_picture()
    send_pictures()
    sleep(60)  # when detected a motion wait 1 minute to detect another one
def main():
    """Main method of pir_int.py

    1.  Start logging
    2.  Check for valid config file
    3.  Add event listener to pin 26, rising edge, motion() as cb function
    4.  loop
    4a. On rising edge: call motion(), send pics to web server
    """
    # declare variables as global
    global token
    global userid
    global containers_url
    global num_pics
    global time_between_pics

    # start logging into file 'pir.log'
    logging.basicConfig(format="%(asctime)s: %(levelname)s: %(message)s", filename="pir.log", level=logging.INFO)
    log_info("PIR-Logging (re)started")

    print "PIR sensor script (STRG+C to exit)"
    sleep(1)

    # get config data
    # handle config errors
    if not path.isfile(CONFIG_FILE):
        treat_missing_config_errors()
        return

    # read out config file
    config.read(CONFIG_FILE)

    # Check if config file contains options url
    if (
        not config.has_option("AUTH", "token")
        or not config.has_option("AUTH", "userid")
        or not config.has_option("API", "url")
        or not config.has_option("CAMERA", "num_pics")
        or not config.has_option("CAMERA", "time_between_pics")
    ):
        treat_missing_config_errors()
        return

    # Get token and user id from config file
    token = config.get("AUTH", "token")
    userid = config.get("AUTH", "userid")
    containers_url = config.get("API", "url") + "containers/"

    num_pics = config.get("CAMERA", "num_pics")
    time_between_pics = config.get("CAMERA", "time_between_pics")

    # Check if token and userid is set
    if not len(token) or not len(userid):
        treat_pairing_errors()
        return

    # Check if url is set
    if not len(containers_url) or not len(num_pics) or not len(time_between_pics):
        treat_missing_config_errors()
        return

    print "Ready"
    try:
        # Add listener to gpio pin 26 to detect rising edge
        GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=motion)
        while 1:
            sleep(3)
    except KeyboardInterrupt:
        print "Exit"
        GPIO.cleanup()