优化代码
This commit is contained in:
@@ -8,13 +8,13 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/proto"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/file"
|
||||
gproto "google.golang.org/protobuf/proto"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
@@ -29,7 +29,7 @@ type loraListener struct {
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
||||
otaRepo repository.OtaRepository
|
||||
comm transport.Communicator
|
||||
deviceCommunicator device.DeviceCommunicator
|
||||
}
|
||||
|
||||
// NewLoRaListener 创建一个新的 loraListener 实例。
|
||||
@@ -42,7 +42,7 @@ func NewLoRaListener(
|
||||
sensorDataRepo repository.SensorDataRepository,
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
||||
otaRepo repository.OtaRepository,
|
||||
comm transport.Communicator,
|
||||
deviceCommunicator device.DeviceCommunicator,
|
||||
) transport.UpstreamHandler {
|
||||
return &loraListener{
|
||||
selfCtx: logs.AddCompName(ctx, "LoRaListener"),
|
||||
@@ -52,7 +52,7 @@ func NewLoRaListener(
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||
otaRepo: otaRepo,
|
||||
comm: comm,
|
||||
deviceCommunicator: deviceCommunicator,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -413,14 +413,7 @@ func (l *loraListener) handleRequestFile(ctx context.Context, sourceAddr string,
|
||||
return fmt.Errorf("处理文件请求失败:未找到任务 %d: %w", req.GetTaskId(), err)
|
||||
}
|
||||
|
||||
// 2. 根据 AreaControllerID 查找 AreaController,获取 NetworkID
|
||||
areaController, err := l.areaControllerRepo.FindByID(reqCtx, task.AreaControllerID)
|
||||
if err != nil {
|
||||
logger.Errorw("处理文件请求失败:未找到对应的区域主控", "error", err)
|
||||
return fmt.Errorf("处理文件请求失败:未找到区域主控 %d: %w", task.AreaControllerID, err)
|
||||
}
|
||||
|
||||
// 3. 构造文件路径并读取文件内容
|
||||
// 2. 构造文件路径并读取文件内容
|
||||
subDir := filepath.Join(models.OTADir, fmt.Sprintf("%d", req.GetTaskId()))
|
||||
content, err := file.ReadTempFile(subDir, req.GetFilepath())
|
||||
if err != nil {
|
||||
@@ -428,32 +421,25 @@ func (l *loraListener) handleRequestFile(ctx context.Context, sourceAddr string,
|
||||
return fmt.Errorf("处理文件请求失败:读取文件 %s 失败: %w", req.GetFilepath(), err)
|
||||
}
|
||||
|
||||
// 4. 构造 FileResponse
|
||||
// 3. 构造 FileResponse
|
||||
fileResp := &proto.FileResponse{
|
||||
TaskId: req.GetTaskId(),
|
||||
Filepath: req.GetFilepath(),
|
||||
Content: content,
|
||||
}
|
||||
|
||||
// 5. 构造并序列化 Instruction
|
||||
instruction := &proto.Instruction{
|
||||
Payload: &proto.Instruction_FileResponse{
|
||||
// 4. 将 FileResponse 包装成 InstructionPayload
|
||||
payload := &proto.Instruction_FileResponse{
|
||||
FileResponse: fileResp,
|
||||
},
|
||||
}
|
||||
message, err := gproto.Marshal(instruction)
|
||||
if err != nil {
|
||||
logger.Errorw("处理文件请求失败:序列化指令失败", "error", err)
|
||||
return fmt.Errorf("处理文件请求失败:序列化指令失败: %w", err)
|
||||
}
|
||||
|
||||
// 6. 发送指令
|
||||
_, err = l.comm.Send(reqCtx, areaController.NetworkID, message)
|
||||
// 5. 使用领域层的 DeviceCommunicator 发送指令,它会处理所有底层细节
|
||||
err = l.deviceCommunicator.Send(reqCtx, task.AreaControllerID, payload, device.WithoutTracking())
|
||||
if err != nil {
|
||||
logger.Errorw("处理文件请求失败:发送指令失败", "error", err)
|
||||
return fmt.Errorf("处理文件请求失败:发送指令到 %s 失败: %w", areaController.NetworkID, err)
|
||||
return fmt.Errorf("处理文件请求失败:发送指令到区域主控 %d 失败: %w", task.AreaControllerID, err)
|
||||
}
|
||||
|
||||
logger.Infow("成功处理文件请求")
|
||||
logger.Infow("成功处理文件请求并发送文件响应")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ func NewApplication(configPath string) (*Application, error) {
|
||||
infra.repos.sensorDataRepo,
|
||||
infra.repos.deviceCommandLogRepo,
|
||||
infra.repos.otaRepo,
|
||||
infra.lora.comm,
|
||||
domain.deviceCommunicator,
|
||||
)
|
||||
|
||||
// 根据 LoRa 模式完成最终的绑定
|
||||
|
||||
Reference in New Issue
Block a user