From e5a6558eb5905580885ddd845b680b7c8db5168e Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 21 Oct 2026 00:47:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(desktop):=20NTP=20=E6=A0=A1=E6=97=B6?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=90=8C=E6=AD=A5=E6=91=84=E5=83=8F=E5=A4=B4?= =?UTF-8?q?=EF=BC=88IPC=EF=BC=89=E6=97=B6=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 录像机与摄像头各有一块钟:只写录像机的话,画面里烧的 OSD 时间还是旧的 (现场有摄像头比录像机快 13 小时)。校完录像机后逐个通道 SET_TIMECFG, 单通道失败不影响其它通道,结果汇总到该录像机的进度行。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- desktop/NtpWindow.xaml.cs | 76 +++++++++++++++++++++++- desktop/Services/Hik/HikDeviceService.cs | 38 ++++++++++-- 2 files changed, 108 insertions(+), 6 deletions(-) diff --git a/desktop/NtpWindow.xaml.cs b/desktop/NtpWindow.xaml.cs index 36868e1..daca498 100644 --- a/desktop/NtpWindow.xaml.cs +++ b/desktop/NtpWindow.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; @@ -332,9 +333,15 @@ public partial class NtpWindow : Window var diff = (after - DateTime.Now.Add(offset)).TotalSeconds; ok++; - lines[lineIndex] = $"✔ {label}:NTP {config.Server} / {config.IntervalMinutes} 分钟 / " + + var summary = $"✔ {label}:NTP {config.Server} / {config.IntervalMinutes} 分钟 / " + $"{(config.Enabled ? "已启用" : "未启用")};设备时间 {before:HH:mm:ss} → {after:HH:mm:ss}" + $"(与 NTP 差 {diff:+0.0;-0.0} 秒)"; + + // 摄像头(IPC)有**自己的一套时钟**:只写录像机的话,画面里烧的 OSD 还是旧时间 + summary += await SyncCamerasAsync(ntpDevice, credential, nvr, offset, summary, lines, lineIndex); + + lines[lineIndex] = summary; } catch (Exception ex) { @@ -368,6 +375,73 @@ public partial class NtpWindow : Window } } + /// + /// 把这台录像机下的摄像头(IPC)逐个校时 + /// + /// 为什么必须单独做:画面里烧的 OSD 时间是**摄像头自己**的时钟,录到录像机上的时间轴 + /// 用的是录像机的时钟 —— 两者互不相干。现场就有摄像头比录像机快 13 小时的(OSD 一眼就能看出来), + /// 只校录像机是修不好这个的。 + /// + /// 逐个通道 `SET_TIMECFG`;某个通道失败(第三方 IPC / 关闭了校时权限)不影响其它通道。 + /// + /// 当前这台录像机的结果行(进度直接改这一行,避免刷屏) + /// 追加到结果行末尾的文案 + private async Task SyncCamerasAsync(HikDeviceService device, NvrCredential credential, ApiNvr nvr, + TimeSpan offset, string baseSummary, List lines, int lineIndex) + { + IReadOnlyList cameras; + try + { + cameras = await _api.CamerasOfNvrAsync(_session.Token, _subjectId, nvr.Id); + } + catch (Exception ex) + { + AppLog.Write($"[ntp] 取摄像头列表失败({nvr.Name}):{ex.Message}"); + return $";摄像头列表取不到({ex.Message})"; + } + + if (cameras.Count == 0) return string.Empty; + + var done = 0; + var failed = new List(); + + for (var i = 0; i < cameras.Count; i++) + { + var camera = cameras[i]; + lines[lineIndex] = $"{baseSummary};摄像头校时 {i}/{cameras.Count}…"; + ShowNvrResult(lines); + + try + { + // SDK 调用是同步阻塞的(每个通道 1~3 秒),放线程池避免界面卡住 + await Task.Run(() => device.SetChannelTime(credential.Host, credential.Port, + credential.Username, credential.Password, camera.ChannelNo, DateTime.Now.Add(offset))); + + done++; + AppLog.Write($"[ntp] 摄像头校时成功:{nvr.Name} 通道{camera.ChannelNo}({camera.Display})"); + } + catch (Exception ex) + { + failed.Add($"{camera.Display}(通道{camera.ChannelNo}):{ex.Message}"); + AppLog.Write($"[ntp] 摄像头校时失败:{nvr.Name} 通道{camera.ChannelNo}({camera.Display}):{ex.Message}"); + } + } + + var suffix = $";摄像头 {done}/{cameras.Count} 已校时"; + if (failed.Count > 0) + { + suffix += $"({failed.Count} 个失败:{string.Join(";", failed.Take(3))}" + + $"{(failed.Count > 3 ? " 等" : string.Empty)})"; + } + + lines[lineIndex] = baseSummary + suffix; + ShowNvrResult(lines); + AppLog.Write($"[ntp] 摄像头校时完成:{nvr.Name} {done}/{cameras.Count}" + + (failed.Count > 0 ? ";失败:" + string.Join(";", failed) : string.Empty)); + + return suffix; + } + private void ShowNvrResult(List lines) => NvrResultBox.Text = string.Join(Environment.NewLine, lines); diff --git a/desktop/Services/Hik/HikDeviceService.cs b/desktop/Services/Hik/HikDeviceService.cs index 8a88fe9..3a558fd 100644 --- a/desktop/Services/Hik/HikDeviceService.cs +++ b/desktop/Services/Hik/HikDeviceService.cs @@ -181,6 +181,18 @@ public sealed class HikDeviceService : IDisposable /// 两边不一致时(现场实测本机慢了 9 秒),录像会整体错位,短回放窗口会直接看不到面单。 /// public DateTime GetDeviceTime(string host, int port, string userName, string password) + => GetTime(host, port, userName, password, 0); + + /// + /// 读某个**摄像头自己**的时间(GET_TIMECFG 带通道号)。 + /// + /// 摄像头画面里烧的 OSD 时间是它自己的时钟,跟录像机是两套时间 —— + /// 校时只写录像机的话,OSD 依旧是旧时间。 + /// + public DateTime GetChannelTime(string host, int port, string userName, string password, int channel) + => GetTime(host, port, userName, password, channel); + + private DateTime GetTime(string host, int port, string userName, string password, int channel) { Validate(host, port, userName, password); @@ -193,11 +205,12 @@ public sealed class HikDeviceService : IDisposable { Marshal.Copy(new byte[TimeCfgBufferSize], 0, buffer, TimeCfgBufferSize); - if (!HikSdk.NET_DVR_GetDVRConfig(userId, HikSdk.NET_DVR_GET_TIMECFG, 0, + if (!HikSdk.NET_DVR_GetDVRConfig(userId, HikSdk.NET_DVR_GET_TIMECFG, channel, buffer, TimeCfgBufferSize, out _)) { var err = HikSdk.NET_DVR_GetLastError(); - throw new HikException(err, $"读取设备时间失败:{HikSdk.DescribeError(err)}"); + var who = channel <= 0 ? "设备" : $"通道{channel}"; + throw new HikException(err, $"读取{who}时间失败:{HikSdk.DescribeError(err)}"); } var year = Marshal.ReadInt32(buffer, 0); @@ -223,6 +236,18 @@ public sealed class HikDeviceService : IDisposable /// 用于「设置 NVR 的 NTP 后立即生效」:先把 NTP 配置写进去,再直接把设备时间校准到 NTP 时间。 /// public void SetDeviceTime(string host, int port, string userName, string password, DateTime time) + => SetTime(host, port, userName, password, 0, time); + + /// + /// 写某个**摄像头自己**的时间(SET_TIMECFG 带通道号,录像机转发给 IPC)。 + /// + /// 只有摄影头像这样校过,画面里的 OSD 时间才会和录像机对齐; + /// 部分前端设备会拒绝校时(第三方 / 关了校时权限),此时抛 。 + /// + public void SetChannelTime(string host, int port, string userName, string password, int channel, DateTime time) + => SetTime(host, port, userName, password, channel, time); + + private void SetTime(string host, int port, string userName, string password, int channel, DateTime time) { Validate(host, port, userName, password); @@ -235,10 +260,11 @@ public sealed class HikDeviceService : IDisposable { Marshal.StructureToPtr(sdkTime, buffer, false); - if (!HikSdk.NET_DVR_SetDVRConfig(userId, HikSdk.NET_DVR_SET_TIMECFG, 0, buffer, (uint)size)) + if (!HikSdk.NET_DVR_SetDVRConfig(userId, HikSdk.NET_DVR_SET_TIMECFG, channel, buffer, (uint)size)) { var err = HikSdk.NET_DVR_GetLastError(); - throw new HikException(err, $"设置设备时间失败:{HikSdk.DescribeError(err)}"); + var who = channel <= 0 ? "设备" : $"通道{channel}"; + throw new HikException(err, $"设置{who}时间失败:{HikSdk.DescribeError(err)}"); } } finally @@ -247,7 +273,9 @@ public sealed class HikDeviceService : IDisposable } }); - AppLog.Write($"[hik] 设备时间已写入 {host}:{port}:{time:yyyy-MM-dd HH:mm:ss}"); + AppLog.Write(channel <= 0 + ? $"[hik] 设备时间已写入 {host}:{port}:{time:yyyy-MM-dd HH:mm:ss}" + : $"[hik] 通道{channel} 时间已写入 {host}:{port}:{time:yyyy-MM-dd HH:mm:ss}"); } ///