Ejemplo n.º 1
0
def test_influx_output():
    loop = asyncio.get_event_loop()
    s = ValueListSensor(1, value_stream)
    p = SensorPub(s)
    b = InfluxDBWriter(msg_format=Sensor(series_name='Sensor',
                                         fields=['val', 'ts'],
                                         tags=['sensor_id']),
                       generate_timestamp=False)
    p.subscribe(b)

    scheduler = Scheduler(loop)
    scheduler.schedule_periodic(p, 0.2)  # sample five times every second
    scheduler.run_forever()

    # Now play back
    c = InfluxDBClient(database='antevents')
    rs = c.query('SELECT * FROM Sensor;').get_points()
    for d in rs:
        print(d)

    # Play back using a publisher
    p = InfluxDBReader('SELECT * FROM Sensor;')
    p.subscribe(CallableAsSubscriber(print))

    scheduler = Scheduler(loop)
    scheduler.schedule_periodic(p, 0.2)  # sample five times every second
    scheduler.run_forever()
    print("That's all folks")
Ejemplo n.º 2
0
    def test_mqtt(self):
        loop = asyncio.get_event_loop()
        s = Scheduler(loop)
        sensor = make_test_publisher_from_vallist(1, sensor_data)
        mqtt_writer = MQTTWriter('localhost', topics=[
            ('bogus/bogus', 0),
        ])
        sensor.to_json().subscribe(mqtt_writer)
        s.schedule_periodic(sensor, 0.5)

        mqtt_reader = MQTTReader("localhost", topics=[
            ('bogus/bogus', 0),
        ])
        vs = ValidationSubscriber(sensor_data, self)
        mqtt_reader.take(5).select(mqtt_msg_to_unicode).from_json(constructor=SensorEvent) \
                       .output().subscribe(vs)
        c = s.schedule_on_private_event_loop(mqtt_reader)
        stop = StopLoopAfter(5, c)
        mqtt_reader.subscribe(stop)
        mqtt_reader.print_downstream()
        sensor.print_downstream()
        s.run_forever()
        loop.stop()
        self.assertTrue(vs.completed)
        print("that's it")
 def test(self):
     scheduler = Scheduler(asyncio.get_event_loop())
     sensor = make_test_publisher_from_vallist(1, values)
     scheduler.schedule_periodic(sensor, 1)
     blocking_subscriber = TestSubscriber(scheduler, values, self)
     sensor.subscribe(blocking_subscriber)
     scheduler.run_forever()
     self.assertTrue(blocking_subscriber.completed)
class TestCase(unittest.TestCase):
    def setUp(self):
        # Creating a new event loop each test case does not seem to work.
        # I think it is due to hbmqtt not cleaning up some state in the asyncio
        # layer.
        #self.loop = asyncio.new_event_loop()
        self.loop = asyncio.get_event_loop()
        self.sched = Scheduler(self.loop)

    def tearDown(self):
        pass
        #self.loop.stop()
        #self.loop.close()

    def test_client_only(self):
        SENSOR_ID = 'sensor-1'
        TOPIC = get_topic_name(self)
        sensor = SensorPub(ValueListSensor(SENSOR_ID, VALUES))
        td = sensor.transduce(PeriodicMedianTransducer(period=3))
        qw = QueueWriter(td, URL, TOPIC, self.sched)
        qw.output()
        self.sched.schedule_periodic(sensor, 0.5)
        self.sched.run_forever()
        self.assertFalse(
            qw.has_pending_requests(),
            "QueueWriter has pending requests: %s" % qw.dump_state())
        print("test_client_only completed")

    def send_and_recv_body(self, sleep_timeout):
        SENSOR_ID = 'sensor-1'
        TOPIC = get_topic_name(self)
        sensor = SensorPub(ValueListSensor(SENSOR_ID, VALUES))
        td = sensor.transduce(PeriodicMedianTransducer(period=3))
        qw = QueueWriter(td, URL, TOPIC, self.sched)
        qw.output()
        qr = QueueReader(URL, TOPIC, self.sched, timeout=sleep_timeout)
        self.sched.schedule_periodic(sensor, 0.5)
        stop_qr = self.sched.schedule_on_main_event_loop(qr)
        vs = ValidateAndStopSubscriber(EXPECTED, self, stop_qr)
        qr.select(msg_to_event).subscribe(vs)
        self.sched.run_forever()
        self.assertFalse(
            qw.has_pending_requests(),
            "QueueWriter has pending requests: %s" % qw.dump_state())
        self.assertEqual(qr.state, QueueReader.FINAL_STATE)
        self.assertEqual(vs.next_idx, len(EXPECTED))
        print("send_and_recv_bod(%s) completed" % sleep_timeout)

    def test_short_timeout(self):
        self.send_and_recv_body(0.1)

    def test_long_timeout(self):
        self.send_and_recv_body(3.0)
Ejemplo n.º 5
0
def test_bokeh_output():
    loop = asyncio.get_event_loop()
    s = ValueListSensor(1, value_stream)
    p = SensorPub(s)
    b = BokehStreamer([ SensorEvent(ts=0,val=10,sensor_id="temp" ) ], io_loop=loop)
    p.subscribe(b)
 
    scheduler = Scheduler(loop)
    scheduler.schedule_periodic(p, 0.5) # sample twice every second
    scheduler.run_forever()
    self.assertTrue(vo.completed,
        "Schedule exited before validation observer completed")
    print("That's all folks")
 def test_passthrough_as_a_method(self):
     """Verify that, when passthrough is used as a method, it can still take
     thunks.
     """
     scheduler = Scheduler(asyncio.get_event_loop())
     luxpub = SensorPub(ValueListSensor('lux-2', lux_data))
     vs1 = ValidationSubscriber([450, 600], self)
     vs2 = ValidationSubscriber(lux_data, self)
     luxpub.passthrough(compose(where(lambda evt: evt.val > 300),
                                vs1)).subscribe(vs2)
     scheduler.schedule_periodic(luxpub, 0.5)
     scheduler.run_forever()
     self.assertTrue(vs1.completed)
     self.assertTrue(vs2.completed)
Ejemplo n.º 7
0
def main_linear():
    obs_stream = from_iterable(
        iter([[[[1.0, 1.0], [2.0, 2.0]], [1.0, 2.0]],
              [[[6.0, 6.0], [9.0, 9.0]], [6.0, 9.0]]]))
    pred_stream = from_iterable(iter([[3.0, 3.0]]))
    model = LinearRegressionModel()
    obs_stream.subscribe(model, topic_mapping=('default', 'train'))
    obs_stream.subscribe(print)

    pred_stream.subscribe(model, topic_mapping=('default', 'observe'))
    model.subscribe(print, topic_mapping=('predict', 'default'))
    scheduler = Scheduler(asyncio.get_event_loop())
    scheduler.schedule_periodic(obs_stream, 1)
    scheduler.schedule_periodic(pred_stream, 5)
    scheduler.run_forever()
 def test_where(self):
     s = make_test_publisher_from_vallist(1, value_stream)
     w = s.where(predicate)
     w.output()
     vo = ValidationSubscriber(expected_stream, self)
     w.subscribe(vo)
     scheduler = Scheduler(asyncio.get_event_loop())
     scheduler.schedule_periodic(s, 0.5)  # sample twice every second
     s.print_downstream()
     scheduler.run_forever()
     self.assertTrue(
         vo.completed,
         "Schedule exited before validation observer completed")
     self.assertTrue(vo.completed)
     print("That's all folks")
Ejemplo n.º 9
0
    def test_case(self):
        sensor = make_test_publisher(1, stop_after_events=10)
        split = SplitPublisher()
        sensor.subscribe(split)
        split.subscribe(lambda x: print("above:%s" % x),
                        topic_mapping=('above', 'default'))
        split.subscribe(lambda x: print("below:%s" % x),
                        topic_mapping=('below', 'default'))
        split.subscribe(lambda x: print("within:%s" % x),
                        topic_mapping=('within', 'default'))

        scheduler = Scheduler(asyncio.get_event_loop())
        scheduler.schedule_periodic(sensor, 1)

        sensor.print_downstream()
        scheduler.run_forever()
        print("that's all")
Ejemplo n.º 10
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.º 11
0
 def test_multiple_timeouts(self):
     """In this testcase, we pass two events, drop two events, etc.
     We set the timeout to a bit longer than the event interval. The last
     good value is supplied when the timeout expires. Thus, we should see
     two good events, two repeats of the first event, two good events, etc.
     """
     sensor = make_test_publisher_from_vallist(1, sensor_values)
     drop = DropPeriodic(sensor, N=2)
     scheduler = Scheduler(asyncio.get_event_loop())
     vo = ValidationSubscriber(expected_values_multiple_timeouts, self)
     drop.supply_event_when_timeout(EventWatcher(), scheduler,
                                    1.1).output().subscribe(vo)
     scheduler.schedule_periodic(sensor, 1)
     sensor.print_downstream()
     scheduler.run_forever()
     self.assertTrue(
         vo.completed,
         "Schedule exited before validation observer completed")
Ejemplo n.º 12
0
 def test_supplying_event_on_timeout(self):
     """In this testcase, we drop every other event.
     We set the timeout to a bit longer than the event interval of
     one second. It then supplies the previous event. The resulting
     output stream will show every other value repeated twice.
     """
     sensor = make_test_publisher_from_vallist(1, sensor_values)
     drop = DropPeriodic(sensor)
     scheduler = Scheduler(asyncio.get_event_loop())
     vo = ValidationSubscriber(expected_values, self)
     drop.supply_event_when_timeout(EventWatcher(), scheduler,
                                    1.1).output().subscribe(vo)
     scheduler.schedule_periodic(sensor, 1)
     sensor.print_downstream()
     scheduler.run_forever()
     self.assertTrue(
         vo.completed,
         "Schedule exited before validation observer completed")
Ejemplo n.º 13
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.º 14
0
def test_bokeh_manager():
    loop = asyncio.get_event_loop()
    s1 = ValueListSensor(1, value_stream)
    p1 = SensorPub(s1)
    s2 = ValueListSensor(1, value_stream2)
    p2 = SensorPub(s2)

    bm = BokehPlotManager()
    bplot1 = BokehPlot('Sensor1', y_axis_label='value')  
    bplot2 = BokehPlot('Sensor2', y_axis_label='value')  
    bm.register(bplot1)
    bm.register(bplot2)
    p1.map(lambda v: ('Sensor1', v)  ).subscribe(bm)
    p2.map(lambda v: ('Sensor2', v)  ).subscribe(bm)
    bm.start()
 
    scheduler = Scheduler(loop)
    scheduler.schedule_periodic(p1, 1.0) # sample every second
    scheduler.schedule_periodic(p2, 0.5) # sample twice every second
    scheduler.run_forever()
    # self.assertTrue(vo.completed,
    #     "Schedule exited before validation observer completed")
    print("That's all folks")
Ejemplo n.º 15
0
def main_kalman():
    dim_x = 2
    dim_u = 1
    dim_z = 1
    initial_state_mean = np.array([[1.0], [0.0]])
    initial_state_covariance = 1000 * np.eye(dim_x)

    F = np.array([[1., 1.], [0., 1.]])
    B = np.zeros((2, 1))
    Q = Q_discrete_white_noise(dim=2, dt=0.1, var=0.13)

    H = np.array([[1., 0.]])
    R = 5 * np.eye(1)

    model = KalmanFilterModel(dim_x, dim_u, dim_z, initial_state_mean,
                              initial_state_covariance, F, B, Q, H, R)
    measurement_stream = from_iterable(iter([[1.0], [0.0]]))
    # measurement_stream = from_iterable(iter([ np.array([ [1.0, 1.0] ]) ]))
    measurement_stream.subscribe(model, topic_mapping=('default', 'observe'))
    model.subscribe(print, topic_mapping=('predict', 'default'))

    scheduler = Scheduler(asyncio.get_event_loop())
    scheduler.schedule_periodic(measurement_stream, 1)
    scheduler.run_forever()
Ejemplo n.º 16
0
        if self.events_since_last == self.period:
            val = median(self.samples)
            event = SensorEvent(sensor_id=v.sensor_id, ts=v.ts, val=val)
            self.events_since_last = 0
            return event
        else:
            self.last_event = v  # save in case we complete before completing a period
            return None

    def complete(self):
        if self.events_since_last > 0:
            # if we have some partial state, we emit one final event that
            # averages whatever we saw since the last emission.
            return SensorEvent(sensor_id=self.last_event.sensor_id,
                               ts=self.last_event.ts,
                               val=median(
                                   self.samples[0:self.events_since_last]))


SENSOR_ID = 'sensor-1'
scheduler = Scheduler(asyncio.get_event_loop())
sensor = SensorPub(
    RandomSensor(SENSOR_ID, mean=10, stddev=5, stop_after_events=12))
sensor.csv_writer('raw_data.csv').subscribe(
    lambda x: print("raw data: %s" % repr(x)))
sensor.transduce(PeriodicMedianTransducer()).mqtt_async_send(
    URL, SENSOR_ID, scheduler).output()
scheduler.schedule_periodic(sensor, 0.5)
scheduler.run_forever()
print("that's all folks")
Ejemplo n.º 17
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!")
Ejemplo n.º 18
0
# Copyright 2016 by MPI-SWS and Data-Ken Research.
# Licensed under the Apache 2.0 License.
import asyncio

from antevents.tcpstreamer import TcpStreamObserver
from antevents.base import make_test_publisher, Scheduler

loop = asyncio.get_event_loop()

s = make_test_publisher(1, stop_after_events=10)

t = TcpStreamObserver(loop, "localhost", 2991)

s.subscribe(t)

scheduler = Scheduler(loop)
scheduler.schedule_periodic(s, 2)  # sample once every 2 seconds

scheduler.run_forever()
scheduler.stop()