Mercurial > templog
annotate rust/src/config.rs @ 599:f71cf1ad745f rust
updated toml serde works OK
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 07 Feb 2017 22:35:29 +0800 |
parents | d4fbfb5c46ff |
children | 9c76f3cf01ea |
rev | line source |
---|---|
595 | 1 extern crate toml; |
2 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
3 use std::fs::File; |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
4 use std::io::Read; |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
5 use serde::{Serialize,Deserialize,Deserializer,Serializer}; |
595 | 6 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
7 #[derive(Deserialize,Serialize,Debug,Clone)] |
595 | 8 #[allow(non_snake_case)] |
9 pub struct Config { | |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
10 pub SENSOR_SLEEP: u64, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
11 pub UPLOAD_SLEEP: u64, |
595 | 12 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
13 pub FRIDGE_DELAY: u64, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
14 pub FRIDGE_WORT_INVALID_TIME: u64, |
595 | 15 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
16 pub MAX_READINGS: u32, |
595 | 17 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
18 pub PARAMS_FILE: String, |
595 | 19 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
20 pub SENSOR_BASE_DIR: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
21 pub FRIDGE_GPIO_PIN: u32, |
595 | 22 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
23 pub AMBIENT_NAME: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
24 pub FRIDGE_NAME: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
25 pub WORT_NAME: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
26 pub INTERNAL_TEMPERATURE: String, |
595 | 27 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
28 pub HMAC_KEY: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
29 pub SERVER_URL: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
30 pub UPDATE_URL: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
31 pub SETTINGS_URL: String, |
595 | 32 } |
33 | |
34 impl Config { | |
35 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
36 pub fn default() -> Self { |
595 | 37 Config { |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
38 SENSOR_SLEEP: 5, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
39 UPLOAD_SLEEP: 83, |
595 | 40 |
41 FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on | |
42 FRIDGE_WORT_INVALID_TIME: 300, // 5 mins | |
43 | |
44 // 12 hours of "offline" readings stored | |
45 MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP | |
46 | |
47 //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf") | |
48 PARAMS_FILE: "tempserver.conf".to_string(), | |
49 | |
50 SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(), | |
51 FRIDGE_GPIO_PIN: 17, | |
52 //WORT_NAME: "28-0000042cf4dd".to_string(), | |
53 //FRIDGE_NAME: "28-0000042cccc4".to_string(), | |
54 //AMBIENT_NAME: "28-0000042c6dbb".to_string(), | |
55 AMBIENT_NAME: "missingambient".to_string(), | |
56 FRIDGE_NAME: "28-0000042c6dbb".to_string(), | |
57 WORT_NAME: "28-0000042cccc4".to_string(), // was fridge | |
58 INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(), | |
59 | |
60 HMAC_KEY: "a key".to_string(), | |
61 SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(), | |
62 UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(), | |
63 SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(), | |
64 } | |
65 } | |
66 | |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
67 pub fn to_toml_string(&self) -> String { |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
68 toml::to_string(self).unwrap() |
595 | 69 } |
597 | 70 |
71 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
72 pub fn merge(&self, conf: &str) -> Result<Self, String>{ |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
73 println!("config {}", conf); |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
74 let mut new_toml = try!(toml::from_str(conf).map_err(|e| e.to_string())); |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
75 let mut ex_val = toml::Value::try_from(self).unwrap(); |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
76 let mut ex_toml = ex_val.as_table_mut().unwrap(); |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
77 ex_toml.append(&mut new_toml); |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
78 let ret: Self = toml::Value::Table(ex_toml.clone()).try_into().unwrap(); |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
79 return Ok(ret) |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
80 |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
81 |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
82 /* |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
83 let mut p = toml::Parser::new(&conf); |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
84 let p = |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
85 p.parse() |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
86 .ok_or_else(|| { |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
87 format!("toml parsing failed: {:?}", p.errors) |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
88 }) |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
89 .map(|mut new_toml| { |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
90 let mut e = toml::Encoder::new(); |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
91 self.serialize(&mut e).unwrap(); // XXX what could go wrong? |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
92 let mut ex_toml = e.toml; |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
93 ex_toml.append(&mut new_toml); |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
94 let mut dec = toml::Decoder::new(toml::Value::Table(ex_toml)); |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
95 Self::deserialize(&mut dec).unwrap() // could this go wrong? |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
96 }) |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
97 */ |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
98 } |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
99 |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
100 pub fn merge_file(&self, filename: &str) -> Result<Self, String>{ |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
101 |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
102 let r = File::open(filename) |
597 | 103 .map_err(|e| e.to_string()) |
104 .and_then(|mut f| { | |
105 let mut s = String::new(); | |
106 f.read_to_string(&mut s) | |
107 .map_err(|e| e.to_string()) | |
108 .map(|_| s) | |
109 }); | |
110 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
111 r.and_then(|s| { |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
112 self.merge(&s) |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
113 }) |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
114 .map_err(|e| { |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
115 format!("Error loading config file {}: {}", |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
116 filename, e) |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
117 }) |
597 | 118 } |
595 | 119 } |