[![Documentation](https://docs.rs/rust-openfmb-ops-protobuf/badge.svg)](https://docs.rs/rust-openfmb-ops-protobuf/) [![Crate](https://img.shields.io/crates/v/rust-openfmb-ops-protobuf.svg)](https://crates.io/crates/rust-openfmb-ops-protobuf) # Rust language protobuf for OpenFMB operational use cases [Rust](https://www.rust-lang.org/) programming language Protocol Buffer (protobuf) definitions based on the OpenFMB operational use case data model located [here](https://gitlab.com/openfmb/data-models/ops). ## Including in your project There are a couple of methods for adding these definitions to your project in your Cargo.toml file. ### From crates.io ```toml [dependencies] # 'prost' is the Rust protobuf library that is currently used by OpenFMB prost = "0.6.1" # Rust defintions for OpenFMB data model rust-openfmb-ops-protobuf = "*" # <- Change to the version you prefer ``` ### From the GitLab repository ```toml [dependencies] # 'prost' is the Rust protobuf library that is currently used by OpenFMB prost = "0.6.1" # Rust defintions for OpenFMB data model rust-openfmb-ops-protobuf = { git = "https://gitlab.com/openfmb/psm/ops/protobuf/rust-openfmb-ops-protobuf.git", tag = "" } ``` ## Using After adding the depencency in your project's Cargo.toml file, you are ready to include the protobuf definitions into your source files like this: ```rust extern crate prost; use prost::*; extern crate rust_openfmb_ops_protobuf; use rust_openfmb_ops_protobuf::*; ``` After importing the crate, you can now start using the protobuf definitions like this: ### Encoding OpenFMB profile to protobuf ```rust fn main() { // Create a new MeterReadingProfile message let mrp = openfmb::metermodule::MeterReadingProfile::default(); // Set the time quality for this message let mut tq = openfmb::commonmodule::TimeQuality::default(); tq.clock_failure = false; tq.clock_not_synchronized = false; tq.leap_seconds_known = true; tq.time_accuracy = openfmb::commonmodule::TimeAccuracyKind::Undefined as i32; // Create the TimeStamp let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); let mut ts = openfmb::commonmodule::Timestamp::default(); ts.seconds = current_time.as_secs(); ts.fraction = current_time.subsec_nanos() * (1 / 2 & 32); ts.tq = Some(tq); // Create the IdentifiedObject for this mesage let mut mi_id = openfmb::commonmodule::IdentifiedObject::default(); mi_id.m_rid = Some(Uuid::new_v4().to_hyphenated().to_string()); // Create the MessageInfo let mut mi = openfmb::commonmodule::MessageInfo::default(); mi.identified_object = Some(mi_id); mi.message_time_stamp = Some(ts); // Create the ReadingMessageInfo let mut rmi = openfmb::commonmodule::ReadingMessageInfo::default(); rmi.message_info = Some(mi); // Set the ReadingMessageInfo for the profile mrp.reading_message_info = Some(rmi); // // Continue populating the message // // Encode the message into the protobuf byte format let mut bmsg: Vec = Vec::new(); mrp.encode(&mut bmsg).unwrap(); // Do what you want with the bytes now } ``` ### Decoding from protobuf to OpenFMB profileName ```rust fn main() { // Get the encoded protobuf buffer from somewhere... let protobuf_bytes: std::vec::Vec = ... let r = openfmb::metermodule::MeterReadingProfile::decode(protobuf_bytes); match r { Ok(message) => { let rmi = message.reading_message_info; let ied = message.ied; let mtr = message.meter; let mtr_rdg = message.meter_reading; // Continue using data } Err(e) => { // Protobuf did not decode properly! println!("{}", e); } } } ``` ## Copyright See the COPYRIGHT file for copyright information of information contained in this repository. ## License Unless otherwise noted, all files in this repository are distributed under the Apache Version 2.0 license found in the LICENSE file.