yepzon-locationer/src/main.rs

128 lines
4.3 KiB
Rust
Raw Normal View History

2020-08-29 01:27:23 +02:00
#![windows_subsystem = "windows"]
mod args;
2020-08-23 02:08:05 +02:00
mod errors;
2020-08-24 22:24:26 +02:00
mod gpx;
2020-08-23 00:53:49 +02:00
use args::*;
2020-08-29 01:27:23 +02:00
use errors::GenericError;
use thingy_lib::api::{LocationModel, TagModel, API};
use thingy_lib::chrono::offset::Local;
use thingy_lib::chrono::{NaiveDate, NaiveDateTime, NaiveTime, ParseError};
use thingy_lib::{Config, MessagedError};
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) {
2020-08-24 01:17:50 +02:00
eprintln!("Critical Error: {}", e);
2020-08-23 02:08:05 +02:00
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 {
2020-08-24 18:50:29 +02:00
Subcommand::Between(opt) => {
let config = Config::from_path(&env.config)?;
let mut api = API::new(config.clone());
2020-08-23 01:18:31 +02:00
2020-08-24 18:50:29 +02:00
let since =
Some(try_get_datetime(opt.since, &config).with_msg("Failed to parse since")?);
2020-08-24 02:33:40 +02:00
let until = match opt.until {
2020-08-24 18:50:29 +02:00
Some(until) => {
Some(try_get_datetime(until, &config).with_msg("Failed to parse until")?)
}
2020-08-24 02:33:40 +02:00
None => None,
};
2020-08-29 01:27:23 +02:00
let (tag, locs) = thingy_lib::between(&mut api, opt.device, since, until)?;
let gpx = gpx::generate_gpx(&tag, &locs, &api.config)?;
gpx::write_gpx(&gpx)?;
2020-08-24 18:50:29 +02:00
dbg!(
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
locs.len(),
);
2020-08-23 02:08:05 +02:00
Ok(())
2020-08-23 01:18:31 +02:00
}
2020-08-24 19:47:11 +02:00
Subcommand::Init(opt) => {
2020-08-29 01:27:23 +02:00
thingy_lib::init(&env.config, opt.api_key)?;
2020-08-23 02:08:05 +02:00
Ok(())
2020-08-23 01:18:31 +02:00
}
Subcommand::Api(_) => {
let config = Config::from_path(&env.config)?;
let api = API::new(config.clone());
2020-08-29 01:27:23 +02:00
match thingy_lib::check_api(&api) {
Ok(_) => {
println!("API verified, no issues");
Ok(())
}
Err(e) => Err(GenericError::MessagedError(
"API integrity failed, or API-key is not valid. ".to_owned(),
2020-08-29 01:27:23 +02:00
Some(Box::new(e.into())),
)),
}
}
2020-08-25 21:18:59 +02:00
Subcommand::ShareToken(opt) => {
let config = Config::from_path(&env.config)?;
let api = API::new(config.clone());
2020-08-29 01:27:23 +02:00
println!("{}", thingy_lib::sharetoken(&api, opt.device)?);
2020-08-25 21:18:59 +02:00
Ok(())
}
2020-08-24 18:50:29 +02:00
Subcommand::Nick(opt) => {
let config = Config::from_path(&env.config)?;
2020-08-24 18:50:29 +02:00
let mut api = API::new(config.clone());
match opt.subcommand {
NickSub::List(_) => {
2020-08-29 01:27:23 +02:00
for ((idx, nick), tag) in thingy_lib::nick_list(&api)? {
println!("{}. {}\n{}", idx, nick, tag);
2020-08-24 18:50:29 +02:00
}
}
NickSub::Set(opt) => {
2020-08-29 01:27:23 +02:00
thingy_lib::nick_set(&mut api, opt.device, opt.nickname)?;
api.config.write_to(&env.config)?;
2020-08-24 18:50:29 +02:00
}
}
Ok(())
2020-08-24 18:50:29 +02:00
}
2020-08-25 20:59:49 +02:00
#[cfg(debug_assertions)]
2020-08-24 23:14:18 +02:00
Subcommand::Get(opt) => {
let config = Config::from_path(&env.config)?;
let api = API::new(config.clone());
2020-08-24 23:14:18 +02:00
match opt.subcommand {
2020-08-25 21:30:13 +02:00
GetSub::Tag(opt) => {
let tags = api.get_tag(&opt.device);
dbg!(&tags);
}
GetSub::Tags(_) => {
let tags = api.get_tags()?;
dbg!(&tags);
}
2020-08-24 23:14:18 +02:00
GetSub::States(opt) => {
2020-08-29 01:27:23 +02:00
API::print_list(thingy_lib::get_states(&api, opt.device));
2020-08-24 23:14:18 +02:00
}
GetSub::Locations(opt) => {
2020-08-29 01:27:23 +02:00
API::print_list(thingy_lib::get_locations(&api, opt.device, opt.state))
2020-08-24 23:14:18 +02:00
}
}
Ok(())
}
2020-08-23 01:18:31 +02:00
}
2020-08-23 00:53:49 +02:00
}
2020-08-24 02:33:40 +02:00
fn try_get_datetime(parsed: String, config: &Config) -> Result<NaiveDateTime, ParseError> {
match NaiveDateTime::parse_from_str(&parsed, &config.timedate_format) {
Ok(timedate) => Ok(timedate),
Err(_) => match NaiveDate::parse_from_str(&parsed, &config.date_format) {
Ok(date) => Ok(date.and_hms(0, 0, 0)),
Err(_) => match NaiveTime::parse_from_str(&parsed, &config.time_format) {
Ok(time) => Ok(Local::today().naive_local().and_time(time)),
Err(e) => Err(e),
},
},
}
}