add protobuf message submodule;

add dto module and implement prost build and proto struct encode/decode simple test;
This commit is contained in:
walker-sheng 2024-08-29 15:45:28 +08:00
parent 6bde9b2035
commit c499b45bf4
20 changed files with 569 additions and 42 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "rtss-proto-msg"]
path = rtss-proto-msg
url = https://gitea.joylink.club/joylink/rtss-proto-msg.git

View File

@ -1,12 +1,19 @@
{
"cSpell.words": [
"cpus",
"Graphi",
"graphiql",
"hashbrown",
"Hasher",
"Joylink",
"jsonwebtoken",
"mplj",
"prost",
"proto",
"protoc",
"protos",
"rtss",
"sysinfo",
"thiserror",
"timestep",
"trackside"

179
Cargo.lock generated
View File

@ -70,6 +70,12 @@ dependencies = [
"libc",
]
[[package]]
name = "anyhow"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "ascii_utils"
version = "0.9.3"
@ -1056,7 +1062,7 @@ dependencies = [
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
"windows-core 0.52.0",
]
[[package]]
@ -1095,6 +1101,15 @@ dependencies = [
"serde",
]
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.11"
@ -1214,12 +1229,27 @@ dependencies = [
"version_check",
]
[[package]]
name = "multimap"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03"
[[package]]
name = "nonmax"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51"
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
@ -1409,6 +1439,16 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "prettyplease"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro-crate"
version = "3.1.0"
@ -1427,6 +1467,59 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "prost"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13db3d3fde688c61e2446b4d843bc27a7e8af269a69440c0308021dc92333cc"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-build"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bb182580f71dd070f88d01ce3de9f4da5021db7115d2e1c3605a754153b77c1"
dependencies = [
"bytes",
"heck",
"itertools",
"log",
"multimap",
"once_cell",
"petgraph",
"prettyplease",
"prost",
"prost-types",
"regex",
"syn",
"tempfile",
]
[[package]]
name = "prost-derive"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18bec9b0adc4eba778b33684b7ba3e7137789434769ee3ce3930463ef904cfca"
dependencies = [
"anyhow",
"itertools",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "prost-types"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee5168b05f49d4b0ca581206eb14a7b22fafd963efe729ac48eb03266e25cc2"
dependencies = [
"prost",
]
[[package]]
name = "quote"
version = "1.0.36"
@ -1562,6 +1655,7 @@ dependencies = [
"rtss_trackside",
"serde",
"serde_json",
"sysinfo",
"tokio",
"tower-http",
]
@ -1577,6 +1671,18 @@ dependencies = [
"bevy_ecs",
]
[[package]]
name = "rtss_db"
version = "0.1.0"
[[package]]
name = "rtss_dto"
version = "0.1.0"
dependencies = [
"prost",
"prost-build",
]
[[package]]
name = "rtss_iscs"
version = "0.1.0"
@ -1836,9 +1942,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.72"
version = "2.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
dependencies = [
"proc-macro2",
"quote",
@ -1857,6 +1963,20 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
[[package]]
name = "sysinfo"
version = "0.31.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b92e0bdf838cbc1c4c9ba14f9c97a7ec6cdcd1ae66b10e1e42775a25553f45d"
dependencies = [
"core-foundation-sys",
"libc",
"memchr",
"ntapi",
"rayon",
"windows",
]
[[package]]
name = "tempfile"
version = "3.12.0"
@ -2371,6 +2491,16 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143"
dependencies = [
"windows-core 0.57.0",
"windows-targets",
]
[[package]]
name = "windows-core"
version = "0.52.0"
@ -2380,6 +2510,49 @@ dependencies = [
"windows-targets",
]
[[package]]
name = "windows-core"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"
dependencies = [
"windows-implement",
"windows-interface",
"windows-result",
"windows-targets",
]
[[package]]
name = "windows-implement"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-result"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.52.0"

View File

@ -1,4 +1,4 @@
[default.extend-words]
[files]
extend-exclude = ["README.md"]
extend-exclude = ["README.md", "rtss-proto-msg/*"]

View File

@ -15,6 +15,7 @@ tower-http = { version = "0.5.0", features = ["cors"] }
async-graphql = { version = "7.0.7", features = ["chrono"] }
async-graphql-axum = "7.0.6"
base64 = "0.22.1"
sysinfo = "0.31.3"
bevy_ecs = { workspace = true }
rtss_log = { path = "../rtss_log" }

View File

@ -2,6 +2,7 @@ mod jwt_auth;
mod server;
mod simulation;
mod simulation_operation;
mod sys_info;
pub use server::*;
pub fn add(left: u64, right: u64) -> u64 {

View File

@ -0,0 +1,89 @@
#[cfg(test)]
mod tests {
use std::{thread, time::Duration};
use sysinfo::{Disks, Networks, System};
const BYTES_IN_GIGABYTE: f32 = 1024.0 * 1024.0 * 1024.0;
#[test]
fn test_sys_info() {
let mut sys = System::new();
for _ in 0..1 {
// sys.refresh_all();
// sys.refresh_memory();
println!("System name: {:?}", System::name());
println!("Kernel version: {:?}", System::kernel_version());
println!("System OS version: {:?}", System::os_version());
println!("System host name: {:?}", System::host_name());
sys.refresh_memory();
// RAM and swap information:
println!("=> memory:");
println!(
"total memory: {} bytes, {:.2} Gi",
sys.total_memory(),
sys.total_memory() as f32 / BYTES_IN_GIGABYTE
);
println!(
"used memory : {} bytes, {:.2} Gi",
sys.used_memory(),
sys.used_memory() as f32 / BYTES_IN_GIGABYTE
);
println!(
"available memory : {} bytes, {:.2} Gi",
sys.available_memory(),
sys.available_memory() as f32 / BYTES_IN_GIGABYTE
);
println!(
"total swap : {} bytes, {} Gi",
sys.total_swap(),
sys.total_swap() as f32 / BYTES_IN_GIGABYTE
);
println!(
"used swap : {} bytes, {} Gi",
sys.used_swap(),
sys.used_swap() as f32 / BYTES_IN_GIGABYTE
);
sys.refresh_cpu_usage();
// Number of CPUs:
println!("=> CPU:");
println!("NB CPUs: {}", sys.cpus().len());
for cpu in sys.cpus() {
print!("{}% ", cpu.cpu_usage());
}
println!("CPU usage: {}%", sys.global_cpu_usage());
// Display processes ID, name na disk usage:
// for (pid, process) in sys.processes() {
// println!("[{pid}] {:?} {:?}", process.name(), process.disk_usage());
// }
// We display all disks' information:
println!("=> disks:");
let disks = Disks::new_with_refreshed_list();
for disk in &disks {
println!("{disk:?}");
}
// Network interfaces name, total data received and total data transmitted:
let networks = Networks::new_with_refreshed_list();
println!("=> networks:");
for (interface_name, data) in &networks {
if interface_name.contains("lo") {
continue;
}
println!(
"{interface_name}: {} B (down) / {} B (up)",
data.total_received(),
data.total_transmitted(),
);
// If you want the amount of data received/transmitted since last call
// to `Networks::refresh`, use `received`/`transmitted`.
}
println!("{}", f32::MAX);
thread::sleep(Duration::from_millis(100))
}
}
}

View File

@ -0,0 +1,6 @@
[package]
name = "rtss_db"
version = "0.1.0"
edition = "2021"
[dependencies]

14
crates/rtss_db/src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View File

@ -0,0 +1,10 @@
[package]
name = "rtss_dto"
version = "0.1.0"
edition = "2021"
[dependencies]
prost = "0.13"
[build-dependencies]
prost-build = "0.13"

19
crates/rtss_dto/build.rs Normal file
View File

@ -0,0 +1,19 @@
use prost_build::Config;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=../../rtss-proto-msg/src/basic_signal_data.proto");
#[cfg(target_os = "windows")]
{
std::env::set_var(
"PROTOC",
"../../rtss-proto-msg/protoc/protoc-27.4-win64/bin/protoc.exe",
);
}
Config::new()
.out_dir("src/pb")
.compile_protos(
&["../../rtss-proto-msg/src/basic_signal_data.proto"],
&["../../rtss-proto-msg/src/"],
)
.unwrap();
}

View File

@ -0,0 +1,26 @@
mod pb;
pub use pb::*;
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use prost::Message;
use super::*;
#[test]
fn test_encode_decode() {
let point = basic_signal_data::Point { x: 1.0, y: 2.0 };
let encoded = point.encode_to_vec();
let decoded = basic_signal_data::Point::decode(encoded.as_ref()).unwrap();
assert_eq!(point, decoded);
println!(
"point: {:?}, encoded: {:?}, decoded: {:?}",
point, encoded, decoded
);
}
}

View File

@ -0,0 +1,137 @@
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Storage {
#[prost(message, optional, tag = "1")]
pub canvas: ::core::option::Option<Canvas>,
#[prost(message, repeated, tag = "2")]
pub stations: ::prost::alloc::vec::Vec<Station>,
}
/// 画布数据
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Canvas {
/// 画布宽
#[prost(int32, tag = "1")]
pub width: i32,
/// 画布高
#[prost(int32, tag = "2")]
pub height: i32,
/// 背景色
#[prost(string, tag = "3")]
pub background_color: ::prost::alloc::string::String,
/// 视口变换
#[prost(message, optional, tag = "4")]
pub viewport_transform: ::core::option::Option<Transform>,
/// 画布格子背景
#[prost(message, optional, tag = "5")]
pub grid_background: ::core::option::Option<Grid>,
}
/// 格子背景
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Grid {
#[prost(bool, tag = "1")]
pub has_grid: bool,
/// 线色
#[prost(string, tag = "2")]
pub line_color: ::prost::alloc::string::String,
/// 间隔
#[prost(int32, tag = "3")]
pub space: i32,
}
/// 坐标点
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
pub struct Point {
/// x坐标
#[prost(float, tag = "1")]
pub x: f32,
/// y坐标
#[prost(float, tag = "2")]
pub y: f32,
}
/// 变换
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
pub struct Transform {
/// 位移
#[prost(message, optional, tag = "1")]
pub position: ::core::option::Option<Point>,
/// 缩放
#[prost(message, optional, tag = "2")]
pub scale: ::core::option::Option<Point>,
/// 旋转弧度
#[prost(float, tag = "3")]
pub rotation: f32,
/// 歪斜
#[prost(message, optional, tag = "4")]
pub skew: ::core::option::Option<Point>,
}
/// 子元素变换
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ChildTransform {
/// 子元素名称
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
/// 子元素变换
#[prost(message, optional, tag = "2")]
pub transform: ::core::option::Option<Transform>,
}
/// 公共属性
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CommonInfo {
/// 数据id
#[prost(uint32, tag = "1")]
pub id: u32,
/// 数据类型
#[prost(string, tag = "2")]
pub graphic_type: ::prost::alloc::string::String,
/// 变换
#[prost(message, optional, tag = "3")]
pub transform: ::core::option::Option<Transform>,
/// 子元素变换
#[prost(message, repeated, tag = "4")]
pub children_transform: ::prost::alloc::vec::Vec<ChildTransform>,
}
/// 公里标
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct KilometerMark {
/// 公里标坐标系
#[prost(string, tag = "1")]
pub coordinate: ::prost::alloc::string::String,
/// 公里标数值
#[prost(int64, tag = "2")]
pub value: i64,
}
/// 车站数据
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Station {
#[prost(message, optional, tag = "1")]
pub common: ::core::option::Option<CommonInfo>,
/// 车站名
#[prost(string, tag = "2")]
pub name: ::prost::alloc::string::String,
/// 车站站名
#[prost(string, tag = "3")]
pub zhan_name: ::prost::alloc::string::String,
/// 车站名拼音简写
#[prost(string, tag = "4")]
pub name_pinyin: ::prost::alloc::string::String,
/// 公里标
#[prost(message, optional, tag = "6")]
pub km: ::core::option::Option<KilometerMark>,
/// 是否集中站
#[prost(bool, tag = "10")]
pub concentration: bool,
/// 是否车辆段
#[prost(bool, tag = "11")]
pub depots: bool,
/// 集中站管理的车站-id
#[prost(uint32, repeated, tag = "13")]
pub manage_station_ids: ::prost::alloc::vec::Vec<u32>,
}

View File

@ -0,0 +1 @@
pub mod basic_signal_data;

View File

@ -0,0 +1,20 @@
use bevy_ecs::bundle::Bundle;
use rtss_common::Uid;
use super::{PsdState, TurnoutState, TwoNormalPositionsTransform};
// 道岔设备组件包
#[derive(Bundle, Default)]
pub struct TurnoutBundle {
pub uid: Uid,
pub turnout_state: TurnoutState,
pub two_normal_positions_conversion: TwoNormalPositionsTransform,
}
/// 屏蔽门设备组件包
#[derive(Bundle, Default)]
pub struct PsdBundle {
pub uid: Uid,
pub psd_state: PsdState,
pub two_normal_positions_conversion: TwoNormalPositionsTransform,
}

View File

@ -1,32 +1,42 @@
use bevy_ecs::{bundle::Bundle, component::Component};
use rtss_common::Uid;
use bevy_ecs::component::Component;
// 两常态位置转换组件,用于像道岔位置,屏蔽门位置等
/// 两常态位置转换组件,用于像道岔位置,屏蔽门位置等
#[derive(Component, Debug, Clone, PartialEq, Default)]
pub struct TwoNormalPositionsTransform {
// 当前实际位置,百分比值,0-100
pub position: i32,
pub position: f32,
// 当前转换速度
pub velocity: f32,
}
// 道岔设备状态组件
/// 道岔设备状态组件
#[derive(Component, Debug, Clone, PartialEq, Eq, Default)]
pub struct TurnoutState {
// 定位表示
pub db: bool,
// 反位表示
pub fb: bool,
// 定操表示
pub dc: bool,
// 反操表示
pub fc: bool,
// 定位表示继电器状态
pub dbj: bool,
// 反位表示继电器状态
pub fbj: bool,
// 是否定位
pub dw: bool,
// 是否反位
pub fw: bool,
// 定操继电器状态
pub dcj: bool,
// 反操继电器状态
pub fcj: bool,
}
// 道岔设备组件包
#[derive(Bundle, Default)]
pub struct TurnoutBundle {
pub uid: Uid,
pub turnout_state: TurnoutState,
pub two_normal_positions_conversion: TwoNormalPositionsTransform,
pub struct SignalState {}
/// 屏蔽门设备状态组件
#[derive(Component, Debug, Clone, PartialEq, Eq, Default)]
pub struct PsdState {
// 门关继电器状态
pub mgj: bool,
// 关门继电器状态
pub gmj: bool,
// 开门继电器状态
pub kmj: bool,
// 门旁路继电器状态(互锁解除)
pub mplj: bool,
}

View File

@ -1,2 +1,4 @@
mod bundle;
mod equipment;
pub use bundle::*;
pub use equipment::*;

View File

@ -18,12 +18,12 @@ pub fn two_normal_position_transform(
if transform.velocity == 0f32 {
continue;
}
let p = transform.position + transform.velocity as i32;
if p > TWO_NORMAL_POSITION_MAX {
transform.position = TWO_NORMAL_POSITION_MAX;
let p = transform.position + transform.velocity;
if p >= TWO_NORMAL_POSITION_MAX as f32 {
transform.position = TWO_NORMAL_POSITION_MAX as f32;
transform.velocity = TWO_NORMAL_POSITION_MIN as f32;
} else if p < TWO_NORMAL_POSITION_MIN {
transform.position = TWO_NORMAL_POSITION_MIN;
} else if p <= TWO_NORMAL_POSITION_MIN as f32 {
transform.position = TWO_NORMAL_POSITION_MIN as f32;
transform.velocity = 0 as f32;
} else {
transform.position = p;

View File

@ -8,6 +8,7 @@ use rtss_log::tracing::debug;
use crate::{
events::TurnoutControlEvent, SimulationConfig, TurnoutState, TwoNormalPositionsTransform,
TWO_NORMAL_POSITION_MAX, TWO_NORMAL_POSITION_MIN,
};
// 道岔控制事件处理系统
@ -27,14 +28,14 @@ pub fn handle_turnout_control(
);
match trigger.event() {
TurnoutControlEvent::DC => {
state.dc = true;
state.fc = false;
state.dcj = true;
state.fcj = false;
conversion.velocity = -v;
debug!("道岔定操处理:{:?}", uid);
}
TurnoutControlEvent::FC => {
state.dc = false;
state.fc = true;
state.dcj = false;
state.fcj = true;
conversion.velocity = v;
debug!("道岔反操处理uid={:?}, conversion={:?}", uid, conversion);
}
@ -50,17 +51,23 @@ pub fn turnout_state_update(
"更新道岔状态Uid={:?}, State={:?}, Conversion={:?}",
uid, state, conversion
);
if conversion.position == 0 {
state.db = true;
state.fb = false;
state.dc = false;
} else if conversion.position == 100 {
state.db = false;
state.fb = true;
state.fc = false;
if conversion.position as i32 == TWO_NORMAL_POSITION_MIN {
state.dw = true;
state.fw = false;
state.dbj = true;
state.fbj = false;
state.dcj = false;
} else if conversion.position as i32 == TWO_NORMAL_POSITION_MAX {
state.fw = true;
state.dw = false;
state.dbj = false;
state.fbj = true;
state.fcj = false;
} else {
state.db = false;
state.fb = false;
state.dw = false;
state.fw = false;
state.dbj = false;
state.fbj = false;
}
}
}

1
rtss-proto-msg Submodule

@ -0,0 +1 @@
Subproject commit 9dee9892e87b1dca12a1753077e64aca7fced4b8