From 1b5351fc4d169efe67d73b1ecf95794178b34bdb Mon Sep 17 00:00:00 2001 From: zqm Date: Tue, 7 Apr 2026 16:41:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E6=97=B6?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86DeviceNumber?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Windows/CS/Framework4.0/Updater/src/main.rs | 72 ++++++++++++++------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/Windows/CS/Framework4.0/Updater/src/main.rs b/Windows/CS/Framework4.0/Updater/src/main.rs index f728d08..9d3d945 100644 --- a/Windows/CS/Framework4.0/Updater/src/main.rs +++ b/Windows/CS/Framework4.0/Updater/src/main.rs @@ -77,6 +77,33 @@ fn resolve_ws_url() -> String { "ws://127.0.0.1:8087/ws".to_string() } +/// 从公共 config.json 读取 DeviceNumber 字段 +fn resolve_device_number() -> String { + let config_path = get_public_config_path(); + if let Ok(content) = fs::read_to_string(&config_path) { + if let Ok(json) = serde_json::from_str::(&content) { + // 尝试多个可能的字段名 + if let Some(id) = json.get("DeviceNumber").and_then(|v| v.as_str()) { + if !id.is_empty() { + return id.to_string(); + } + } + if let Some(id) = json.get("StationId").and_then(|v| v.as_str()) { + if !id.is_empty() { + return id.to_string(); + } + } + if let Some(id) = json.get("Station").and_then(|v| v.as_str()) { + if !id.is_empty() { + return id.to_string(); + } + } + } + } + // 读取失败 → 降级到默认值 + "UNKNOWN".to_string() +} + fn is_process_running(process_name: &str) -> bool { use std::process::id; @@ -160,28 +187,29 @@ async fn run_updater(debug_mode: bool) { // 设置首次连接回调 - 发送 GetFileVer 命令 let debug_for_first = debug_mode; + let device_number = resolve_device_number(); client.on_first_connect(move |_url, sender| { + let device_number = device_number.clone(); Box::pin(async move { if debug_for_first { println!("[首次连接] 发送 GetFileVer 命令..."); } - // 构造 GetFileVer 消息 - let msg = serde_json::json!({ - "Type": "GetFileVer", - "Data": { - "file": "BootLoader.exe" - } - }); - let msg_str = msg.to_string(); + // 构造 GetFileVer 消息 - Type在前,DeviceNumber从公共配置读取,file_list数组 + // 注意:手动拼接字符串确保 Type 在 JSON 的第一位(serde_json::json! 会按字母排序) + let msg_str = format!( + r#"{{"Type":"GetFileVer","DeviceNumber":"{}","Data":{{"file_list":["BootLoader.exe"]}}}}"#, + device_number + ); + + if debug_for_first { + println!("[首次连接] GetFileVer 已发送: {}", msg_str); + } // 通过 sender 发送消息 (使用 .lock().await) let sender_guard = sender.lock().await; if let Some(ref tx) = *sender_guard { let _ = tx.try_send(cube_lib::websocket::OutgoingMessage::Text(msg_str)); } - if debug_for_first { - println!("[首次连接] GetFileVer 已发送"); - } }) as Pin + Send + Sync>> }); @@ -204,28 +232,28 @@ async fn run_updater(debug_mode: bool) { // 设置重连成功回调 - 每次重连成功后也发送 GetFileVer let debug_for_reconnected = debug_mode; + let device_number = resolve_device_number(); client.on_reconnected(move |_url, sender| { + let device_number = device_number.clone(); Box::pin(async move { if debug_for_reconnected { println!("[重连成功] 发送 GetFileVer 命令..."); } - // 构造 GetFileVer 消息 - let msg = serde_json::json!({ - "Type": "GetFileVer", - "Data": { - "file": "BootLoader.exe" - } - }); - let msg_str = msg.to_string(); + // 构造 GetFileVer 消息 - Type在前,DeviceNumber从公共配置读取,file_list数组 + let msg_str = format!( + r#"{{"Type":"GetFileVer","DeviceNumber":"{}","Data":{{"file_list":["BootLoader.exe"]}}}}"#, + device_number + ); + + if debug_for_reconnected { + println!("[重连成功] GetFileVer 已发送: {}", msg_str); + } // 通过 sender 发送消息 let sender_guard = sender.lock().await; if let Some(ref tx) = *sender_guard { let _ = tx.try_send(cube_lib::websocket::OutgoingMessage::Text(msg_str)); } - if debug_for_reconnected { - println!("[重连成功] GetFileVer 已发送"); - } }) as Pin + Send + Sync>> });