view rust/src/types.rs @ 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 7bda01659426
line wrap: on
line source

use std::collections::HashMap;
use std::time::Duration;
use std::error::Error;
use std::fmt;

use serde::{Deserialize,Serialize};

#[derive(Deserialize, Serialize, Debug)]
pub struct Params {
    pub fridge_setpoint: f32,
    pub fridge_difference: f32,
    pub overshoot_delay: u32,
    pub overshoot_factor: f32,
    pub disabled: bool,
    pub nowort: bool,
    pub fridge_range_lower: f32,
    pub fridge_range_upper: f32,
}

impl Params {
    pub fn defaults() -> Params {
        Params {
            fridge_setpoint: 16.0,
            fridge_difference: 0.2,
            overshoot_delay: 720, // 12 minutes
            overshoot_factor: 1.0,
            disabled: false,
            nowort: false,
            fridge_range_lower: 3.0,
            fridge_range_upper: 3.0,
            }
    }
}

#[derive(Debug)]
pub struct ParamHolder {
    pub p: Params,
    epoch: String, // XXX or a byte array?
}

impl ParamHolder {
    pub fn new() -> ParamHolder {
        ParamHolder {
            p: Params::defaults(),
            epoch: String::new(),
        }
    }
}

#[derive(Debug)]
pub struct Readings {
    pub temps: HashMap<String, f32>,
}

impl Readings {
    pub fn new() -> Readings {
        Readings {
            temps: HashMap::new(),
        }
    }

    pub fn add(&mut self, name: &str, v: f32) {
        self.temps.insert(name.to_string(), v);
    }

    pub fn get_temp(&self, name: &str) -> Option<f32> {
        self.temps.get(name).map(|f| *f)
    }
}

#[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(),
        }
    }
}