拆分device.Service接口
This commit is contained in:
@@ -56,17 +56,9 @@ func (l *loraListener) HandleInstruction(upstreamCtx context.Context, sourceAddr
|
||||
case *proto.Instruction_CollectResult:
|
||||
return l.handleCollectResult(ctx, sourceAddr, p.CollectResult)
|
||||
|
||||
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: 在这里实现设备日志的处理逻辑
|
||||
return nil
|
||||
|
||||
default:
|
||||
logger.Warnw("收到一个当前未处理的上行指令类型", "来源地址", sourceAddr, "类型", fmt.Sprintf("%T", p))
|
||||
return nil
|
||||
@@ -277,37 +269,6 @@ func (l *loraListener) recordSensorData(ctx context.Context, areaControllerID ui
|
||||
}
|
||||
}
|
||||
|
||||
// handleOtaStatus 处理设备上报的OTA升级状态。
|
||||
func (l *loraListener) handleOtaStatus(ctx context.Context, sourceAddr string, status *proto.OtaUpgradeStatus) error {
|
||||
reqCtx, logger := logs.Trace(ctx, l.selfCtx, "handleOtaStatus")
|
||||
logger.Infow("开始处理OTA升级状态",
|
||||
"来源地址", sourceAddr,
|
||||
"状态码", status.StatusCode,
|
||||
"当前版本", status.CurrentFirmwareVersion,
|
||||
)
|
||||
|
||||
// 1. 查找区域主控
|
||||
areaController, err := l.areaControllerRepo.FindByNetworkID(reqCtx, sourceAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("处理OTA状态失败:无法找到区域主控: %w", err)
|
||||
}
|
||||
|
||||
// 2. 更新固件版本号
|
||||
// 我们信任设备上报的版本号,无论成功失败都进行更新
|
||||
if status.CurrentFirmwareVersion != "" {
|
||||
err = l.areaControllerRepo.UpdateFirmwareVersion(reqCtx, areaController.ID, status.CurrentFirmwareVersion)
|
||||
if err != nil {
|
||||
logger.Errorw("更新区域主控固件版本号失败", "主控ID", areaController.ID, "error", err)
|
||||
return fmt.Errorf("更新固件版本号失败: %w", err)
|
||||
}
|
||||
logger.Infow("成功更新区域主控固件版本号", "主控ID", areaController.ID, "新版本", status.CurrentFirmwareVersion)
|
||||
}
|
||||
|
||||
// TODO: 后续可以在这里增加逻辑,比如记录一条操作日志,或者发送一个通知
|
||||
|
||||
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")
|
||||
|
||||
@@ -53,7 +53,7 @@ type deviceService struct {
|
||||
deviceRepo repository.DeviceRepository
|
||||
areaControllerRepo repository.AreaControllerRepository
|
||||
deviceTemplateRepo repository.DeviceTemplateRepository
|
||||
deviceDomainSvc device.Service
|
||||
deviceDomainSvc device.DeviceOperator
|
||||
thresholdAlarmService ThresholdAlarmService
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func NewDeviceService(
|
||||
deviceRepo repository.DeviceRepository,
|
||||
areaControllerRepo repository.AreaControllerRepository,
|
||||
deviceTemplateRepo repository.DeviceTemplateRepository,
|
||||
deviceDomainSvc device.Service,
|
||||
deviceDomainSvc device.DeviceOperator,
|
||||
thresholdAlarmService ThresholdAlarmService,
|
||||
) DeviceService {
|
||||
return &deviceService{
|
||||
|
||||
Reference in New Issue
Block a user