예제 #1
0
def main(argv):

    config_file = None
    read_only = False
    verbose = False

    try:
        # -h (optional), -c (mandatory, hence 'c:'), -r (optional)
        opts, args = getopt.getopt(argv,"hc:rv",[])
    except getopt.GetoptError:
        print 'repository.py [-h] [-r] [-v] -c <cfgfile>'
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print 'repository.py [-h] [-r] [-v] -c <cfgfile>'
            sys.exit(0)
        elif opt in ("-c", "--cfgfile"):
            config_file = arg
        elif opt == '-r':
            read_only = True
        elif opt == '-v':
            verbose = True

    if config_file is None:
        print 'repository.py [-h] [-r] [-v] -c <cfgfile>'
        sys.exit(2)

    if verbose: print 'Using cfg file:', config_file
    repository = Repository(config_file, verbose)
    
    config = ConfigParser.RawConfigParser()
    config.read(config_file)

    sensor_names = config.get("sensors", "names").strip().split(',')
    if verbose: print 'Sensor names:', sensor_names

    sensors = []

    for name in sensor_names:
        if verbose: print 'Adding sensor:', name
        sensors.append(Sensor(name, config_file))
    
    for sensor in sensors:
        if verbose: print 'Reading sensor', sensor.sensor_id

        readings = sensor.get_readings()
        if readings is not None:
            if read_only:
                s=json.dumps(readings, sort_keys=True, indent=4, separators=(',', ': '))
                print s
            else:
                repository.save_readings(readings)

    print 'Done'