Add get debug command

This commit is contained in:
Sofia 2020-08-25 00:14:18 +03:00
parent b5c93017a4
commit 23cf2c370d
3 changed files with 72 additions and 2 deletions

View File

@ -87,10 +87,10 @@ impl Display for TagModel {
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct StateModel {
pub id: Option<String>,
pub timestamp: Option<String>,
#[serde(skip_deserializing)]
pub state: Option<String>,
#[serde(rename(deserialize = "realState"))]
pub real_state: Option<String>,
@ -150,6 +150,17 @@ impl API {
Ok(response.json()?)
}
pub fn get_locations(
&mut self,
tag_id: &String,
state_id: &String,
) -> Result<Vec<LocationModel>, 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);

View File

@ -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,
}

View File

@ -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);