Ejemplo n.º 1
0
def main(argv=sys.argv[1:]):
    parser = argparse.ArgumentParser(
        description='Distributed lux example, data capture process')
    parser.add_argument('-i',
                        '--interval',
                        type=float,
                        default=5.0,
                        help="Sample interval in seconds")
    parser.add_argument(
        '-t',
        '--threshold',
        type=float,
        default=25.0,
        help="Threshold lux level above which light should be turned on")
    parser.add_argument('broker',
                        metavar="BROKER",
                        type=str,
                        help="hostname or ip address of mqtt broker")
    parsed_args = parser.parse_args(argv)
    (lux, led) = setup(parsed_args.broker, parsed_args.threshold)
    scheduler = Scheduler(asyncio.get_event_loop())
    stop = scheduler.schedule_periodic_on_separate_thread(
        lux, parsed_args.interval)
    print("starting run...")
    try:
        scheduler.run_forever()
    except KeyboardInterrupt:
        led.on_completed()
        stop()
    return 0
 def test_blocking_output_thing(self):
     o = BlockingOutputThing()
     o.output()
     scheduler = Scheduler(asyncio.get_event_loop())
     c = scheduler.schedule_periodic_on_separate_thread(o, 1)
     vs = ValidationInputThing([i + 1 for i in range(EVENTS)],
                               self,
                               extract_value_fn=lambda v: v)
     o.connect(vs)
     o.connect(StopLoopAfter(EVENTS, c))
     o.print_downstream()
     scheduler.run_forever()
     print("that's it")
def main(argv=sys.argv[1:]):
    if len(argv)!=2:
        print("%s threshold interval" % sys.argv[0])
        return 1
    threshold = float(argv[0])
    interval = float(argv[1])
    print("%f seconds interval and an led threshold of %f lux" %
          (interval, threshold))
    (lux, led) = setup(threshold)
    scheduler = Scheduler(asyncio.get_event_loop())
    stop = scheduler.schedule_periodic_on_separate_thread(lux, interval)
    print("starting run...")
    try:
        scheduler.run_forever()
    except KeyboardInterrupt:
        led.on_completed()
        stop()
    return 0
def create_dispatch_rule(sensor_id):
    return (lambda evt: evt.sensor_id == sensor_id, sensor_id)


dispatch_rules = [
    create_dispatch_rule(remote_id) for remote_id in REMOTE_SENSORS
]

scheduler = Scheduler(asyncio.get_event_loop())

if HAS_LOCAL_SENSOR:
    sensor = SensorAsOutputThing(LuxSensor(sensor_id=LOCAL_SENSOR_ID))
    sensor.rolling_csv_writer(DIRECTORY, LOCAL_SENSOR_ID)
    sensor.output()
    scheduler.schedule_periodic_on_separate_thread(sensor, 60)

mqtt_reader = MQTTReader('localhost',
                         client_id='rpi',
                         topics=[
                             ('remote-sensors', 0),
                         ])
# we convert the tuple received into a SensorEvent, overwriting the timestamp.
dispatcher = mqtt_reader.map(lambda m:(m.payload).decode("utf-8"))\
                        .from_json()\
                        .map(lambda tpl: SensorEvent(sensor_id=tpl[0], ts=time.time(), val=tpl[2]))\
                        .dispatch(dispatch_rules)
# For each remote sensor, we create a separate csv writer
for remote in REMOTE_SENSORS:
    dispatcher.rolling_csv_writer(DIRECTORY, remote, sub_port=remote).output()
dispatcher.connect(lambda x: print("Unexpected sensor %s, full event was %s" %