2个稳定版本
1.0.2 | 2020年2月14日 |
---|---|
1.0.1 | 2019年5月2日 |
#1908 in 编码
195KB
2.5K SLoC
Rust语言OpenFMB操作用例的protobuf
Rust编程语言基于OpenFMB操作用例数据模型(位于此处)的Protocol Buffer(protobuf)定义。
包含在你的项目中
在Cargo.toml文件中添加这些定义到你的项目中有几种方法。
从crates.io
[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
从GitLab仓库
[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 = "<release-tag-label>" }
使用
在项目Cargo.toml文件中添加依赖后,你可以将protobuf定义包含到源文件中,如下所示
extern crate prost;
use prost::*;
extern crate rust_openfmb_ops_protobuf;
use rust_openfmb_ops_protobuf::*;
导入crate后,你现在可以开始使用protobuf定义,如下所示
将OpenFMB配置文件编码为protobuf
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<u8> = Vec::new();
mrp.encode(&mut bmsg).unwrap();
// Do what you want with the bytes now
}
从protobuf解码到OpenFMB配置文件名
fn main() {
// Get the encoded protobuf buffer from somewhere...
let protobuf_bytes: std::vec::Vec<u8> = ...
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文件。
许可证
除非另有说明,否则本存储库中的所有文件均根据LICENSE文件中找到的Apache Version 2.0许可证分发。
依赖关系
~3MB
~62K SLoC