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>,
}
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct TagModel {
pub id: Option<String>,
pub imei: Option<String>,
@ -44,6 +44,16 @@ impl TagModel {
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 {

View File

@ -27,11 +27,11 @@ pub enum Subcommand {
#[argh(
subcommand,
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 {
#[argh(positional, description = "tag to be fetched")]
pub tag: String,
#[argh(positional, description = "device to be fetched")]
pub device: String, // Device = tag, but in human speech
#[argh(
option,
short = 's',
@ -51,7 +51,11 @@ pub struct BetweenOpt {
pub struct InitOpt {}
#[derive(FromArgs)]
#[argh(subcommand, name = "nick", description = "provides nicknaming to tags")]
#[argh(
subcommand,
name = "nick",
description = "provides nicknaming to devices"
)]
pub struct NickOpt {
#[argh(subcommand)]
pub subcommand: NickSub,
@ -68,7 +72,7 @@ pub enum NickSub {
#[argh(
subcommand,
name = "list",
description = "List all tags and their current nicknames"
description = "List all devices and their current nicknames"
)]
pub struct NickListOpt {}
@ -76,11 +80,11 @@ pub struct NickListOpt {}
#[argh(
subcommand,
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 {
#[argh(positional, description = "tag index, see index with nick list")]
#[argh(positional, description = "device index, see index with nick list")]
pub index: i32,
#[argh(positional, description = "the new nickname for the tag")]
#[argh(positional, description = "the new nickname for the device")]
pub nickname: String,
}

View File

@ -36,7 +36,7 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
None => None,
};
let locs = run(&config, opt.tag, since, until)?;
let locs = run(&config, opt.device, since, until)?;
dbg!(
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
locs.len(),
@ -75,13 +75,13 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
Ok(())
} else {
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,
))
}
} else {
Err(GenericError::MessagedError(
format!("Could not find tag with index {}", opt.index),
format!("Could not find device with index {}", opt.index),
None,
))
}
@ -100,15 +100,18 @@ fn run(
let mut api = API::new(config.clone());
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 mut locations = Vec::new();
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 mut counter = 0;
@ -170,33 +173,24 @@ fn find_tag(
tag_str: String,
tags: &Vec<TagModel>,
config: &Config,
) -> Result<String, GenericError> {
) -> Result<TagModel, GenericError> {
let mut tag = None;
if let Ok(num) = tag_str.parse::<i32>() {
if let Some(found) = tags.get((num - 1).max(0) as usize) {
tag = Some(&*found);
tag = Some(found.clone());
}
}
for curr in tags.iter() {
if let Some(nick) = curr.get_nick(config) {
if nick == tag_str {
tag = Some(curr);
tag = Some(curr.clone());
}
}
}
match tag {
Some(t) => match &t.id {
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,
)),
},
Some(tag) => Ok(tag),
None => Err(GenericError::MessagedError(
format!("Could not find tag {}", tag_str),
format!("Could not find device {}", tag_str),
None,
)),
}