实现 LoRaMeshUartPassthroughTransport
This commit is contained in:
@@ -150,11 +150,12 @@ type LoraConfig struct {
|
||||
|
||||
// LoraMeshConfig 代表Lora Mesh配置
|
||||
type LoraMeshConfig struct {
|
||||
UARTPort string `yaml:"uart_port"`
|
||||
BaudRate int `yaml:"baud_rate"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
LoraMeshMode string `yaml:"lora_mesh_mode"`
|
||||
MaxChunkSize int `yaml:"max_chunk_size"`
|
||||
UARTPort string `yaml:"uart_port"`
|
||||
BaudRate int `yaml:"baud_rate"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
LoraMeshMode string `yaml:"lora_mesh_mode"`
|
||||
MaxChunkSize int `yaml:"max_chunk_size"`
|
||||
ReassemblyTimeout int `yaml:"reassembly_timeout"`
|
||||
}
|
||||
|
||||
// NewConfig 创建并返回一个新的配置实例
|
||||
|
||||
@@ -1,31 +1,103 @@
|
||||
package lora
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
|
||||
"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"
|
||||
"github.com/google/uuid"
|
||||
"github.com/tarm/serial"
|
||||
gproto "google.golang.org/protobuf/proto"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
// LoRaMeshUartPassthroughTransport 实现 transport.Communicator 接口, 用于 LoRa 网状网络 UART 透传
|
||||
// transportState 定义了传输层的内部状态
|
||||
type transportState int
|
||||
|
||||
const (
|
||||
stateIdle transportState = iota // 空闲状态
|
||||
stateReceiving // 接收状态:正在接收一个(可能分片的)消息
|
||||
stateSending // 发送状态:正在发送一个(可能分片的)消息
|
||||
)
|
||||
|
||||
// message 是一个内部结构,用于封装一个完整的、已重组的消息及其元数据
|
||||
type message struct {
|
||||
SourceAddr string // 源地址
|
||||
DestAddr string // 目标地址
|
||||
Payload []byte // 有效载荷
|
||||
}
|
||||
|
||||
// LoRaMeshUartPassthroughTransport 实现了 transport.Communicator 和 transport.Listener 接口
|
||||
type LoRaMeshUartPassthroughTransport struct {
|
||||
config config.LoraMeshConfig
|
||||
logger *logs.Logger
|
||||
mu sync.Mutex // 保护对 LoRa 模块的并发访问
|
||||
port *serial.Port
|
||||
|
||||
mu sync.Mutex // 用于保护对外的公共方法(如Send)的并发调用
|
||||
state transportState
|
||||
|
||||
stopChan chan struct{} // 用于优雅地停止worker协程
|
||||
wg sync.WaitGroup // 用于等待worker协程完全退出
|
||||
sendChan chan *sendRequest // 发送任务的请求通道
|
||||
|
||||
// --- 接收与重组相关 ---
|
||||
reassemblyBuffers map[uint16]*reassemblyBuffer // 键为源地址SourceAddr,值为对应的重组缓冲区
|
||||
currentRecvSource uint16 // 当前正在接收的源地址
|
||||
reassemblyTimeout *time.Timer // 分片重组的超时定时器
|
||||
reassemblyTimeoutCh chan uint16 // 当超时触发时,用于传递源地址
|
||||
|
||||
// --- 依赖注入的仓库 ---
|
||||
areaControllerRepo repository.AreaControllerRepository
|
||||
pendingCollectionRepo repository.PendingCollectionRepository
|
||||
deviceRepo repository.DeviceRepository
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
}
|
||||
|
||||
// NewLoRaMeshUartPassthroughTransport 创建一个新的 LoRaMeshUartPassthroughTransport
|
||||
func NewLoRaMeshUartPassthroughTransport(config config.LoraMeshConfig, logger *logs.Logger) (*LoRaMeshUartPassthroughTransport, error) {
|
||||
// sendRequest 封装了一次发送请求
|
||||
type sendRequest struct {
|
||||
address string
|
||||
payload []byte
|
||||
result chan *sendResultTuple
|
||||
}
|
||||
|
||||
// sendResultTuple 用于在通道中安全地传递Send方法的返回值
|
||||
type sendResultTuple struct {
|
||||
result *transport.SendResult
|
||||
err error
|
||||
}
|
||||
|
||||
// reassemblyBuffer 用于缓存和重组来自同一源的分片
|
||||
type reassemblyBuffer struct {
|
||||
chunks map[uint8][]byte // 键为当前包序号CurrentChunk
|
||||
totalChunks uint8
|
||||
receivedChunks int
|
||||
}
|
||||
|
||||
// NewLoRaMeshUartPassthroughTransport 创建一个新的 LoRaMeshUartPassthroughTransport 实例
|
||||
func NewLoRaMeshUartPassthroughTransport(
|
||||
config config.LoraMeshConfig,
|
||||
logger *logs.Logger,
|
||||
areaControllerRepo repository.AreaControllerRepository,
|
||||
pendingCollectionRepo repository.PendingCollectionRepository,
|
||||
deviceRepo repository.DeviceRepository,
|
||||
sensorDataRepo repository.SensorDataRepository,
|
||||
) (*LoRaMeshUartPassthroughTransport, error) {
|
||||
c := &serial.Config{
|
||||
Name: config.UARTPort,
|
||||
Baud: config.BaudRate,
|
||||
ReadTimeout: time.Second * time.Duration(config.Timeout),
|
||||
ReadTimeout: time.Millisecond * time.Duration(config.Timeout),
|
||||
}
|
||||
|
||||
port, err := serial.OpenPort(c)
|
||||
@@ -33,26 +105,441 @@ func NewLoRaMeshUartPassthroughTransport(config config.LoraMeshConfig, logger *l
|
||||
return nil, fmt.Errorf("无法打开串口 %s: %w", config.UARTPort, err)
|
||||
}
|
||||
|
||||
return &LoRaMeshUartPassthroughTransport{
|
||||
config: config,
|
||||
logger: logger,
|
||||
mu: sync.Mutex{},
|
||||
port: port,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Send 将数据发送到指定的地址
|
||||
func (t *LoRaMeshUartPassthroughTransport) Send(address string, payload []byte) (*transport.SendResult, error) {
|
||||
// TODO: 实现发送逻辑
|
||||
return nil, nil
|
||||
t := &LoRaMeshUartPassthroughTransport{
|
||||
config: config,
|
||||
logger: logger,
|
||||
port: port,
|
||||
state: stateIdle,
|
||||
stopChan: make(chan struct{}),
|
||||
sendChan: make(chan *sendRequest),
|
||||
reassemblyBuffers: make(map[uint16]*reassemblyBuffer),
|
||||
reassemblyTimeoutCh: make(chan uint16, 1),
|
||||
|
||||
// 注入依赖
|
||||
areaControllerRepo: areaControllerRepo,
|
||||
pendingCollectionRepo: pendingCollectionRepo,
|
||||
deviceRepo: deviceRepo,
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// Listen 启动后台监听协程(非阻塞)
|
||||
func (t *LoRaMeshUartPassthroughTransport) Listen() error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
t.wg.Add(1)
|
||||
go t.workerLoop()
|
||||
t.logger.Info("LoRa传输层工作协程已启动")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *LoRaMeshUartPassthroughTransport) Stop() error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
// Send 将发送任务提交给worker协程
|
||||
func (t *LoRaMeshUartPassthroughTransport) Send(address string, payload []byte) (*transport.SendResult, error) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
resultChan := make(chan *sendResultTuple, 1)
|
||||
req := &sendRequest{
|
||||
address: address,
|
||||
payload: payload,
|
||||
result: resultChan,
|
||||
}
|
||||
|
||||
select {
|
||||
case t.sendChan <- req:
|
||||
// 等待worker协程处理完毕
|
||||
res := <-resultChan
|
||||
return res.result, res.err
|
||||
case <-t.stopChan:
|
||||
return nil, fmt.Errorf("传输层正在停止")
|
||||
}
|
||||
}
|
||||
|
||||
// Stop 停止传输层
|
||||
func (t *LoRaMeshUartPassthroughTransport) Stop() error {
|
||||
close(t.stopChan)
|
||||
t.wg.Wait()
|
||||
return t.port.Close()
|
||||
}
|
||||
|
||||
// workerLoop 是核心的状态机和调度器
|
||||
func (t *LoRaMeshUartPassthroughTransport) workerLoop() {
|
||||
defer t.wg.Done()
|
||||
|
||||
readBuffer := make([]byte, 1024)
|
||||
parserBuffer := new(bytes.Buffer)
|
||||
|
||||
for {
|
||||
// 1. 检查是否需要停止 (优先检查,以便快速退出)
|
||||
select {
|
||||
case <-t.stopChan:
|
||||
if t.reassemblyTimeout != nil {
|
||||
t.reassemblyTimeout.Stop()
|
||||
}
|
||||
t.logger.Info("LoRa传输层工作协程已停止")
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
// 2. 尝试从串口读取数据
|
||||
n, err := t.port.Read(readBuffer)
|
||||
if n > 0 {
|
||||
parserBuffer.Write(readBuffer[:n])
|
||||
}
|
||||
if err != nil && err != io.EOF {
|
||||
// 忽略预期的超时错误(io.EOF),只记录真正的IO错误
|
||||
t.logger.Errorf("从串口读取数据时发生错误: %v", err)
|
||||
}
|
||||
|
||||
// 3. 循环解析缓冲区中的完整物理帧
|
||||
for {
|
||||
frame := t.parseCompleteFrame(parserBuffer)
|
||||
if frame == nil {
|
||||
break // 缓冲区中没有更多完整帧了
|
||||
}
|
||||
t.handleFrame(frame)
|
||||
}
|
||||
|
||||
// 4. 根据当前状态执行主要逻辑
|
||||
switch t.state {
|
||||
case stateIdle:
|
||||
t.runIdleState()
|
||||
case stateReceiving:
|
||||
t.runReceivingState()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// runIdleState 处理空闲状态下的逻辑,主要是检查并启动发送任务
|
||||
func (t *LoRaMeshUartPassthroughTransport) runIdleState() {
|
||||
select {
|
||||
case req := <-t.sendChan:
|
||||
t.state = stateSending
|
||||
// 此处为阻塞式发送
|
||||
result, err := t.executeSend(req)
|
||||
req.result <- &sendResultTuple{result: result, err: err}
|
||||
t.state = stateIdle
|
||||
default:
|
||||
// 没有发送任务,保持空闲
|
||||
}
|
||||
}
|
||||
|
||||
// runReceivingState 处理接收状态下的逻辑,主要是检查超时
|
||||
func (t *LoRaMeshUartPassthroughTransport) runReceivingState() {
|
||||
select {
|
||||
case sourceAddr := <-t.reassemblyTimeoutCh:
|
||||
t.logger.Warnf("接收来自 0x%04X 的消息超时", sourceAddr)
|
||||
delete(t.reassemblyBuffers, sourceAddr)
|
||||
t.state = stateIdle
|
||||
default:
|
||||
// 等待更多分片或超时
|
||||
}
|
||||
}
|
||||
|
||||
// executeSend 执行完整的发送流程(分片、构建、写入)
|
||||
func (t *LoRaMeshUartPassthroughTransport) executeSend(req *sendRequest) (*transport.SendResult, error) {
|
||||
chunks := splitPayload(req.payload, t.config.MaxChunkSize)
|
||||
totalChunks := uint8(len(chunks))
|
||||
|
||||
destAddr, err := strconv.ParseUint(req.address, 16, 16)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的目标地址: %s", req.address)
|
||||
}
|
||||
|
||||
for i, chunk := range chunks {
|
||||
currentChunk := uint8(i)
|
||||
frame := new(bytes.Buffer)
|
||||
frame.WriteByte(0xED) // 帧头
|
||||
frame.WriteByte(uint8(len(chunk) + 2)) // 数据长度 = 数据块 + 2 (总包数+当前包序号)
|
||||
binary.Write(frame, binary.BigEndian, uint16(destAddr)) // 目标地址
|
||||
frame.WriteByte(totalChunks) // 总包数
|
||||
frame.WriteByte(currentChunk) // 当前包序号
|
||||
frame.Write(chunk) // 数据块
|
||||
|
||||
_, err := t.port.Write(frame.Bytes())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("写入串口失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
msgID := uuid.New().String()
|
||||
return &transport.SendResult{MessageID: msgID}, nil
|
||||
}
|
||||
|
||||
// handleFrame 处理一个从串口解析出的完整物理帧
|
||||
func (t *LoRaMeshUartPassthroughTransport) handleFrame(frame []byte) {
|
||||
if len(frame) < 8 {
|
||||
t.logger.Warnf("收到了一个无效长度的帧: %d", len(frame))
|
||||
return
|
||||
}
|
||||
|
||||
destAddr := binary.BigEndian.Uint16(frame[2:4])
|
||||
totalChunks := frame[4]
|
||||
currentChunk := frame[5]
|
||||
sourceAddr := binary.BigEndian.Uint16(frame[len(frame)-2:])
|
||||
chunkData := frame[6 : len(frame)-2]
|
||||
|
||||
// 如果是单包消息
|
||||
if totalChunks == 1 {
|
||||
msg := &message{
|
||||
SourceAddr: fmt.Sprintf("%04X", sourceAddr),
|
||||
DestAddr: fmt.Sprintf("%04X", destAddr),
|
||||
Payload: chunkData,
|
||||
}
|
||||
go t.handleUpstreamMessage(msg)
|
||||
return
|
||||
}
|
||||
|
||||
// --- 处理分片消息 ---
|
||||
switch t.state {
|
||||
case stateIdle:
|
||||
if currentChunk == 0 {
|
||||
t.state = stateReceiving
|
||||
t.currentRecvSource = sourceAddr
|
||||
t.reassemblyBuffers[sourceAddr] = &reassemblyBuffer{
|
||||
chunks: make(map[uint8][]byte),
|
||||
totalChunks: totalChunks,
|
||||
receivedChunks: 0,
|
||||
}
|
||||
t.reassemblyBuffers[sourceAddr].chunks[currentChunk] = chunkData
|
||||
t.reassemblyBuffers[sourceAddr].receivedChunks++
|
||||
|
||||
if t.reassemblyTimeout != nil {
|
||||
t.reassemblyTimeout.Stop()
|
||||
}
|
||||
t.reassemblyTimeout = time.AfterFunc(time.Duration(t.config.ReassemblyTimeout)*time.Second, func() {
|
||||
t.reassemblyTimeoutCh <- sourceAddr
|
||||
})
|
||||
} else {
|
||||
t.logger.Warnf("在空闲状态下收到了一个来自 0x%04X 的非首包分片,已忽略。", sourceAddr)
|
||||
}
|
||||
|
||||
case stateReceiving:
|
||||
if sourceAddr != t.currentRecvSource {
|
||||
t.logger.Warnf("正在接收来自 0x%04X 的数据时,收到了另一个源 0x%04X 的分片,已忽略。", t.currentRecvSource, sourceAddr)
|
||||
return
|
||||
}
|
||||
|
||||
buffer, ok := t.reassemblyBuffers[sourceAddr]
|
||||
if !ok {
|
||||
t.logger.Errorf("内部错误: 处于接收状态,但没有为 0x%04X 找到缓冲区", sourceAddr)
|
||||
t.state = stateIdle // 重置状态
|
||||
return
|
||||
}
|
||||
|
||||
// 存入分片并重置超时
|
||||
buffer.chunks[currentChunk] = chunkData
|
||||
buffer.receivedChunks++
|
||||
t.reassemblyTimeout.Reset(time.Duration(t.config.ReassemblyTimeout) * time.Second)
|
||||
|
||||
// 检查是否已全部收到
|
||||
if buffer.receivedChunks == int(buffer.totalChunks) {
|
||||
t.reassemblyTimeout.Stop()
|
||||
|
||||
// 重组消息
|
||||
fullPayload := new(bytes.Buffer)
|
||||
for i := 0; i < int(buffer.totalChunks); i++ {
|
||||
fullPayload.Write(buffer.chunks[uint8(i)])
|
||||
}
|
||||
|
||||
msg := &message{
|
||||
SourceAddr: fmt.Sprintf("%04X", sourceAddr),
|
||||
DestAddr: fmt.Sprintf("%04X", destAddr),
|
||||
Payload: fullPayload.Bytes(),
|
||||
}
|
||||
go t.handleUpstreamMessage(msg)
|
||||
|
||||
// 清理并返回空闲状态
|
||||
delete(t.reassemblyBuffers, sourceAddr)
|
||||
t.state = stateIdle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleUpstreamMessage 在独立的协程中处理单个上行的、完整的消息。
|
||||
func (t *LoRaMeshUartPassthroughTransport) handleUpstreamMessage(msg *message) {
|
||||
t.logger.Infof("开始处理来自 %s 的上行消息", msg.SourceAddr)
|
||||
|
||||
// 1. 解析外层 "信封"
|
||||
var instruction proto.Instruction
|
||||
if err := gproto.Unmarshal(msg.Payload, &instruction); err != nil {
|
||||
t.logger.Errorf("解析上行 Instruction Protobuf 失败: %v, 源地址: %s, 原始数据: %x", err, msg.SourceAddr, msg.Payload)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 使用 type switch 从 oneof payload 中提取 CollectResult
|
||||
var collectResp *proto.CollectResult
|
||||
switch p := instruction.GetPayload().(type) {
|
||||
case *proto.Instruction_CollectResult:
|
||||
collectResp = p.CollectResult
|
||||
default:
|
||||
// 如果上行的数据不是采集结果,记录日志并忽略
|
||||
t.logger.Infof("收到一个非采集响应的上行指令 (类型: %T),无需处理。源地址: %s", p, msg.SourceAddr)
|
||||
return
|
||||
}
|
||||
|
||||
if collectResp == nil {
|
||||
t.logger.Errorf("从 Instruction 中提取的 CollectResult 为 nil。源地址: %s", msg.SourceAddr)
|
||||
return
|
||||
}
|
||||
|
||||
correlationID := collectResp.CorrelationId
|
||||
t.logger.Infof("成功解析采集响应 (CorrelationID: %s),包含 %d 个值。", correlationID, len(collectResp.Values))
|
||||
|
||||
// 3. 查找区域主控 (注意:LoRa Mesh 的 SourceAddr 对应于区域主控的 NetworkID)
|
||||
regionalController, err := t.areaControllerRepo.FindByNetworkID(msg.SourceAddr)
|
||||
if err != nil {
|
||||
t.logger.Errorf("处理上行消息失败:无法通过源地址 '%s' 找到区域主控设备: %v", msg.SourceAddr, err)
|
||||
return
|
||||
}
|
||||
if err := regionalController.SelfCheck(); err != nil {
|
||||
t.logger.Errorf("处理上行消息失败:区域主控 %v(ID: %d) 未通过自检: %v", regionalController.Name, regionalController.ID, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 4. 根据 CorrelationID 查找待处理请求
|
||||
pendingReq, err := t.pendingCollectionRepo.FindByCorrelationID(correlationID)
|
||||
if err != nil {
|
||||
t.logger.Errorf("处理采集响应失败:无法找到待处理请求 (CorrelationID: %s): %v", correlationID, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查状态,防止重复处理
|
||||
if pendingReq.Status != models.PendingStatusPending && pendingReq.Status != models.PendingStatusTimedOut {
|
||||
t.logger.Warnf("收到一个已处理过的采集响应 (CorrelationID: %s, Status: %s),将忽略。", correlationID, pendingReq.Status)
|
||||
return
|
||||
}
|
||||
|
||||
// 5. 匹配数据并存入数据库
|
||||
deviceIDs := pendingReq.CommandMetadata
|
||||
values := collectResp.Values
|
||||
if len(deviceIDs) != len(values) {
|
||||
t.logger.Errorf("数据不匹配:下行指令要求采集 %d 个设备,但上行响应包含 %d 个值 (CorrelationID: %s)", len(deviceIDs), len(values), correlationID)
|
||||
err = t.pendingCollectionRepo.UpdateStatusToFulfilled(correlationID, time.Now())
|
||||
if err != nil {
|
||||
t.logger.Errorf("处理采集响应失败:无法更新待处理请求 (CorrelationID: %s) 的状态为完成: %v", correlationID, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for i, deviceID := range deviceIDs {
|
||||
rawSensorValue := values[i]
|
||||
|
||||
if math.IsNaN(float64(rawSensorValue)) {
|
||||
t.logger.Warnf("设备 (ID: %d) 上报了一个无效的 NaN 值,已跳过当前值的记录。", deviceID)
|
||||
continue
|
||||
}
|
||||
|
||||
dev, err := t.deviceRepo.FindByID(deviceID)
|
||||
if err != nil {
|
||||
t.logger.Errorf("处理采集数据失败:无法找到设备 (ID: %d): %v", deviceID, err)
|
||||
continue
|
||||
}
|
||||
if err := dev.SelfCheck(); err != nil {
|
||||
t.logger.Warnf("跳过设备 %d,因其未通过自检: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
if err := dev.DeviceTemplate.SelfCheck(); err != nil {
|
||||
t.logger.Warnf("跳过设备 %d,因其设备模板未通过自检: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
var valueDescriptors []*models.ValueDescriptor
|
||||
if err := dev.DeviceTemplate.ParseValues(&valueDescriptors); err != nil {
|
||||
t.logger.Warnf("跳过设备 %d,因其设备模板的 Values 属性解析失败: %v", dev.ID, err)
|
||||
continue
|
||||
}
|
||||
if len(valueDescriptors) == 0 {
|
||||
t.logger.Warnf("跳过设备 %d,因其设备模板缺少 ValueDescriptor 定义", dev.ID)
|
||||
continue
|
||||
}
|
||||
valueDescriptor := valueDescriptors[0]
|
||||
|
||||
parsedValue := float64(rawSensorValue)*valueDescriptor.Multiplier + valueDescriptor.Offset
|
||||
|
||||
var dataToRecord interface{}
|
||||
switch valueDescriptor.Type {
|
||||
case models.SensorTypeTemperature:
|
||||
dataToRecord = models.TemperatureData{TemperatureCelsius: parsedValue}
|
||||
case models.SensorTypeHumidity:
|
||||
dataToRecord = models.HumidityData{HumidityPercent: parsedValue}
|
||||
case models.SensorTypeWeight:
|
||||
dataToRecord = models.WeightData{WeightKilograms: parsedValue}
|
||||
default:
|
||||
t.logger.Warnf("未知的传感器类型 '%s',将使用通用格式记录", valueDescriptor.Type)
|
||||
dataToRecord = map[string]float64{"value": parsedValue}
|
||||
}
|
||||
|
||||
t.recordSensorData(regionalController.ID, dev.ID, time.Now(), valueDescriptor.Type, dataToRecord)
|
||||
t.logger.Infof("成功记录传感器数据: 设备ID=%d, 类型=%s, 原始值=%f, 解析值=%.2f", dev.ID, valueDescriptor.Type, rawSensorValue, parsedValue)
|
||||
}
|
||||
|
||||
// 6. 更新请求状态为“已完成”
|
||||
if err := t.pendingCollectionRepo.UpdateStatusToFulfilled(correlationID, time.Now()); err != nil {
|
||||
t.logger.Errorf("更新待采集请求状态为 'fulfilled' 失败 (CorrelationID: %s): %v", correlationID, err)
|
||||
} else {
|
||||
t.logger.Infof("成功完成并关闭采集请求 (CorrelationID: %s)", correlationID)
|
||||
}
|
||||
}
|
||||
|
||||
// recordSensorData 是一个通用方法,用于将传感器数据存入数据库。
|
||||
func (t *LoRaMeshUartPassthroughTransport) recordSensorData(regionalControllerID uint, sensorDeviceID uint, eventTime time.Time, sensorType models.SensorType, data interface{}) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
t.logger.Errorf("记录传感器数据失败:序列化数据为 JSON 时出错: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
sensorData := &models.SensorData{
|
||||
Time: eventTime,
|
||||
DeviceID: sensorDeviceID,
|
||||
RegionalControllerID: regionalControllerID,
|
||||
SensorType: sensorType,
|
||||
Data: datatypes.JSON(jsonData),
|
||||
}
|
||||
|
||||
if err := t.sensorDataRepo.Create(sensorData); err != nil {
|
||||
t.logger.Errorf("记录传感器数据失败:存入数据库时出错: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// parseCompleteFrame 实现粘包和半包处理
|
||||
func (t *LoRaMeshUartPassthroughTransport) parseCompleteFrame(buffer *bytes.Buffer) []byte {
|
||||
for {
|
||||
headerIndex := bytes.IndexByte(buffer.Bytes(), 0xED)
|
||||
if headerIndex == -1 {
|
||||
return nil
|
||||
}
|
||||
buffer.Next(headerIndex)
|
||||
|
||||
if buffer.Len() < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
lengthField := buffer.Bytes()[1]
|
||||
frameLength := 1 + 1 + 2 + int(lengthField) + 2
|
||||
|
||||
if buffer.Len() < frameLength {
|
||||
return nil
|
||||
}
|
||||
|
||||
return buffer.Next(frameLength)
|
||||
}
|
||||
}
|
||||
|
||||
// splitPayload 将数据块按最大长度进行切分
|
||||
func splitPayload(payload []byte, maxChunkSize int) [][]byte {
|
||||
if len(payload) == 0 {
|
||||
return [][]byte{{}}
|
||||
}
|
||||
|
||||
var chunks [][]byte
|
||||
for i := 0; i < len(payload); i += maxChunkSize {
|
||||
end := i + maxChunkSize
|
||||
if end > len(payload) {
|
||||
end = len(payload)
|
||||
}
|
||||
chunks = append(chunks, payload[i:end])
|
||||
}
|
||||
return chunks
|
||||
}
|
||||
|
||||
415
internal/infra/transport/proto/device.pb.go
Normal file
415
internal/infra/transport/proto/device.pb.go
Normal file
@@ -0,0 +1,415 @@
|
||||
// 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"
|
||||
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)
|
||||
)
|
||||
|
||||
// 平台生成的原始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
|
||||
}
|
||||
|
||||
// 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[1]
|
||||
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[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 BatchCollectCommand.ProtoReflect.Descriptor instead.
|
||||
func (*BatchCollectCommand) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
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
|
||||
// 定义了单个采集任务的“意图”。
|
||||
type CollectTask struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Command *Raw485Command `protobuf:"bytes,1,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[2]
|
||||
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[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 CollectTask.ProtoReflect.Descriptor instead.
|
||||
func (*CollectTask) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
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[3]
|
||||
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[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 CollectResult.ProtoReflect.Descriptor instead.
|
||||
func (*CollectResult) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 指令 (所有从平台下发到设备的数据都应该被包装在这里面)
|
||||
// 使用 oneof 来替代 google.protobuf.Any,这是嵌入式环境下的标准做法。
|
||||
// 它高效、类型安全,且只解码一次。
|
||||
type Instruction struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
// Types that are valid to be assigned to Payload:
|
||||
//
|
||||
// *Instruction_Raw_485Command
|
||||
// *Instruction_BatchCollectCommand
|
||||
// *Instruction_CollectResult
|
||||
Payload isInstruction_Payload `protobuf_oneof:"payload"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Instruction) Reset() {
|
||||
*x = Instruction{}
|
||||
mi := &file_device_proto_msgTypes[4]
|
||||
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[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 Instruction.ProtoReflect.Descriptor instead.
|
||||
func (*Instruction) Descriptor() ([]byte, []int) {
|
||||
return file_device_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *Instruction) GetPayload() isInstruction_Payload {
|
||||
if x != nil {
|
||||
return x.Payload
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Instruction) GetRaw_485Command() *Raw485Command {
|
||||
if x != nil {
|
||||
if x, ok := x.Payload.(*Instruction_Raw_485Command); ok {
|
||||
return x.Raw_485Command
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Instruction) GetBatchCollectCommand() *BatchCollectCommand {
|
||||
if x != nil {
|
||||
if x, ok := x.Payload.(*Instruction_BatchCollectCommand); ok {
|
||||
return x.BatchCollectCommand
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Instruction) GetCollectResult() *CollectResult {
|
||||
if x != nil {
|
||||
if x, ok := x.Payload.(*Instruction_CollectResult); ok {
|
||||
return x.CollectResult
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type isInstruction_Payload interface {
|
||||
isInstruction_Payload()
|
||||
}
|
||||
|
||||
type Instruction_Raw_485Command struct {
|
||||
Raw_485Command *Raw485Command `protobuf:"bytes,1,opt,name=raw_485_command,json=raw485Command,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Instruction_BatchCollectCommand struct {
|
||||
BatchCollectCommand *BatchCollectCommand `protobuf:"bytes,2,opt,name=batch_collect_command,json=batchCollectCommand,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Instruction_CollectResult struct {
|
||||
CollectResult *CollectResult `protobuf:"bytes,3,opt,name=collect_result,json=collectResult,proto3,oneof"` // ADDED:用于上行数据
|
||||
}
|
||||
|
||||
func (*Instruction_Raw_485Command) isInstruction_Payload() {}
|
||||
|
||||
func (*Instruction_BatchCollectCommand) isInstruction_Payload() {}
|
||||
|
||||
func (*Instruction_CollectResult) isInstruction_Payload() {}
|
||||
|
||||
var File_device_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_device_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\fdevice.proto\x12\x06device\"S\n" +
|
||||
"\rRaw485Command\x12\x1d\n" +
|
||||
"\n" +
|
||||
"bus_number\x18\x01 \x01(\x05R\tbusNumber\x12#\n" +
|
||||
"\rcommand_bytes\x18\x02 \x01(\fR\fcommandBytes\"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\x01 \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\"\xec\x01\n" +
|
||||
"\vInstruction\x12?\n" +
|
||||
"\x0fraw_485_command\x18\x01 \x01(\v2\x15.device.Raw485CommandH\x00R\rraw485Command\x12Q\n" +
|
||||
"\x15batch_collect_command\x18\x02 \x01(\v2\x1b.device.BatchCollectCommandH\x00R\x13batchCollectCommand\x12>\n" +
|
||||
"\x0ecollect_result\x18\x03 \x01(\v2\x15.device.CollectResultH\x00R\rcollectResultB\t\n" +
|
||||
"\apayloadB\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_msgTypes = make([]protoimpl.MessageInfo, 5)
|
||||
var file_device_proto_goTypes = []any{
|
||||
(*Raw485Command)(nil), // 0: device.Raw485Command
|
||||
(*BatchCollectCommand)(nil), // 1: device.BatchCollectCommand
|
||||
(*CollectTask)(nil), // 2: device.CollectTask
|
||||
(*CollectResult)(nil), // 3: device.CollectResult
|
||||
(*Instruction)(nil), // 4: device.Instruction
|
||||
}
|
||||
var file_device_proto_depIdxs = []int32{
|
||||
2, // 0: device.BatchCollectCommand.tasks:type_name -> device.CollectTask
|
||||
0, // 1: device.CollectTask.command:type_name -> device.Raw485Command
|
||||
0, // 2: device.Instruction.raw_485_command:type_name -> device.Raw485Command
|
||||
1, // 3: device.Instruction.batch_collect_command:type_name -> device.BatchCollectCommand
|
||||
3, // 4: device.Instruction.collect_result:type_name -> device.CollectResult
|
||||
5, // [5:5] is the sub-list for method output_type
|
||||
5, // [5:5] is the sub-list for method input_type
|
||||
5, // [5:5] is the sub-list for extension type_name
|
||||
5, // [5:5] is the sub-list for extension extendee
|
||||
0, // [0:5] 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
|
||||
}
|
||||
file_device_proto_msgTypes[4].OneofWrappers = []any{
|
||||
(*Instruction_Raw_485Command)(nil),
|
||||
(*Instruction_BatchCollectCommand)(nil),
|
||||
(*Instruction_CollectResult)(nil),
|
||||
}
|
||||
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: 0,
|
||||
NumMessages: 5,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
GoTypes: file_device_proto_goTypes,
|
||||
DependencyIndexes: file_device_proto_depIdxs,
|
||||
MessageInfos: file_device_proto_msgTypes,
|
||||
}.Build()
|
||||
File_device_proto = out.File
|
||||
file_device_proto_goTypes = nil
|
||||
file_device_proto_depIdxs = nil
|
||||
}
|
||||
51
internal/infra/transport/proto/device.proto
Normal file
51
internal/infra/transport/proto/device.proto
Normal file
@@ -0,0 +1,51 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package device;
|
||||
|
||||
// import "google/protobuf/any.proto"; // REMOVED: Not suitable for embedded systems.
|
||||
|
||||
option go_package = "internal/domain/device/proto";
|
||||
|
||||
// --- Concrete Command & Data Structures ---
|
||||
|
||||
// 平台生成的原始485指令,单片机直接发送到总线
|
||||
message Raw485Command {
|
||||
int32 bus_number = 1; // 总线号,用于指示单片机将指令发送到哪个总线
|
||||
bytes command_bytes = 2; // 原始485指令的字节数组
|
||||
}
|
||||
|
||||
// BatchCollectCommand
|
||||
// 一个完整的、包含所有元数据的批量采集任务。
|
||||
message BatchCollectCommand {
|
||||
string correlation_id = 1; // 用于关联请求和响应的唯一ID
|
||||
repeated CollectTask tasks = 2; // 采集任务列表
|
||||
}
|
||||
|
||||
// CollectTask
|
||||
// 定义了单个采集任务的“意图”。
|
||||
message CollectTask {
|
||||
Raw485Command command = 1; // 平台生成的原始485指令
|
||||
}
|
||||
|
||||
// CollectResult
|
||||
// 这是设备响应的、极致精简的数据包。
|
||||
message CollectResult {
|
||||
string correlation_id = 1; // 从下行指令中原样返回的关联ID
|
||||
repeated float values = 2; // 按预定顺序排列的采集值
|
||||
}
|
||||
|
||||
|
||||
// --- Main Downlink Instruction Wrapper ---
|
||||
|
||||
// 指令 (所有从平台下发到设备的数据都应该被包装在这里面)
|
||||
// 使用 oneof 来替代 google.protobuf.Any,这是嵌入式环境下的标准做法。
|
||||
// 它高效、类型安全,且只解码一次。
|
||||
message Instruction {
|
||||
oneof payload {
|
||||
Raw485Command raw_485_command = 1;
|
||||
BatchCollectCommand batch_collect_command = 2;
|
||||
CollectResult collect_result = 3; // ADDED:用于上行数据
|
||||
// 如果未来有其他指令类型,比如开关控制,可以直接在这里添加
|
||||
// SwitchCommand switch_command = 3;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user