ota升级超时检查任务

This commit is contained in:
2025-12-03 17:34:38 +08:00
parent a1deb0011b
commit 9d9b5f801f
9 changed files with 267 additions and 18 deletions

View File

@@ -54,6 +54,9 @@ type Config struct {
// AI AI服务配置
AI AIConfig `yaml:"ai"`
// OTA OTA升级配置
OTA OTAConfig `yaml:"ota"`
}
// AppConfig 代表应用基础配置
@@ -248,6 +251,16 @@ type Gemini struct {
Timeout int `yaml:"timeout"` // AI 请求超时时间 (秒)
}
// OTAConfig 代表 OTA 升级配置
type OTAConfig struct {
// DefaultTimeoutSeconds 升级任务的全局超时时间(秒)
DefaultTimeoutSeconds int `yaml:"default_timeout_seconds"`
// DefaultRequestTimeoutSeconds 等待设备响应的单次请求超时时间(秒)
DefaultRequestTimeoutSeconds int `yaml:"default_request_timeout_seconds"`
// DefaultRetryCount 默认的固件块请求重试次数
DefaultRetryCount int `yaml:"default_retry_count"`
}
// NewConfig 创建并返回一个新的配置实例
func NewConfig() *Config {
// 默认值可以在这里设置,但我们优先使用配置文件中的值

View File

@@ -42,15 +42,16 @@ const (
type TaskType string
const (
TaskPlanAnalysis TaskType = "计划分析" // 解析Plan的Task列表并添加到待执行队列的特殊任务
TaskTypeWaiting TaskType = "等待" // 等待任务
TaskTypeReleaseFeedWeight TaskType = "下料" // 下料口释放指定重量任务
TaskTypeFullCollection TaskType = "全量采集" // 新增的全量采集任务
TaskTypeHeartbeat TaskType = "心跳检测" // 区域主控心跳检测任务
TaskTypeAlarmNotification TaskType = "告警通知" // 告警通知任务
TaskTypeNotificationRefresh TaskType = "通知刷新" // 通知刷新任务
TaskTypeDeviceThresholdCheck TaskType = "设备阈值检查" // 设备阈值检查任务
TaskTypeAreaCollectorThresholdCheck TaskType = "区域阈值检查" // 区域阈值检查任务
TaskPlanAnalysis TaskType = "计划分析" // 解析Plan的Task列表并添加到待执行队列的特殊任务
TaskTypeWaiting TaskType = "等待" // 等待任务
TaskTypeReleaseFeedWeight TaskType = "下料" // 下料口释放指定重量任务
TaskTypeFullCollection TaskType = "全量采集" // 新增的全量采集任务
TaskTypeHeartbeat TaskType = "心跳检测" // 区域主控心跳检测任务
TaskTypeAlarmNotification TaskType = "告警通知" // 告警通知任务
TaskTypeNotificationRefresh TaskType = "通知刷新" // 通知刷新任务
TaskTypeDeviceThresholdCheck TaskType = "设备阈值检查" // 设备阈值检查任务
TaskTypeAreaCollectorThresholdCheck TaskType = "区域阈值检查" // 区域阈值检查任务
TaskTypeOTACheck TaskType = "OTA升级检查任务" // OTA升级超时检查任务
)
// -- Task Parameters --

View File

@@ -0,0 +1,52 @@
package repository
import (
"context"
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"gorm.io/gorm"
)
// OtaRepository 定义了与 OTA 升级任务相关的数据库操作接口。
type OtaRepository interface {
// FindTasksByStatusesAndCreationTime 根据状态列表和创建时间查找任务。
FindTasksByStatusesAndCreationTime(ctx context.Context, statuses []models.OTATaskStatus, createdBefore time.Time) ([]*models.OTATask, error)
// Update 更新单个 OTA 任务。
Update(ctx context.Context, task *models.OTATask) error
}
// gormOtaRepository 是 OtaRepository 的 GORM 实现
type gormOtaRepository struct {
ctx context.Context
db *gorm.DB
}
// NewGormOtaRepository 创建一个新的 OtaRepository GORM 实现实例
func NewGormOtaRepository(ctx context.Context, db *gorm.DB) OtaRepository {
return &gormOtaRepository{
ctx: ctx,
db: db,
}
}
// FindTasksByStatusesAndCreationTime 实现了根据状态和创建时间查找任务的逻辑。
func (r *gormOtaRepository) FindTasksByStatusesAndCreationTime(ctx context.Context,
statuses []models.OTATaskStatus,
createdBefore time.Time,
) ([]*models.OTATask, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTasksByStatusesAndCreationTime")
var tasks []*models.OTATask
err := r.db.WithContext(repoCtx).
Where("status IN ? AND created_at < ?", statuses, createdBefore).
Find(&tasks).Error
return tasks, err
}
// Update 实现了更新单个 OTA 任务的逻辑。
func (r *gormOtaRepository) Update(ctx context.Context, task *models.OTATask) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Update")
return r.db.WithContext(repoCtx).Save(task).Error
}

View File

@@ -374,12 +374,14 @@ func (x *Pong) GetFirmwareVersion() string {
// PrepareUpdateReq: 平台发送给设备,通知设备准备开始 OTA 升级 (下行)
type PrepareUpdateReq struct {
state protoimpl.MessageState `protogen:"open.v1"`
Version string `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"` // 新固件版本号
TaskId uint32 `protobuf:"varint,2,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` // 升级任务唯一ID
ManifestMd5 string `protobuf:"bytes,3,opt,name=manifest_md5,json=manifestMd5,proto3" json:"manifest_md5,omitempty"` // 清单文件的 MD5 校验和,用于设备初步校验清单文件完整性
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
state protoimpl.MessageState `protogen:"open.v1"`
Version string `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"` // 新固件版本号
TaskId uint32 `protobuf:"varint,2,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` // 升级任务唯一ID
ManifestMd5 string `protobuf:"bytes,3,opt,name=manifest_md5,json=manifestMd5,proto3" json:"manifest_md5,omitempty"` // 清单文件的 MD5 校验和,用于设备初步校验清单文件完整性
RetryCount uint32 `protobuf:"varint,4,opt,name=retry_count,json=retryCount,proto3" json:"retry_count,omitempty"` // 建议的重试次数
RequestTimeoutSeconds uint32 `protobuf:"varint,5,opt,name=request_timeout_seconds,json=requestTimeoutSeconds,proto3" json:"request_timeout_seconds,omitempty"` // 建议的单次请求超时时间
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *PrepareUpdateReq) Reset() {
@@ -433,6 +435,20 @@ func (x *PrepareUpdateReq) GetManifestMd5() string {
return ""
}
func (x *PrepareUpdateReq) GetRetryCount() uint32 {
if x != nil {
return x.RetryCount
}
return 0
}
func (x *PrepareUpdateReq) GetRequestTimeoutSeconds() uint32 {
if x != nil {
return x.RequestTimeoutSeconds
}
return 0
}
// RequestFile: 设备向平台请求特定文件 (包括清单文件和固件文件) (上行)
type RequestFile struct {
state protoimpl.MessageState `protogen:"open.v1"`
@@ -842,11 +858,14 @@ const file_device_proto_rawDesc = "" +
"\x06values\x18\x02 \x03(\x02R\x06values\"\x06\n" +
"\x04Ping\"1\n" +
"\x04Pong\x12)\n" +
"\x10firmware_version\x18\x01 \x01(\tR\x0ffirmwareVersion\"h\n" +
"\x10firmware_version\x18\x01 \x01(\tR\x0ffirmwareVersion\"\xc1\x01\n" +
"\x10PrepareUpdateReq\x12\x18\n" +
"\aversion\x18\x01 \x01(\tR\aversion\x12\x17\n" +
"\atask_id\x18\x02 \x01(\rR\x06taskId\x12!\n" +
"\fmanifest_md5\x18\x03 \x01(\tR\vmanifestMd5\"B\n" +
"\fmanifest_md5\x18\x03 \x01(\tR\vmanifestMd5\x12\x1f\n" +
"\vretry_count\x18\x04 \x01(\rR\n" +
"retryCount\x126\n" +
"\x17request_timeout_seconds\x18\x05 \x01(\rR\x15requestTimeoutSeconds\"B\n" +
"\vRequestFile\x12\x17\n" +
"\atask_id\x18\x01 \x01(\rR\x06taskId\x12\x1a\n" +
"\bfilepath\x18\x02 \x01(\tR\bfilepath\"]\n" +

View File

@@ -48,6 +48,8 @@ message PrepareUpdateReq {
string version = 1; // 新固件版本号
uint32 task_id = 2; // 升级任务唯一ID
string manifest_md5 = 3; // 清单文件的 MD5 校验和,用于设备初步校验清单文件完整性
uint32 retry_count = 4; // 建议的重试次数
uint32 request_timeout_seconds = 5; // 建议的单次请求超时时间
}
// RequestFile: 设备向平台请求特定文件 (包括清单文件和固件文件) (上行)