Files
pig-farm-controller/internal/domain/device/device_service.go
2025-12-03 15:12:43 +08:00

76 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package device
import (
"context"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/proto"
)
// 设备行为
type DeviceAction string
var (
DeviceActionStart DeviceAction = "start" // 启动
DeviceActionStop DeviceAction = "stop" // 停止
)
// 指令类型
type Method string
var (
MethodSwitch Method = "switch" // 启停指令
)
// SendOptions 包含了发送通用指令时的可选参数。
type SendOptions struct {
// NotTrackable 如果为 true则指示本次发送无需被追踪。
// 这将阻止系统为本次发送创建 device_command_logs 记录。
// 默认为 false即需要追踪。
NotTrackable bool
}
// SendOption 是一个函数类型,用于修改 SendOptions。
// 这是实现 "Functional Options Pattern" 的核心。
type SendOption func(*SendOptions)
// WithoutTracking 是一个公开的选项函数,用于明确指示本次发送无需追踪。
// 调用方在发送 Ping 等无需响应确认的指令时,应使用此选项。
func WithoutTracking() SendOption {
return func(opts *SendOptions) {
opts.NotTrackable = true
}
}
// DeviceOperator 提供了对单个或多个设备进行具体操作的接口,
// 如开关、触发采集等。它通常用于响应用户的直接指令或执行具体的业务任务。
type DeviceOperator interface {
// Switch 用于切换指定设备的状态, 比如启动和停止。
Switch(ctx context.Context, device *models.Device, action DeviceAction) error
// Collect 用于发起对指定区域主控下的多个设备的批量采集请求。
Collect(ctx context.Context, areaControllerID uint32, devicesToCollect []*models.Device) error
}
// DeviceCommunicator 抽象了与设备进行底层通信的能力。
// 它负责将一个标准的指令载荷发送到指定的区域主控。
type DeviceCommunicator interface {
// Send 是一个通用的发送方法,它负责将载荷包装、序列化、
// 调用底层发送器,并默认记录下行命令日志。
Send(ctx context.Context, areaControllerID uint32, payload proto.InstructionPayload, opts ...SendOption) error
}
// 设备操作指令通用结构(最外层)
type DeviceRequest struct {
MessageID int // 消息ID, 用于后续匹配响应
Method string // 这是什么指令
Data interface{} // 指令参数
}
// 设备操作指令通用响应结构(最外层)
type DeviceResponse struct {
MessageID int // 消息ID, 用于匹配这是哪一个请求的响应
Message string
Data interface{} // 响应内容
}