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/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-10-10 15:58:40 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/proto"
|
2025-09-30 00:07:16 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/command_generater"
|
|
|
|
|
|
|
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"
|
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 23:46:33 +08:00
|
|
|
|
// 3. 使用模型层预定义的 Bus485Properties 结构体解析设备属性
|
|
|
|
|
|
var deviceProps models.Bus485Properties
|
2025-09-29 18:13:19 +08:00
|
|
|
|
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-30 00:07:16 +08:00
|
|
|
|
// 4. 解析设备模板中的开关指令参数
|
|
|
|
|
|
var switchCmd models.SwitchCommands
|
|
|
|
|
|
if err := device.DeviceTemplate.ParseCommands(&switchCmd); err != nil {
|
|
|
|
|
|
return fmt.Errorf("解析设备 %v(id=%v) 的开关指令失败: %w", device.Name, device.ID, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 根据 action 生成 Modbus RTU 写入指令
|
|
|
|
|
|
onOffState := true // 默认为开启
|
|
|
|
|
|
if action == DeviceActionStop { // 如果是停止动作,则设置为关闭
|
|
|
|
|
|
onOffState = false
|
2025-09-29 18:13:19 +08:00
|
|
|
|
}
|
2025-09-16 17:12:27 +08:00
|
|
|
|
|
2025-09-30 00:07:16 +08:00
|
|
|
|
modbusCommandBytes, err := command_generater.GenerateModbusRTUSwitchCommand(
|
|
|
|
|
|
deviceProps.BusAddress,
|
|
|
|
|
|
switchCmd.ModbusStartAddress,
|
|
|
|
|
|
onOffState,
|
|
|
|
|
|
)
|
2025-09-16 17:12:27 +08:00
|
|
|
|
if err != nil {
|
2025-09-30 00:07:16 +08:00
|
|
|
|
return fmt.Errorf("生成Modbus RTU写入指令失败: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 6. 构建 Protobuf Raw485Command,包含总线号
|
|
|
|
|
|
raw485Cmd := &proto.Raw485Command{
|
|
|
|
|
|
BusNumber: int32(deviceProps.BusNumber), // 添加总线号
|
|
|
|
|
|
CommandBytes: modbusCommandBytes,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 17:12:27 +08:00
|
|
|
|
instruction := &proto.Instruction{
|
2025-10-07 16:14:47 +08:00
|
|
|
|
Payload: &proto.Instruction_Raw_485Command{
|
|
|
|
|
|
Raw_485Command: raw485Cmd,
|
|
|
|
|
|
},
|
2025-09-16 17:12:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-30 00:07:16 +08:00
|
|
|
|
// 7. 发送指令
|
2025-09-29 18:13:19 +08:00
|
|
|
|
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-30 00:07:16 +08:00
|
|
|
|
// 8. 创建并保存命令日志
|
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-30 00:07:16 +08:00
|
|
|
|
// 使用模板的 ParseCommands 方法获取传感器指令参数
|
2025-09-29 18:13:19 +08:00
|
|
|
|
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 23:46:33 +08:00
|
|
|
|
// 使用模型层预定义的 Bus485Properties 结构体解析设备属性
|
|
|
|
|
|
var deviceProps models.Bus485Properties
|
2025-09-29 18:13:19 +08:00
|
|
|
|
if err := dev.ParseProperties(&deviceProps); err != nil {
|
|
|
|
|
|
g.logger.Warnf("跳过设备 %d,因其属性解析失败: %v", dev.ID, err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2025-09-26 22:50:08 +08:00
|
|
|
|
|
2025-09-30 00:07:16 +08:00
|
|
|
|
// 生成 Modbus RTU 读取指令
|
|
|
|
|
|
modbusCommandBytes, err := command_generater.GenerateModbusRTUReadCommand(
|
|
|
|
|
|
deviceProps.BusAddress,
|
|
|
|
|
|
sensorCmd.ModbusFunctionCode,
|
|
|
|
|
|
sensorCmd.ModbusStartAddress,
|
|
|
|
|
|
sensorCmd.ModbusQuantity,
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
g.logger.Warnf("跳过设备 %d,因生成Modbus RTU读取指令失败: %v", dev.ID, err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 构建 Raw485Command,包含总线号
|
|
|
|
|
|
raw485Cmd := &proto.Raw485Command{
|
|
|
|
|
|
BusNumber: int32(deviceProps.BusNumber), // 添加总线号
|
|
|
|
|
|
CommandBytes: modbusCommandBytes,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-26 22:50:08 +08:00
|
|
|
|
collectTasks = append(collectTasks, &proto.CollectTask{
|
2025-09-30 00:07:16 +08:00
|
|
|
|
Command: raw485Cmd,
|
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,
|
|
|
|
|
|
}
|
|
|
|
|
|
instruction := &proto.Instruction{
|
2025-10-07 16:14:47 +08:00
|
|
|
|
Payload: &proto.Instruction_BatchCollectCommand{
|
|
|
|
|
|
BatchCollectCommand: batchCmd,
|
|
|
|
|
|
},
|
2025-09-26 22:50:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|