把海康取流从扫码服务主进程拆到 --hik-worker 子进程,原生 SDK 崩溃
不再连带扫码收码;新增局域网询问-应答与跨网下载通道。
隔离与守护
- 新增 HikWorker(子进程):独占 HCNetSDK 与 NVR 会话池
- 新增 HikWorkerSupervisor(主进程):懒启动、管道通信、
5 分钟内失败 3 次熔断 10 分钟
- 管道协议为 [4 字节长度][JSON],进程退出即知,不占端口
- ServiceInstaller 复制 native(HCNetSDK/ffmpeg),先删后拷,
避免 SDK 升级残留旧 DLL;安装时放行 TCP 17311 / UDP 17310
局域网询问-应答(不是主动公告)
- UDP 17310 收询问,只回同 subjectId;带了 nvrHosts 时先实测可达,
连不上就不应答,让查询机继续走本机
- 应答带 TCP 17311,查询机一次连接即可判定是否同 LAN
跨网下载
- TCP 17311 提供 /health、/probe、/download、/download/{id}
- 除 /health 外均要求 X-Edge-Subject 且与配置主体一致
- 成片在传输完成后立即删除,另有 done 10 分钟 / failed 15 分钟
TTL 兜底,避免现场磁盘被临时文件吃满
桌面端选路
- DownloadAsync 先探 NVR 8000(600ms):通则沿用原有本机直连,
不通才广播找取流服务,取回后落到本机录像目录
- DownloadByPlanAsync 拆分:不查库、不需要用户令牌,
供只有设备令牌的服务端复用
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
130 lines
5.9 KiB
C#
130 lines
5.9 KiB
C#
using System;
|
||
using System.IO;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>
|
||
/// 桌面端(用户会话)与后台扫码服务(LocalSystem)**共用的数据目录**。
|
||
///
|
||
/// - 普通模式:通道 / 扫码记录放 `%LOCALAPPDATA%\YuncangTrace`(和以前一样);
|
||
/// - 服务模式(设备服务以 Windows 服务方式运行):两边都读写
|
||
/// `%ProgramData%\YuncangTrace`,这样退出桌面端、用户注销后服务照样收码上报,
|
||
/// 重新打开桌面端也能看到服务写的通道状态/扫码记录。
|
||
///
|
||
/// 服务进程只认 <see cref="DataRoot"/> 下的文件,桌面端通过
|
||
/// <see cref="ConfigFile"/> / <see cref="StatusFile"/> / <see cref="CommandFile"/> 与它交互。
|
||
/// </summary>
|
||
public static class AgentPaths
|
||
{
|
||
/// <summary>true = 当前处于「服务模式」(通道/扫码记录走 ProgramData)</summary>
|
||
public static bool MachineMode { get; set; }
|
||
|
||
/// <summary>当前进程是不是后台服务进程(服务进程不回读自己写的文件)</summary>
|
||
public static bool IsServiceProcess { get; set; }
|
||
|
||
/// <summary>ProgramData 根:`C:\ProgramData\YuncangTrace`</summary>
|
||
public static string MachineRoot => Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "YuncangTrace");
|
||
|
||
/// <summary>用户数据根:`%LOCALAPPDATA%\YuncangTrace`</summary>
|
||
public static string UserRoot => Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "YuncangTrace");
|
||
|
||
/// <summary>当前生效的数据根(由 <see cref="MachineMode"/> 决定)</summary>
|
||
public static string DataRoot => MachineMode ? MachineRoot : UserRoot;
|
||
|
||
/// <summary>通道配置(桌面端和服务共用同名文件)</summary>
|
||
public static string ChannelsFile => Path.Combine(DataRoot, "channels.json");
|
||
|
||
/// <summary>本地扫码记录(桌面端只读展示,服务负责写)</summary>
|
||
public static string RecordsFile => Path.Combine(DataRoot, "scan-records.json");
|
||
|
||
/// <summary>后台服务程序目录(服务跑自己的副本,避免 MSI 升级时 exe 被占用)</summary>
|
||
public static string AgentDir => Path.Combine(MachineRoot, "agent");
|
||
|
||
/// <summary>后台服务实际运行的 exe(从安装目录复制过来)</summary>
|
||
public static string AgentExe => Path.Combine(AgentDir, "YuncangTraceAgent.exe");
|
||
|
||
/// <summary>服务配置(服务器地址 + 长期设备令牌 + NTP 设置),由桌面端写入</summary>
|
||
public static string ConfigFile => Path.Combine(MachineRoot, "agent.json");
|
||
|
||
/// <summary>
|
||
/// 服务令牌(访问令牌 + 刷新令牌),由服务在自动续期后更新;桌面端开启服务时也会写一份。
|
||
/// 单独放文件是为了避免与桌面端写 agent.json 时互相覆盖(NTP 修改等)。
|
||
/// </summary>
|
||
public static string TokenFile => Path.Combine(MachineRoot, "agent-token.json");
|
||
|
||
/// <summary>服务运行状态(桌面端只读)</summary>
|
||
public static string StatusFile => Path.Combine(MachineRoot, "agent-status.json");
|
||
|
||
/// <summary>桌面端 → 服务的命令(立即上传 / 清空记录 / 立即校时)</summary>
|
||
public static string CommandFile => Path.Combine(MachineRoot, "agent-command.json");
|
||
|
||
/// <summary>服务发现到的设备列表(桌面端「扫码通道」页展示用)</summary>
|
||
public static string DiscoveredFile => Path.Combine(MachineRoot, "agent-discovered.json");
|
||
|
||
/// <summary>
|
||
/// 后台服务的录像成片根目录(`C:\ProgramData\YuncangTrace\video`)。
|
||
///
|
||
/// 服务进程没有用户桌面、也读不到用户的「录像根目录」设置,
|
||
/// 所以跨网下载由 worker 落到这里;桌面端要取用时按同一路径访问。
|
||
/// </summary>
|
||
public static string VideosRoot => Path.Combine(MachineRoot, "video");
|
||
|
||
/// <summary>
|
||
/// 开启服务模式时把用户目录里已有的通道/扫码记录搬到 ProgramData(**不覆盖已有文件**)。
|
||
/// </summary>
|
||
public static string? MigrateUserDataToMachine()
|
||
{
|
||
try
|
||
{
|
||
Directory.CreateDirectory(MachineRoot);
|
||
|
||
var copied = new System.Collections.Generic.List<string>();
|
||
foreach (var name in new[] { "channels.json", "scan-records.json" })
|
||
{
|
||
var from = Path.Combine(UserRoot, name);
|
||
var to = Path.Combine(MachineRoot, name);
|
||
if (!File.Exists(from) || File.Exists(to)) continue;
|
||
File.Copy(from, to, overwrite: false);
|
||
copied.Add(name);
|
||
}
|
||
|
||
return copied.Count == 0 ? null : $"已迁移本地数据:{string.Join("、", copied)}";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return $"迁移本地数据失败:{ex.Message}";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭服务模式时把 ProgramData 里的通道/扫码记录写回用户目录(覆盖),
|
||
/// 让后续的普通模式继续沿用这些数据。
|
||
/// </summary>
|
||
public static string? CopyMachineDataToUser()
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(MachineRoot)) return null;
|
||
|
||
var copied = new System.Collections.Generic.List<string>();
|
||
foreach (var name in new[] { "channels.json", "scan-records.json" })
|
||
{
|
||
var from = Path.Combine(MachineRoot, name);
|
||
var to = Path.Combine(UserRoot, name);
|
||
if (!File.Exists(from)) continue;
|
||
Directory.CreateDirectory(UserRoot);
|
||
File.Copy(from, to, overwrite: true);
|
||
copied.Add(name);
|
||
}
|
||
|
||
return copied.Count == 0 ? null : $"已回写本地数据:{string.Join("、", copied)}";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return $"回写本地数据失败:{ex.Message}";
|
||
}
|
||
}
|
||
}
|