Mercurial > templog
annotate rust/src/main.rs @ 639:89818a14648b rust tip
- switch to using anyhow for errors, surf for http
runs but surf has problems
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 28 Nov 2019 23:57:00 +0800 |
parents | a9f353f488d0 |
children |
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 | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
7 use anyhow::{Result}; |
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 |
638 | 15 use std::time::Duration; |
634 | 16 |
17 use riker::actors::*; | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
18 |
634 | 19 use structopt::StructOpt; |
595 | 20 |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
21 fn run(conf_file: &str, nowait: bool, testmode: bool) -> Result<()> { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
22 |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
23 let cf = config::Config::load(conf_file)?; |
587 | 24 |
634 | 25 let sys = ActorSystem::new().unwrap(); |
638 | 26 |
27 let props = Props::new_args(fridge::Fridge::new_actor, (cf.clone(), testmode, nowait)); | |
28 sys.actor_of(props, "fridge").unwrap(); | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
29 |
638 | 30 loop { |
31 // TODO: wait for a semaphore or something? | |
32 std::thread::sleep(Duration::from_millis(60000)); | |
33 } | |
587 | 34 } |
35 | |
634 | 36 #[derive(Debug, StructOpt)] |
37 #[structopt(name = "Wort Temperature", about = "Matt Johnston 2019 [email protected]")] | |
38 struct Opt { | |
39 /// Replace existing running instance | |
40 #[structopt(long)] | |
41 new: bool, | |
42 | |
43 /// Run in background | |
44 #[structopt(short = "D", long)] | |
45 daemon: bool, | |
597 | 46 |
634 | 47 #[structopt(short, long)] |
48 debug: bool, | |
49 | |
50 /// Use fake sensors etc | |
51 #[structopt(long)] | |
52 test: bool, | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
53 |
634 | 54 /// Skip initial fridge wait |
55 #[structopt(long)] | |
56 nowait: bool, | |
57 | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
58 /// Print default config (customise in local.toml) |
634 | 59 #[structopt(long)] |
60 defconf: bool, | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
61 |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
62 /// Config file |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
63 #[structopt(short = "c", long, default_value = "local.toml")] |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
64 config: String, |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
65 } |
597 | 66 |
634 | 67 fn handle_args() -> Opt { |
68 let args = Opt::from_args(); | |
632
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
69 |
634 | 70 if args.defconf { |
632
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
71 println!("Default configuration:\n{}\n\n{}", |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
72 "(custom options go in local.toml)", |
634 | 73 config::Config::default_toml()); |
632
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
74 std::process::exit(0); |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
75 } |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
76 args |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
77 } |
bde302def78e
moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents:
631
diff
changeset
|
78 |
634 | 79 // fn setup_log(debug: bool) { |
80 // let loglevel = if debug { | |
81 // log::LevelFilter::Debug | |
82 // } else { | |
83 // log::LevelFilter::Info | |
84 // }; | |
597 | 85 |
634 | 86 // let format = |record: &log::Record| { |
87 // let datefmt = "%Y-%m-%d %I:%M:%S %p"; | |
88 // let ts = chrono::Local::now().format(datefmt); | |
89 // format!("{}: {} - {}", ts, record.level(), record.args()) | |
90 // }; | |
597 | 91 |
92 | |
634 | 93 // let mut builder = env_logger::Builder::new(); |
94 // builder.format(format).filter(Some("wort_templog"), loglevel); | |
95 // builder.init().unwrap(); | |
96 // } | |
601 | 97 |
597 | 98 fn main() { |
99 | |
100 let args = handle_args(); | |
634 | 101 // setup_log(args.debug); |
597 | 102 //env_logger::init().unwrap(); |
103 | |
104 info!("wort-templog"); | |
105 debug!("debug mode"); | |
106 | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
107 let r = run(&args.config, args.nowait, args.test); |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
108 if let Err(e) = r { |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
109 println!("Error running: {}", e); |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
110 } |
597 | 111 } |