2025-09-15 18:56:11 +08:00
|
|
|
|
package transport
|
|
|
|
|
|
|
2025-11-05 22:22:46 +08:00
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
2025-12-01 14:32:50 +08:00
|
|
|
|
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/proto"
|
2025-11-05 22:22:46 +08:00
|
|
|
|
)
|
2025-10-20 19:31:19 +08:00
|
|
|
|
|
2025-09-15 18:57:47 +08:00
|
|
|
|
// Communicator 用于其他设备通信
|
|
|
|
|
|
type Communicator interface {
|
2025-09-15 21:27:54 +08:00
|
|
|
|
// Send 用于发送一条单向数据(不等待回信)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
// 成功时,它返回一个包含 MessageID 的 SendResult,以便调用方追踪。
|
2025-11-05 22:22:46 +08:00
|
|
|
|
Send(ctx context.Context, address string, payload []byte) (*SendResult, error)
|
2025-09-26 22:50:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SendResult 包含了 SendGo 方法成功执行后返回的结果。
|
|
|
|
|
|
type SendResult struct {
|
|
|
|
|
|
// MessageID 是通信服务为此次发送分配的唯一标识符。
|
|
|
|
|
|
// 调用方需要保存此 ID,以便后续关联 ACK 等事件。
|
|
|
|
|
|
MessageID string
|
2025-10-20 19:31:19 +08:00
|
|
|
|
|
|
|
|
|
|
// AcknowledgedAt 记录设备确认收到下行消息的时间。
|
|
|
|
|
|
// 并非所有发送实现都会同步返回收到时间
|
|
|
|
|
|
AcknowledgedAt *time.Time
|
|
|
|
|
|
|
|
|
|
|
|
// ReceivedSuccess 表示设备是否成功接收到下行消息。
|
|
|
|
|
|
// 并非所有发送实现都会同步返回是否送达
|
|
|
|
|
|
ReceivedSuccess *bool
|
2025-09-15 18:56:11 +08:00
|
|
|
|
}
|
2025-10-09 23:43:19 +08:00
|
|
|
|
|
|
|
|
|
|
// Listener 用于监听其他设备发送过来的数据
|
|
|
|
|
|
type Listener interface {
|
|
|
|
|
|
// Listen 用于开始监听其他设备发送过来的数据
|
2025-11-05 22:22:46 +08:00
|
|
|
|
Listen(ctx context.Context) error
|
2025-10-09 23:43:19 +08:00
|
|
|
|
|
|
|
|
|
|
// Stop 用于停止监听
|
2025-11-05 22:22:46 +08:00
|
|
|
|
Stop(ctx context.Context) error
|
2025-10-09 23:43:19 +08:00
|
|
|
|
}
|
2025-12-01 14:32:50 +08:00
|
|
|
|
|
|
|
|
|
|
// UpstreamHandler 定义了处理所有来源的上行数据的统一协约。
|
|
|
|
|
|
// 任何实现了上行消息监听的基础设施(如串口、MQTT客户端),都应该在收到消息后调用此接口的实现者。
|
|
|
|
|
|
// 这样,基础设施层只负责“接收和解析”,而将“业务处理”的控制权交给了上层。
|
|
|
|
|
|
type UpstreamHandler interface {
|
|
|
|
|
|
// HandleInstruction 处理来自设备的、已解析为Instruction的业务指令。
|
|
|
|
|
|
HandleInstruction(ctx context.Context, sourceAddr string, instruction *proto.Instruction) error
|
|
|
|
|
|
|
|
|
|
|
|
// HandleStatus 处理非业务指令的设备状态更新,例如信号强度、电量等。
|
|
|
|
|
|
HandleStatus(ctx context.Context, sourceAddr string, status map[string]interface{}) error
|
|
|
|
|
|
|
|
|
|
|
|
// HandleAck 处理对下行指令的确认(ACK)事件。
|
|
|
|
|
|
HandleAck(ctx context.Context, sourceAddr string, deduplicationID string, acknowledged bool, eventTime time.Time) error
|
|
|
|
|
|
}
|