comparison rust/src/types.rs @ 594:aff50ee77252 rust

rust working better now with streams and sinks.
author Matt Johnston <matt@ucc.asn.au>
date Wed, 04 Jan 2017 17:18:44 +0800
parents bf138339d20a
children e87655ed8429
comparison
equal deleted inserted replaced
593:bf138339d20a 594:aff50ee77252
1 use std::collections::HashMap;
2 use std::time::Duration;
3
1 #[derive(RustcDecodable, RustcEncodable, Debug, Clone)] 4 #[derive(RustcDecodable, RustcEncodable, Debug, Clone)]
2 pub struct Params { 5 pub struct Params {
3 pub fridge_setpoint: f32, 6 pub fridge_setpoint: f32,
4 pub fridge_difference: f32, 7 pub fridge_difference: f32,
5 pub overshoot_delay: u32, 8 pub overshoot_delay: u32,
39 } 42 }
40 } 43 }
41 } 44 }
42 45
43 #[derive(Debug)] 46 #[derive(Debug)]
44 pub struct Reading {
45 name: String,
46 value: Option<f32>,
47 }
48
49 impl Reading {
50 pub fn new(name: String, value: f32) -> Reading {
51 Reading { name: name, value: Some(value) }
52 }
53 pub fn new_none(name: String) -> Reading {
54 Reading { name: name, value: None }
55 }
56 }
57
58 #[derive(Debug)]
59 pub struct Readings { 47 pub struct Readings {
60 temps: Vec<Vec<Reading>>, 48 temps: HashMap<String, Option<f32>>,
61 } 49 }
62 50
63 impl Readings { 51 impl Readings {
64 pub fn new() -> Readings { 52 pub fn new() -> Readings {
65 Readings { 53 Readings {
66 temps: Vec::new(), 54 temps: HashMap::new(),
67 } 55 }
68 } 56 }
69 57
70 pub fn push(&mut self, vals: Vec<Reading>) { 58 pub fn add(&mut self, name: &str, v: Option<f32>) {
71 self.temps.push(vals); 59 if let Some(prev) = self.temps.insert(name.to_string(), v) {
60 warn!("Replaced existing reading '{}' {:?} -> {:?}",
61 name, prev, v);
62 }
72 } 63 }
73 64
74 pub fn fridge(&self) -> Option<f32> { 65 pub fn fridge(&self) -> Option<f32> {
75 unimplemented!(); 66 if let Some(t) = self.temps.get("fridge") {
67 t.clone()
68 } else {
69 warn!("No fridge reading was added");
70 None
71 }
76 } 72 }
77 73
78 pub fn wort(&self) -> Option<f32> { 74 pub fn wort(&self) -> Option<f32> {
79 unimplemented!(); 75 if let Some(t) = self.temps.get("wort") {
76 t.clone()
77 } else {
78 warn!("No wort reading was added");
79 None
80 }
80 } 81 }
81 } 82 }
82 83