|
|
|
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;
|
|
|
|
private string NeConfigKey;
|
|
|
|
public CancellationTokenSource token =new CancellationTokenSource();
|
|
|
|
/// <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("网络命令配置未初始化");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string SetNeConfigKey(string key)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return NeConfigKey= key ?? throw new ArgumentNullException(nameof(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetNeConfigKey()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return NeConfigKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <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="NeConfigKey">网络接口名称</param>
|
|
|
|
/// <returns>网络状态</returns>
|
|
|
|
public CellularNetworkState GetOrCreateNetworkState(string NeConfigKey)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
if (!_networkStates.TryGetValue(NeConfigKey, out var state))
|
|
|
|
{
|
|
|
|
state = new CellularNetworkState(NeConfigKey);
|
|
|
|
_networkStates[NeConfigKey] = state;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 移除网络状态
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="NeConfigKey">网络接口名称</param>
|
|
|
|
public void RemoveNetworkState(string NeConfigKey)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
_networkStates.Remove(NeConfigKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取所有网络状态
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>所有网络状态的集合</returns>
|
|
|
|
public IReadOnlyCollection<CellularNetworkState> GetAllNetworkStates()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return _networkStates.Values.ToList().AsReadOnly();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 蜂窝网络状态
|
|
|
|
/// </summary>
|
|
|
|
public class CellularNetworkState
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 网络接口名称
|
|
|
|
/// </summary>
|
|
|
|
public string NeConfigKey { 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 NetworkType CurrentNetworkType { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CellularNetworkState(string _NeConfigKey)
|
|
|
|
{
|
|
|
|
NeConfigKey = _NeConfigKey;
|
|
|
|
IsInitialized = false;
|
|
|
|
CurrentStatus = NetworkStatus.Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 更新网络状态
|
|
|
|
/// </summary>
|
|
|
|
public void UpdateStatus(NetworkStatus status)
|
|
|
|
{
|
|
|
|
CurrentStatus = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 更新网络类型
|
|
|
|
/// </summary>
|
|
|
|
public void UpdateNetworkType(NetworkType type)
|
|
|
|
{
|
|
|
|
CurrentNetworkType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
CurrentNetworkType = NetworkType.Unknown;
|
|
|
|
}
|
|
|
|
}
|