yepzon-locationer/src/main.rs

102 lines
2.7 KiB
Rust
Raw Normal View History

2020-08-23 00:53:49 +02:00
mod api;
2020-08-23 01:18:31 +02:00
mod cmd;
2020-08-23 00:53:49 +02:00
mod config;
2020-08-23 02:08:05 +02:00
mod errors;
2020-08-23 00:53:49 +02:00
use api::API;
2020-08-23 02:08:05 +02:00
use chrono::NaiveDateTime;
2020-08-23 01:18:31 +02:00
use cmd::*;
2020-08-23 00:53:49 +02:00
use config::Config;
2020-08-23 02:08:05 +02:00
use errors::GenericError;
2020-08-23 00:53:49 +02:00
use std::fs::File;
use std::io::prelude::*;
2020-08-23 19:54:34 +02:00
use std::sync::mpsc::TryRecvError;
2020-08-23 00:53:49 +02:00
fn main() {
2020-08-23 01:18:31 +02:00
let env: EnvOpt = argh::from_env();
2020-08-23 02:08:05 +02:00
if let Err(e) = from_env(env) {
eprintln!("Error: {:?}", e);
std::process::exit(1);
}
}
2020-08-23 00:53:49 +02:00
2020-08-23 02:08:05 +02:00
fn from_env(env: EnvOpt) -> Result<(), GenericError> {
2020-08-23 01:18:31 +02:00
match env.subcommand {
Subcommand::Run(opt) => {
2020-08-23 02:08:05 +02:00
let mut file = File::open("config.toml")?;
2020-08-23 01:18:31 +02:00
let mut string = String::new();
2020-08-23 02:08:05 +02:00
file.read_to_string(&mut string)?;
2020-08-23 01:18:31 +02:00
2020-08-23 02:08:05 +02:00
let config: Config = toml::from_str(&string)?;
2020-08-23 01:18:31 +02:00
2020-08-23 19:54:34 +02:00
run(
&config,
Some("1.1.2019 00:00:00".to_owned()),
Some("1.5.2020 00:00:00".to_owned()),
)?;
2020-08-23 02:08:05 +02:00
Ok(())
2020-08-23 01:18:31 +02:00
}
Subcommand::Init(opt) => {
let config = Config::default();
let mut file = File::create("config.toml").unwrap();
2020-08-23 02:08:05 +02:00
file.write_all(&toml::to_vec(&config).unwrap())?;
Ok(())
2020-08-23 01:18:31 +02:00
}
}
2020-08-23 00:53:49 +02:00
}
2020-08-23 02:08:05 +02:00
fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(), GenericError> {
2020-08-23 00:53:49 +02:00
let mut api = API::new(config.clone());
let from = get_opt(from.map(|f| NaiveDateTime::parse_from_str(&f, &config.between_format)))?;
let to = get_opt(to.map(|t| NaiveDateTime::parse_from_str(&t, &config.between_format)))?;
2020-08-23 02:08:05 +02:00
let tags = api.get_tags()?;
2020-08-23 00:53:49 +02:00
let first_tag = tags[0].id.clone().unwrap();
2020-08-23 02:08:05 +02:00
let state_list = api.get_states(&first_tag)?;
2020-08-23 00:53:49 +02:00
let states = API::get_between(state_list.clone(), from, to, true, &config);
2020-08-23 19:54:34 +02:00
//let mut locations = Vec::new();
for (_, state) in states.iter() {
api.queue_location(&first_tag, state.id.as_ref().unwrap());
}
let receiver = api.begin_location_fetch();
loop {
match receiver.try_recv() {
Ok(res) => match res {
Ok(loc) => {
let mut loc = loc;
println!("Received! {}", loc);
//locations.append(&mut loc);
}
Err(e) => eprintln!("{:?}", e),
},
Err(e) => {
if let TryRecvError::Disconnected = e {
break;
}
}
}
2020-08-23 00:53:49 +02:00
}
2020-08-23 19:54:34 +02:00
//dbg!(API::get_between(locations, from, to, false, &config));
2020-08-23 00:53:49 +02:00
Ok(())
}
fn get_opt<A, B>(option: Option<Result<A, B>>) -> Result<Option<A>, B> {
if let Some(res) = option {
match res {
Ok(a) => Ok(Some(a)),
Err(b) => Err(b),
}
} else {
Ok(None)
}
}
fn exp_time(api: &API, reqs_left: u32) -> f32 {
(reqs_left * api.last_response_ping) as f32 / 1000.
}