def test_influx_output(self):
        loop = asyncio.get_event_loop()
        s = ValueListSensor(1, value_stream)
        p = SensorAsOutputThing(s)
        b = InfluxDBWriter(msg_format=Sensor(series_name='Sensor',
                                             fields=['val', 'ts'],
                                             tags=['sensor_id']),
                           generate_timestamp=False,
                           username=INFLUXDB_USER,
                           password=INFLUXDB_PASSWORD,
                           database=INFLUXDB_DATABASE)
        p.connect(b)

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

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

        # Play back using an output thing
        p = InfluxDBReader('SELECT * FROM Sensor;',
                           database=INFLUXDB_DATABASE,
                           username=INFLUXDB_USER,
                           password=INFLUXDB_PASSWORD)
        p.connect(CallableAsInputThing(print))

        scheduler = Scheduler(loop)
        scheduler.schedule_periodic(p, 0.2)  # sample five times every second
        scheduler.run_forever()
        print("That's all folks")
Exemplo n.º 2
0
    def test_function_filter_error_handling(self):
        """Verify the error handling functionality of the function filter. We do
        this by connecting two downstream paths to the sensor. The first includes
        a function filter that throws an error when it encouters a sensor reading
        of 120. This should disconnect th stream at this point. The second is
        a normal validation input thing. It is connected directly to the sensor,
        and thus should not see any errors.
        """
        s = ValueListSensor(1, value_stream)
        st = SensorAsOutputThing(s)
        captured_list_ref = [[]]
        got_completed_ref = [
            False,
        ]
        got_on_error_ref = [
            False,
        ]

        def on_next_throw_exc(self, x):
            if x.val == 120:
                raise Exception("expected exc")
            else:
                captured_list_ref[0].append(x.val)
                self._dispatch_next(x)

        def on_completed(self):
            got_completed_ref[0] = True
            self._dispatch_completed()

        def on_error(self, e):
            got_on_error_ref[0] = True
            self._dispatch_error(e)

        ff = FunctionFilter(st,
                            on_next=on_next_throw_exc,
                            on_completed=on_completed,
                            on_error=on_error)
        ct = CaptureInputThing(expecting_error=True)
        ff.map(lambda x: x.val).connect(ct)
        vo = ValidationInputThing(value_stream,
                                  self.test_function_filter_error_handling)
        st.connect(vo)
        st.print_downstream()
        scheduler = Scheduler(asyncio.get_event_loop())
        scheduler.schedule_periodic(st, 0.5)  # sample twice every second
        scheduler.run_forever()
        self.assertTrue(
            vo.completed,
            "Schedule exited before validation observer completed")
        self.assertFalse(ct.completed,
                         "Capture thing should not have completed")
        self.assertTrue(ct.errored, "Capture thing should have seen an error")
        self.assertFalse(got_completed_ref[0])
        self.assertTrue(got_on_error_ref[0])
        self.assertEqual([20, 30, 100], ct.events,
                         "Capture thing event mismatch")
        self.assertEqual([20, 30, 100], captured_list_ref[0],
                         "captured_list_ref mismatch")
        print("That's all folks")
def setup(threshold=25):
    lux = SensorAsOutputThing(LuxSensor())
    lux.connect(print)
    lux.csv_writer(os.path.expanduser('~/lux.csv'))
    led = GpioPinOut()
    actions = lux.map(lambda event: event.val > threshold)
    actions.connect(led)
    actions.connect(lambda v: print('ON' if v else 'OFF'))
    lux.print_downstream()
    return (lux, led)
Exemplo n.º 4
0
def setup(broker, threshold):
    lux = SensorAsOutputThing(LuxSensor())
    lux.connect(print)
    led = GpioPinOut()
    actions = lux.map(lambda event: event.val > threshold)
    actions.connect(led)
    actions.connect(lambda v: print('ON' if v else 'OFF'))
    lux.to_json().connect(MQTTWriter(broker, topics=[('bogus/bogus', 0)]))
    lux.print_downstream()
    return (lux, led)
Exemplo n.º 5
0
 def schedule(self):
     self.logger.info("Scheduling sensors...")
     informer = Informer(data=self.data, id=self.id, conn=self.conn)
     informer_out = SensorAsOutputThing(informer)
     informer_out.connect(StdoutConnector(informer.info))
     informer_out.connect(
         self.conn.writer('informer',
                          informer.info,
                          topic='micronet/devices/' + self.id + '/online'))
     self.scheduler.schedule_periodic(informer_out, 5)
     for k, v in self.sensors.items():
         id = k.split(':')[0]
         unit = id
         if 'unit' in v:
             unit = v['unit']
         try:
             module = __import__("sensor_" + unit)
             SensorClass = getattr(module, to_pascal_case(id + "_sensor"))
             v['id'] = k
             sensor = SensorClass(**v)
             sensor_output = SensorAsOutputThing(sensor)
             self.data['sensors'][k] = sensor.info
             sensor_output.connect(StdoutConnector(sensor.info))
             sensor_output.connect(self.conn.writer(k, sensor.info))
             self.scheduler.schedule_periodic(sensor_output, v['freq'])
             self.logger.info("Sensor '{0}' sampling every {1}s".format(
                 k, v['freq']))
         except Exception as e:
             self.logger.warning(e)
Exemplo n.º 6
0
 def test_pandas(self):
     s = ValueListSensor(1, value_stream)
     p = SensorAsOutputThing(s)
     import thingflow.adapters.pandas
     import numpy
     w = thingflow.adapters.pandas.PandasSeriesWriter()
     p.connect(w)
     sch = Scheduler(asyncio.get_event_loop())
     sch.schedule_recurring(p)
     sch.run_forever()
     self.assertTrue(w.result is not None, "Result of pandas never set")
     # now we verify each element
     for (i, v) in enumerate(value_stream):
         pv = w.result[i]
         self.assertTrue(
             isinstance(pv, numpy.int64),
             "Expecting pandas value '%s' to be numpy.int64, but instead was %s"
             % (pv, repr(type(pv))))
         self.assertEqual(
             v, pv, "Pandas value '%s' not equal to original value '%s'" %
             (repr(pv), repr(v)))
     print("Validate pandas array")