view 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
line wrap: on
line source

#![feature(async_closure)]
#[macro_use] extern crate log;
// riker has its own logging?
//extern crate env_logger;

#[macro_use] extern crate lazy_static;


mod config;
mod sensor;
mod fridge;
mod types;
mod params;


use riker::actors::*;

use structopt::StructOpt;

use types::TemplogError;

fn run(conf_file: &str, nowait: bool, testmode: bool) -> Result<(), TemplogError> {

    let cf = config::Config::load(conf_file)?;

    let sys = ActorSystem::new().unwrap();
    let props = Props::new_args(params::ParamWaiter::new, cf.clone());
    sys.actor_of(props, "paramwaiter").unwrap();

    if testmode {
        let props = Props::new_args(sensor::TestSensor::new, cf.clone());
        sys.actor_of(props, "sensor").unwrap()
    } else {
        let props = Props::new_args(sensor::OneWireSensor::new, cf.clone());
        sys.actor_of(props, "sensor").unwrap()
    };

    let props = Props::new_args(fridge::Fridge::new_actor, (cf.clone(), nowait));
    sys.actor_of(props, "fridge").unwrap();
    Ok(())
}

#[derive(Debug, StructOpt)]
#[structopt(name = "Wort Temperature", about = "Matt Johnston 2019 [email protected]")]
struct Opt {
    /// Replace existing running instance
    #[structopt(long)]
    new: bool,

    /// Run in background
    #[structopt(short = "D", long)]
    daemon: bool,

    #[structopt(short, long)]
    debug: bool,

    /// Use fake sensors etc
    #[structopt(long)]
    test: bool,

    /// Skip initial fridge wait
    #[structopt(long)]
    nowait: bool,

    /// Print default config (customise in local.toml)
    #[structopt(long)]
    defconf: bool,

    /// Config file
    #[structopt(short = "c", long, default_value = "local.toml")]
    config: String,
}

fn handle_args() -> Opt {
    let args = Opt::from_args();

    if args.defconf {
        println!("Default configuration:\n{}\n\n{}",
            "(custom options go in local.toml)",
            config::Config::default_toml());
        std::process::exit(0);
    }
    args
}

// fn setup_log(debug: bool) {
//     let loglevel = if debug {
//        log::LevelFilter::Debug
//     } else {
//        log::LevelFilter::Info
//     };

//     let format = |record: &log::Record| {
//         let datefmt = "%Y-%m-%d %I:%M:%S %p";
//             let ts = chrono::Local::now().format(datefmt);
//         format!("{}: {} - {}", ts, record.level(), record.args())
//     };


//     let mut builder = env_logger::Builder::new();
//     builder.format(format).filter(Some("wort_templog"), loglevel);
//     builder.init().unwrap();
// }

fn main() {

    let args = handle_args();
    // setup_log(args.debug);
    //env_logger::init().unwrap();

    info!("wort-templog");
    debug!("debug mode");

    let r = run(&args.config, args.nowait, args.test);
    if let Err(e) = r {
        println!("Error running: {}", e);
    }
}