changeset 604:278f1002b5c7 rust

sensor regex, custom error type
author Matt Johnston <matt@ucc.asn.au>
date Thu, 16 Feb 2017 23:53:46 +0800
parents b45b8b4cf0f5
children 8dd63473b6d8
files rust/Cargo.lock rust/Cargo.toml rust/src/main.rs rust/src/sensor.rs rust/src/types.rs
diffstat 5 files changed, 55 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Thu Feb 16 23:19:12 2017 +0800
+++ b/rust/Cargo.lock	Thu Feb 16 23:53:46 2017 +0800
@@ -6,8 +6,10 @@
  "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "futures 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
  "futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_derive 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)",
--- a/rust/Cargo.toml	Thu Feb 16 23:19:12 2017 +0800
+++ b/rust/Cargo.toml	Thu Feb 16 23:53:46 2017 +0800
@@ -15,6 +15,8 @@
 docopt = "0.7"
 rustc-serialize = "0.3"
 time = "0.1"
+lazy_static = "0.2"
+regex = "0.2"
 
 [dependencies.toml]
 git = "https://github.com/alexcrichton/toml-rs"
--- a/rust/src/main.rs	Thu Feb 16 23:19:12 2017 +0800
+++ b/rust/src/main.rs	Thu Feb 16 23:53:46 2017 +0800
@@ -6,6 +6,9 @@
 extern crate rustc_serialize;
 extern crate time;
 
+#[macro_use] 
+extern crate lazy_static;
+
 #[macro_use]
 extern crate serde_derive;
 extern crate serde;
--- a/rust/src/sensor.rs	Thu Feb 16 23:19:12 2017 +0800
+++ b/rust/src/sensor.rs	Thu Feb 16 23:53:46 2017 +0800
@@ -1,6 +1,7 @@
 extern crate tokio_core;
 extern crate futures;
 extern crate futures_cpupool;
+extern crate regex;
 
 use std::time::Duration;
 use std::io;
@@ -9,6 +10,7 @@
 use std::path::PathBuf;
 use std::error::Error;
 use std::sync::Arc;
+use std::str::FromStr;
 
 use tokio_core::reactor::Interval;
 use tokio_core::reactor::Handle;
@@ -16,8 +18,6 @@
 use types::*;
 use ::Config;
 
-
-
 pub trait Sensor {
     fn stream(&self, handle: &Handle)
         -> Box<Stream<Item=Readings, Error=io::Error>>;
@@ -52,11 +52,23 @@
     }
 
     fn read_sensor(&self, n: &str) -> Result<f32, Box<Error>> {
+        lazy_static! {
+            // multiline
+            static ref THERM_RE: regex::Regex = regex::Regex::new("(?m).* YES\n.*t=(.*)\n").unwrap();
+        }
         let mut path = PathBuf::from(&self.config.SENSOR_BASE_DIR);
         path.push(n);
         path.push("w1_slave");
-        let f = BufReader::new(File::open(path)?);
-        Ok(3.4)
+        let mut s = String::new();
+        File::open(path)?.read_to_string(&mut s)?;
+        let caps = THERM_RE.captures(&s).ok_or_else(|| {
+                TemplogError::new("Bad sensor contents match")
+            })?;
+        let v = caps.get(1).ok_or_else(|| {
+                TemplogError::new("Bad field contents match")
+            })?.as_str();
+
+        Ok(f32::from_str(v)?)
     }
 
     fn sensor_names(&self) -> Result<Vec<String>, Box<Error>> {
--- a/rust/src/types.rs	Thu Feb 16 23:19:12 2017 +0800
+++ b/rust/src/types.rs	Thu Feb 16 23:53:46 2017 +0800
@@ -1,5 +1,8 @@
 use std::collections::HashMap;
 use std::time::Duration;
+use std::error::Error;
+use std::fmt;
+
 use serde::{Deserialize,Serialize};
 
 #[derive(Deserialize, Serialize, Debug)]
@@ -65,3 +68,32 @@
     }
 }
 
+#[derive(Debug)]
+pub struct TemplogError {
+    desc: String,
+}
+
+impl Error for TemplogError {
+    fn description(&self) -> &str { &self.desc }
+    fn cause(&self) -> Option<&Error> { None }
+}
+
+impl fmt::Display for TemplogError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "TemplogError: {}", self.desc);
+        Ok(())
+    }
+
+
+}
+
+impl TemplogError {
+    pub fn new(desc: &str) -> Self {
+        TemplogError { 
+            desc: desc.to_string(),
+        }
+    }
+}
+
+
+