use openigtlink_rust::error::Result;
use openigtlink_rust::io::{ClientBuilder, SyncIgtlClient};
use openigtlink_rust::protocol::message::IgtlMessage;
use openigtlink_rust::protocol::types::StringMessage;
use std::thread;
use std::time::Duration;
fn main() {
if let Err(e) = run() {
eprintln!("[ERROR] {}", e);
std::process::exit(1);
}
}
fn run() -> Result<()> {
println!("=== STRING Message: Device Command & Control ===\n");
let mut client = ClientBuilder::new().tcp("127.0.0.1:18944").sync().build()?;
println!("[INFO] Connected to OpenIGTLink server\n");
println!("[SCENARIO 1] System Lifecycle Commands");
println!(" Controlling imaging device startup/shutdown sequence...\n");
send_command(&mut client, "INIT", "Initialize imaging system")?;
thread::sleep(Duration::from_secs(1));
send_command(&mut client, "START", "Begin image acquisition")?;
thread::sleep(Duration::from_secs(2));
send_command(&mut client, "STOP", "Stop image acquisition")?;
thread::sleep(Duration::from_secs(1));
send_command(&mut client, "SHUTDOWN", "Power down system")?;
thread::sleep(Duration::from_secs(1));
println!("\n[SCENARIO 2] Configuration Commands");
println!(" Setting device parameters...\n");
send_command(&mut client, "SET_GAIN 75", "Adjust image gain to 75%")?;
thread::sleep(Duration::from_millis(500));
send_command(
&mut client,
"SET_FREQUENCY 5.0",
"Set ultrasound frequency to 5 MHz",
)?;
thread::sleep(Duration::from_millis(500));
send_command(
&mut client,
"SET_DEPTH 120",
"Configure imaging depth to 120mm",
)?;
thread::sleep(Duration::from_millis(500));
println!("\n[SCENARIO 3] Query Commands");
println!(" Requesting device information...\n");
send_command(&mut client, "GET_STATUS", "Request device status")?;
thread::sleep(Duration::from_millis(500));
send_command(&mut client, "GET_VERSION", "Query firmware version")?;
thread::sleep(Duration::from_millis(500));
send_command(&mut client, "GET_CAPABILITIES", "List device capabilities")?;
println!("\n[INFO] All commands sent successfully");
Ok(())
}
fn send_command(client: &mut SyncIgtlClient, command: &str, description: &str) -> Result<()> {
let string_msg = StringMessage::utf8(command);
println!(" → Sending: \"{}\"", command);
println!(" Description: {}", description);
println!(" Encoding: UTF-8 (MIBenum=106)");
println!(" Length: {} bytes", string_msg.len());
let msg = IgtlMessage::new(string_msg, "CommandInterface")?;
client.send(&msg)?;
println!(" ✓ Sent\n");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_command_creation() {
let cmd = StringMessage::utf8("START");
assert_eq!(cmd.as_str(), "START");
assert_eq!(cmd.encoding, 106); }
#[test]
fn test_command_with_parameters() {
let cmd = StringMessage::utf8("SET_GAIN 75");
assert_eq!(cmd.as_str(), "SET_GAIN 75");
assert!(!cmd.is_empty());
}
#[test]
fn test_ascii_encoding() {
let cmd = StringMessage::new("INIT");
assert_eq!(cmd.encoding, 3); }
#[test]
fn test_command_length() {
let cmd = StringMessage::utf8("GET_STATUS");
assert_eq!(cmd.len(), 10);
}
}