调整目录结构
This commit is contained in:
42
internal/domain/device/device_service.go
Normal file
42
internal/domain/device/device_service.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package device
|
||||
|
||||
import "git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
|
||||
// 设备行为
|
||||
type DeviceAction string
|
||||
|
||||
var (
|
||||
DeviceActionStart DeviceAction = "start" // 启动
|
||||
DeviceActionStop DeviceAction = "stop" // 停止
|
||||
)
|
||||
|
||||
// 指令类型
|
||||
type Method string
|
||||
|
||||
var (
|
||||
MethodSwitch Method = "switch" // 启停指令
|
||||
)
|
||||
|
||||
// Service 抽象了一组方法用于控制设备行为
|
||||
type Service interface {
|
||||
|
||||
// Switch 用于切换指定设备的状态, 比如启动和停止
|
||||
Switch(device *models.Device, action DeviceAction) error
|
||||
|
||||
// Collect 用于发起对指定区域主控下的多个设备的批量采集请求。
|
||||
Collect(regionalControllerID uint, devicesToCollect []*models.Device) error
|
||||
}
|
||||
|
||||
// 设备操作指令通用结构(最外层)
|
||||
type DeviceRequest struct {
|
||||
MessageID int // 消息ID, 用于后续匹配响应
|
||||
Method string // 这是什么指令
|
||||
Data interface{} // 指令参数
|
||||
}
|
||||
|
||||
// 设备操作指令通用响应结构(最外层)
|
||||
type DeviceResponse struct {
|
||||
MessageID int // 消息ID, 用于匹配这是哪一个请求的响应
|
||||
Message string
|
||||
Data interface{} // 响应内容
|
||||
}
|
||||
249
internal/domain/device/general_device_service.go
Normal file
249
internal/domain/device/general_device_service.go
Normal file
@@ -0,0 +1,249 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device/proto"
|
||||
"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/utils/command_generater"
|
||||
|
||||
"github.com/google/uuid"
|
||||
gproto "google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
type GeneralDeviceService struct {
|
||||
deviceRepo repository.DeviceRepository
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
||||
pendingCollectionRepo repository.PendingCollectionRepository
|
||||
logger *logs.Logger
|
||||
comm transport.Communicator
|
||||
}
|
||||
|
||||
// NewGeneralDeviceService 创建一个通用设备服务
|
||||
func NewGeneralDeviceService(
|
||||
deviceRepo repository.DeviceRepository,
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
||||
pendingCollectionRepo repository.PendingCollectionRepository,
|
||||
logger *logs.Logger,
|
||||
comm transport.Communicator,
|
||||
) Service {
|
||||
return &GeneralDeviceService{
|
||||
deviceRepo: deviceRepo,
|
||||
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||
pendingCollectionRepo: pendingCollectionRepo,
|
||||
logger: logger,
|
||||
comm: comm,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GeneralDeviceService) Switch(device *models.Device, action DeviceAction) error {
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 2. 检查预加载的 AreaController 是否有效
|
||||
areaController := &device.AreaController
|
||||
if err := areaController.SelfCheck(); err != nil {
|
||||
return fmt.Errorf("区域主控 %v(id=%v) 未通过自检: %w", areaController.Name, areaController.ID, err)
|
||||
}
|
||||
|
||||
// 3. 使用模型层预定义的 Bus485Properties 结构体解析设备属性
|
||||
var deviceProps models.Bus485Properties
|
||||
if err := device.ParseProperties(&deviceProps); err != nil {
|
||||
return fmt.Errorf("解析设备 %v(id=%v) 的属性失败: %w", device.Name, device.ID, err)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
modbusCommandBytes, err := command_generater.GenerateModbusRTUSwitchCommand(
|
||||
deviceProps.BusAddress,
|
||||
switchCmd.ModbusStartAddress,
|
||||
onOffState,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("生成Modbus RTU写入指令失败: %w", err)
|
||||
}
|
||||
|
||||
// 6. 构建 Protobuf Raw485Command,包含总线号
|
||||
raw485Cmd := &proto.Raw485Command{
|
||||
BusNumber: int32(deviceProps.BusNumber), // 添加总线号
|
||||
CommandBytes: modbusCommandBytes,
|
||||
}
|
||||
|
||||
data, err := anypb.New(raw485Cmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建 Raw485Command Any Protobuf 失败: %w", err)
|
||||
}
|
||||
|
||||
instruction := &proto.Instruction{
|
||||
Method: proto.MethodType_INSTRUCTION, // 使用通用指令类型
|
||||
Data: data,
|
||||
}
|
||||
|
||||
message, err := gproto.Marshal(instruction)
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化指令失败: %w", err)
|
||||
}
|
||||
|
||||
// 7. 发送指令
|
||||
networkID := areaController.NetworkID
|
||||
sendResult, err := g.comm.Send(networkID, message)
|
||||
if err != nil {
|
||||
return fmt.Errorf("发送指令到 %s 失败: %w", networkID, err)
|
||||
}
|
||||
|
||||
// 8. 创建并保存命令日志
|
||||
logRecord := &models.DeviceCommandLog{
|
||||
MessageID: sendResult.MessageID,
|
||||
DeviceID: areaController.ID,
|
||||
SentAt: time.Now(),
|
||||
}
|
||||
if err := g.deviceCommandLogRepo.Create(logRecord); err != nil {
|
||||
// 记录日志失败是一个需要关注的问题,但可能不应该中断主流程。
|
||||
// 我们记录一个错误日志,然后成功返回。
|
||||
g.logger.Errorf("创建指令日志失败 (MessageID: %s): %v", sendResult.MessageID, err)
|
||||
}
|
||||
|
||||
g.logger.Infof("成功发送指令到 %s 并创建日志 (MessageID: %s)", networkID, sendResult.MessageID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Collect 实现了 Service 接口,用于发起对指定区域主控下的多个设备的批量采集请求。
|
||||
func (g *GeneralDeviceService) Collect(regionalControllerID uint, devicesToCollect []*models.Device) error {
|
||||
if len(devicesToCollect) == 0 {
|
||||
g.logger.Info("待采集设备列表为空,无需执行采集任务。")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 1. 从设备列表中获取预加载的区域主控,并进行校验
|
||||
regionalController := &devicesToCollect[0].AreaController
|
||||
if regionalController.ID != regionalControllerID {
|
||||
return fmt.Errorf("设备列表与指定的区域主控ID (%d) 不匹配", regionalControllerID)
|
||||
}
|
||||
if err := regionalController.SelfCheck(); err != nil {
|
||||
return fmt.Errorf("区域主控 (ID: %d) 未通过自检: %w", regionalControllerID, err)
|
||||
}
|
||||
|
||||
// 2. 准备采集任务列表
|
||||
var childDeviceIDs []uint
|
||||
var collectTasks []*proto.CollectTask
|
||||
|
||||
for _, dev := range devicesToCollect {
|
||||
// 依赖模型自身的 SelfCheck 进行全面校验
|
||||
if err := dev.SelfCheck(); err != nil {
|
||||
g.logger.Warnf("跳过设备 %d,因其未通过自检: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
if err := dev.DeviceTemplate.SelfCheck(); err != nil {
|
||||
g.logger.Warnf("跳过设备 %d,因其设备模板未通过自检: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 使用模板的 ParseCommands 方法获取传感器指令参数
|
||||
var sensorCmd models.SensorCommands
|
||||
if err := dev.DeviceTemplate.ParseCommands(&sensorCmd); err != nil {
|
||||
g.logger.Warnf("跳过设备 %d,因其模板指令无法解析为 SensorCommands: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 使用模型层预定义的 Bus485Properties 结构体解析设备属性
|
||||
var deviceProps models.Bus485Properties
|
||||
if err := dev.ParseProperties(&deviceProps); err != nil {
|
||||
g.logger.Warnf("跳过设备 %d,因其属性解析失败: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 生成 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,
|
||||
}
|
||||
|
||||
collectTasks = append(collectTasks, &proto.CollectTask{
|
||||
Command: raw485Cmd,
|
||||
})
|
||||
childDeviceIDs = append(childDeviceIDs, dev.ID)
|
||||
}
|
||||
|
||||
if len(childDeviceIDs) == 0 {
|
||||
return errors.New("经过滤后,没有可通过自检的有效设备")
|
||||
}
|
||||
|
||||
// 3. 构建并发送指令
|
||||
networkID := regionalController.NetworkID
|
||||
|
||||
// 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, // 使用 COLLECT 指令类型
|
||||
Data: anyData,
|
||||
}
|
||||
payload, err := gproto.Marshal(instruction)
|
||||
if err != nil {
|
||||
g.logger.Errorf("序列化采集指令失败 (CorrelationID: %s): %v", correlationID, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := g.comm.Send(networkID, payload); err != nil {
|
||||
g.logger.DPanicf("待采集请求 (CorrelationID: %s) 已创建,但发送到设备失败: %v。数据可能不一致!", correlationID, err)
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Infof("成功将采集请求 (CorrelationID: %s) 发送到设备 %s", correlationID, networkID)
|
||||
return nil
|
||||
}
|
||||
417
internal/domain/device/proto/device.pb.go
Normal file
417
internal/domain/device/proto/device.pb.go
Normal file
@@ -0,0 +1,417 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.9
|
||||
// protoc v6.32.1
|
||||
// source: device.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
anypb "google.golang.org/protobuf/types/known/anypb"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// 指令类型
|
||||
type MethodType int32
|
||||
|
||||
const (
|
||||
MethodType_INSTRUCTION MethodType = 0 // 下发指令
|
||||
MethodType_COLLECT MethodType = 1 // 批量采集
|
||||
)
|
||||
|
||||
// Enum value maps for MethodType.
|
||||
var (
|
||||
MethodType_name = map[int32]string{
|
||||
0: "INSTRUCTION",
|
||||
1: "COLLECT",
|
||||
}
|
||||
MethodType_value = map[string]int32{
|
||||
"INSTRUCTION": 0,
|
||||
"COLLECT": 1,
|
||||
}
|
||||
)
|
||||
|
||||
func (x MethodType) Enum() *MethodType {
|
||||
p := new(MethodType)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x MethodType) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
||||
func (MethodType) Descriptor() protoreflect.EnumDescriptor {
|
||||
return file_device_proto_enumTypes[0].Descriptor()
|
||||
}
|
||||
|
||||
func (MethodType) Type() protoreflect.EnumType {
|
||||
return &file_device_proto_enumTypes[0]
|
||||
}
|
||||
|
||||
func (x MethodType) Number() protoreflect.EnumNumber {
|
||||
return protoreflect.EnumNumber(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use MethodType.Descriptor instead.
|
||||
func (MethodType) EnumDescriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
// 平台生成的原始485指令,单片机直接发送到总线
|
||||
type Raw485Command struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
BusNumber int32 `protobuf:"varint,1,opt,name=bus_number,json=busNumber,proto3" json:"bus_number,omitempty"` // 总线号,用于指示单片机将指令发送到哪个总线
|
||||
CommandBytes []byte `protobuf:"bytes,2,opt,name=command_bytes,json=commandBytes,proto3" json:"command_bytes,omitempty"` // 原始485指令的字节数组
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Raw485Command) Reset() {
|
||||
*x = Raw485Command{}
|
||||
mi := &file_device_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *Raw485Command) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Raw485Command) ProtoMessage() {}
|
||||
|
||||
func (x *Raw485Command) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_device_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Raw485Command.ProtoReflect.Descriptor instead.
|
||||
func (*Raw485Command) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *Raw485Command) GetBusNumber() int32 {
|
||||
if x != nil {
|
||||
return x.BusNumber
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Raw485Command) GetCommandBytes() []byte {
|
||||
if x != nil {
|
||||
return x.CommandBytes
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 指令 (所有空中数据都会被包装在这里面)
|
||||
// data字段现在可以包含 Raw485Command,表示平台生成的原始485指令。
|
||||
type Instruction struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Method MethodType `protobuf:"varint,1,opt,name=method,proto3,enum=device.MethodType" json:"method,omitempty"`
|
||||
Data *anypb.Any `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` // 可以是 Switch, Raw485Command 等
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Instruction) Reset() {
|
||||
*x = Instruction{}
|
||||
mi := &file_device_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *Instruction) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Instruction) ProtoMessage() {}
|
||||
|
||||
func (x *Instruction) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_device_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Instruction.ProtoReflect.Descriptor instead.
|
||||
func (*Instruction) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *Instruction) GetMethod() MethodType {
|
||||
if x != nil {
|
||||
return x.Method
|
||||
}
|
||||
return MethodType_INSTRUCTION
|
||||
}
|
||||
|
||||
func (x *Instruction) GetData() *anypb.Any {
|
||||
if x != nil {
|
||||
return x.Data
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchCollectCommand
|
||||
// 用于在平台内部构建一个完整的、包含所有元数据的批量采集任务。
|
||||
// 这个消息本身不会被发送到设备。
|
||||
type BatchCollectCommand struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
CorrelationId string `protobuf:"bytes,1,opt,name=correlation_id,json=correlationId,proto3" json:"correlation_id,omitempty"` // 用于关联请求和响应的唯一ID
|
||||
Tasks []*CollectTask `protobuf:"bytes,2,rep,name=tasks,proto3" json:"tasks,omitempty"` // 采集任务列表
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *BatchCollectCommand) Reset() {
|
||||
*x = BatchCollectCommand{}
|
||||
mi := &file_device_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *BatchCollectCommand) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*BatchCollectCommand) ProtoMessage() {}
|
||||
|
||||
func (x *BatchCollectCommand) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_device_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use BatchCollectCommand.ProtoReflect.Descriptor instead.
|
||||
func (*BatchCollectCommand) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *BatchCollectCommand) GetCorrelationId() string {
|
||||
if x != nil {
|
||||
return x.CorrelationId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *BatchCollectCommand) GetTasks() []*CollectTask {
|
||||
if x != nil {
|
||||
return x.Tasks
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CollectTask
|
||||
// 定义了单个采集任务的“意图”。现在直接包含平台生成的原始485指令,并带上总线号。
|
||||
type CollectTask struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Command *Raw485Command `protobuf:"bytes,2,opt,name=command,proto3" json:"command,omitempty"` // 平台生成的原始485指令
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CollectTask) Reset() {
|
||||
*x = CollectTask{}
|
||||
mi := &file_device_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CollectTask) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CollectTask) ProtoMessage() {}
|
||||
|
||||
func (x *CollectTask) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_device_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CollectTask.ProtoReflect.Descriptor instead.
|
||||
func (*CollectTask) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *CollectTask) GetCommand() *Raw485Command {
|
||||
if x != nil {
|
||||
return x.Command
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CollectResult
|
||||
// 这是设备响应的、极致精简的数据包。
|
||||
type CollectResult struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
CorrelationId string `protobuf:"bytes,1,opt,name=correlation_id,json=correlationId,proto3" json:"correlation_id,omitempty"` // 从下行指令中原样返回的关联ID
|
||||
Values []float32 `protobuf:"fixed32,2,rep,packed,name=values,proto3" json:"values,omitempty"` // 按预定顺序排列的采集值
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CollectResult) Reset() {
|
||||
*x = CollectResult{}
|
||||
mi := &file_device_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CollectResult) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CollectResult) ProtoMessage() {}
|
||||
|
||||
func (x *CollectResult) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_device_proto_msgTypes[4]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CollectResult.ProtoReflect.Descriptor instead.
|
||||
func (*CollectResult) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *CollectResult) GetCorrelationId() string {
|
||||
if x != nil {
|
||||
return x.CorrelationId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CollectResult) GetValues() []float32 {
|
||||
if x != nil {
|
||||
return x.Values
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_device_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_device_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\fdevice.proto\x12\x06device\x1a\x19google/protobuf/any.proto\"S\n" +
|
||||
"\rRaw485Command\x12\x1d\n" +
|
||||
"\n" +
|
||||
"bus_number\x18\x01 \x01(\x05R\tbusNumber\x12#\n" +
|
||||
"\rcommand_bytes\x18\x02 \x01(\fR\fcommandBytes\"c\n" +
|
||||
"\vInstruction\x12*\n" +
|
||||
"\x06method\x18\x01 \x01(\x0e2\x12.device.MethodTypeR\x06method\x12(\n" +
|
||||
"\x04data\x18\x02 \x01(\v2\x14.google.protobuf.AnyR\x04data\"g\n" +
|
||||
"\x13BatchCollectCommand\x12%\n" +
|
||||
"\x0ecorrelation_id\x18\x01 \x01(\tR\rcorrelationId\x12)\n" +
|
||||
"\x05tasks\x18\x02 \x03(\v2\x13.device.CollectTaskR\x05tasks\">\n" +
|
||||
"\vCollectTask\x12/\n" +
|
||||
"\acommand\x18\x02 \x01(\v2\x15.device.Raw485CommandR\acommand\"N\n" +
|
||||
"\rCollectResult\x12%\n" +
|
||||
"\x0ecorrelation_id\x18\x01 \x01(\tR\rcorrelationId\x12\x16\n" +
|
||||
"\x06values\x18\x02 \x03(\x02R\x06values**\n" +
|
||||
"\n" +
|
||||
"MethodType\x12\x0f\n" +
|
||||
"\vINSTRUCTION\x10\x00\x12\v\n" +
|
||||
"\aCOLLECT\x10\x01B\x1eZ\x1cinternal/domain/device/protob\x06proto3"
|
||||
|
||||
var (
|
||||
file_device_proto_rawDescOnce sync.Once
|
||||
file_device_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_device_proto_rawDescGZIP() []byte {
|
||||
file_device_proto_rawDescOnce.Do(func() {
|
||||
file_device_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_device_proto_rawDesc), len(file_device_proto_rawDesc)))
|
||||
})
|
||||
return file_device_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_device_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
|
||||
var file_device_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
|
||||
var file_device_proto_goTypes = []any{
|
||||
(MethodType)(0), // 0: device.MethodType
|
||||
(*Raw485Command)(nil), // 1: device.Raw485Command
|
||||
(*Instruction)(nil), // 2: device.Instruction
|
||||
(*BatchCollectCommand)(nil), // 3: device.BatchCollectCommand
|
||||
(*CollectTask)(nil), // 4: device.CollectTask
|
||||
(*CollectResult)(nil), // 5: device.CollectResult
|
||||
(*anypb.Any)(nil), // 6: google.protobuf.Any
|
||||
}
|
||||
var file_device_proto_depIdxs = []int32{
|
||||
0, // 0: device.Instruction.method:type_name -> device.MethodType
|
||||
6, // 1: device.Instruction.data:type_name -> google.protobuf.Any
|
||||
4, // 2: device.BatchCollectCommand.tasks:type_name -> device.CollectTask
|
||||
1, // 3: device.CollectTask.command:type_name -> device.Raw485Command
|
||||
4, // [4:4] is the sub-list for method output_type
|
||||
4, // [4:4] is the sub-list for method input_type
|
||||
4, // [4:4] is the sub-list for extension type_name
|
||||
4, // [4:4] is the sub-list for extension extendee
|
||||
0, // [0:4] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_device_proto_init() }
|
||||
func file_device_proto_init() {
|
||||
if File_device_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_device_proto_rawDesc), len(file_device_proto_rawDesc)),
|
||||
NumEnums: 1,
|
||||
NumMessages: 5,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
GoTypes: file_device_proto_goTypes,
|
||||
DependencyIndexes: file_device_proto_depIdxs,
|
||||
EnumInfos: file_device_proto_enumTypes,
|
||||
MessageInfos: file_device_proto_msgTypes,
|
||||
}.Build()
|
||||
File_device_proto = out.File
|
||||
file_device_proto_goTypes = nil
|
||||
file_device_proto_depIdxs = nil
|
||||
}
|
||||
51
internal/domain/device/proto/device.proto
Normal file
51
internal/domain/device/proto/device.proto
Normal file
@@ -0,0 +1,51 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package device;
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
|
||||
option go_package = "internal/domain/device/proto";
|
||||
|
||||
// --- 通用指令结构 ---
|
||||
|
||||
// 指令类型
|
||||
enum MethodType {
|
||||
INSTRUCTION = 0; // 下发指令
|
||||
COLLECT = 1; // 批量采集
|
||||
}
|
||||
|
||||
// 平台生成的原始485指令,单片机直接发送到总线
|
||||
message Raw485Command {
|
||||
int32 bus_number = 1; // 总线号,用于指示单片机将指令发送到哪个总线
|
||||
bytes command_bytes = 2; // 原始485指令的字节数组
|
||||
}
|
||||
|
||||
// 指令 (所有空中数据都会被包装在这里面)
|
||||
// data字段现在可以包含 Raw485Command,表示平台生成的原始485指令。
|
||||
message Instruction {
|
||||
MethodType method = 1;
|
||||
google.protobuf.Any data = 2; // 可以是 Switch, Raw485Command 等
|
||||
}
|
||||
|
||||
// --- 批量采集相关结构 ---
|
||||
|
||||
// BatchCollectCommand
|
||||
// 用于在平台内部构建一个完整的、包含所有元数据的批量采集任务。
|
||||
// 这个消息本身不会被发送到设备。
|
||||
message BatchCollectCommand {
|
||||
string correlation_id = 1; // 用于关联请求和响应的唯一ID
|
||||
repeated CollectTask tasks = 2; // 采集任务列表
|
||||
}
|
||||
|
||||
// CollectTask
|
||||
// 定义了单个采集任务的“意图”。现在直接包含平台生成的原始485指令,并带上总线号。
|
||||
message CollectTask {
|
||||
Raw485Command command = 2; // 平台生成的原始485指令
|
||||
}
|
||||
|
||||
// CollectResult
|
||||
// 这是设备响应的、极致精简的数据包。
|
||||
message CollectResult {
|
||||
string correlation_id = 1; // 从下行指令中原样返回的关联ID
|
||||
repeated float values = 2; // 按预定顺序排列的采集值
|
||||
}
|
||||
Reference in New Issue
Block a user