view rust/src/main.rs @ 638:a9f353f488d0 rust

fix channels
author Matt Johnston <matt@ucc.asn.au>
date Sat, 09 Nov 2019 11:35:59 +0800
parents 4424a8b30f9c
children 89818a14648b
line wrap: on
line source

#[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 std::time::Duration;

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(fridge::Fridge::new_actor, (cf.clone(), testmode, nowait));
    sys.actor_of(props, "fridge").unwrap();

    loop {
        // TODO: wait for a semaphore or something?
        std::thread::sleep(Duration::from_millis(60000));
   }
}

#[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);
    }
}