using CoreAgent.Domain.Interfaces.Network; using CoreAgent.Domain.Models.Network; using CoreAgent.Domain.Models.System; namespace CoreAgent.Domain.Contexts; /// /// 蜂窝网络领域上下文 /// public class CellularNetworkContext { private static readonly Lazy _instance = new(() => new CellularNetworkContext()); private readonly object _lock = new(); private readonly Dictionary _networkStates = new(); private NetworkCommandConfig _networkCommandConfig; private string NeConfigKey; public CancellationTokenSource token =new CancellationTokenSource(); /// /// 获取单例实例 /// public static CellularNetworkContext Instance => _instance.Value; private CellularNetworkContext() { } /// /// 设置网络命令配置 /// /// 网络命令配置 public void SetNetworkCommandConfig(NetworkCommandConfig config) { lock (_lock) { _networkCommandConfig = config ?? throw new ArgumentNullException(nameof(config)); } } /// /// 获取网络命令配置 /// /// 网络命令配置 public NetworkCommandConfig GetNetworkCommandConfig() { lock (_lock) { return _networkCommandConfig ?? throw new InvalidOperationException("网络命令配置未初始化"); } } public string SetNeConfigKey(string key) { lock (_lock) { return NeConfigKey= key ?? throw new ArgumentNullException(nameof(key)); } } public string GetNeConfigKey() { lock (_lock) { return NeConfigKey; } } /// /// 获取指定类型的命令配置 /// /// 命令类型 /// 命令配置列表 public List GetCommandsByType(NetworkCommandType type) { lock (_lock) { return _networkCommandConfig?.GetCommandsByType(type) ?? new List(); } } /// /// 获取所有命令类型 /// /// 命令类型数组 public NetworkCommandType[] GetCommandTypes() { lock (_lock) { return _networkCommandConfig?.GetCommandTypes() ?? Array.Empty(); } } /// /// 获取或创建网络状态 /// /// 网络接口名称 /// 网络状态 public CellularNetworkState GetOrCreateNetworkState(string NeConfigKey) { lock (_lock) { if (!_networkStates.TryGetValue(NeConfigKey, out var state)) { state = new CellularNetworkState(NeConfigKey); _networkStates[NeConfigKey] = state; } return state; } } /// /// 移除网络状态 /// /// 网络接口名称 public void RemoveNetworkState(string NeConfigKey) { lock (_lock) { _networkStates.Remove(NeConfigKey); } } /// /// 获取所有网络状态 /// /// 所有网络状态的集合 public IReadOnlyCollection GetAllNetworkStates() { lock (_lock) { return _networkStates.Values.ToList().AsReadOnly(); } } } /// /// 蜂窝网络状态 /// public class CellularNetworkState { /// /// 网络接口名称 /// public string NeConfigKey { get; } /// /// 是否已初始化 /// public bool IsInitialized { get; private set; } /// /// 最后启动时间 /// public DateTime? LastStartTime { get; private set; } /// /// 最后停止时间 /// public DateTime? LastStopTime { get; private set; } /// /// 当前运行状态 /// public NetworkStatus CurrentStatus { get; private set; } /// /// 当前网络类型 /// public NetworkType CurrentNetworkType { get; private set; } public CellularNetworkState(string _NeConfigKey) { NeConfigKey = _NeConfigKey; IsInitialized = false; CurrentStatus = NetworkStatus.Unknown; } /// /// 更新网络状态 /// public void UpdateStatus(NetworkStatus status) { CurrentStatus = status; } /// /// 更新网络类型 /// public void UpdateNetworkType(NetworkType type) { CurrentNetworkType = type; } /// /// 标记为已启动 /// public void MarkAsStarted() { IsInitialized = true; LastStartTime = DateTime.Now; CurrentStatus = NetworkStatus.Connected; } /// /// 标记为已停止 /// public void MarkAsStopped() { IsInitialized = false; LastStopTime = DateTime.Now; CurrentStatus = NetworkStatus.Disconnected; CurrentNetworkType = NetworkType.Unknown; } }