You've already forked VptPassiveAdapter
refactor(continuity): 优化连续性检查逻辑和时间格式
- 将时间格式从 ISO 8601 修改为标准日期时间格式 - 修复连续性检查中 Gaps 字段的空数组初始化问题 - 重构连续性检查循环逻辑,启动时立即执行一次检查 - 提取连续性检查逻辑到独立的 performContinuityCheck 函数 - 优化检查时间范围的判断逻辑
This commit is contained in:
@@ -156,8 +156,8 @@ func reportSingleDevice(ctx context.Context, result dto.ContinuityCheckResult) e
|
|||||||
|
|
||||||
// convertToReportRequest 将内部检查结果转换为接口请求格式
|
// convertToReportRequest 将内部检查结果转换为接口请求格式
|
||||||
func convertToReportRequest(result dto.ContinuityCheckResult) dto.ContinuityReportRequest {
|
func convertToReportRequest(result dto.ContinuityCheckResult) dto.ContinuityReportRequest {
|
||||||
// 时间格式:ISO 8601
|
// 时间格式
|
||||||
const timeFormat = "2006-01-02T15:04:05"
|
const timeFormat = "2006-01-02 15:04:05"
|
||||||
|
|
||||||
request := dto.ContinuityReportRequest{
|
request := dto.ContinuityReportRequest{
|
||||||
DeviceNo: result.DeviceNo,
|
DeviceNo: result.DeviceNo,
|
||||||
@@ -182,6 +182,8 @@ func convertToReportRequest(result dto.ContinuityCheckResult) dto.ContinuityRepo
|
|||||||
GapEndTime: gap.NextStartTime.Format(timeFormat),
|
GapEndTime: gap.NextStartTime.Format(timeFormat),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
request.Gaps = []dto.ContinuityReportGap{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return request
|
return request
|
||||||
|
|||||||
@@ -47,18 +47,28 @@ func RunContinuityCheckLoop(ctx context.Context) {
|
|||||||
zap.Int("checkStartHour", checkStartHour),
|
zap.Int("checkStartHour", checkStartHour),
|
||||||
zap.Int("checkEndHour", checkEndHour))
|
zap.Int("checkEndHour", checkEndHour))
|
||||||
|
|
||||||
|
// 启动时立即执行一次检查
|
||||||
|
performContinuityCheck(ctx)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
logger.Info("连续性检查循环已停止")
|
logger.Info("连续性检查循环已停止")
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
|
performContinuityCheck(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// performContinuityCheck 执行一次连续性检查
|
||||||
|
func performContinuityCheck(ctx context.Context) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
// 只在9点到18点之间执行检查
|
// 只在9点到18点之间执行检查
|
||||||
currentHour := now.Hour()
|
currentHour := now.Hour()
|
||||||
if currentHour < checkStartHour || currentHour >= checkEndHour {
|
if currentHour < checkStartHour || currentHour >= checkEndHour {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime := now.Add(-time.Duration(rangeStartMinutes) * time.Minute)
|
startTime := now.Add(-time.Duration(rangeStartMinutes) * time.Minute)
|
||||||
@@ -73,8 +83,6 @@ func RunContinuityCheckLoop(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckAllDevicesContinuity 检查所有设备的连续性
|
// CheckAllDevicesContinuity 检查所有设备的连续性
|
||||||
func CheckAllDevicesContinuity(ctx context.Context, startTime, endTime time.Time) []dto.ContinuityCheckResult {
|
func CheckAllDevicesContinuity(ctx context.Context, startTime, endTime time.Time) []dto.ContinuityCheckResult {
|
||||||
|
|||||||
Reference in New Issue
Block a user