Files
pig-farm-controller/internal/domain/device/device_service.go

95 lines
3.6 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{} // 响应内容
}
// OtaService 定义了设备 OTA 升级相关的业务逻辑。
type OtaService interface {
// StartUpgrade 用于启动一个 OTA 升级任务。
// areaControllerID: 目标区域主控的设备 ID。
// firmwarePath: 新固件文件所在的临时目录的绝对路径。
// 返回创建的 OTA 任务 ID 和可能发生的错误。
StartUpgrade(ctx context.Context, areaControllerID uint32, firmwarePath string) (uint32, error)
// GetUpgradeProgress 用于查询指定 OTA 任务的进度。
// taskID: 要查询的 OTA 任务 ID。
// 返回 OTA 任务的已执行步骤数和总步骤数和当前阶段和可能发生的错误。
GetUpgradeProgress(ctx context.Context, taskID uint32) (executed, total uint32, CurrentStage models.OTATaskStatus, err error)
// StopUpgrade 用于请求停止一个正在进行的 OTA 升级任务。
// taskID: 要停止的 OTA 任务 ID。
// 注意:这只是一个尽力而为的操作。如果设备已开始下载或处理,可能无法立即中止。
StopUpgrade(ctx context.Context, taskID uint32) error
}