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.

286 lines
7.4 KiB

using CoreAgent.Domain.Interfaces;
using CoreAgent.Domain.Interfaces.Network;
using CoreAgent.Domain.Models.Network;
using CoreAgent.Domain.Models.System;
using Microsoft.Extensions.Options;
namespace CoreAgent.Infrastructure.Contexts;
/// <summary>
/// 蜂窝网络领域上下文
/// </summary>
public class CellularNetworkContext : ICellularNetworkContext, IDisposable
{
private readonly object _lock = new();
private CellularNetworkState _networkState;
private readonly NetworkCommandConfig _networkCommandConfig;
private readonly AppSettings _appSettings;
private string _neConfigKey = string.Empty;
private CancellationTokenSource _token;
private bool _isDisposed;
private bool _isInitialized;
private readonly INetworkIPEndPointManager _networkIPEndPointManager;
private NetworkConfigType _currentConfigType;
/// <summary>
/// 获取取消令牌源
/// </summary>
public CancellationTokenSource TokenSource => _token;
/// <summary>
/// 是否已初始化
/// </summary>
public bool IsInitialized => _isInitialized;
/// <summary>
/// 网络IP端点管理器
/// </summary>
public INetworkIPEndPointManager NetworkIPEndPointManager => _networkIPEndPointManager;
/// <summary>
/// 当前网络配置类型
/// </summary>
public NetworkConfigType CurrentConfigType => _currentConfigType;
public CellularNetworkContext(
IOptions<NetworkCommandConfig> networkCommandConfig,
IOptions<AppSettings> appSettings,
INetworkIPEndPointManager networkIPEndPointManager)
{
_isDisposed = false;
_isInitialized = false;
_token = new CancellationTokenSource();
_networkCommandConfig = networkCommandConfig?.Value ?? throw new ArgumentNullException(nameof(networkCommandConfig));
_appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings));
_networkIPEndPointManager = networkIPEndPointManager ?? throw new ArgumentNullException(nameof(networkIPEndPointManager));
_currentConfigType = NetworkConfigType.None;
}
/// <summary>
/// 初始化上下文
/// </summary>
/// <param name="neConfigKey">网络配置键</param>
public void Initialize(string neConfigKey)
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (_isInitialized)
{
return;
}
if (string.IsNullOrEmpty(neConfigKey))
{
throw new ArgumentNullException(nameof(neConfigKey));
}
lock (_lock)
{
_networkIPEndPointManager.Clear();
_neConfigKey = neConfigKey;
_networkState = new CellularNetworkState(_neConfigKey);
_currentConfigType = NetworkConfigType.None;
_isInitialized = true;
}
}
/// <summary>
/// 更新网络配置类型
/// </summary>
/// <param name="configType">网络配置类型</param>
public void UpdateNetworkConfigType(NetworkConfigType configType)
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
throw new InvalidOperationException("上下文未初始化");
}
lock (_lock)
{
_currentConfigType = configType;
}
}
/// <summary>
/// 获取网络命令配置
/// </summary>
/// <returns>网络命令配置</returns>
public NetworkCommandConfig GetNetworkCommandConfig()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
throw new InvalidOperationException("上下文未初始化");
}
lock (_lock)
{
return _networkCommandConfig ?? throw new InvalidOperationException("网络命令配置未设置");
}
}
/// <summary>
/// 获取网络配置键
/// </summary>
public string GetNeConfigKey()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
throw new InvalidOperationException("上下文未初始化");
}
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>
/// <returns>网络状态</returns>
public CellularNetworkState GetNetworkState()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
throw new InvalidOperationException("上下文未初始化");
}
lock (_lock)
{
return _networkState ?? throw new InvalidOperationException("网络状态未初始化");
}
}
/// <summary>
/// 重置上下文状态
/// </summary>
public void Reset()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
return;
}
lock (_lock)
{
_token.Cancel();
_token = new CancellationTokenSource();
_neConfigKey = string.Empty;
_isInitialized = false;
_networkState = new CellularNetworkState(string.Empty);
_networkIPEndPointManager.Clear();
_currentConfigType = NetworkConfigType.None;
}
}
/// <summary>
/// 取消操作
/// </summary>
public void Cancel()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
lock (_lock)
{
_token.Cancel();
}
}
/// <summary>
/// 获取应用设置
/// </summary>
public AppSettings GetAppSettings()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
throw new InvalidOperationException("上下文未初始化");
}
return _appSettings;
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (_isDisposed)
{
return;
}
lock (_lock)
{
if (_isDisposed)
{
return;
}
_token?.Cancel();
_token?.Dispose();
_isInitialized = false;
_isDisposed = true;
}
}
}