You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
238 lines
6.1 KiB
238 lines
6.1 KiB
6 days ago
|
using CoreAgent.Domain.Interfaces.Network;
|
||
|
using CoreAgent.Domain.Models.Network;
|
||
|
using CoreAgent.Domain.Models.System;
|
||
|
|
||
|
namespace CoreAgent.Domain.Contexts;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 蜂窝网络领域上下文
|
||
|
/// </summary>
|
||
|
public class CellularNetworkContext
|
||
|
{
|
||
|
private static readonly Lazy<CellularNetworkContext> _instance = new(() => new CellularNetworkContext());
|
||
|
private readonly object _lock = new();
|
||
|
private readonly Dictionary<string, CellularNetworkState> _networkStates = new();
|
||
|
private NetworkCommandConfig _networkCommandConfig;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取单例实例
|
||
|
/// </summary>
|
||
|
public static CellularNetworkContext Instance => _instance.Value;
|
||
|
|
||
|
private CellularNetworkContext() { }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 设置网络命令配置
|
||
|
/// </summary>
|
||
|
/// <param name="config">网络命令配置</param>
|
||
|
public void SetNetworkCommandConfig(NetworkCommandConfig config)
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
_networkCommandConfig = config ?? throw new ArgumentNullException(nameof(config));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取网络命令配置
|
||
|
/// </summary>
|
||
|
/// <returns>网络命令配置</returns>
|
||
|
public NetworkCommandConfig GetNetworkCommandConfig()
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
return _networkCommandConfig ?? throw new InvalidOperationException("网络命令配置未初始化");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取指定类型的命令配置
|
||
|
/// </summary>
|
||
|
/// <param name="type">命令类型</param>
|
||
|
/// <returns>命令配置列表</returns>
|
||
|
public List<CommandTemplateConfig> GetCommandsByType(NetworkCommandType type)
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
return _networkCommandConfig?.GetCommandsByType(type) ?? new List<CommandTemplateConfig>();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取所有命令类型
|
||
|
/// </summary>
|
||
|
/// <returns>命令类型数组</returns>
|
||
|
public NetworkCommandType[] GetCommandTypes()
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
return _networkCommandConfig?.GetCommandTypes() ?? Array.Empty<NetworkCommandType>();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取或创建网络状态
|
||
|
/// </summary>
|
||
|
/// <param name="interfaceName">网络接口名称</param>
|
||
|
/// <returns>网络状态</returns>
|
||
|
public CellularNetworkState GetOrCreateNetworkState(string interfaceName)
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
if (!_networkStates.TryGetValue(interfaceName, out var state))
|
||
|
{
|
||
|
state = new CellularNetworkState(interfaceName);
|
||
|
_networkStates[interfaceName] = state;
|
||
|
}
|
||
|
return state;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 移除网络状态
|
||
|
/// </summary>
|
||
|
/// <param name="interfaceName">网络接口名称</param>
|
||
|
public void RemoveNetworkState(string interfaceName)
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
_networkStates.Remove(interfaceName);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取所有网络状态
|
||
|
/// </summary>
|
||
|
/// <returns>所有网络状态的集合</returns>
|
||
|
public IReadOnlyCollection<CellularNetworkState> GetAllNetworkStates()
|
||
|
{
|
||
|
lock (_lock)
|
||
|
{
|
||
|
return _networkStates.Values.ToList().AsReadOnly();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 蜂窝网络状态
|
||
|
/// </summary>
|
||
|
public class CellularNetworkState
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 网络接口名称
|
||
|
/// </summary>
|
||
|
public string InterfaceName { get; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 是否已初始化
|
||
|
/// </summary>
|
||
|
public bool IsInitialized { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 最后启动时间
|
||
|
/// </summary>
|
||
|
public DateTime? LastStartTime { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 最后停止时间
|
||
|
/// </summary>
|
||
|
public DateTime? LastStopTime { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 当前运行状态
|
||
|
/// </summary>
|
||
|
public NetworkStatus CurrentStatus { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 当前信号强度
|
||
|
/// </summary>
|
||
|
public SignalStrength CurrentSignalStrength { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 当前网络类型
|
||
|
/// </summary>
|
||
|
public NetworkType CurrentNetworkType { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 当前发射功率
|
||
|
/// </summary>
|
||
|
public int CurrentTransmitPower { get; private set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 网络配置
|
||
|
/// </summary>
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新网络状态
|
||
|
/// </summary>
|
||
|
public void UpdateStatus(NetworkStatus status)
|
||
|
{
|
||
|
CurrentStatus = status;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新信号强度
|
||
|
/// </summary>
|
||
|
public void UpdateSignalStrength(SignalStrength strength)
|
||
|
{
|
||
|
CurrentSignalStrength = strength;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新网络类型
|
||
|
/// </summary>
|
||
|
public void UpdateNetworkType(NetworkType type)
|
||
|
{
|
||
|
CurrentNetworkType = type;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新发射功率
|
||
|
/// </summary>
|
||
|
public void UpdateTransmitPower(int power)
|
||
|
{
|
||
|
CurrentTransmitPower = power;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新网络配置
|
||
|
/// </summary>
|
||
|
public void UpdateConfig(CellularNetworkConfig config)
|
||
|
{
|
||
|
Config = config;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 标记为已启动
|
||
|
/// </summary>
|
||
|
public void MarkAsStarted()
|
||
|
{
|
||
|
IsInitialized = true;
|
||
|
LastStartTime = DateTime.Now;
|
||
|
CurrentStatus = NetworkStatus.Connected;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 标记为已停止
|
||
|
/// </summary>
|
||
|
public void MarkAsStopped()
|
||
|
{
|
||
|
IsInitialized = false;
|
||
|
LastStopTime = DateTime.Now;
|
||
|
CurrentStatus = NetworkStatus.Disconnected;
|
||
|
CurrentSignalStrength = SignalStrength.NoSignal;
|
||
|
CurrentNetworkType = NetworkType.Unknown;
|
||
|
CurrentTransmitPower = 0;
|
||
|
}
|
||
|
}
|