view rust/src/types.rs @ 610:af0dac00d40b rust

untested step integrator
author Matt Johnston <matt@ucc.asn.au>
date Thu, 23 Feb 2017 23:27:09 +0800
parents 7bda01659426
children f3e39e2107fd
line wrap: on
line source

use std::collections::HashMap;
use std::time::{Duration,Instant};
use std::error::Error;
use std::fmt;
use std::cmp;
use std::cell::Cell;
use std::iter::FromIterator;

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,
}

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

    pub fn receive(&mut self, p: &Params, epoch: &String)
    {

    }
}

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


/// Call closures with a rate limit. Useful for log message ratelimiting
pub struct NotTooOften {
    last: Cell<Instant>,
    limit: Duration,
}

impl NotTooOften {
    pub fn new(limit_secs: u64) -> Self {
        NotTooOften {
            limit: Duration::new(limit_secs, 0),
            last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)),
        }
    }

    pub fn and_then<F>(&self, op: F)
        where F: Fn() {
        let now = Instant::now();
        if now - self.last.get() > self.limit {
            self.last.set(now);
            op();
        }
    }
}

struct Period {
    start: Instant,
    end: Option<Instant>,
}

pub struct StepIntegrator {
    on_periods: Vec<Period>,
    limit: Duration,
}

impl StepIntegrator {
    pub fn new(limit: Duration) -> Self {
        StepIntegrator {
            on_periods: Vec::new(),
            limit: limit,
        }
    }

    pub fn turn(&mut self, on: bool) {
        self.trim();

        if self.on_periods.is_empty() {
            self.on_periods.push( Period { start: Instant::now(), end: None });
            return;
        }

        let current_on = self.on_periods.last().unwrap().end.is_none();
        if on == current_on {
            // state is unchanged
            return;
        }

        if on {
            self.on_periods.push( Period { start: Instant::now(), end: None });
        } else {
            self.on_periods.last_mut().unwrap().end = Some(Instant::now());
        }
    }

    pub fn set_limit(&mut self, limit: Duration) {
        self.limit = limit;
        self.trim();
    }

    fn integrate(&self) -> Duration {
        let durs = self.on_periods.iter().map(|p| {
            let end = p.end.unwrap_or_else(|| Instant::now());
            end - p.start
        });
        // TODO rust 1.16: impl Sum for Duration 
        // durs.sum()
        durs.fold(Duration::new(0,0), |acc, d| acc + d)
    }

    fn trim(&mut self) {
        let begin = Instant::now() - self.limit;

        self.on_periods.retain(|p| {
            // remove expired endtimes
            if let Some(e) = p.end {
                e >= begin && e != p.start
            } else {
                true
            }
        });

        for p in &mut self.on_periods {
            p.start = cmp::max(p.start, begin);
        }
    }
}