Skip to main content

ptz_control/
ptz_control.rs

1use dvrip_rs::{Authentication, Connection, DVRIPCam, PTZ, PTZCommand};
2use std::time::Duration;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let args: Vec<String> = std::env::args().collect();
7    if args.len() < 4 {
8        println!("Usage: {} <IP> <Username> <Password>", args[0]);
9        return Ok(());
10    }
11
12    let ip = &args[1];
13    let user = &args[2];
14    let pass = &args[3];
15
16    let mut cam = DVRIPCam::new(ip);
17
18    cam.connect(Duration::from_secs(5)).await?;
19    cam.login(user, pass).await?;
20
21    println!("Performing PTZ operations...");
22
23    // 1. Step movement
24    println!("Stepping Left...");
25    cam.ptz_step(PTZCommand::DirectionLeft, 10).await?;
26    tokio::time::sleep(Duration::from_millis(500)).await;
27
28    println!("Stepping Right...");
29    cam.ptz_step(PTZCommand::DirectionRight, 10).await?;
30    tokio::time::sleep(Duration::from_millis(500)).await;
31
32    // 2. Continuous movement
33    println!("Starting continuous Up movement...");
34    cam.ptz(PTZCommand::DirectionUp, 3, -1, 0).await?;
35
36    tokio::time::sleep(Duration::from_secs(1)).await;
37
38    // 3. Zooming (using ptz_step which handles start/stop)
39    println!("Zooming Tile...");
40    cam.ptz_step(PTZCommand::ZoomTile, 4).await?;
41    tokio::time::sleep(Duration::from_secs(1)).await;
42    println!("Zooming Wide...");
43    cam.ptz_step(PTZCommand::ZoomWide, 4).await?;
44
45    // 4. Presets
46    println!("Moving to Preset 1...");
47    cam.ptz(PTZCommand::GotoPreset, 3, 1, 0).await?;
48
49    cam.close().await?;
50    println!("Done.");
51
52    Ok(())
53}