Ejemplo n.º 1
0
def setup(broker, threshold):
    lux = SensorPub(LuxSensor())
    lux.subscribe(print)
    led = GpioPinOut()
    actions = lux.map(lambda event: event.val > threshold)
    actions.subscribe(led)
    actions.subscribe(lambda v: print('ON' if v else 'OFF'))
    lux.to_json().subscribe(MQTTWriter(broker, topics=[('bogus/bogus', 0)]))
    lux.print_downstream()
    return (lux, led)
def setup(threshold=25):
    lux = SensorPub(LuxSensor())
    lux.subscribe(print)
    lux.csv_writer(os.path.expanduser('~/lux.csv'))
    led = GpioPinOut()
    actions = lux.map(lambda event: event.val > threshold)
    actions.subscribe(led)
    actions.subscribe(lambda v: print('ON' if v else 'OFF'))
    lux.print_downstream()
    return (lux, led)
Ejemplo n.º 3
0
 def test_where(self):
     """In this version, we create a publisher and use method chaining to
     compose the filters"""
     s = ValueListSensor(1, value_stream)
     p = SensorPub(s)
     w = p.where(predicate)
     w.output()
     vo = ValidationSubscriber(expected_stream, self.test_where)
     w.subscribe(vo)
     scheduler = Scheduler(asyncio.get_event_loop())
     scheduler.schedule_periodic(p, 0.5)  # sample twice every second
     p.print_downstream()
     scheduler.run_forever()
     self.assertTrue(
         vo.completed,
         "Schedule exited before validation observer completed")
     print("That's all folks")
Ejemplo n.º 4
0
def run_example():
    sensor = SensorPub(DummyTempSensor('temp-1', input_sequence))
    sensor.output()  # let us see the raw values
    dispatcher = sensor.transduce(RunningAvg(4))\
                       .passthrough(lambda evt:
                                    print("Running avg temp: %s" %
                                          round(evt.val, 2))) \
                       .dispatch([(lambda v: v[2]>=T_high, 't_high'),
                                  (lambda v: v[2]<=T_low, 't_low')])
    controller = Controller()
    dispatcher.subscribe(controller, topic_mapping=('t_high', 't_high'))
    dispatcher.subscribe(controller, topic_mapping=('t_low', 't_low'))
    dispatcher.subscribe(controller, topic_mapping=('default', 'between'))
    controller.subscribe(BypassValveActuator())
    sensor.print_downstream()
    scheduler = Scheduler(asyncio.get_event_loop())
    scheduler.schedule_periodic(sensor, 0.5)
    scheduler.run_forever()
    print("got to the end")
Ejemplo n.º 5
0
        print("LED Completed")

    def __str__(self):
        return 'LED'


# instantiate an LED
led = LED()

# Now, build a pipeline to sample events returned from the sensor,
# convert to a boolean based on whether the value is greater than
# the mean, and output to the LED.
import antevents.linq.select
sensor.select(lambda evt: evt.val > MEAN).subscribe(led)

# If you want to see the raw value of each sensor, just add the output() element
import antevents.linq.output
sensor.output()

# Call a debug method on the base publisher class to see the element tree rooted
# at sensor.
sensor.print_downstream()

# Now, we need to schedule the sensor to be sampled
import asyncio
from antevents.base import Scheduler
scheduler = Scheduler(asyncio.get_event_loop())
scheduler.schedule_periodic(sensor, 1.0)  # sample once a second
scheduler.run_forever()  # run until all sensors complete
print("That's all folks!")