diff --git a/internal/config/config.go b/internal/config/config.go index 9e45038..c471d36 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,6 +20,7 @@ type Config struct { MediaRepoPath string `json:"media_repo_path,omitempty"` // explicit import-only source; not used for runtime rendering AlarmRetentionDays int `json:"alarm_retention_days"` // auto-delete alarms older than N days, 0=keep forever CompanyName string `json:"company_name,omitempty"` + TimeSyncThresholdSec int `json:"time_sync_threshold_sec"` DeviceAliases map[string]string `json:"device_aliases,omitempty"` path string } diff --git a/internal/service/task.go b/internal/service/task.go index 48ba0c9..cf90511 100644 --- a/internal/service/task.go +++ b/internal/service/task.go @@ -442,7 +442,11 @@ func (s *TaskService) executeOnDevice(task *models.Task, did string) { if diff < 0 { diff = -diff } - if diff <= 5 { + threshold := int64(s.cfg.TimeSyncThresholdSec) + if threshold <= 0 { + threshold = 5 + } + if diff <= threshold { s.updateDeviceStatus(task.ID, did, models.TaskSuccess, 1.0, "时间已同步") s.appendAuditLog(task, did, models.TaskSuccess, "") return diff --git a/internal/web/ui.go b/internal/web/ui.go index fb906f4..e09ad3e 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -117,6 +117,7 @@ type PageData struct { SystemAlarmRetention int SystemConcurrency int SystemCompanyName string + TimeSyncThreshold int AlarmFilterDevices []string AlarmFilterRuleTypes []string AlarmDeviceNames map[string]string @@ -4400,6 +4401,10 @@ func (u *UI) renderSystemPage(w http.ResponseWriter, r *http.Request, status int if u.cfg != nil { data.SystemAlarmRetention = u.cfg.AlarmRetentionDays data.SystemCompanyName = u.cfg.CompanyName + data.TimeSyncThreshold = u.cfg.TimeSyncThresholdSec + if data.TimeSyncThreshold <= 0 { + data.TimeSyncThreshold = 5 + } } u.render(w, r, "system", data) } @@ -4422,6 +4427,10 @@ func (u *UI) actionSystemConfig(w http.ResponseWriter, r *http.Request) { u.cfg.CompanyName = company changed = true } + if tval, err := strconv.Atoi(strings.TrimSpace(r.FormValue("time_sync_threshold_sec"))); err == nil && tval >= 1 && u.cfg != nil { + u.cfg.TimeSyncThresholdSec = tval + changed = true + } if changed && u.cfg != nil && strings.TrimSpace(u.configPath) != "" { if data, err := json.MarshalIndent(u.cfg, "", " "); err == nil { os.WriteFile(u.configPath, data, 0o644) @@ -5292,6 +5301,10 @@ func (u *UI) deviceOverviewPageData(r *http.Request, selectedIDs []string, errMs // Check time drift now := time.Now().UnixMilli() + threshold := int64(5) // default + if u.cfg != nil && u.cfg.TimeSyncThresholdSec > 0 { + threshold = int64(u.cfg.TimeSyncThresholdSec) + } for i := range rows { dev := rows[i].Device if dev == nil || !dev.Online || u.agent == nil { @@ -5313,6 +5326,12 @@ func (u *UI) deviceOverviewPageData(r *http.Request, selectedIDs []string, errMs } } + // Common threshold for time drift display + if threshold <= 0 { + threshold = 5 + } + // (threshold used in template comparison: gt .TimeDriftSec $threshold) + data := PageData{ Title: "设备管理", Devices: devices, diff --git a/internal/web/ui/templates/devices.html b/internal/web/ui/templates/devices.html index 01a497b..704ef07 100644 --- a/internal/web/ui/templates/devices.html +++ b/internal/web/ui/templates/devices.html @@ -47,7 +47,7 @@ {{if $allOffline}} {{end}} - {{$drift := false}}{{range .DeviceRows}}{{if and (gt .TimeDriftSec 5) (hasString $.SelectedDeviceIDs .Device.DeviceID)}}{{$drift = true}}{{end}}{{end}} + {{$drift := false}}{{range .DeviceRows}}{{if and (gt .TimeDriftSec $.TimeSyncThreshold) (hasString $.SelectedDeviceIDs .Device.DeviceID)}}{{$drift = true}}{{end}}{{end}} {{if $drift}} {{end}} @@ -97,7 +97,7 @@ 未知 {{end}} {{if .IntegrationStale}}需重新下发{{end}} - {{if gt .TimeDriftSec 5}}⏱ 偏差{{.TimeDriftSec}}秒{{end}} + {{if gt .TimeDriftSec $.TimeSyncThreshold}}⏱ 偏差{{.TimeDriftSec}}秒{{end}} diff --git a/internal/web/ui/templates/system.html b/internal/web/ui/templates/system.html index 2dc5258..0176f2a 100644 --- a/internal/web/ui/templates/system.html +++ b/internal/web/ui/templates/system.html @@ -23,6 +23,13 @@ +