packServer/desktop/Services/AgentPaths.cs

122 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>
/// 开启服务模式时把用户目录里已有的通道/扫码记录搬到 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}";
}
}
}