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; /// /// 获取单例实例 /// 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 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 interfaceName) { lock (_lock) { if (!_networkStates.TryGetValue(interfaceName, out var state)) { state = new CellularNetworkState(interfaceName); _networkStates[interfaceName] = state; } return state; } } /// /// 移除网络状态 /// /// 网络接口名称 public void RemoveNetworkState(string interfaceName) { lock (_lock) { _networkStates.Remove(interfaceName); } } /// /// 获取所有网络状态 /// /// 所有网络状态的集合 public IReadOnlyCollection GetAllNetworkStates() { lock (_lock) { return _networkStates.Values.ToList().AsReadOnly(); } } } /// /// 蜂窝网络状态 /// public class CellularNetworkState { /// /// 网络接口名称 /// public string InterfaceName { 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 SignalStrength CurrentSignalStrength { get; private set; } /// /// 当前网络类型 /// public NetworkType CurrentNetworkType { get; private set; } /// /// 当前发射功率 /// public int CurrentTransmitPower { get; private set; } /// /// 网络配置 /// public CellularNetworkConfig Config { get; private set; } public CellularNetworkState(string interfaceName) { InterfaceName = interfaceName; IsInitialized = false; CurrentStatus = NetworkStatus.Unknown; CurrentSignalStrength = SignalStrength.NoSignal; CurrentNetworkType = NetworkType.Unknown; CurrentTransmitPower = 0; } /// /// 更新网络状态 /// public void UpdateStatus(NetworkStatus status) { CurrentStatus = status; } /// /// 更新信号强度 /// public void UpdateSignalStrength(SignalStrength strength) { CurrentSignalStrength = strength; } /// /// 更新网络类型 /// public void UpdateNetworkType(NetworkType type) { CurrentNetworkType = type; } /// /// 更新发射功率 /// public void UpdateTransmitPower(int power) { CurrentTransmitPower = power; } /// /// 更新网络配置 /// public void UpdateConfig(CellularNetworkConfig config) { Config = config; } /// /// 标记为已启动 /// public void MarkAsStarted() { IsInitialized = true; LastStartTime = DateTime.Now; CurrentStatus = NetworkStatus.Connected; } /// /// 标记为已停止 /// public void MarkAsStopped() { IsInitialized = false; LastStopTime = DateTime.Now; CurrentStatus = NetworkStatus.Disconnected; CurrentSignalStrength = SignalStrength.NoSignal; CurrentNetworkType = NetworkType.Unknown; CurrentTransmitPower = 0; } }