Skip to content

Commit

Permalink
Add support for status query (#4)
Browse files Browse the repository at this point in the history
* Add support for status query

Signed-off-by: jldeen <jessicadeen@me.com>
  • Loading branch information
jldeen committed May 2, 2022
1 parent b96ca8c commit aa47ae4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "keylight"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ This is a cross platform lightweight CLI tool to simply and easily control your

## Work To Be Done

- [X] ~~Support for `on` / `off` toggle arguements. **Added in v0.2.0**~~
- [X] ~~Add help menu with `-h` flag. **Added in v0.2.0**~~
- [ ] Add status to query for on/off
- [X] ~~Support for `on` / `off` toggle arguements.~~ **Added in v0.2.0**
- [X] ~~Add help menu with `-h` flag.~~ **Added in v0.2.0**
- [X] ~~Add status to query for on/off~~ **Added in v0.2.2**
- [ ] Support for brightness and temperature via preset arguments, I.E, `low`, `medium`, and `high` or `warm`, `medium`, and `cool`.
- [ ] Support for brightness by percentage.
- [ ] Autodiscovery support.
- [ ] Testing with more than 1 Elgato Keylight.

## Building The App

This app should build with minimal dependencies. It's been tested with Rust 1.56 on macOS Monterey 12.1 and 1 Elgato Keylight.
This app should build with minimal dependencies. It's been tested with Rust 1.60 on macOS Monterey 12.4 and 1 Elgato Keylight.

`cargo build`

Expand All @@ -25,32 +25,36 @@ This app should build with minimal dependencies. It's been tested with Rust 1.5
This CLI tool has three mandatory parameters and two optional ones (that have default values). There are environment variables that can be provided in place of CLI arguments.

```
keylight cli v0.2.0
keylight v0.2.2
Jessica Deen <jessica.deen@microsoft.com>
A Simple lightweight CLI to control Elgato Keylights from the command line
Easy CLI to control Elgato Keylight
USAGE:
keylight [FLAGS] --elgato-ip <Local Elgato IP Address> --number-of-lights <Number of Elgato Keylights> --brightness <Brightness> --temperature <Temperature> [ARGS]
keylight [OPTIONS] --elgato-ip <elgato_ip> --number-of-lights <number_of_lights> <on/off/status>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
-v, --verbose Verbose mode enabled
ARGS:
<on/off/status> Toggle light on, off, or query current power state [possible values: off,
on, status]
OPTIONS:
-i, --elgato-ip <Local Elgato IP Address>
Local Elgato IP Address [env: elgato_ip=]
-b, --brightness <brightness>
Brightness value for light [env: brightness=] [default: 20]
-n, --number-of-lights <Number of Elgato Keylights>
Number of Elgato Keylights to control [env: number_of_lights=]
-h, --help
Print help information
-b, --brightness <Brightness>
Brightness level [env: brightness=] [default: 20]
-i, --elgato-ip <elgato_ip>
Elgato Keylight IP address [env: elgato_ip=192.168.184.166]
-t, --temperature <Temperature>
Temperature level [env: temperature=] [default: 213]
-n, --number-of-lights <number_of_lights>
Number of Elgato Keylights in use [env: number_of_lights=1]
-t, --temperature <temperature>
Temperature value for light [env: temperature=] [default: 213]
ARGS:
<on/off> Toggle switch value to turn keylight(s) on or off.
```
-v, --verbose
Log Level
-V, --version
Print version information
```
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub fn get_app_cli(version: &str) -> Command {
Arg::new("switch")
.index(1)
.required(true)
.value_name("on/off")
.possible_values(&["off", "on"])
.help("Toggle light on or off"),
.value_name("on/off/status")
.possible_values(&["off", "on", "status"])
.help("Toggle light on, off, or query current power state"),
)
.arg(
Arg::new("brightness")
Expand Down
34 changes: 27 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::Write;
// Required deps
use cli::get_app_cli;
use reqwest::Client;
use serde_json::json;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -43,13 +43,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let switch = match matches.value_of("switch").unwrap() {
"off" => 0,
"on" => 1,
"status" => 2,
_ => 0,
};

// status
if switch == 0 {
let power_status = "off";
println!("Elgato Keylight is: {}", power_status);
} else {
} else if switch == 1 {
let power_status = "on";
println!("Elgato Keylight is: {}", power_status);
}
Expand All @@ -76,19 +78,37 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
});

let url = format!("http://{}:{}", elgato_ip, "9123/elgato/lights");
log::info!("State: {}", url);
log::info!("status: {}", url);

let client = Client::new();

let response = client.put(url).json(&body).send().await?;

let status = response.status();
let response_body = response.text().await?;
let response_json: serde_json::Value = serde_json::from_str(&response_body)?;
// response status code
let response_success = response.status();
log::info!("Response status: {}", response_success);

log::info!("Response status: {}", status);
// body
let response_body = response.text().await?;
log::info!("Response text: {}", response_body);

// Json data
let response_json: serde_json::Value = serde_json::from_str(&response_body)?;
log::info!("Response json: {:?}", response_json);

if switch == 2 {
let v: Value = serde_json::from_str(&response_body)?;

let query = &v["lights"][0]["on"];

if query == 0 {
let status = "off";
println!("Elgato light is: {}", status);
} else if query == 1 {
let status = "on";
println!("Elgato light is: {}", status);
}
}

Ok(())
}

0 comments on commit aa47ae4

Please sign in to comment.