using System; using System.IO; namespace YuncangTrace.LocalService.Services; /// /// 桌面端(用户会话)与后台扫码服务(LocalSystem)**共用的数据目录**。 /// /// - 普通模式:通道 / 扫码记录放 `%LOCALAPPDATA%\YuncangTrace`(和以前一样); /// - 服务模式(设备服务以 Windows 服务方式运行):两边都读写 /// `%ProgramData%\YuncangTrace`,这样退出桌面端、用户注销后服务照样收码上报, /// 重新打开桌面端也能看到服务写的通道状态/扫码记录。 /// /// 服务进程只认 下的文件,桌面端通过 /// / / 与它交互。 /// public static class AgentPaths { /// true = 当前处于「服务模式」(通道/扫码记录走 ProgramData) public static bool MachineMode { get; set; } /// 当前进程是不是后台服务进程(服务进程不回读自己写的文件) public static bool IsServiceProcess { get; set; } /// ProgramData 根:`C:\ProgramData\YuncangTrace` public static string MachineRoot => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "YuncangTrace"); /// 用户数据根:`%LOCALAPPDATA%\YuncangTrace` public static string UserRoot => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "YuncangTrace"); /// 当前生效的数据根(由 决定) public static string DataRoot => MachineMode ? MachineRoot : UserRoot; /// 通道配置(桌面端和服务共用同名文件) public static string ChannelsFile => Path.Combine(DataRoot, "channels.json"); /// 本地扫码记录(桌面端只读展示,服务负责写) public static string RecordsFile => Path.Combine(DataRoot, "scan-records.json"); /// 后台服务程序目录(服务跑自己的副本,避免 MSI 升级时 exe 被占用) public static string AgentDir => Path.Combine(MachineRoot, "agent"); /// 后台服务实际运行的 exe(从安装目录复制过来) public static string AgentExe => Path.Combine(AgentDir, "YuncangTraceAgent.exe"); /// 服务配置(服务器地址 + 长期设备令牌 + NTP 设置),由桌面端写入 public static string ConfigFile => Path.Combine(MachineRoot, "agent.json"); /// /// 服务令牌(访问令牌 + 刷新令牌),由服务在自动续期后更新;桌面端开启服务时也会写一份。 /// 单独放文件是为了避免与桌面端写 agent.json 时互相覆盖(NTP 修改等)。 /// public static string TokenFile => Path.Combine(MachineRoot, "agent-token.json"); /// 服务运行状态(桌面端只读) public static string StatusFile => Path.Combine(MachineRoot, "agent-status.json"); /// 桌面端 → 服务的命令(立即上传 / 清空记录 / 立即校时) public static string CommandFile => Path.Combine(MachineRoot, "agent-command.json"); /// 服务发现到的设备列表(桌面端「扫码通道」页展示用) public static string DiscoveredFile => Path.Combine(MachineRoot, "agent-discovered.json"); /// /// 后台服务的录像成片根目录(`C:\ProgramData\YuncangTrace\video`)。 /// /// 服务进程没有用户桌面、也读不到用户的「录像根目录」设置, /// 所以跨网下载由 worker 落到这里;桌面端要取用时按同一路径访问。 /// public static string VideosRoot => Path.Combine(MachineRoot, "video"); /// /// 开启服务模式时把用户目录里已有的通道/扫码记录搬到 ProgramData(**不覆盖已有文件**)。 /// public static string? MigrateUserDataToMachine() { try { Directory.CreateDirectory(MachineRoot); var copied = new System.Collections.Generic.List(); 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}"; } } /// /// 关闭服务模式时把 ProgramData 里的通道/扫码记录写回用户目录(覆盖), /// 让后续的普通模式继续沿用这些数据。 /// public static string? CopyMachineDataToUser() { try { if (!Directory.Exists(MachineRoot)) return null; var copied = new System.Collections.Generic.List(); 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}"; } } }