issue-18 优化代码(只保证编译通过没检查)

This commit is contained in:
2025-09-26 22:50:08 +08:00
parent d9fe1683d2
commit 23b7f66d74
17 changed files with 767 additions and 251 deletions

View File

@@ -73,3 +73,70 @@ func (log *TaskExecutionLog) AfterFind(tx *gorm.DB) (err error) {
}
return
}
// --- 指令与采集 ---
// PendingCollectionStatus 定义了待采集请求的状态
type PendingCollectionStatus string
const (
PendingStatusPending PendingCollectionStatus = "pending" // 请求已发送,等待设备响应
PendingStatusFulfilled PendingCollectionStatus = "fulfilled" // 已收到设备响应并成功处理
PendingStatusTimedOut PendingCollectionStatus = "timed_out" // 请求超时,未收到设备响应
)
// DeviceCommandLog 记录所有“发后即忘”的下行指令日志。
// 这张表主要用于追踪指令是否被网关成功发送 (ack)。
type DeviceCommandLog struct {
// MessageID 是下行消息的唯一标识符。
// 可以是 ChirpStack 的 DeduplicationID 或其他系统生成的ID。
MessageID string `gorm:"primaryKey" json:"message_id"`
// DeviceID 是接收此下行任务的设备的ID。
// 对于 LoRaWAN这通常是区域主控设备的ID。
DeviceID uint `gorm:"not null;index" json:"device_id"`
// SentAt 记录下行任务最初发送的时间。
SentAt time.Time `gorm:"not null" json:"sent_at"`
// AcknowledgedAt 记录设备确认收到下行消息的时间。
// 如果设备未确认,则为零值或 NULL。使用指针类型 *time.Time 允许 NULL 值。
AcknowledgedAt *time.Time `json:"acknowledged_at"`
// ReceivedSuccess 表示设备是否成功接收到下行消息。
// true 表示设备已确认收到false 表示设备未确认收到或下发失败。
ReceivedSuccess bool `gorm:"not null" json:"received_success"`
}
// TableName 自定义 GORM 使用的数据库表名
func (DeviceCommandLog) TableName() string {
return "device_command_log"
}
// PendingCollection 记录所有需要设备响应的“待采集请求”。
// 这是一张状态机表,追踪从请求发送到收到响应的整个生命周期。
type PendingCollection struct {
// CorrelationID 是由平台生成的、在请求和响应之间全局唯一的关联ID作为主键。
CorrelationID string `gorm:"primaryKey"`
// DeviceID 是接收此任务的设备ID
// 对于 LoRaWAN这通常是区域主控设备的ID。
DeviceID uint `gorm:"index"`
// CommandMetadata 存储了此次采集任务对应的设备ID列表顺序与设备响应值的顺序一致。
CommandMetadata UintArray `gorm:"type:bigint[]"`
// Status 是该请求的当前状态,用于状态机管理和超时处理。
Status PendingCollectionStatus `gorm:"index"`
// FulfilledAt 是收到设备响应并成功处理的时间。使用指针以允许 NULL 值。
FulfilledAt *time.Time
// CreatedAt 是 GORM 的标准字段,记录请求创建时间。
CreatedAt time.Time
}
// TableName 自定义 GORM 使用的数据库表名
func (PendingCollection) TableName() string {
return "pending_collections"
}