From 23cf2c370dc5411454c8bce8f9d197ca13f7e7c2 Mon Sep 17 00:00:00 2001 From: Teascade Date: Tue, 25 Aug 2020 00:14:18 +0300 Subject: [PATCH] Add get debug command --- src/api.rs | 13 ++++++++++++- src/cmd.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 18 +++++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/api.rs b/src/api.rs index 80df376..9e1eb59 100644 --- a/src/api.rs +++ b/src/api.rs @@ -87,10 +87,10 @@ impl Display for TagModel { } #[derive(Deserialize, Debug, Clone)] +#[serde(deny_unknown_fields)] pub struct StateModel { pub id: Option, pub timestamp: Option, - #[serde(skip_deserializing)] pub state: Option, #[serde(rename(deserialize = "realState"))] pub real_state: Option, @@ -150,6 +150,17 @@ impl API { Ok(response.json()?) } + pub fn get_locations( + &mut self, + tag_id: &String, + state_id: &String, + ) -> Result, GenericError> { + let url = str::replace(&self.config.locations_url, "{tag}", &tag_id); + let url = str::replace(&url, "{state}", &state_id); + let response = self.request(url)?; + Ok(response.json()?) + } + pub fn queue_location(&mut self, tag_id: &String, state_id: &String) { let url = str::replace(&self.config.locations_url, "{tag}", &tag_id); let url = str::replace(&url, "{state}", &state_id); diff --git a/src/cmd.rs b/src/cmd.rs index 4c8ca23..827ebbd 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -21,6 +21,7 @@ pub enum Subcommand { Between(BetweenOpt), Init(InitOpt), Nick(NickOpt), + Get(GetOpt), } #[derive(FromArgs)] @@ -95,3 +96,45 @@ pub struct NickSetOpt { #[argh(positional, description = "the new nickname for the device")] pub nickname: String, } + +#[derive(FromArgs)] +#[argh( + subcommand, + name = "get", + description = "provides nicknaming to devices" +)] +pub struct GetOpt { + #[argh(subcommand)] + pub subcommand: GetSub, +} + +#[derive(FromArgs)] +#[argh(subcommand)] +pub enum GetSub { + States(GetStatesOpt), + Locations(GetLocationsOpt), +} + +#[derive(FromArgs)] +#[argh( + subcommand, + name = "states", + description = "Get states for the given device" +)] +pub struct GetStatesOpt { + #[argh(positional, description = "the device in question")] + pub device: String, +} + +#[derive(FromArgs)] +#[argh( + subcommand, + name = "locations", + description = "Get locations for a state for the given device" +)] +pub struct GetLocationsOpt { + #[argh(positional, description = "the device in question")] + pub device: String, + #[argh(positional, description = "the state id")] + pub state: String, +} diff --git a/src/main.rs b/src/main.rs index d9d4a67..ea533d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,6 +95,22 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> { } } } + Subcommand::Get(opt) => { + let config = Config::from_path(&env.config)?; + let mut api = API::new(config.clone()); + let tags = api.get_tags()?; + match opt.subcommand { + GetSub::States(opt) => { + let tag = find_tag(opt.device, &tags, &config)?; + dbg!(api.get_states(&tag.get_id()?)); + } + GetSub::Locations(opt) => { + let tag = find_tag(opt.device, &tags, &config)?; + dbg!(api.get_locations(&tag.get_id()?, &opt.state)); + } + } + Ok(()) + } } } @@ -152,7 +168,7 @@ fn run( } } } - println!("{:<100}", "Done!"); + println!("{:<100}", "\nDone!"); locations.sort_by(|loc1, loc2| loc2.timestamp.cmp(&loc1.timestamp)); let locs = API::get_between(&locations, from, to, false, &config);