Mercurial > templog
annotate rust/src/main.rs @ 633:490e9e15b98c rust
move some bits to riker
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 04 Sep 2019 23:24:13 +0800 |
parents | bde302def78e |
children | a5721c02d3ee |
rev | line source |
---|---|
632
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
1 #[macro_use] extern crate log; |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
2 // riker has its own logging? |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
3 //extern crate env_logger; |
631 | 4 |
633 | 5 #[macro_use] extern crate lazy_static; |
6 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
7 use sensor::Sensor; |
589
f2508125adf1
Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents:
588
diff
changeset
|
8 |
595 | 9 mod config; |
590 | 10 mod sensor; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
607
diff
changeset
|
11 mod fridge; |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
12 mod types; |
615 | 13 mod params; |
591 | 14 |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
15 use types::*; |
597 | 16 use config::Config; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
17 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
18 fn run(config: &Config, nowait: bool, testmode: bool) { |
595 | 19 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
20 let mut core = Core::new().unwrap(); |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
21 let handle = core.handle(); |
587 | 22 |
615 | 23 let params = params::Params::load(&config); |
24 let mut fridge = fridge::Fridge::new(&config, nowait, params, &handle); | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
25 |
597 | 26 let sensor_stream = if testmode { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
27 sensor::TestSensor::new(config).stream(&handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
28 } else { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
29 sensor::OneWireSensor::new(config).stream(&handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
30 }; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
31 |
597 | 32 // Send the sensors of interest to the fridge (fridge_reading_s), |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
33 // while streaming them all to the web sender. |
620 | 34 let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1); |
35 let fridge_reading_r = fridge_reading_r.map_err(|e| TemplogError::new("Problem with fridge_reading_r channel")); | |
36 let sensor_stream = sensor_stream.map(|r| { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
37 debug!("sensors {:?}", r); |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
38 let msg = fridge::Message::Sensor { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
39 wort: r.get_temp(&config.WORT_NAME), |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
40 fridge: r.get_temp(&config.FRIDGE_NAME) |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
41 }; |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
42 let t = fridge_reading_s.clone().send(msg) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
43 .map(|_| ()) |
595 | 44 .map_err(|e| { |
597 | 45 warn!("Send error in fridge_reading_s: {}", e.to_string()); |
595 | 46 () |
47 }); | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
48 handle.spawn(t); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
49 r |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
50 }); |
587 | 51 |
627 | 52 let param_stream = params::ParamWaiter::stream(config.clone(), handle.clone()); |
620 | 53 let param_stream = param_stream.map(|p| { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
54 fridge::Message::Params(p) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
55 }); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
56 |
597 | 57 let timeouts = fridge.wakeups(); |
591 | 58 |
620 | 59 // forward all the different types of messages to the fridge |
60 let all_fridge = param_stream.select(timeouts).select(fridge_reading_r).forward(fridge) .map(|_| () ); | |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
61 |
620 | 62 let all_readings = sensor_stream.for_each(|_| Ok(())); |
63 | |
64 // run forever | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
65 let all = all_fridge.select(all_readings); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
66 core.run(all).ok(); |
587 | 67 } |
68 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
69 const USAGE: &'static str = "\ |
597 | 70 Wort Temperature |
71 Matt Johnston 2017 [email protected] | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
72 Usage: wort-templog [--help] [--new] [--daemon] [--debug] [--test] [--defconf] [--thisconf] [--nowait] |
597 | 73 |
74 Options: | |
75 -h, --help | |
76 --new Replace existing running instance | |
77 -D, --daemon Run in background | |
78 -d, --debug | |
79 -t, --test Use fake sensors etc | |
80 --nowait Skip initial fridge wait | |
615 | 81 --defconf Print default config (customise in local.conf) |
597 | 82 --thisconf Print used config |
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 |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
85 #[derive(RustcDecodable)] |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
86 struct Args { |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
87 flag_new: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
88 flag_daemon: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
89 flag_debug: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
90 flag_test: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
91 flag_defconf: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
92 flag_thisconf: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
93 flag_nowait: bool, |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
94 } |
597 | 95 |
632
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
96 fn handle_args() -> Args { |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
97 let args: Args = docopt::Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
98 |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
99 if args.flag_defconf { |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
100 println!("Default configuration:\n{}\n\n{}", |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
101 "(custom options go in local.conf)", |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
102 config::Config::default().to_toml_string()); |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
103 std::process::exit(0); |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
104 } |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
105 args |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
106 } |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
107 |
597 | 108 fn setup_log(debug: bool) { |
109 let loglevel = if debug { | |
110 log::LogLevelFilter::Debug | |
111 } else { | |
112 log::LogLevelFilter::Info | |
113 }; | |
114 | |
115 let format = |record: &log::LogRecord| { | |
116 let datefmt = "%Y-%m-%d %I:%M:%S %p"; | |
117 let ts = time::strftime(datefmt, &time::now()).unwrap(); | |
118 format!("{}: {} - {}", ts, record.level(), record.args()) | |
119 }; | |
120 | |
121 | |
122 let mut builder = env_logger::LogBuilder::new(); | |
123 builder.format(format).filter(Some("wort_templog"), loglevel); | |
124 builder.init().unwrap(); | |
125 } | |
126 | |
601 | 127 fn load_config() -> Config { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
128 let nconfig = config::Config::default(); |
601 | 129 |
615 | 130 let conf_filename = "local.conf"; |
601 | 131 nconfig.merge_file(conf_filename) |
132 .unwrap_or_else(|e| { | |
623 | 133 if let TemplogErrorKind::Io(ref ioe) = *e.kind() { |
134 if let Some(errno) = ioe.raw_os_error() { | |
135 if errno == libc::ENOENT { | |
136 return nconfig; | |
137 } | |
138 } | |
139 } | |
140 | |
601 | 141 println!("Couldn't parse {}: {}", conf_filename, e); |
142 std::process::exit(1); | |
143 }) | |
144 } | |
145 | |
597 | 146 fn main() { |
147 | |
148 let args = handle_args(); | |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
149 setup_log(args.flag_debug); |
597 | 150 //env_logger::init().unwrap(); |
151 | |
152 info!("wort-templog"); | |
153 debug!("debug mode"); | |
154 | |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
155 let config = load_config(); |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
156 |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
157 if args.flag_thisconf { |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
158 println!("Current configuration:\n\n{}", |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
159 config.to_toml_string()); |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
160 std::process::exit(0); |
597 | 161 } |
162 | |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
163 run(&config, args.flag_nowait, args.flag_test); |
597 | 164 } |