56 lines
1.8 KiB
Rust
56 lines
1.8 KiB
Rust
//! 设备消息处理逻辑(轻量化)。
|
||
//!
|
||
//! 设备服务只处理通信和指令解析,不处理业务逻辑:
|
||
//! - 所有设备上报消息统一转发到 Redis `device:reports` 队列
|
||
//! - 不写 DB、不验证签名、不处理业务规则
|
||
//! - auth_str/login 等协议级消息也由 API 服务处理后通过 `device:commands` 回复
|
||
|
||
use super::connection::ConnectionPool;
|
||
use super::protocol::DeviceMessage;
|
||
|
||
/// 分发消息:统一转发到 Redis 上报队列。
|
||
///
|
||
/// 设备服务不做任何业务处理,仅负责解析 JSON 并转发。
|
||
pub async fn handle_message(
|
||
msg: DeviceMessage,
|
||
_pool: &ConnectionPool,
|
||
redis: &redis::aio::ConnectionManager,
|
||
) -> Option<super::protocol::ServerResponse> {
|
||
// 所有消息统一转发到 Redis,由 API 服务的 report_worker 处理
|
||
forward_to_redis(redis, &msg).await;
|
||
|
||
// 不直接回复设备,等待 API 服务通过 device:commands 队列回复
|
||
None
|
||
}
|
||
|
||
/// 将设备消息转发到 Redis 统一上报队列 `device:reports`
|
||
async fn forward_to_redis(
|
||
redis: &redis::aio::ConnectionManager,
|
||
msg: &DeviceMessage,
|
||
) {
|
||
if msg.dev_id.is_none() {
|
||
tracing::warn!("[forward] 消息缺少 dev_id, act={}", msg.act);
|
||
return;
|
||
}
|
||
|
||
let json_str = serde_json::to_string(msg).unwrap_or_default();
|
||
let mut conn = redis.clone();
|
||
|
||
let result: Result<(), _> = redis::cmd("LPUSH")
|
||
.arg("device:reports")
|
||
.arg(&json_str)
|
||
.query_async(&mut conn)
|
||
.await;
|
||
|
||
match result {
|
||
Ok(_) => {
|
||
tracing::debug!(
|
||
"[forward] 已推送 dev_id={} act={}",
|
||
msg.dev_id.as_deref().unwrap_or("?"),
|
||
msg.act
|
||
);
|
||
}
|
||
Err(e) => tracing::error!("[forward] Redis LPUSH 失败: {}", e),
|
||
}
|
||
}
|