def test_thunk_builder_handling(self):
     """We have logic where we can pass a thunk builder into a combinator and
     it will do the right thing. Check that it works"""
     scheduler = Scheduler(asyncio.get_event_loop())
     lux = ValueListSensor('lux-2', lux_data)
     vs = ValidationSubscriber(lux_data, self)
     scheduler.schedule_sensor(
         lux,
         0.5,
         passthrough(output()),  # output() evaluates to a thunk
         passthrough(output),  # output is a thunk builder
         passthrough(output(sys.stdout)),  # output can take an arg
         vs,
         print_downstream=True)
     scheduler.run_forever()
     self.assertTrue(vs.completed)
 def test_blocking_sensor(self):
     s = BlockingSensor(1, stop_after=EVENTS)
     scheduler = Scheduler(asyncio.get_event_loop())
     scheduler.schedule_sensor_on_separate_thread(s, 1,
         passthrough(output()),
         ValidationSubscriber([i+1 for i in range(EVENTS)], self,
                              extract_value_fn=lambda v:v),
         make_event_fn=lambda s, v: v)
     scheduler.run_forever()
     print("that's it")
Пример #3
0
 def test_schedule_sensor(self):
     """In this version, we pass the sensor directly to the scheduler and use
     a functional style to compose the filters"""
     s = ValueListSensor(1, value_stream)
     vo = ValidationSubscriber(expected_stream, self.test_schedule_sensor)
     scheduler = Scheduler(asyncio.get_event_loop())
     scheduler.schedule_sensor(s, 0.5, where(predicate), passthrough(vo),
                               output())
     scheduler.run_forever()
     self.assertTrue(
         vo.completed,
         "Schedule exited before validation observer completed")
     print("That's all folks")
 def test_complex_workflow(self):
     THRESHOLD = 300
     lux = ValueListSensor('lux-1', lux_data)
     scheduler = Scheduler(asyncio.get_event_loop())
     vs1 = ValidationSubscriber(lux_data, self)
     vs2 = ValidationSubscriber(
         [False, False, False, True, False, False, True],
         self,
         extract_value_fn=lambda v: v)
     scheduler.schedule_sensor(
         lux,
         0.5,
         passthrough(output()),
         passthrough(vs1),
         map(lambda event: event.val > THRESHOLD),
         passthrough(lambda v: print('ON' if v else 'OFF')),
         vs2,
         print_downstream=True)
     scheduler.run_forever()
     self.assertTrue(vs1.completed)
     self.assertTrue(vs2.completed)
     print("That's all folks")
Пример #5
0
        return "DummyLuxSensor(%s, %s, %s)" % \
            (self.sensor_id, self.mean, self.stddev)


class DummyLed(DefaultSubscriber):
    def on_next(seelf, x):
        if x:
            print("LED ON")
        else:
            print("LED OFF")

    def __repr__(self):
        return 'DummyLed'


THRESHOLD = 300

# Instantiate the sensor and use the functional API to build a flow
lux = DummyLuxSensor("lux-1")
scheduler = Scheduler(asyncio.get_event_loop())
scheduler.schedule_sensor(lux,
                          1.0,
                          passthrough(output()),
                          passthrough(csv_writer('/tmp/lux.csv')),
                          map(lambda event: event.val > THRESHOLD),
                          passthrough(lambda v: print('ON' if v else 'OFF')),
                          DummyLed(),
                          print_downstream=True)
scheduler.run_forever()
print("That's all folks")