Skip to content

Commit b901bff

Browse files
author
Dave Horner
committed
feat: add Windows Notepad example and update RPC field parsing
- Added new example "helloworld_windows_notepad.rs" for automating Notepad on Windows. - Updated auth logging to use tracing and return early when password is empty. - Changed RPC response field lookups in device and system modules from "deviceID"/"edid" to "result". - Simplified main workflow by removing redundant keyboard automation.
1 parent 8dbd1b7 commit b901bff

File tree

5 files changed

+57
-39
lines changed

5 files changed

+57
-39
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use anyhow::Result as AnyResult;
2+
use jetkvm_control::jetkvm_config::JetKvmConfig;
3+
use jetkvm_control::jetkvm_rpc_client::JetKvmRpcClient;
4+
use jetkvm_control::keyboard::*;
5+
use tokio::time::{sleep, Duration};
6+
use tracing::{error, info};
7+
use tracing_subscriber;
8+
9+
#[tokio::main]
10+
async fn main() -> AnyResult<()> {
11+
tracing_subscriber::fmt()
12+
.with_max_level(tracing::Level::DEBUG) // Set debug level
13+
.init();
14+
15+
let config = JetKvmConfig::load()?;
16+
let mut client = JetKvmRpcClient::new(config);
17+
18+
if let Err(err) = client.connect().await {
19+
error!("Failed to connect to RPC server: {:?}", err);
20+
std::process::exit(1);
21+
}
22+
client.wait_for_channel_open().await?;
23+
send_windows_key(&client).await.ok();
24+
sleep(Duration::from_millis(250)).await;
25+
rpc_sendtext(&client, "notepad").await.ok();
26+
sleep(Duration::from_millis(250)).await;
27+
send_return(&client).await.ok();
28+
sleep(Duration::from_millis(250)).await;
29+
rpc_sendtext(&client, "Hello World").await.ok();
30+
sleep(Duration::from_millis(100)).await;
31+
send_ctrl_a(&client).await.ok();
32+
sleep(Duration::from_millis(100)).await;
33+
send_ctrl_x(&client).await.ok();
34+
sleep(Duration::from_millis(100)).await;
35+
send_ctrl_v(&client).await.ok();
36+
sleep(Duration::from_millis(100)).await;
37+
send_return(&client).await.ok();
38+
send_ctrl_v(&client).await.ok();
39+
sleep(Duration::from_millis(100)).await;
40+
send_return(&client).await.ok();
41+
send_ctrl_v(&client).await.ok();
42+
sleep(Duration::from_millis(100)).await;
43+
send_return(&client).await.ok();
44+
45+
46+
Ok(())
47+
}
48+

src/auth.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use anyhow::{anyhow, Result as AnyResult};
22
use reqwest::Client;
33
use serde_json::json;
4+
use tracing::{debug, info};
45

56
/// Logs in to JetKVM via HTTP and returns an authenticated reqwest::Client.
67
pub async fn login_local(host: &str, password: &str) -> AnyResult<Client> {
78
let login_url = format!("http://{}/auth/login-local", host);
89
let client = Client::builder().cookie_store(true).build()?;
10+
if password.len()==0 {
11+
return Ok(client)
12+
}
913
let resp = client
1014
.post(&login_url)
1115
.json(&json!({ "password": password }))
@@ -21,9 +25,9 @@ pub async fn login_local(host: &str, password: &str) -> AnyResult<Client> {
2125
}
2226
let header_map = resp.headers().clone();
2327
let body_text = resp.text().await.unwrap_or_default();
24-
println!("Login successful. Server responded: {}", body_text);
28+
info!("Login successful. Server responded: {}", body_text);
2529
for cookie in header_map.get_all(reqwest::header::SET_COOKIE).iter() {
26-
println!("Set-Cookie: {}", cookie.to_str().unwrap_or_default());
30+
debug!("Set-Cookie: {}", cookie.to_str().unwrap_or_default());
2731
}
2832
Ok(client)
2933
}

src/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub async fn rpc_ping(client: &JetKvmRpcClient) -> AnyResult<Value> {
1111
pub async fn rpc_get_device_id(client: &JetKvmRpcClient) -> AnyResult<String> {
1212
let res = client.send_rpc("getDeviceID", json!({})).await?;
1313
Ok(res
14-
.get("deviceID")
14+
.get("result")
1515
.and_then(|v| v.as_str())
1616
.unwrap_or("")
1717
.to_string())

src/main.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,14 @@ async fn main() -> AnyResult<()> {
2323
error!("Failed to connect to RPC server: {:?}", err);
2424
std::process::exit(1);
2525
}
26-
27-
info!("wait");
2826
client.wait_for_channel_open().await?;
29-
info!("done");
3027
let ping = rpc_ping(&client).await;
3128
info!("Ping: {:?}", ping);
32-
3329
let device_id = rpc_get_device_id(&client).await;
3430
info!("Device ID: {:?}", device_id);
35-
use jetkvm_control::keyboard::*;
36-
send_windows_key(&client).await.ok();
37-
sleep(Duration::from_millis(100)).await;
38-
rpc_sendtext(&client, "notepad").await.ok();
39-
sleep(Duration::from_millis(100)).await;
40-
send_return(&client).await.ok();
41-
sleep(Duration::from_millis(100)).await;
42-
rpc_sendtext(&client, "Hello World").await.ok();
43-
sleep(Duration::from_millis(100)).await;
44-
send_ctrl_a(&client).await.ok();
45-
sleep(Duration::from_millis(100)).await;
46-
send_ctrl_x(&client).await.ok();
47-
sleep(Duration::from_millis(100)).await;
48-
send_ctrl_v(&client).await.ok();
49-
sleep(Duration::from_millis(100)).await;
50-
send_return(&client).await.ok();
51-
send_ctrl_v(&client).await.ok();
52-
sleep(Duration::from_millis(100)).await;
53-
send_return(&client).await.ok();
54-
send_ctrl_v(&client).await.ok();
55-
sleep(Duration::from_millis(100)).await;
56-
send_return(&client).await.ok();
57-
rpc_left_click(&client, 100, 500).await.ok();
58-
rpc_right_click(&client, 200, 500).await.ok();
59-
rpc_middle_click(&client, 300, 500).await.ok();
60-
rpc_double_click(&client, 100, 500).await.ok();
61-
62-
let mouse_resp = rpc_abs_mouse_report(&client, 100, 200, 1).await;
63-
info!("Mouse Report: {:?}", mouse_resp);
6431

6532
let edid = rpc_get_edid(&client).await;
6633
info!("EDID: {:?}", edid);
67-
let ping = rpc_ping(&client).await;
68-
info!("Ping: {:?}", ping);
34+
6935
Ok(())
7036
}

src/system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde_json::{json, Value};
66
pub async fn rpc_get_edid(client: &JetKvmRpcClient) -> AnyResult<String> {
77
let res = client.send_rpc("getEDID", json!({})).await?;
88
Ok(res
9-
.get("edid")
9+
.get("result")
1010
.and_then(|v| v.as_str())
1111
.unwrap_or("")
1212
.to_string())

0 commit comments

Comments
 (0)