# HG changeset patch # User Matt Johnston # Date 1487260426 -28800 # Node ID 278f1002b5c7e7e447485517f18a482716ad5137 # Parent b45b8b4cf0f5df9bd3c953e6c72db57e8ddb6311 sensor regex, custom error type diff -r b45b8b4cf0f5 -r 278f1002b5c7 rust/Cargo.lock --- 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)", diff -r b45b8b4cf0f5 -r 278f1002b5c7 rust/Cargo.toml --- 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" diff -r b45b8b4cf0f5 -r 278f1002b5c7 rust/src/main.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; diff -r b45b8b4cf0f5 -r 278f1002b5c7 rust/src/sensor.rs --- 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>; @@ -52,11 +52,23 @@ } fn read_sensor(&self, n: &str) -> Result> { + 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, Box> { diff -r b45b8b4cf0f5 -r 278f1002b5c7 rust/src/types.rs --- 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(), + } + } +} + + +