use std::sync::Arc;
use r2d2;
use redis::Commands;
use redis::RedisError;
use ::api;
use ::types::RedisPool;
pub struct SensorSpec {
pub template: Box<api::SensorTemplate>,
pub data_key: String,
}
error_type! {
#[derive(Debug)]
pub enum SensorError {
UnknownSensor(String) {
desc (sensor) &sensor;
},
Redis(RedisError) {
cause;
},
R2d2(r2d2::GetTimeout) {
cause;
}
}
}
pub type SafeSensorSpecs = Arc<Vec<SensorSpec>>;
impl SensorSpec {
pub fn get_sensor_value(&self, redis_pool: RedisPool) -> Result<String, SensorError> {
let conn = try!(redis_pool.get());
let value: String = try!(conn.get(&*self.data_key));
Ok(value)
}
pub fn set_sensor_value(&self, redis_pool: RedisPool, value: &str) -> Result<(), SensorError> {
let conn = try!(redis_pool.get());
let _: String = try!(conn.set(&*self.data_key, value));
Ok(())
}
}