changeset 595:e87655ed8429 rust

add config
author Matt Johnston <matt@ucc.asn.au>
date Thu, 05 Jan 2017 23:26:00 +0800
parents aff50ee77252
children ca8102feaca6
files rust/Cargo.lock rust/Cargo.toml rust/src/config.rs rust/src/fridge.rs rust/src/main.rs rust/src/sensor.rs rust/src/types.rs
diffstat 7 files changed, 191 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Wed Jan 04 17:18:44 2017 +0800
+++ b/rust/Cargo.lock	Thu Jan 05 23:26:00 2017 +0800
@@ -6,8 +6,10 @@
  "futures 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)",
  "tokio-core 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -61,7 +63,7 @@
 
 [[package]]
 name = "libc"
-version = "0.2.18"
+version = "0.2.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
@@ -74,7 +76,7 @@
 version = "0.1.11"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -84,7 +86,7 @@
 dependencies = [
  "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -111,7 +113,7 @@
 dependencies = [
  "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
  "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -123,18 +125,23 @@
 dependencies = [
  "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
  "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
+name = "quote"
+version = "0.3.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "rand"
 version = "0.3.15"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -178,17 +185,57 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "serde"
+version = "0.8.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "serde_codegen"
+version = "0.8.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "quote 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_codegen_internals 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_codegen_internals"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "syn 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_derive"
+version = "0.8.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "serde_codegen 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "slab"
 version = "0.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "syn"
+version = "0.10.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "quote 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "thread-id"
 version = "2.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -212,6 +259,20 @@
 ]
 
 [[package]]
+name = "toml"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "utf8-ranges"
 version = "0.1.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -248,13 +309,14 @@
 "checksum futures 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "177a82a61dd7e528022ce97f24e54b499dd2fee4d4646a0f283c5fb500dbfe20"
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
 "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b"
-"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
+"checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352"
 "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
 "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
 "checksum mio 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5b493dc9fd96bd2077f2117f178172b0765db4dfda3ea4d8000401e6d65d3e80"
 "checksum miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3e690c5df6b2f60acd45d56378981e827ff8295562fc8d34f573deb267a59cd1"
 "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2"
 "checksum nix 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0d95c5fa8b641c10ad0b8887454ebaafa3c92b5cd5350f8fc693adafd178e7b"
+"checksum quote 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6732e32663c9c271bfc7c1823486b471f18c47a2dbf87c066897b7b51afc83be"
 "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
 "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
 "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
@@ -262,10 +324,17 @@
 "checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084"
 "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d"
 "checksum semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac"
+"checksum serde 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)" = "7b7c6bf11cf766473ea1d53eb4e3bc4e80f31f50082fc24077cf06f600279a66"
+"checksum serde_codegen 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)" = "64e0d87d19ec28bf431ffa9bad1f1e4ea3b381cd616c6cc56dca9eedbc7f6ab8"
+"checksum serde_codegen_internals 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "afad7924a009f859f380e4a2e3a509a845c2ac66435fcead74a4d983b21ae806"
+"checksum serde_derive 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)" = "6e7ad1e74679b92730ca39c361ea125e2846df337c5d94d084eb2f7837c1843d"
 "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
+"checksum syn 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1a437f8b4353179418870f014113876cd4cd4f642e42dbc5ed4f328d5f808246"
 "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
 "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
 "checksum tokio-core 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "52416b3e937abac22a543a7f1c66bd37feb60137ff1ab42390fa02df85347e58"
+"checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4"
+"checksum unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "36dff09cafb4ec7c8cf0023eb0b686cb6ce65499116a12201c9e11840ca01beb"
 "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
--- a/rust/Cargo.toml	Wed Jan 04 17:18:44 2017 +0800
+++ b/rust/Cargo.toml	Thu Jan 05 23:26:00 2017 +0800
@@ -6,10 +6,15 @@
 [dependencies]
 futures = "0.1"
 tokio-core = "0.1"
-rustc-serialize = "0.3"
+serde = "0.8"
+serde_derive = "0.8"
 rand = "0.3"
 log = "0.3"
 env_logger = "0.3"
 
 [features]
 testmode = []
+
+[dependencies.toml]
+version = "0.2"
+features = [ "serde" ]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/src/config.rs	Thu Jan 05 23:26:00 2017 +0800
@@ -0,0 +1,74 @@
+extern crate toml;
+
+use toml::{Encoder,Decoder};
+use serde::Serializer;
+use serde::Serialize;
+
+#[derive(Deserialize,Serialize,Debug)]
+#[allow(non_snake_case)]
+pub struct Config {
+    FRIDGE_SLEEP: u32,
+    SENSOR_SLEEP: u32,
+    UPLOAD_SLEEP: u32,
+
+    FRIDGE_DELAY: u32,
+    FRIDGE_WORT_INVALID_TIME: u32,
+
+    MAX_READINGS: u32,
+
+    PARAMS_FILE: String,
+
+    SENSOR_BASE_DIR: String,
+    FRIDGE_GPIO_PIN: u32,
+
+    AMBIENT_NAME: String,
+    FRIDGE_NAME: String,
+    WORT_NAME: String,
+    INTERNAL_TEMPERATURE: String,
+
+    HMAC_KEY: String,
+    SERVER_URL: String,
+    UPDATE_URL: String, 
+    SETTINGS_URL: String,
+}
+
+impl Config {
+
+    pub fn new() -> Self {
+        Config {
+            FRIDGE_SLEEP: 60, // this value works. may affect the algorithm
+            SENSOR_SLEEP: 15, // same for this.
+            UPLOAD_SLEEP: 83, // nice and prime
+
+            FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on
+            FRIDGE_WORT_INVALID_TIME: 300, // 5 mins
+
+            // 12 hours of "offline" readings stored
+            MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP
+
+            //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf")
+            PARAMS_FILE: "tempserver.conf".to_string(),
+
+            SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(),
+            FRIDGE_GPIO_PIN: 17,
+            //WORT_NAME: "28-0000042cf4dd".to_string(),
+            //FRIDGE_NAME: "28-0000042cccc4".to_string(),
+            //AMBIENT_NAME: "28-0000042c6dbb".to_string(),
+            AMBIENT_NAME: "missingambient".to_string(),
+            FRIDGE_NAME: "28-0000042c6dbb".to_string(),
+            WORT_NAME: "28-0000042cccc4".to_string(), // was fridge
+            INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(),
+
+            HMAC_KEY: "a key".to_string(),
+            SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(),
+            UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(),
+            SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(),
+        }
+    }
+
+    pub fn to_toml_string(self) -> String {
+        let mut e = toml::Encoder::new();
+        self.serialize(&mut e).unwrap();
+        toml::Value::Table(e.toml).to_string()
+    }
+}
--- a/rust/src/fridge.rs	Wed Jan 04 17:18:44 2017 +0800
+++ b/rust/src/fridge.rs	Thu Jan 05 23:26:00 2017 +0800
@@ -97,7 +97,10 @@
             .map_err(|_| ())
             .and_then(move |_| {
                 waker.send(v)
-                    .map_err(|_| ())
+                    .map_err(|e| {
+                        warn!("Send error in tick(): {}", e.to_string());
+                        ()
+                    })
             })
             .map(|_| ());
         self.handle.spawn(t);
--- a/rust/src/main.rs	Wed Jan 04 17:18:44 2017 +0800
+++ b/rust/src/main.rs	Thu Jan 05 23:26:00 2017 +0800
@@ -1,10 +1,18 @@
+#![feature(proc_macro)]
+
 extern crate tokio_core;
 extern crate futures;
-extern crate rustc_serialize;
 #[macro_use]
 extern crate log;
 extern crate env_logger;
 
+#[macro_use]
+extern crate serde_derive;
+extern crate serde;
+
+extern crate toml;
+
+
 use std::io;
 
 use tokio_core::reactor::Core;
@@ -12,6 +20,7 @@
 use futures::sync::{mpsc};
 use sensor::Sensor;
 
+mod config;
 mod sensor;
 mod fridge;
 mod types;
@@ -25,6 +34,9 @@
     println!("Wort Templog");
     debug!("debug log level");
 
+    let config = config::Config::new();
+    println!("{}", config.to_toml_string());
+
     let mut core = Core::new().unwrap();
     let handle = core.handle();
 
@@ -46,7 +58,10 @@
         debug!("sensors {:?}", r);
         let t = sensor_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()})
                     .map(|_| ())
-                    .map_err(|_| ());
+                    .map_err(|e| {
+                        warn!("Send error in sensor_s: {}", e.to_string());
+                        ()
+                    });
         handle.spawn(t);
         r
     });
--- a/rust/src/sensor.rs	Wed Jan 04 17:18:44 2017 +0800
+++ b/rust/src/sensor.rs	Thu Jan 05 23:26:00 2017 +0800
@@ -17,11 +17,15 @@
 }
 
 pub struct OneWireSensor {
+    master_dir: String,
 }
 
 impl OneWireSensor {
     fn new() -> OneWireSensor {
-        OneWireSensor {}
+        OneWireSensor {
+            master_dir: String::new(), // XXX
+            
+        }
         // todo
     }
 
@@ -32,6 +36,11 @@
         debug!("sensor step {:?}", r);
         r
     }
+
+    fn sensor_names(self) -> Vec<String> {
+        let mut names = vec![];
+        names
+    }
 }
 
 impl Sensor for OneWireSensor {
--- a/rust/src/types.rs	Wed Jan 04 17:18:44 2017 +0800
+++ b/rust/src/types.rs	Thu Jan 05 23:26:00 2017 +0800
@@ -1,7 +1,8 @@
 use std::collections::HashMap;
 use std::time::Duration;
+use serde::{Deserialize,Serialize};
 
-#[derive(RustcDecodable, RustcEncodable, Debug, Clone)]
+#[derive(Deserialize, Serialize, Debug, Clone)]
 pub struct Params {
     pub fridge_setpoint: f32,
     pub fridge_difference: f32,