1个不稳定版本

0.1.0 2020年10月10日

#1220硬件支持


用于 axctl

MIT/Apache

140KB
3K SLoC

vapix

AXIS通信设备VAPIX API的客户端。要点

  • 异步
  • #![禁止(不安全代码)]

特性

  • axis::Device 监控和控制运行AXIS固件 >= 5.00 的设备
  • axis::Transport 将库与 http 解耦

可选特性

  • hyper: 通过 axis::HyperTransport 进行HTTP(默认启用)

基本使用

// Instantiate a Device using `hyper` to communicate
let uri = http::Uri::from_static("http://user:[email protected]");
let device = axis::Device::new(axis::HyperTransport::default(), uri);

// Probe for VAPIX APIs supported by this device
let services = device.services().await?;

// If it supports the basic device info service...
if let Some(basic_device_info) = services.basic_device_info.as_ref() {
    // ...ask for those properties
    let properties = basic_device_info.properties().await?;
    println!("product_full_name: {:?}", properties.product_full_name);
    println!("    serial_number: {:?}", properties.serial_number);
} else {
    // If not, assume it supports the legacy parameters API, and retrieve those parameters
    let parameters = device.parameters().list(None).await?;
    println!("product_full_name: {:?}", parameters["root.Brand.ProdFullName"]);
    println!("    serial_number: {:?}", parameters["root.Properties.System.Soc"]);
}

开发

使用 cargo testcargo checkcargo clippyrustfmt 进行常规开发。使用 issues 打开问题,使用 changesets 打开PR。

许多API测试使用 crate::mock_device(),绑定到测试 Transport,该传输直接指向块而不是网络。如果您想确保某个函数发送了正确的请求或对某个响应做了正确处理,请编写一个覆盖该情况的测试。

#[tokio::test]
async fn update() {
    let device = crate::mock_device(|req| {
        assert_eq!(req.method(), http::Method::GET);
        assert_eq!(
            req.uri().path_and_query().map(|pq| pq.as_str()),
            Some("/axis-cgi/param.cgi?action=update&foo.bar=baz+quxx")
        );

        http::Response::builder()
            .status(http::StatusCode::OK)
            .header(http::header::CONTENT_TYPE, "text/plain")
            .body(vec![b"OK".to_vec()])
    });

    let response = device
        .parameters()
        .update(vec![("foo.bar", "baz quxx")])
        .await;
    match response {
        Ok(()) => {}
        Err(e) => panic!("update should succeed: {}", e),
    };
}

覆盖安全调用的API的测试使用 create::test_with_devices() 来测试与实际设备的录制。 test_with_devices() 重复调用一个异步块,该块使用不同的 TestDevice 调用,这些设备包含元数据和 Device。测试可以自由失败,使用 Error::UnsupportedFeature,但断言失败或其他错误会导致包含的测试失败。

#[test]
fn test_something() {
    crate::test_with_devices(|test_device| async move {
        // This block is called with various test_devices. If the block fails for any device, the
        // test will fail. If the test depends on particular device features, the test must probe
        // for those features, and it must succeed if the feature is missing.
        // 
        // test_device.device_info.{model, firmware_version, ..} describe the test device
        // test_device.device is a crate::Device<_> and can make arbitrary calls

        let parameters = test_device.device.parameters();
        let all_params = parameters.list_definitions(None).await?;

        // assert!() things which should always be true on every device ever
        // If behavior depends on firmware version, inquire about test_device's metadata
    });
}

录音是通过测试套件本身产生的。当设置 RECORD_DEVICE_URI 时,每个 test_with_devices() 块还会针对实际设备运行,并生成一个配置。

$ RECORD_DEVICE_URI=http://user:[email protected] cargo test

$ git status
On branch master

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	fixtures/recordings/ACCC8EF7DE6B v9.80.2.2.json
$

希望收集现场设备上的录音,因此 test_with_devices() 块必须避免修改设备状态。

依赖项

~14MB
~273K SLoC