实现 LoRaMeshUartPassthroughTransport 构造函数

This commit is contained in:
2025-10-10 00:01:21 +08:00
parent 38a01f4a6e
commit 8a5f6dc34e
4 changed files with 37 additions and 84 deletions

View File

@@ -1,19 +1,44 @@
package lora
import (
"fmt"
"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/transport"
"github.com/tarm/serial"
)
// LoRaMeshUartPassthroughTransport 实现 transport.Communicator 接口, 用于 LoRa 网状网络 UART 透传
type LoRaMeshUartPassthroughTransport struct {
// 在这里添加必要的字段
config config.LoraMeshConfig
logger *logs.Logger
mu sync.Mutex // 保护对 LoRa 模块的并发访问
port *serial.Port
}
// NewLoRaMeshUartPassthroughTransport 创建一个新的 LoRaMeshUartPassthroughTransport
func NewLoRaMeshUartPassthroughTransport(config config.LoraMeshConfig, logger *logs.Logger) *LoRaMeshUartPassthroughTransport {
return &LoRaMeshUartPassthroughTransport{}
func NewLoRaMeshUartPassthroughTransport(config config.LoraMeshConfig, logger *logs.Logger) (*LoRaMeshUartPassthroughTransport, error) {
c := &serial.Config{
Name: config.UARTPort,
Baud: config.BaudRate,
ReadTimeout: time.Second * time.Duration(config.Timeout),
}
port, err := serial.OpenPort(c)
if err != nil {
return nil, fmt.Errorf("无法打开串口 %s: %w", config.UARTPort, err)
}
return &LoRaMeshUartPassthroughTransport{
config: config,
logger: logger,
mu: sync.Mutex{},
port: port,
}, nil
}
// Send 将数据发送到指定的地址