diff rust/src/main.rs @ 588:038734052b20 rust

fiddling with futures-rs instead
author Matt Johnston <matt@ucc.asn.au>
date Fri, 16 Dec 2016 01:10:57 +0800
parents 646f03870762
children f2508125adf1
line wrap: on
line diff
--- a/rust/src/main.rs	Wed Dec 14 00:15:14 2016 +0800
+++ b/rust/src/main.rs	Fri Dec 16 01:10:57 2016 +0800
@@ -1,24 +1,74 @@
-extern crate robots;
+#![feature(conservative_impl_trait)]
+extern crate tokio_core;
+extern crate futures;
+
+use std::time::Duration;
+use std::cell::RefCell;
 
-mod sensor;
+use tokio_core::reactor::Interval;
+use tokio_core::reactor::Core;
+use tokio_core::reactor::Handle;
+use futures::Stream;
+use futures::Future;
+use futures::IntoFuture;
+
+pub struct Reading {
+    name: String,
+    value: Option<f32>,
+}
+
+pub type Readings = Vec<Reading>;
+
+pub struct Sensor {
+    current: f32,
+}
 
-use std::sync::Arc;
-use std::time::Duration;
+impl Sensor {
+
+    fn step(&mut self) -> Readings {
+        let mut r = Vec::new();
+        self.current = self.current + 0.1;
+        r.push(Reading { name: "aaa".to_string(), value: Some(self.current) });
+        r
+    }
 
-use robots::actors::{Actor, ActorSystem, ActorCell, ActorContext, Props};
+    pub fn new() -> Sensor {
+        Sensor { current: 22.0 }
+    }
+
+    pub fn run(handle: &Handle) -> impl Stream<Item=Readings> {
+        let mut s = Sensor::new();
+
+        let dur = Duration::from_millis(400);
+        Interval::new(dur, handle).unwrap().map(move |()| {
+            println!("each one");
+            // TODO read the sensor here
+            s.step()
+        }   )
+    }
+}
 
 fn main() {
     println!("Wort Templog");
-    let actor_system = ActorSystem::new("templog".to_string());
+
+    let mut core = Core::new().unwrap();
+    let handle = core.handle();
 
-    let props = Props::new(Arc::new(sensor::Sensor::new), ());
-    let sensor = actor_system.actor_of(props, "sensor".to_string());
+    let s = Sensor::run(&handle);
+
+    let mut re = Readings::new();
 
-    actor_system.tell(sensor, "yeah".to_string());
-    
-    std::thread::sleep(Duration::from_millis(100));
-    actor_system.shutdown();
+    let h = s.for_each(|r| {
+        re = r;
+        for rx in &re {
+            match rx.value {
+                Some(x) => println!("re is {} {}", rx.name, x),
+                None => println!("re is {} broken", rx.name),
+            }
+        };
+        Ok(())
+    });
 
-
+    core.run(h);
 }