annotate rust/src/sensor.rs @ 591:4a944663fa8d rust

more skeleton
author Matt Johnston <matt@ucc.asn.au>
date Fri, 23 Dec 2016 00:33:19 +0800
parents dccd8504aa38
children 03b48ec0bb03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
1 extern crate tokio_core;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
2 extern crate futures;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
3
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
4 use std::time::Duration;
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
5 use std::io;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
7 use tokio_core::reactor::Interval;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
8 use tokio_core::reactor::Handle;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
9 use futures::Stream;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
10
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
11 #[derive(Debug)]
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
12 pub struct Reading {
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
13 name: String,
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
14 value: Option<f32>,
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
15 }
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 pub struct Sensor {
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
18 current: f32,
591
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
19 suf: String,
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
22 impl Sensor {
591
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
23 fn step(&mut self) -> Vec<Reading> {
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
24 let mut r = Vec::new();
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
25 self.current = self.current + 0.1;
591
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
26 r.push(Reading { name: "aaa".to_string() + &self.suf, value: Some(self.current) });
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
27 r.push(Reading { name: "b".to_string() + &self.suf, value: Some(self.current/3.0) });
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
28 r
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
29 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
30
591
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
31 fn new(suffix: String) -> Self {
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
32 Sensor { current: 22.0, suf: suffix }
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
33 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
34
591
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
35 pub fn run(handle: &Handle, rate: u64, suffix: String) -> Box<Stream<Item=Vec<Reading>, Error=io::Error>> {
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
36 let mut s = Sensor::new(suffix);
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
37
591
4a944663fa8d more skeleton
Matt Johnston <matt@ucc.asn.au>
parents: 590
diff changeset
38 let dur = Duration::from_millis(rate);
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
39 Interval::new(dur, handle).unwrap().map(move |()| {
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
40 s.step()
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
41 }).boxed()
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44