Mercurial > templog
comparison rust/src/main.rs @ 590:dccd8504aa38 rust
it runs
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 21 Dec 2016 21:40:32 +0800 |
parents | f2508125adf1 |
children | 4a944663fa8d |
comparison
equal
deleted
inserted
replaced
589:f2508125adf1 | 590:dccd8504aa38 |
---|---|
1 #![feature(conservative_impl_trait)] | |
2 extern crate tokio_core; | 1 extern crate tokio_core; |
3 extern crate futures; | 2 extern crate futures; |
4 | 3 |
5 use std::time::Duration; | 4 use tokio_core::reactor::Core; |
6 use std::cell::RefCell; | 5 use futures::Stream; |
7 use std::io::{self, Read, Write}; | |
8 | 6 |
9 use tokio_core::reactor::Interval; | 7 mod sensor; |
10 use tokio_core::reactor::Core; | |
11 use tokio_core::reactor::Handle; | |
12 use futures::{Future, failed, Poll, Async, Stream}; | |
13 | |
14 pub struct Reading { | |
15 name: String, | |
16 value: Option<f32>, | |
17 } | |
18 | |
19 pub type Readings = Vec<Reading>; | |
20 | |
21 pub struct Sensor { | |
22 current: f32, | |
23 } | |
24 | |
25 struct Periodic<T> { | |
26 interval: Interval, | |
27 } | |
28 | |
29 trait Ticker { | |
30 type TickItem; | |
31 fn tick(&self) -> Self::TickItem; | |
32 } | |
33 | |
34 impl<T: Ticker> Stream for Periodic<T> { | |
35 type Item = T::TickItem; | |
36 type Error = io::Error; | |
37 | |
38 fn poll(&mut self) -> Poll<Option<Self::Item>, io::Error> { | |
39 self.interval.poll().map(|a| { | |
40 if let Async::Ready(t) = a { | |
41 Async::Ready(self.tick()) | |
42 } else { | |
43 a | |
44 } | |
45 }) | |
46 } | |
47 } | |
48 | |
49 impl Sensor { | |
50 | |
51 fn step(&mut self) -> Readings { | |
52 let mut r = Vec::new(); | |
53 self.current = self.current + 0.1; | |
54 r.push(Reading { name: "aaa".to_string(), value: Some(self.current) }); | |
55 r | |
56 } | |
57 | |
58 pub fn new() -> Sensor { | |
59 Sensor { current: 22.0 } | |
60 } | |
61 | |
62 pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> { | |
63 let mut s = Sensor::new(); | |
64 | |
65 let dur = Duration::from_millis(400); | |
66 Interval::new(dur, handle).unwrap().map(move |()| { | |
67 println!("each one"); | |
68 // TODO read the sensor here | |
69 s.step() | |
70 } ) | |
71 } | |
72 } | |
73 | 8 |
74 fn main() { | 9 fn main() { |
75 println!("Wort Templog"); | 10 println!("Wort Templog"); |
76 | 11 |
77 let mut core = Core::new().unwrap(); | 12 let mut core = Core::new().unwrap(); |
78 let handle = core.handle(); | 13 let handle = core.handle(); |
79 | 14 |
80 let s = Sensor::run(&handle); | 15 let s = sensor::Sensor::run(&handle); |
81 | |
82 let mut re = Readings::new(); | |
83 | 16 |
84 let h = s.for_each(|r| { | 17 let h = s.for_each(|r| { |
85 re = r; | 18 println!("readings {:?}", r); |
86 for rx in &re { | |
87 match rx.value { | |
88 Some(x) => println!("re is {} {}", rx.name, x), | |
89 None => println!("re is {} broken", rx.name), | |
90 } | |
91 }; | |
92 Ok(()) | 19 Ok(()) |
93 }); | 20 }); |
94 | 21 |
95 core.run(h); | 22 core.run(h); |
96 } | 23 } |