annotate rust/src/config.rs @ 622:5b456905eaac rust

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