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