Skip to content

Commit 6a9b83b

Browse files
author
Dave Horner
committed
feat: add Lua script execution mode and update configuration handling
- Update README with instructions for overriding host and port via the command-line. - Add new Lua example script for Windows Notepad (lua-examples/windows-notepad-helloworld.lua). - Refactor load_and_override_config to use references and clone values. - Implement conditional Lua mode: if enabled, read and execute a Lua script; otherwise, perform normal RPC operations. - Remove unused RPC calls from the main function in normal mode.
1 parent 2a42d0a commit 6a9b83b

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ jetkvm_control = "0.1.0" # or use a git dependency / local path during developm
3737
port = "80"
3838
api = "/webrtc/session"
3939
```
40+
41+
You can also override these values via the command-line. For example:
42+
```
43+
cargo run -- --host 192.168.1.100 --port 8080
44+
```
45+
4046
3. **Running the Project**
4147
After setting up your configuration, you can build and run the project with Cargo:
4248
```bash
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
print("Executing Lua script...")
2+
send_windows_key()
3+
delay(250)
4+
send_text("notepad")
5+
send_return()
6+
delay(250)
7+
delay(250)
8+
send_text("Hello World!")
9+
send_ctrl_a()
10+
send_ctrl_c()
11+
delay(250)
12+
send_ctrl_v()
13+
delay(250)
14+
send_ctrl_v()
15+

src/main.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use anyhow::Result as AnyResult;
22
use clap::{CommandFactory, Parser};
3-
use jetkvm_control::device::{rpc_get_device_id, rpc_ping};
43
use jetkvm_control::jetkvm_config::JetKvmConfig;
54
use jetkvm_control::jetkvm_rpc_client::JetKvmRpcClient;
6-
use jetkvm_control::system::rpc_get_edid;
75
use tokio::time::{sleep, Duration};
86
use tracing::{error, info, warn};
97
use tracing_subscriber;
@@ -26,10 +24,16 @@ struct CliConfig {
2624
/// The password for authentication.
2725
#[arg(short = 'P', long)]
2826
password: Option<String>,
27+
28+
// When the "lua" feature is enabled, the first positional argument is the Lua script path.
29+
#[cfg(feature = "lua")]
30+
/// Path to the Lua script to execute.
31+
#[arg(required = true, index = 1)]
32+
lua_script: String,
2933
}
3034

3135
/// Loads configuration from file (or uses the default) and then applies CLI overrides.
32-
fn load_and_override_config(cli_config: CliConfig) -> JetKvmConfig {
36+
fn load_and_override_config(cli_config: &CliConfig) -> JetKvmConfig {
3337
let mut config = JetKvmConfig::load().unwrap_or_else(|err| {
3438
warn!(
3539
"Failed to load config.toml ({}). Using default configuration.",
@@ -38,17 +42,17 @@ fn load_and_override_config(cli_config: CliConfig) -> JetKvmConfig {
3842
JetKvmConfig::default()
3943
});
4044

41-
if let Some(host) = cli_config.host {
42-
config.host = host;
45+
if let Some(host) = &cli_config.host {
46+
config.host = host.clone();
4347
}
44-
if let Some(port) = cli_config.port {
45-
config.port = port;
48+
if let Some(port) = &cli_config.port {
49+
config.port = port.clone();
4650
}
47-
if let Some(api) = cli_config.api {
48-
config.api = api;
51+
if let Some(api) = &cli_config.api {
52+
config.api = api.clone();
4953
}
50-
if let Some(password) = cli_config.password {
51-
config.password = password;
54+
if let Some(password) = &cli_config.password {
55+
config.password = password.clone();
5256
}
5357
config
5458
}
@@ -65,7 +69,7 @@ async fn main() -> AnyResult<()> {
6569
info!("CLI config provided: {:?}", cli_config);
6670

6771
// Load configuration from file (or default) and override with CLI options.
68-
let config = load_and_override_config(cli_config);
72+
let config = load_and_override_config(&cli_config);
6973

7074
// Validate that the critical field 'host' is set.
7175
if config.host.trim().is_empty() {
@@ -76,49 +80,48 @@ async fn main() -> AnyResult<()> {
7680
std::process::exit(1);
7781
}
7882

79-
// Create and use the client.
83+
// Create and connect the client.
8084
let mut client = JetKvmRpcClient::new(config);
81-
8285
if let Err(err) = client.connect().await {
8386
error!("Failed to connect to RPC server: {:?}", err);
8487
std::process::exit(1);
8588
}
8689
client.wait_for_channel_open().await?;
87-
let ping = rpc_ping(&client).await;
88-
info!("Ping: {:?}", ping);
89-
let device_id = rpc_get_device_id(&client).await;
90-
info!("Device ID: {:?}", device_id);
91-
let edid = rpc_get_edid(&client).await;
92-
info!("EDID: {:?}", edid);
9390

91+
// Lua mode: if the "lua" feature is enabled, read and execute the provided Lua script.
9492
#[cfg(feature = "lua")]
9593
{
9694
use jetkvm_control::lua_engine::LuaEngine;
9795
use std::sync::Arc;
9896
use tokio::sync::Mutex;
99-
let client_arc = Arc::new(Mutex::new(client));
10097

101-
// Create and configure the Lua engine.
98+
// Read the Lua script from the file path given as the first positional argument.
99+
let script = tokio::fs::read_to_string(&cli_config.lua_script).await?;
100+
info!("Executing Lua script from {}", &cli_config.lua_script);
101+
102+
// Wrap the client in an Arc/Mutex for the Lua engine.
103+
let client_arc = Arc::new(Mutex::new(client));
102104
let lua_engine = LuaEngine::new(client_arc);
103105
lua_engine.register_builtin_functions()?;
104106

105-
// Optionally, execute a Lua script.
106-
let script = r#"
107-
print("Executing Lua script...")
108-
send_windows_key()
109-
delay(250)
110-
send_text("notepad")
111-
send_return()
112-
delay(250)
113-
send_text("Hello World!")
114-
send_ctrl_a()
115-
send_ctrl_c()
116-
right_click(100, 200)
117-
"#;
118-
119-
lua_engine.exec_script(script).await?;
107+
lua_engine.exec_script(&script).await?;
120108
info!("Lua script executed successfully.");
121109
}
122110

111+
// Normal mode: if the "lua" feature is not enabled, perform normal actions.
112+
#[cfg(not(feature = "lua"))]
113+
{
114+
use jetkvm_control::device::{rpc_get_device_id, rpc_ping};
115+
use jetkvm_control::system::rpc_get_edid;
116+
117+
let ping = rpc_ping(&client).await;
118+
info!("Ping: {:?}", ping);
119+
let device_id = rpc_get_device_id(&client).await;
120+
info!("Device ID: {:?}", device_id);
121+
let edid = rpc_get_edid(&client).await;
122+
info!("EDID: {:?}", edid);
123+
}
124+
123125
Ok(())
124126
}
127+

0 commit comments

Comments
 (0)