Do some cleaning up

This commit is contained in:
Sofia 2020-08-24 20:05:34 +03:00
parent 0ba80a0578
commit 4046f018c3
3 changed files with 37 additions and 29 deletions

View File

@ -20,7 +20,7 @@ pub struct ErrorModel {
pub message: Option<String>, pub message: Option<String>,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug, Clone)]
pub struct TagModel { pub struct TagModel {
pub id: Option<String>, pub id: Option<String>,
pub imei: Option<String>, pub imei: Option<String>,
@ -44,6 +44,16 @@ impl TagModel {
None None
} }
} }
pub fn get_id(&self) -> Result<String, GenericError> {
match &self.id {
Some(id) => Ok(id.to_string()),
None => Err(GenericError::MessagedError(
"Could not find device id. Error probably on Yetzon server side.".to_owned(),
None,
)),
}
}
} }
impl Display for TagModel { impl Display for TagModel {

View File

@ -27,11 +27,11 @@ pub enum Subcommand {
#[argh( #[argh(
subcommand, subcommand,
name = "between", name = "between",
description = "fetch locations for a given section in time.\n<tag> nickname, or index from nick list.\nsince and until in format dd.mm.yyy, OR hh:mm:ss, OR dd.mm.yyy-hh:mm:ss" description = "fetch locations for a given section in time.\n<device> nickname, or index from nick list.\nsince and until in format dd.mm.yyy, OR hh:mm:ss, OR dd.mm.yyy-hh:mm:ss"
)] )]
pub struct BetweenOpt { pub struct BetweenOpt {
#[argh(positional, description = "tag to be fetched")] #[argh(positional, description = "device to be fetched")]
pub tag: String, pub device: String, // Device = tag, but in human speech
#[argh( #[argh(
option, option,
short = 's', short = 's',
@ -51,7 +51,11 @@ pub struct BetweenOpt {
pub struct InitOpt {} pub struct InitOpt {}
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand, name = "nick", description = "provides nicknaming to tags")] #[argh(
subcommand,
name = "nick",
description = "provides nicknaming to devices"
)]
pub struct NickOpt { pub struct NickOpt {
#[argh(subcommand)] #[argh(subcommand)]
pub subcommand: NickSub, pub subcommand: NickSub,
@ -68,7 +72,7 @@ pub enum NickSub {
#[argh( #[argh(
subcommand, subcommand,
name = "list", name = "list",
description = "List all tags and their current nicknames" description = "List all devices and their current nicknames"
)] )]
pub struct NickListOpt {} pub struct NickListOpt {}
@ -76,11 +80,11 @@ pub struct NickListOpt {}
#[argh( #[argh(
subcommand, subcommand,
name = "set", name = "set",
description = "Set nickname for given tag index. see index with nick list" description = "Set nickname for given device index. see index with nick list"
)] )]
pub struct NickSetOpt { pub struct NickSetOpt {
#[argh(positional, description = "tag index, see index with nick list")] #[argh(positional, description = "device index, see index with nick list")]
pub index: i32, pub index: i32,
#[argh(positional, description = "the new nickname for the tag")] #[argh(positional, description = "the new nickname for the device")]
pub nickname: String, pub nickname: String,
} }

View File

@ -36,7 +36,7 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
None => None, None => None,
}; };
let locs = run(&config, opt.tag, since, until)?; let locs = run(&config, opt.device, since, until)?;
dbg!( dbg!(
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(), &locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
locs.len(), locs.len(),
@ -75,13 +75,13 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
Ok(()) Ok(())
} else { } else {
Err(GenericError::MessagedError( Err(GenericError::MessagedError(
format!("Tag with index {} does not have an id", opt.index), format!("Device with index {} does not have an id", opt.index),
None, None,
)) ))
} }
} else { } else {
Err(GenericError::MessagedError( Err(GenericError::MessagedError(
format!("Could not find tag with index {}", opt.index), format!("Could not find device with index {}", opt.index),
None, None,
)) ))
} }
@ -100,15 +100,18 @@ fn run(
let mut api = API::new(config.clone()); let mut api = API::new(config.clone());
let tags = api.get_tags()?; let tags = api.get_tags()?;
let tag = find_tag(tag_str, &tags, config)?; let tag_id = find_tag(tag_str, &tags, config)?.get_id()?;
let state_list = api.get_states(&tag)?; print!("Preparing..\r");
std::io::stdout().lock().flush().ok();
let state_list = api.get_states(&tag_id)?;
let states = API::get_between(&state_list, from, to, true, &config); let states = API::get_between(&state_list, from, to, true, &config);
let mut locations = Vec::new(); let mut locations = Vec::new();
for (_, state) in states.iter() { for (_, state) in states.iter() {
api.queue_location(&tag, state.id.as_ref().unwrap()); api.queue_location(&tag_id, state.id.as_ref().unwrap());
} }
let receiver = api.begin_location_fetch(); let receiver = api.begin_location_fetch();
let mut counter = 0; let mut counter = 0;
@ -170,33 +173,24 @@ fn find_tag(
tag_str: String, tag_str: String,
tags: &Vec<TagModel>, tags: &Vec<TagModel>,
config: &Config, config: &Config,
) -> Result<String, GenericError> { ) -> Result<TagModel, GenericError> {
let mut tag = None; let mut tag = None;
if let Ok(num) = tag_str.parse::<i32>() { if let Ok(num) = tag_str.parse::<i32>() {
if let Some(found) = tags.get((num - 1).max(0) as usize) { if let Some(found) = tags.get((num - 1).max(0) as usize) {
tag = Some(&*found); tag = Some(found.clone());
} }
} }
for curr in tags.iter() { for curr in tags.iter() {
if let Some(nick) = curr.get_nick(config) { if let Some(nick) = curr.get_nick(config) {
if nick == tag_str { if nick == tag_str {
tag = Some(curr); tag = Some(curr.clone());
} }
} }
} }
match tag { match tag {
Some(t) => match &t.id { Some(tag) => Ok(tag),
Some(id) => Ok(id.to_string()),
None => Err(GenericError::MessagedError(
format!(
"Could not find tag {} id. Error probably on Yetzon server side.",
tag_str
),
None,
)),
},
None => Err(GenericError::MessagedError( None => Err(GenericError::MessagedError(
format!("Could not find tag {}", tag_str), format!("Could not find device {}", tag_str),
None, None,
)), )),
} }