def test_client_only(self): SENSOR_ID = 'sensor-1' TOPIC = get_topic_name(self) sensor = SensorAsOutputThing(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 = SensorAsOutputThing(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 = ValidateAndStopInputThing(EXPECTED, self, stop_qr) qr.select(msg_to_event).connect(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 run_example(): sensor = SensorAsOutputThing(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.connect(controller, port_mapping=('t_high', 't_high')) dispatcher.connect(controller, port_mapping=('t_low', 't_low')) dispatcher.connect(controller, port_mapping=('default', 'between')) controller.connect(BypassValveActuator()) sensor.print_downstream() scheduler = Scheduler(asyncio.get_event_loop()) scheduler.schedule_periodic(sensor, 0.5) scheduler.run_forever() print("got to the end")
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 = SensorAsOutputThing( RandomSensor(SENSOR_ID, mean=10, stddev=5, stop_after_events=12)) sensor.csv_writer('raw_data.csv').connect( 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")