comparison rust/src/config.rs @ 634:a5721c02d3ee rust

build succeeds
author Matt Johnston <matt@ucc.asn.au>
date Sun, 22 Sep 2019 20:35:40 +0800
parents f3e39e2107fd
children 4424a8b30f9c
comparison
equal deleted inserted replaced
633:490e9e15b98c 634:a5721c02d3ee
1 extern crate toml; 1 use serde::{Serialize,Deserialize};
2 2
3 use std::error::Error; 3 use super::types::*;
4 use std::fs::File;
5 use std::io::Read;
6 use serde::{Serialize,Deserialize,Deserializer,Serializer};
7
8 use types::*;
9 4
10 #[derive(Deserialize,Serialize,Debug,Clone)] 5 #[derive(Deserialize,Serialize,Debug,Clone)]
11 #[allow(non_snake_case)] 6 #[allow(non_snake_case)]
12 pub struct Config { 7 pub struct Config {
13 pub SENSOR_SLEEP: u64, 8 pub SENSOR_SLEEP: u64,
33 pub UPDATE_URL: String, 28 pub UPDATE_URL: String,
34 pub SETTINGS_URL: String, 29 pub SETTINGS_URL: String,
35 } 30 }
36 31
37 impl Config { 32 impl Config {
38 33 pub fn default_toml() -> &'static str {
39 pub fn default() -> Self { 34 include_str!("defconfig.toml")
40 Config {
41 SENSOR_SLEEP: 5,
42 UPLOAD_SLEEP: 83,
43
44 FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on
45 FRIDGE_WORT_INVALID_TIME: 300, // 5 mins
46
47 // 12 hours of "offline" readings stored
48 MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP
49
50 //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf")
51 PARAMS_FILE: "tempserver.conf".to_string(),
52
53 SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(),
54 FRIDGE_GPIO_PIN: 17,
55 //WORT_NAME: "28-0000042cf4dd".to_string(),
56 //FRIDGE_NAME: "28-0000042cccc4".to_string(),
57 //AMBIENT_NAME: "28-0000042c6dbb".to_string(),
58 AMBIENT_NAME: "missingambient".to_string(),
59 FRIDGE_NAME: "28-0000042c6dbb".to_string(),
60 WORT_NAME: "28-0000042cccc4".to_string(), // was fridge
61 INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(),
62
63 HMAC_KEY: "a key".to_string(),
64 SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(),
65 UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(),
66 SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(),
67 }
68 } 35 }
69 36
70 pub fn to_toml_string(&self) -> String { 37 pub fn load() -> Result<Self, TemplogError> {
71 toml::to_string(self).unwrap() 38 let mut c = config::Config::default();
72 } 39 c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml));
73 40 c.merge(config::File::with_name("local.conf")).unwrap();
74 pub fn merge(&self, conf: &str) -> Result<Self, TemplogError> { 41 c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap();
75 // convert existing and new toml into tables, combine them. 42 Ok(c.try_into().unwrap())
76 let mut new_toml = toml::from_str(conf)?;
77 let mut ex_val = toml::Value::try_from(self).unwrap();
78 let mut ex_toml = ex_val.as_table_mut().unwrap();
79 ex_toml.append(&mut new_toml);
80 // TODO: wrap the error with a better message?
81 let ret = toml::Value::Table(ex_toml.clone()).try_into()?;
82 Ok(ret)
83 }
84
85 pub fn merge_file(&self, filename: &str) -> Result<Self, TemplogError> {
86
87 let mut s = String::new();
88 File::open(filename)?.read_to_string(&mut s)?;
89
90 self.merge(&s)
91 } 43 }
92 } 44 }