2025-09-15 21:28:22 +08:00
|
|
|
|
package device
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-09-26 22:50:08 +08:00
|
|
|
|
"errors"
|
2025-09-16 17:12:27 +08:00
|
|
|
|
"fmt"
|
2025-09-26 22:50:08 +08:00
|
|
|
|
"time"
|
2025-09-16 17:12:27 +08:00
|
|
|
|
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/device/proto"
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
2025-09-15 21:28:22 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
2025-09-16 17:12:27 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
2025-09-15 22:25:05 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport"
|
2025-09-26 22:50:08 +08:00
|
|
|
|
"github.com/google/uuid"
|
2025-09-16 17:12:27 +08:00
|
|
|
|
gproto "google.golang.org/protobuf/proto"
|
|
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2025-09-15 21:28:22 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type GeneralDeviceService struct {
|
2025-09-26 22:50:08 +08:00
|
|
|
|
deviceRepo repository.DeviceRepository
|
|
|
|
|
|
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
|
|
|
|
|
pendingCollectionRepo repository.PendingCollectionRepository
|
|
|
|
|
|
logger *logs.Logger
|
|
|
|
|
|
comm transport.Communicator
|
2025-09-15 21:28:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 17:12:27 +08:00
|
|
|
|
// NewGeneralDeviceService 创建一个通用设备服务
|
2025-09-26 22:50:08 +08:00
|
|
|
|
func NewGeneralDeviceService(
|
|
|
|
|
|
deviceRepo repository.DeviceRepository,
|
|
|
|
|
|
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
|
|
|
|
|
pendingCollectionRepo repository.PendingCollectionRepository,
|
|
|
|
|
|
logger *logs.Logger,
|
|
|
|
|
|
comm transport.Communicator,
|
|
|
|
|
|
) Service {
|
2025-09-16 17:12:27 +08:00
|
|
|
|
return &GeneralDeviceService{
|
2025-09-26 22:50:08 +08:00
|
|
|
|
deviceRepo: deviceRepo,
|
|
|
|
|
|
deviceCommandLogRepo: deviceCommandLogRepo,
|
|
|
|
|
|
pendingCollectionRepo: pendingCollectionRepo,
|
|
|
|
|
|
logger: logger,
|
|
|
|
|
|
comm: comm,
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
2025-09-15 21:28:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-25 11:17:13 +08:00
|
|
|
|
func (g *GeneralDeviceService) Switch(device *models.Device, action DeviceAction) error {
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 1. 依赖模型自身的 SelfCheck 进行全面校验
|
|
|
|
|
|
if err := device.SelfCheck(); err != nil {
|
|
|
|
|
|
return fmt.Errorf("设备 %v(id=%v) 未通过自检: %w", device.Name, device.ID, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := device.DeviceTemplate.SelfCheck(); err != nil {
|
|
|
|
|
|
return fmt.Errorf("设备 %v(id=%v) 的模板未通过自检: %w", device.Name, device.ID, err)
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 2. 检查预加载的 AreaController 是否有效
|
|
|
|
|
|
areaController := &device.AreaController
|
|
|
|
|
|
if err := areaController.SelfCheck(); err != nil {
|
|
|
|
|
|
return fmt.Errorf("区域主控 %v(id=%v) 未通过自检: %w", areaController.Name, areaController.ID, err)
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 3. 使用模型层预定义的 BusProperties 结构体解析设备属性
|
|
|
|
|
|
var deviceProps models.BusProperties
|
|
|
|
|
|
if err := device.ParseProperties(&deviceProps); err != nil {
|
|
|
|
|
|
return fmt.Errorf("解析设备 %v(id=%v) 的属性失败: %w", device.Name, device.ID, err)
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
var command models.SwitchCommands
|
|
|
|
|
|
// 前面的 device.DeviceTemplate.SelfCheck()保障了解析一定成功
|
|
|
|
|
|
_ = device.DeviceTemplate.ParseCommands(&command)
|
|
|
|
|
|
deviceAction := command.On
|
|
|
|
|
|
if action == DeviceActionStop {
|
|
|
|
|
|
deviceAction = command.Off
|
|
|
|
|
|
}
|
2025-09-16 17:12:27 +08:00
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 4. 构建 Protobuf 指令
|
2025-09-16 17:12:27 +08:00
|
|
|
|
data, err := anypb.New(&proto.Switch{
|
2025-09-29 18:13:19 +08:00
|
|
|
|
DeviceAction: deviceAction,
|
|
|
|
|
|
BusNumber: int32(deviceProps.BusNumber),
|
|
|
|
|
|
BusAddress: int32(deviceProps.BusAddress),
|
|
|
|
|
|
RelayChannel: int32(deviceProps.RelayChannel),
|
2025-09-16 17:12:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
2025-09-29 18:13:19 +08:00
|
|
|
|
return fmt.Errorf("创建指令失败: %w", err)
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
instruction := &proto.Instruction{
|
|
|
|
|
|
Method: proto.MethodType_SWITCH,
|
|
|
|
|
|
Data: data,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
message, err := gproto.Marshal(instruction)
|
|
|
|
|
|
if err != nil {
|
2025-09-29 18:13:19 +08:00
|
|
|
|
return fmt.Errorf("序列化指令失败: %w", err)
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 5. 发送指令
|
|
|
|
|
|
networkID := areaController.NetworkID
|
|
|
|
|
|
sendResult, err := g.comm.Send(networkID, message)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
if err != nil {
|
2025-09-29 18:13:19 +08:00
|
|
|
|
return fmt.Errorf("发送指令到 %s 失败: %w", networkID, err)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 6. 创建并保存命令日志
|
2025-09-26 22:50:08 +08:00
|
|
|
|
logRecord := &models.DeviceCommandLog{
|
|
|
|
|
|
MessageID: sendResult.MessageID,
|
2025-09-29 18:13:19 +08:00
|
|
|
|
DeviceID: areaController.ID,
|
2025-09-26 22:50:08 +08:00
|
|
|
|
SentAt: time.Now(),
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := g.deviceCommandLogRepo.Create(logRecord); err != nil {
|
|
|
|
|
|
// 记录日志失败是一个需要关注的问题,但可能不应该中断主流程。
|
|
|
|
|
|
// 我们记录一个错误日志,然后成功返回。
|
|
|
|
|
|
g.logger.Errorf("创建指令日志失败 (MessageID: %s): %v", sendResult.MessageID, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
g.logger.Infof("成功发送指令到 %s 并创建日志 (MessageID: %s)", networkID, sendResult.MessageID)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Collect 实现了 Service 接口,用于发起对指定区域主控下的多个设备的批量采集请求。
|
|
|
|
|
|
func (g *GeneralDeviceService) Collect(regionalControllerID uint, devicesToCollect []*models.Device) error {
|
|
|
|
|
|
if len(devicesToCollect) == 0 {
|
|
|
|
|
|
g.logger.Info("待采集设备列表为空,无需执行采集任务。")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 1. 从设备列表中获取预加载的区域主控,并进行校验
|
|
|
|
|
|
regionalController := &devicesToCollect[0].AreaController
|
|
|
|
|
|
if regionalController.ID != regionalControllerID {
|
|
|
|
|
|
return fmt.Errorf("设备列表与指定的区域主控ID (%d) 不匹配", regionalControllerID)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
}
|
2025-09-29 18:13:19 +08:00
|
|
|
|
if err := regionalController.SelfCheck(); err != nil {
|
|
|
|
|
|
return fmt.Errorf("区域主控 (ID: %d) 未通过自检: %w", regionalControllerID, err)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 2. 准备采集任务列表
|
2025-09-26 22:50:08 +08:00
|
|
|
|
var childDeviceIDs []uint
|
|
|
|
|
|
var collectTasks []*proto.CollectTask
|
|
|
|
|
|
|
|
|
|
|
|
for _, dev := range devicesToCollect {
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 依赖模型自身的 SelfCheck 进行全面校验
|
|
|
|
|
|
if err := dev.SelfCheck(); err != nil {
|
|
|
|
|
|
g.logger.Warnf("跳过设备 %d,因其未通过自检: %v", dev.ID, err)
|
|
|
|
|
|
continue
|
2025-09-26 22:50:08 +08:00
|
|
|
|
}
|
2025-09-29 18:13:19 +08:00
|
|
|
|
if err := dev.DeviceTemplate.SelfCheck(); err != nil {
|
|
|
|
|
|
g.logger.Warnf("跳过设备 %d,因其设备模板未通过自检: %v", dev.ID, err)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 使用模板的 ParseCommands 方法获取指令
|
|
|
|
|
|
var sensorCmd models.SensorCommands
|
|
|
|
|
|
if err := dev.DeviceTemplate.ParseCommands(&sensorCmd); err != nil {
|
|
|
|
|
|
g.logger.Warnf("跳过设备 %d,因其模板指令无法解析为 SensorCommands: %v", dev.ID, err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2025-09-26 22:50:08 +08:00
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 使用模型层预定义的 BusProperties 结构体解析设备属性
|
|
|
|
|
|
var deviceProps models.BusProperties
|
|
|
|
|
|
if err := dev.ParseProperties(&deviceProps); err != nil {
|
|
|
|
|
|
g.logger.Warnf("跳过设备 %d,因其属性解析失败: %v", dev.ID, err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2025-09-26 22:50:08 +08:00
|
|
|
|
|
|
|
|
|
|
collectTasks = append(collectTasks, &proto.CollectTask{
|
2025-09-29 18:13:19 +08:00
|
|
|
|
DeviceAction: sensorCmd.Read, // 使用从模板中获取的指令
|
|
|
|
|
|
BusNumber: int32(deviceProps.BusNumber),
|
|
|
|
|
|
BusAddress: int32(deviceProps.BusAddress),
|
2025-09-26 22:50:08 +08:00
|
|
|
|
})
|
|
|
|
|
|
childDeviceIDs = append(childDeviceIDs, dev.ID)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(childDeviceIDs) == 0 {
|
|
|
|
|
|
return errors.New("经过滤后,没有可通过自检的有效设备")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
// 3. 构建并发送指令
|
|
|
|
|
|
networkID := regionalController.NetworkID
|
2025-09-26 22:50:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 4. 创建待处理请求记录
|
|
|
|
|
|
correlationID := uuid.New().String()
|
|
|
|
|
|
pendingReq := &models.PendingCollection{
|
|
|
|
|
|
CorrelationID: correlationID,
|
|
|
|
|
|
DeviceID: regionalController.ID,
|
|
|
|
|
|
CommandMetadata: childDeviceIDs,
|
|
|
|
|
|
Status: models.PendingStatusPending,
|
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := g.pendingCollectionRepo.Create(pendingReq); err != nil {
|
|
|
|
|
|
g.logger.Errorf("创建待采集请求失败 (CorrelationID: %s): %v", correlationID, err)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
g.logger.Infof("成功创建待采集请求 (CorrelationID: %s, DeviceID: %d)", correlationID, regionalController.ID)
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 构建最终的空中载荷
|
|
|
|
|
|
batchCmd := &proto.BatchCollectCommand{
|
|
|
|
|
|
CorrelationId: correlationID,
|
|
|
|
|
|
Tasks: collectTasks,
|
|
|
|
|
|
}
|
|
|
|
|
|
anyData, err := anypb.New(batchCmd)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
g.logger.Errorf("创建 Any Protobuf 失败 (CorrelationID: %s): %v", correlationID, err)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
instruction := &proto.Instruction{
|
|
|
|
|
|
Method: proto.MethodType_COLLECT,
|
|
|
|
|
|
Data: anyData,
|
|
|
|
|
|
}
|
|
|
|
|
|
payload, err := gproto.Marshal(instruction)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
g.logger.Errorf("序列化采集指令失败 (CorrelationID: %s): %v", correlationID, err)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
if _, err := g.comm.Send(networkID, payload); err != nil {
|
2025-09-26 22:50:08 +08:00
|
|
|
|
g.logger.DPanicf("待采集请求 (CorrelationID: %s) 已创建,但发送到设备失败: %v。数据可能不一致!", correlationID, err)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 18:13:19 +08:00
|
|
|
|
g.logger.Infof("成功将采集请求 (CorrelationID: %s) 发送到设备 %s", correlationID, networkID)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
return nil
|
2025-09-15 21:28:22 +08:00
|
|
|
|
}
|