发送消息时增加了DeviceNumber
This commit is contained in:
@@ -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::<serde_json::Value>(&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<Box<dyn std::future::Future<Output = ()> + 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<Box<dyn std::future::Future<Output = ()> + Send + Sync>>
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user