增加ping指令并获取带版本号的响应

This commit is contained in:
2025-12-01 20:42:21 +08:00
parent d5056af676
commit 2e6a0abac3
17 changed files with 557 additions and 44 deletions

View File

@@ -59,6 +59,9 @@ func (l *loraListener) HandleInstruction(upstreamCtx context.Context, sourceAddr
case *proto.Instruction_OtaUpgradeStatus:
return l.handleOtaStatus(ctx, sourceAddr, p.OtaUpgradeStatus)
case *proto.Instruction_Pong:
return l.handlePong(ctx, sourceAddr, p.Pong)
case *proto.Instruction_LogUploadRequest:
logger.Infow("收到设备日志上传请求,暂未实现处理逻辑", "来源地址", sourceAddr, "日志条数", len(p.LogUploadRequest.Entries))
// TODO: 在这里实现设备日志的处理逻辑
@@ -280,7 +283,6 @@ func (l *loraListener) handleOtaStatus(ctx context.Context, sourceAddr string, s
logger.Infow("开始处理OTA升级状态",
"来源地址", sourceAddr,
"状态码", status.StatusCode,
"处理结果", status.StatusCode == 0,
"当前版本", status.CurrentFirmwareVersion,
)
@@ -305,3 +307,33 @@ func (l *loraListener) handleOtaStatus(ctx context.Context, sourceAddr string, s
return nil
}
// handlePong 处理设备上报的Pong响应或主动心跳。
func (l *loraListener) handlePong(ctx context.Context, sourceAddr string, pong *proto.Pong) error {
reqCtx, logger := logs.Trace(ctx, l.selfCtx, "handlePong")
logger.Infow("开始处理Pong", "来源地址", sourceAddr, "携带版本", pong.FirmwareVersion)
// 1. 查找区域主控
areaController, err := l.areaControllerRepo.FindByNetworkID(reqCtx, sourceAddr)
if err != nil {
return fmt.Errorf("处理Pong失败无法找到区域主控: %w", err)
}
// 2. 如果 Pong 中包含版本号,则更新
if pong.FirmwareVersion != "" {
err := l.areaControllerRepo.UpdateFirmwareVersion(reqCtx, areaController.ID, pong.FirmwareVersion)
if err != nil {
// 只记录错误,不中断流程,因为还要记录在线状态
logger.Errorw("处理Pong时更新固件版本失败", "主控ID", areaController.ID, "error", err)
} else {
logger.Infow("处理Pong时成功更新固件版本", "主控ID", areaController.ID, "新版本", pong.FirmwareVersion)
}
}
// 3. 记录在线状态
onlineStatus := models.OnlineStatusData{State: models.StateOnline}
l.recordSensorData(reqCtx, areaController.ID, areaController.ID, time.Now(), models.SensorTypeOnlineStatus, onlineStatus)
logger.Infow("已记录区域主控为在线状态", "主控ID", areaController.ID)
return nil
}