yuanan пре 1 месец
родитељ
комит
598692c75e
10 измењених фајлова са 69 додато и 43 уклоњено
  1. 25 25
      Cargo.lock
  2. 2 0
      emu-config.yaml
  3. 5 4
      src/cmd/cmd.rs
  4. 11 1
      src/cmd/config.rs
  5. 6 2
      src/ems/bms/bms.rs
  6. 1 1
      src/ems/emu/emu.rs
  7. 6 2
      src/ems/pcs/pcs.rs
  8. 6 5
      src/internal/utils/log.rs
  9. 6 2
      src/internal/utils/mq.rs
  10. 1 1
      src/main.rs

+ 25 - 25
Cargo.lock

@@ -186,9 +186,9 @@ checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9"
 
 [[package]]
 name = "cc"
-version = "1.2.13"
+version = "1.2.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda"
+checksum = "0c3d1b2e905a3a7b00a6141adb0e4c0bb941d11caf55349d863942a1cc44e3c9"
 dependencies = [
  "shlex",
 ]
@@ -221,9 +221,9 @@ dependencies = [
 
 [[package]]
 name = "clap"
-version = "4.5.28"
+version = "4.5.30"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3e77c3243bd94243c03672cb5154667347c457ca271254724f9f393aee1c05ff"
+checksum = "92b7b18d71fad5313a1e320fa9897994228ce274b60faa4d694fe0ea89cd9e6d"
 dependencies = [
  "clap_builder",
  "clap_derive",
@@ -231,9 +231,9 @@ dependencies = [
 
 [[package]]
 name = "clap_builder"
-version = "4.5.27"
+version = "4.5.30"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7"
+checksum = "a35db2071778a7344791a4fb4f95308b5673d219dee3ae348b86642574ecc90c"
 dependencies = [
  "anstream",
  "anstyle",
@@ -316,9 +316,9 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
 
 [[package]]
 name = "equivalent"
-version = "1.0.1"
+version = "1.0.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
+checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
 
 [[package]]
 name = "flate2"
@@ -584,9 +584,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
 
 [[package]]
 name = "miniz_oxide"
-version = "0.8.3"
+version = "0.8.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"
+checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b"
 dependencies = [
  "adler2",
 ]
@@ -744,7 +744,7 @@ checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
 dependencies = [
  "rand_chacha",
  "rand_core",
- "zerocopy 0.8.17",
+ "zerocopy 0.8.20",
 ]
 
 [[package]]
@@ -759,12 +759,12 @@ dependencies = [
 
 [[package]]
 name = "rand_core"
-version = "0.9.0"
+version = "0.9.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff"
+checksum = "a88e0da7a2c97baa202165137c158d0a2e824ac465d13d81046727b34cb247d3"
 dependencies = [
  "getrandom",
- "zerocopy 0.8.17",
+ "zerocopy 0.8.20",
 ]
 
 [[package]]
@@ -926,9 +926,9 @@ dependencies = [
 
 [[package]]
 name = "smallvec"
-version = "1.13.2"
+version = "1.14.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
+checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd"
 
 [[package]]
 name = "socket2"
@@ -1143,9 +1143,9 @@ dependencies = [
 
 [[package]]
 name = "unicode-ident"
-version = "1.0.16"
+version = "1.0.17"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034"
+checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe"
 
 [[package]]
 name = "utf8parse"
@@ -1155,9 +1155,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
 
 [[package]]
 name = "uuid"
-version = "1.13.1"
+version = "1.13.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ced87ca4be083373936a67f8de945faa23b6b42384bd5b64434850802c6dccd0"
+checksum = "8c1f41ffb7cf259f1ecc2876861a17e7142e63ead296f671f81f6ae85903e0d6"
 dependencies = [
  "getrandom",
 ]
@@ -1375,11 +1375,11 @@ dependencies = [
 
 [[package]]
 name = "zerocopy"
-version = "0.8.17"
+version = "0.8.20"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "aa91407dacce3a68c56de03abe2760159582b846c6a4acd2f456618087f12713"
+checksum = "dde3bb8c68a8f3f1ed4ac9221aad6b10cece3e60a8e2ea54a6a2dec806d0084c"
 dependencies = [
- "zerocopy-derive 0.8.17",
+ "zerocopy-derive 0.8.20",
 ]
 
 [[package]]
@@ -1395,9 +1395,9 @@ dependencies = [
 
 [[package]]
 name = "zerocopy-derive"
-version = "0.8.17"
+version = "0.8.20"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "06718a168365cad3d5ff0bb133aad346959a2074bd4a85c121255a11304a8626"
+checksum = "eea57037071898bf96a6da35fd626f4f27e9cee3ead2a6c703cf09d472b2e700"
 dependencies = [
  "proc-macro2",
  "quote",

+ 2 - 0
emu-config.yaml

@@ -10,3 +10,5 @@ rabbitmq:
   port: 5672
   username: "guest"
   password: "yuanan520."
+log:
+  path: "logs"

+ 5 - 4
src/cmd/cmd.rs

@@ -1,5 +1,6 @@
-use crate::cmd::config::{AppConfig, EMU_CONFIG};
+use crate::cmd::config::{AppConfig, APP_CONFIG};
 use crate::ems;
+use crate::internal::utils::mq::{RabbitMQ, MQ};
 use clap::Parser;
 use std::sync::Arc;
 use tokio::fs::read_to_string;
@@ -25,8 +26,8 @@ pub async fn cmd() {
 /// 初始化系统资源以及配置
 async fn init_system(config_path: String) -> anyhow::Result<()> {
     init_config(config_path).await?;
-    // let rabbitmq = RabbitMQ::new().await?;
-    // MQ.get_or_init(|| rabbitmq);
+    let rabbitmq = RabbitMQ::new().await?;
+    MQ.get_or_init(|| rabbitmq);
     Ok(())
 }
 
@@ -34,6 +35,6 @@ async fn init_system(config_path: String) -> anyhow::Result<()> {
 async fn init_config(config_path: String) -> anyhow::Result<()> {
     let content = read_to_string(config_path).await?;
     let app_config: AppConfig = serde_yml::from_str(content.as_str())?;
-    EMU_CONFIG.set(app_config).expect("配置载入内存错误");
+    APP_CONFIG.set(app_config).expect("配置载入内存错误");
     Ok(())
 }

+ 11 - 1
src/cmd/config.rs

@@ -2,12 +2,18 @@ use serde::{Deserialize, Serialize};
 use std::sync::OnceLock;
 
 //全局配置
-pub static EMU_CONFIG: OnceLock<AppConfig> = OnceLock::new();
+pub static APP_CONFIG: OnceLock<AppConfig> = OnceLock::new();
+
+#[inline]
+pub fn app_config() -> &'static AppConfig {
+    APP_CONFIG.get().expect("配置尚未加载")
+}
 
 #[derive(Debug, Serialize, Deserialize)]
 pub struct AppConfig {
     pub emu: EmuConfig,
     pub rabbitmq: RabbitMQConfig,
+    pub log: LogConfig,
 }
 
 #[derive(Serialize, Deserialize, Debug)]
@@ -35,3 +41,7 @@ pub struct RabbitMQConfig {
     pub username: String,
     pub password: String,
 }
+#[derive(Serialize, Deserialize, Debug)]
+pub struct LogConfig {
+    pub path: String,
+}

+ 6 - 2
src/ems/bms/bms.rs

@@ -1,3 +1,4 @@
+use crate::cmd::config::app_config;
 use crate::ems::{Consumer, Producer, Service};
 use crate::internal::utils;
 use log::info;
@@ -15,7 +16,7 @@ impl Bms {
     /// 初始化BMS
     pub async fn new(producer: Producer, consumer: Consumer) -> Self {
         let id = utils::generate_random_str(12);
-        utils::log::init_log("inpower_iot_mgc_rs::ems::bms::*", "logs/bms.log").await;
+        utils::log::init_log("inpower_iot_mgc_rs::ems::bms::*", "bms/bms.log").await;
         info!("BMS [{}] 初始化成功", id);
         Bms {
             id,
@@ -28,7 +29,10 @@ impl Bms {
 async fn read_task(producer: Producer) {
     loop {
         producer.send("PCS 你好".to_string()).await.unwrap();
-        tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
+        tokio::time::sleep(tokio::time::Duration::from_millis(
+            app_config().emu.ems.read_interval,
+        ))
+        .await;
     }
 }
 

+ 1 - 1
src/ems/emu/emu.rs

@@ -32,7 +32,7 @@ impl Emu {
     /// 创建EMU实例
     pub async fn new() -> Self {
         //初始化日志
-        utils::log::init_log("inpower_iot_mgc_rs::ems::emu::*", "logs/emu.log").await;
+        utils::log::init_log("inpower_iot_mgc_rs::ems::emu::*", "emu/emu.log").await;
         //EMU消息广播器
         let (tx, _) = sync::broadcast::channel::<String>(8);
         let mut devices = HashMap::new();

+ 6 - 2
src/ems/pcs/pcs.rs

@@ -1,3 +1,4 @@
+use crate::cmd::config::app_config;
 use crate::ems::{Consumer, Producer, Service};
 use crate::internal::utils;
 use log::info;
@@ -15,7 +16,7 @@ impl Pcs {
     /// 初始化PCS
     pub async fn new(producer: Producer, consumer: Consumer) -> Self {
         let id = utils::generate_random_str(12);
-        utils::log::init_log("inpower_iot_mgc_rs::ems::pcs::*", "logs/pcs.log").await;
+        utils::log::init_log("inpower_iot_mgc_rs::ems::pcs::*", "pcs/pcs.log").await;
         info!("PCS [{}] 初始化成功", id);
         Pcs {
             id,
@@ -28,7 +29,10 @@ impl Pcs {
 async fn read_task(producer: Producer) {
     loop {
         producer.send("PCS nihao".to_string()).await.unwrap();
-        tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
+        tokio::time::sleep(tokio::time::Duration::from_millis(
+            app_config().emu.ems.read_interval,
+        ))
+        .await;
     }
 }
 

+ 6 - 5
src/internal/utils/log.rs

@@ -1,3 +1,4 @@
+use crate::cmd::config::app_config;
 use tklog::handle::FileMixedMode;
 use tklog::{Format, LogOption, ASYNC_LOG, LEVEL};
 
@@ -30,13 +31,13 @@ pub async fn init_log(module: &str, filename: &str) {
                 formatter: Some(String::from("{time} {level} {message}\n")),
                 fileoption: Some(Box::new(FileMixedMode::new(
                     //日志文件名
-                    filename,
-                    //滚动字节数, 目前这里是1G
-                    1 << 30,
+                    format!("{}/{}", app_config().log.path, filename).as_str(),
+                    //滚动字节数, 目前这里是512M
+                    1 << 29,
                     //同时每天滚动
                     tklog::MODE::DAY,
-                    //最多备份数
-                    10,
+                    //最多备份数, 30天
+                    30,
                     //是否开启压缩
                     true,
                 ))),

+ 6 - 2
src/internal/utils/mq.rs

@@ -1,17 +1,21 @@
-use crate::cmd::config::EMU_CONFIG;
+use crate::cmd::config::app_config;
 use amqprs::connection::{Connection, OpenConnectionArguments};
 use std::sync::OnceLock;
 
 /// RabbitMQ 单例
 pub static MQ: OnceLock<RabbitMQ> = OnceLock::new();
 
+pub fn mq_conn() -> &'static RabbitMQ {
+    MQ.get().unwrap()
+}
+
 pub struct RabbitMQ {
     pub conn: Connection,
 }
 
 impl RabbitMQ {
     pub async fn new() -> Result<Self, anyhow::Error> {
-        let config = &EMU_CONFIG.get().unwrap().rabbitmq;
+        let config = &app_config().rabbitmq;
         let conn = Connection::open(&OpenConnectionArguments::new(
             config.host.as_str(),
             config.port,

+ 1 - 1
src/main.rs

@@ -3,4 +3,4 @@ use inpower_iot_mgc_rs::cmd::cmd::cmd;
 #[tokio::main]
 async fn main() {
     cmd().await;
-}
+}