annotate rust/src/config.rs @ 595:e87655ed8429 rust

add config
author Matt Johnston <matt@ucc.asn.au>
date Thu, 05 Jan 2017 23:26:00 +0800
parents
children ca8102feaca6
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 use toml::{Encoder,Decoder};
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 use serde::Serializer;
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 use serde::Serialize;
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 #[derive(Deserialize,Serialize,Debug)]
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #[allow(non_snake_case)]
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 pub struct Config {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 FRIDGE_SLEEP: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 SENSOR_SLEEP: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 UPLOAD_SLEEP: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 FRIDGE_DELAY: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 FRIDGE_WORT_INVALID_TIME: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 MAX_READINGS: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 PARAMS_FILE: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 SENSOR_BASE_DIR: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 FRIDGE_GPIO_PIN: u32,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 AMBIENT_NAME: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 FRIDGE_NAME: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 WORT_NAME: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 INTERNAL_TEMPERATURE: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 HMAC_KEY: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 SERVER_URL: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 UPDATE_URL: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 SETTINGS_URL: String,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 }
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 impl Config {
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 pub fn new() -> Self {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 Config {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 FRIDGE_SLEEP: 60, // this value works. may affect the algorithm
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 SENSOR_SLEEP: 15, // same for this.
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 UPLOAD_SLEEP: 83, // nice and prime
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 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
44 FRIDGE_WORT_INVALID_TIME: 300, // 5 mins
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 // 12 hours of "offline" readings stored
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf")
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 PARAMS_FILE: "tempserver.conf".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 FRIDGE_GPIO_PIN: 17,
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 //WORT_NAME: "28-0000042cf4dd".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 //FRIDGE_NAME: "28-0000042cccc4".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 //AMBIENT_NAME: "28-0000042c6dbb".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 AMBIENT_NAME: "missingambient".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 FRIDGE_NAME: "28-0000042c6dbb".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 WORT_NAME: "28-0000042cccc4".to_string(), // was fridge
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 HMAC_KEY: "a key".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(),
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 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
66 }
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 pub fn to_toml_string(self) -> String {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 let mut e = toml::Encoder::new();
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 self.serialize(&mut e).unwrap();
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 toml::Value::Table(e.toml).to_string()
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 }