Mercurial > templog
annotate rust/src/config.rs @ 601:8c21df3711e2 rust
rigid_config
more on sensors
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 15 Feb 2017 23:58:02 +0800 |
parents | 9c76f3cf01ea |
children | f3e39e2107fd |
rev | line source |
---|---|
595 | 1 extern crate toml; |
2 | |
601 | 3 use std::error::Error; |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
4 use std::fs::File; |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
5 use std::io::Read; |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
6 use serde::{Serialize,Deserialize,Deserializer,Serializer}; |
595 | 7 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
8 #[derive(Deserialize,Serialize,Debug,Clone)] |
595 | 9 #[allow(non_snake_case)] |
10 pub struct Config { | |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
11 pub SENSOR_SLEEP: u64, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
12 pub UPLOAD_SLEEP: u64, |
595 | 13 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
14 pub FRIDGE_DELAY: u64, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
15 pub FRIDGE_WORT_INVALID_TIME: u64, |
595 | 16 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
17 pub MAX_READINGS: u32, |
595 | 18 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
19 pub PARAMS_FILE: String, |
595 | 20 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
21 pub SENSOR_BASE_DIR: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
22 pub FRIDGE_GPIO_PIN: u32, |
595 | 23 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
24 pub AMBIENT_NAME: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
25 pub FRIDGE_NAME: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
26 pub WORT_NAME: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
27 pub INTERNAL_TEMPERATURE: String, |
595 | 28 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
29 pub HMAC_KEY: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
30 pub SERVER_URL: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
31 pub UPDATE_URL: String, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
32 pub SETTINGS_URL: String, |
595 | 33 } |
34 | |
35 impl Config { | |
36 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
37 pub fn default() -> Self { |
595 | 38 Config { |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
39 SENSOR_SLEEP: 5, |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
40 UPLOAD_SLEEP: 83, |
595 | 41 |
42 FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on | |
43 FRIDGE_WORT_INVALID_TIME: 300, // 5 mins | |
44 | |
45 // 12 hours of "offline" readings stored | |
46 MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP | |
47 | |
48 //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf") | |
49 PARAMS_FILE: "tempserver.conf".to_string(), | |
50 | |
51 SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(), | |
52 FRIDGE_GPIO_PIN: 17, | |
53 //WORT_NAME: "28-0000042cf4dd".to_string(), | |
54 //FRIDGE_NAME: "28-0000042cccc4".to_string(), | |
55 //AMBIENT_NAME: "28-0000042c6dbb".to_string(), | |
56 AMBIENT_NAME: "missingambient".to_string(), | |
57 FRIDGE_NAME: "28-0000042c6dbb".to_string(), | |
58 WORT_NAME: "28-0000042cccc4".to_string(), // was fridge | |
59 INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(), | |
60 | |
61 HMAC_KEY: "a key".to_string(), | |
62 SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(), | |
63 UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(), | |
64 SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(), | |
65 } | |
66 } | |
67 | |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
68 pub fn to_toml_string(&self) -> String { |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
69 toml::to_string(self).unwrap() |
595 | 70 } |
597 | 71 |
601 | 72 pub fn merge(&self, conf: &str) -> Result<Self, Box<Error>> { |
600 | 73 // convert existing and new toml into tables, combine them. |
601 | 74 let mut new_toml = toml::from_str(conf)?; |
599
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); |
601 | 78 // TODO: wrap the error with a better message? |
79 let ret = toml::Value::Table(ex_toml.clone()).try_into()?; | |
600 | 80 Ok(ret) |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
81 } |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
82 |
601 | 83 pub fn merge_file(&self, filename: &str) -> Result<Self, Box<Error>> { |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
84 |
601 | 85 let mut s = String::new(); |
86 File::open(filename)?.read_to_string(&mut s)?; | |
597 | 87 |
601 | 88 self.merge(&s) |
597 | 89 } |
595 | 90 } |