#tokio #data #resol #v-bus #wrap #asynchronous #conjunction

tokio-resol-vbus

一个用于异步处理RESOL VBus数据的Rust库

1 个不稳定版本

0.1.0 2019年1月6日

#3 in #conjunction

MIT/Apache

71KB
1.5K SLoC

tokio-resol-vbus.rs

一个库,用于封装resol-vbus crate,以便与tokio crate异步联合使用。

Crates.io Travis Build Status

文档 仓库

示例

将实时VBus数据记录到持久文件格式中

use std::fs::File;

use resol_vbus::{DataSet, RecordingWriter};
use tokio::{net::TcpStream, prelude::*};

use tokio_resol_vbus::{Error, LiveDataStream, TcpClientHandshake};

fn main() {
    // Create an recording file and hand it to a `RecordingWriter`
    let file = File::create("test.vbus").expect("Unable to create output file");
    let mut rw = RecordingWriter::new(file);

    // Parse the address of the DL2 to connect to
    let addr = "192.168.13.45:7053"
        .parse()
        .expect("Unable to parse address");

    // Connect to the DL2
    let handler = TcpStream::connect(&addr)
        .map_err(Error::from)

        // Start the handshake
        .and_then(TcpClientHandshake::start)

        // Authenticate using a password
        .and_then(|hs| hs.send_pass_command("vbus"))

        // Switch to VBus data mode
        .and_then(|hs| hs.send_data_command())

        .and_then(|socket| {
            // Wrap the socket in a VBus `LiveDataStream`
            let (reader, writer) = socket.split();
            let stream = LiveDataStream::new(reader, writer, 0, 0x0020);

            // Read VBus `Data` values from the `LiveDataStream`
            stream.for_each(move |data| {
                println!("{}", data.id_string());

                // Add `Data` value into `DataSet` to be stored
                let mut data_set = DataSet::new();
                data_set.timestamp = data.as_ref().timestamp;
                data_set.add_data(data);

                // Write the `DataSet` into the `RecordingWriter` for permanent storage
                rw.write_data_set(&data_set)
                    .expect("Unable to write data set");

                Ok(())
            })
        })

        .map_err(|err| {
            eprintln!("{}", err);
        });

    // Start the tokio runtime
    tokio::run(handler);
}

依赖项

~5MB
~73K SLoC