Browse Source

Initial commit

Thomas Dy 9 years ago
commit
03031808c5
5 changed files with 92 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 7 0
      Cargo.toml
  3. 3 0
      src/comm.rs
  4. 33 0
      src/main.rs
  5. 47 0
      src/sensors.rs

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+target
+Cargo.lock

+ 7 - 0
Cargo.toml

@@ -0,0 +1,7 @@
+[package]
+name = "panel"
+version = "0.1.0"
+authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
+
+[dependencies]
+time = "0.1"

+ 3 - 0
src/comm.rs

@@ -0,0 +1,3 @@
+use std::sync::mpsc::{Sender};
+
+pub type Channel = Sender<String>;

+ 33 - 0
src/main.rs

@@ -0,0 +1,33 @@
+mod sensors;
+mod comm;
+
+use comm::Channel;
+use std::io;
+use std::sync::mpsc;
+use std::thread;
+
+
+fn main() {
+    let (tx, rx) = mpsc::channel::<String>();
+    make_thread(&tx, stdin);
+    make_thread(&tx, sensors::sensors);
+    loop {
+        println!("%{{r}}{}", rx.recv().ok().unwrap());
+    }
+}
+
+fn make_thread(tx: &Channel, func: fn(&Channel) -> ()) {
+    let thread_tx = tx.clone();
+    thread::spawn(move || {
+        func(&thread_tx);
+    });
+}
+
+fn stdin(tx: &Channel) {
+    let mut line = String::new();
+    loop {
+        io::stdin().read_line(&mut line).ok().expect("Failed to read line");
+        tx.send(line.clone()).ok().expect("Couldn't send data");
+    }
+}
+

+ 47 - 0
src/sensors.rs

@@ -0,0 +1,47 @@
+extern crate time;
+
+use comm::Channel;
+use std::fs::File;
+use std::io::prelude::*;
+use std::thread;
+
+pub fn sensors(tx: &Channel) {
+    loop {
+        let right = vec![
+            temp(),
+            time_utc(),
+            datetime()
+        ];
+
+        tx.send(format!("{}", reduce(right))).ok().expect("Couldn't send data");
+        thread::sleep_ms(1000);
+    }
+}
+
+fn reduce(arr: Vec<String>) -> String {
+    let result = arr.into_iter()
+        .fold("".to_string(), |acc, elem| format!("{} | {}", acc, elem));
+    if result.len() > 0 { result[3..].to_string() } else { result }
+}
+
+fn temp() -> String {
+    let path = "/sys/class/thermal/thermal_zone0/temp";
+    let mut f = File::open(path).unwrap();
+    let mut s = String::new();
+    assert!(f.read_to_string(&mut s).is_ok());
+    let i : u32 = s.trim().parse().unwrap();
+    format!("{}°C", i/1000)
+}
+
+fn datetime() -> String {
+    let now = time::now();
+    let localtime = time::strftime("%Y-%m-%d %H:%M", &now);
+    localtime.unwrap()
+}
+
+fn time_utc() -> String {
+    let now = time::now_utc();
+    let utctime = time::strftime("%H:%M", &now);
+    format!("UTC {}", utctime.unwrap())
+}
+