30 changed files with 1465 additions and 701 deletions
@ -1,26 +1,30 @@ |
|||||
{ |
{ |
||||
"configKey": "Default", |
"networkConfigurations": [ |
||||
"ragConfig": "Config/Ran/default.cfg", |
|
||||
"coreOrImsConfigs": [ |
|
||||
{ |
{ |
||||
"index": 1, |
"configKey": "Default", |
||||
"plmn": "46000", |
"ragConfig": "Config/Ran/default.cfg", |
||||
"coreNetworkConfig": "Config/CoreNetwork/plmn_46000.cfg", |
"coreOrImsConfigs": [ |
||||
"imsConfig": "Config/Ims/plmn_46000.cfg" |
{ |
||||
}, |
"index": 1, |
||||
{ |
"plmn": "46000", |
||||
"index": 2, |
"coreNetworkConfig": "Config/CoreNetwork/plmn_46000.cfg", |
||||
"plmn": "46001", |
"imsConfig": "Config/Ims/plmn_46000.cfg" |
||||
"coreNetworkConfig": "Config/CoreNetwork/plmn_46001.cfg", |
}, |
||||
"imsConfig": "Config/Ims/plmn_46001.cfg" |
{ |
||||
|
"index": 2, |
||||
|
"plmn": "46001", |
||||
|
"coreNetworkConfig": "Config/CoreNetwork/plmn_46001.cfg", |
||||
|
"imsConfig": "Config/Ims/plmn_46001.cfg" |
||||
|
} |
||||
|
], |
||||
|
"apn": "Internet", |
||||
|
"band": [ |
||||
|
"B1", |
||||
|
"B3", |
||||
|
"B5", |
||||
|
"B8" |
||||
|
], |
||||
|
"comment": "默认网络配置" |
||||
} |
} |
||||
], |
] |
||||
"apn": "Internet", |
|
||||
"band": [ |
|
||||
"B1", |
|
||||
"B3", |
|
||||
"B5", |
|
||||
"B8" |
|
||||
], |
|
||||
"comment": "默认网络配置" |
|
||||
} |
} |
@ -1,62 +1,158 @@ |
|||||
{ |
{ |
||||
"NetworkCommand": { |
"NetworkCommand": { |
||||
"DefaultRetryCount": 3, |
"DefaultRetryCount": 3, |
||||
"DefaultRetryInterval": 1000, |
"DefaultRetryInterval": 1000, |
||||
"NetworkCommands": [ |
"NetworkCommands": [ |
||||
{ |
{ |
||||
"type": 1, |
"type": 0, |
||||
"template": "network init", |
"template": "lte", |
||||
"timeout": 30000, |
"timeout": 5000, |
||||
"isEnabled": true |
"isEnabled": true, |
||||
}, |
"needReturnResult": false, |
||||
{ |
"needBackgroundExecution": false, |
||||
"type": 1, |
"canBeKilled": true, |
||||
"template": "network check", |
"hasParameters": false |
||||
"timeout": 15000, |
}, |
||||
"isEnabled": true |
{ |
||||
}, |
"type": 0, |
||||
{ |
"template": "ntp", |
||||
"type": 1, |
"timeout": 5000, |
||||
"template": "network verify", |
"isEnabled": true, |
||||
"timeout": 20000, |
"needReturnResult": false, |
||||
"isEnabled": true |
"needBackgroundExecution": false, |
||||
}, |
"canBeKilled": true, |
||||
{ |
"hasParameters": false |
||||
"type": 2, |
}, |
||||
"template": "network start", |
{ |
||||
"timeout": 25000, |
"type": 0, |
||||
"isEnabled": true |
"template": "ping", |
||||
}, |
"timeout": 5000, |
||||
{ |
"isEnabled": true, |
||||
"type": 2, |
"needReturnResult": false, |
||||
"template": "network resume", |
"needBackgroundExecution": false, |
||||
"timeout": 20000, |
"canBeKilled": true, |
||||
"isEnabled": true |
"hasParameters": false |
||||
}, |
}, |
||||
{ |
{ |
||||
"type": 3, |
"type": 0, |
||||
"template": "network stop", |
"template": "tcpdump", |
||||
"timeout": 8000, |
"timeout": 5000, |
||||
"isEnabled": true |
"isEnabled": true, |
||||
}, |
"needReturnResult": false, |
||||
{ |
"needBackgroundExecution": false, |
||||
"type": 3, |
"canBeKilled": true, |
||||
"template": "network cleanup", |
"hasParameters": false |
||||
"timeout": 5000, |
}, |
||||
"isEnabled": true |
{ |
||||
}, |
"type": 0, |
||||
{ |
"template": "chmod 777 /etc/shscripts/ntp.sh", |
||||
"type": 4, |
"timeout": 5000, |
||||
"template": "network status", |
"isEnabled": true, |
||||
"timeout": 5000, |
"needReturnResult": false, |
||||
"isEnabled": true |
"needBackgroundExecution": false, |
||||
}, |
"canBeKilled": false, |
||||
{ |
"hasParameters": false |
||||
"type": 4, |
}, |
||||
"template": "network info", |
{ |
||||
"timeout": 5000, |
"type": 0, |
||||
"isEnabled": true |
"template": "nohup sh /etc/shscripts/ntp.sh >/tmp/ntp.log 2>&1 &", |
||||
} |
"timeout": 15000, |
||||
] |
"isEnabled": true, |
||||
} |
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "chmod 777 /root/enb/lte_init.sh", |
||||
|
"timeout": 5000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "chmod 777 /root/mme/lte_init.sh", |
||||
|
"timeout": 5000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "sh /root/enb/lte_init.sh", |
||||
|
"timeout": 20000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "sh /root/mme/lte_init.sh", |
||||
|
"timeout": 20000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "rm -f /tmp/*", |
||||
|
"timeout": 5000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "rm -f /var/log/lte/*", |
||||
|
"timeout": 5000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "killall screen", |
||||
|
"timeout": 2000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 0, |
||||
|
"template": "rm -f /tmp/*.log", |
||||
|
"timeout": 2000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": false |
||||
|
}, |
||||
|
{ |
||||
|
"type": 1, |
||||
|
"template": "sudo sh /etc/shscripts/network_se_{0}.sh", |
||||
|
"timeout": 15000, |
||||
|
"isEnabled": true, |
||||
|
"needReturnResult": false, |
||||
|
"needBackgroundExecution": false, |
||||
|
"canBeKilled": false, |
||||
|
"hasParameters": true |
||||
|
} |
||||
|
] |
||||
|
} |
||||
} |
} |
@ -0,0 +1,87 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Application.Commands.NetworkConfig; |
||||
|
using CoreAgent.Application.Queries.NetworkConfig; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using MediatR; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
|
||||
|
namespace CoreAgent.API.Controllers |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 网络配置控制器
|
||||
|
/// </summary>
|
||||
|
[ApiVersion("1.0")] |
||||
|
public class NetworkConfigController : BaseApiController |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="mediator">中介者</param>
|
||||
|
/// <param name="logger">日志记录器</param>
|
||||
|
public NetworkConfigController(IMediator mediator, ILogger<NetworkConfigController> logger) |
||||
|
: base(mediator, logger) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置
|
||||
|
/// </summary>
|
||||
|
/// <returns>网络配置列表</returns>
|
||||
|
[HttpGet] |
||||
|
[ProducesResponseType(typeof(List<NetworkConfiguration>), 200)] |
||||
|
public async Task<IActionResult> GetAll() |
||||
|
{ |
||||
|
return await HandleRequest<GetAllNetworkConfigurationsQuery, List<NetworkConfiguration>>( |
||||
|
new GetAllNetworkConfigurationsQuery()); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <returns>网络配置</returns>
|
||||
|
[HttpGet("{configKey}")] |
||||
|
[ProducesResponseType(typeof(NetworkConfiguration), 200)] |
||||
|
[ProducesResponseType(404)] |
||||
|
public async Task<IActionResult> GetByKey(string configKey) |
||||
|
{ |
||||
|
return await HandleRequest<GetNetworkConfigurationByKeyQuery, NetworkConfiguration>( |
||||
|
new GetNetworkConfigurationByKeyQuery { ConfigKey = configKey }); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 创建网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="command">创建网络配置命令</param>
|
||||
|
/// <returns>创建的网络配置</returns>
|
||||
|
[HttpPost] |
||||
|
[ProducesResponseType(typeof(NetworkConfiguration), 201)] |
||||
|
[ProducesResponseType(400)] |
||||
|
public async Task<IActionResult> Create([FromBody] CreateNetworkConfigurationCommand command) |
||||
|
{ |
||||
|
var result = await HandleRequest<CreateNetworkConfigurationCommand, NetworkConfiguration>(command); |
||||
|
if (result is OkObjectResult okResult) |
||||
|
{ |
||||
|
return CreatedAtAction(nameof(GetByKey), |
||||
|
new { configKey = command.ConfigKey }, |
||||
|
okResult.Value); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <returns>操作结果</returns>
|
||||
|
[HttpDelete("{configKey}")] |
||||
|
[ProducesResponseType(204)] |
||||
|
[ProducesResponseType(404)] |
||||
|
public async Task<IActionResult> Delete(string configKey) |
||||
|
{ |
||||
|
var command = new DeleteNetworkConfigurationCommand { ConfigKey = configKey }; |
||||
|
return await HandleRequest<DeleteNetworkConfigurationCommand, Unit>(command); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using MediatR; |
||||
|
|
||||
|
namespace CoreAgent.Application.Commands.NetworkConfig |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 创建网络配置命令
|
||||
|
/// </summary>
|
||||
|
public class CreateNetworkConfigurationCommand : IRequest<NetworkConfiguration> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 配置键值
|
||||
|
/// </summary>
|
||||
|
public string ConfigKey { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// RAN配置文件路径
|
||||
|
/// </summary>
|
||||
|
public string RagConfig { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 核心网和IMS配置列表
|
||||
|
/// </summary>
|
||||
|
public List<CoreImsConfig> CoreOrImsConfigs { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// APN配置
|
||||
|
/// </summary>
|
||||
|
public string Apn { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 频段配置
|
||||
|
/// </summary>
|
||||
|
public List<string> Band { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 配置说明
|
||||
|
/// </summary>
|
||||
|
public string Comment { get; set; } |
||||
|
} |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
using MediatR; |
||||
|
|
||||
|
namespace CoreAgent.Application.Commands.NetworkConfig |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 删除网络配置命令
|
||||
|
/// </summary>
|
||||
|
public class DeleteNetworkConfigurationCommand : IRequest<Unit> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 配置键值
|
||||
|
/// </summary>
|
||||
|
public string ConfigKey { get; set; } |
||||
|
} |
||||
|
} |
@ -0,0 +1,81 @@ |
|||||
|
using System; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Application.Commands.NetworkConfig; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using CoreAgent.Domain.Interfaces; |
||||
|
using MediatR; |
||||
|
|
||||
|
namespace CoreAgent.Application.Handlers.NetworkConfig.Commands |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 创建网络配置命令
|
||||
|
/// </summary>
|
||||
|
public class CreateNetworkConfigurationCommand : IRequest<NetworkConfiguration> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 配置键值
|
||||
|
/// </summary>
|
||||
|
public string ConfigKey { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// RAN配置文件路径
|
||||
|
/// </summary>
|
||||
|
public string RagConfig { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 核心网和IMS配置列表
|
||||
|
/// </summary>
|
||||
|
public List<CoreImsConfig> CoreOrImsConfigs { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// APN配置
|
||||
|
/// </summary>
|
||||
|
public string Apn { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 频段配置
|
||||
|
/// </summary>
|
||||
|
public List<string> Band { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 配置说明
|
||||
|
/// </summary>
|
||||
|
public string Comment { get; set; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 创建网络配置命令处理器
|
||||
|
/// </summary>
|
||||
|
public class CreateNetworkConfigurationCommandHandler : IRequestHandler<CreateNetworkConfigurationCommand, NetworkConfiguration> |
||||
|
{ |
||||
|
private readonly INetworkConfigurationService _service; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="service">网络配置服务</param>
|
||||
|
public CreateNetworkConfigurationCommandHandler(INetworkConfigurationService service) |
||||
|
{ |
||||
|
_service = service; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 处理创建网络配置命令
|
||||
|
/// </summary>
|
||||
|
/// <param name="request">创建网络配置命令</param>
|
||||
|
/// <param name="cancellationToken">取消令牌</param>
|
||||
|
/// <returns>创建的网络配置</returns>
|
||||
|
public async Task<NetworkConfiguration> Handle(CreateNetworkConfigurationCommand request, CancellationToken cancellationToken) |
||||
|
{ |
||||
|
return await _service.CreateAsync( |
||||
|
request.ConfigKey, |
||||
|
request.RagConfig, |
||||
|
request.CoreOrImsConfigs, |
||||
|
request.Apn, |
||||
|
request.Band, |
||||
|
request.Comment |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,38 @@ |
|||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Application.Commands.NetworkConfig; |
||||
|
using CoreAgent.Domain.Interfaces; |
||||
|
using MediatR; |
||||
|
|
||||
|
namespace CoreAgent.Application.Handlers.NetworkConfig.Commands |
||||
|
{ |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置命令处理器
|
||||
|
/// </summary>
|
||||
|
public class DeleteNetworkConfigurationCommandHandler : IRequestHandler<DeleteNetworkConfigurationCommand, Unit> |
||||
|
{ |
||||
|
private readonly INetworkConfigurationService _service; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="service">网络配置服务</param>
|
||||
|
public DeleteNetworkConfigurationCommandHandler(INetworkConfigurationService service) |
||||
|
{ |
||||
|
_service = service; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 处理删除网络配置命令
|
||||
|
/// </summary>
|
||||
|
/// <param name="request">删除网络配置命令</param>
|
||||
|
/// <param name="cancellationToken">取消令牌</param>
|
||||
|
/// <returns>操作结果</returns>
|
||||
|
public async Task<Unit> Handle(DeleteNetworkConfigurationCommand request, CancellationToken cancellationToken) |
||||
|
{ |
||||
|
await _service.DeleteAsync(request.ConfigKey); |
||||
|
return Unit.Value; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,66 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Application.Queries.NetworkConfig; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using CoreAgent.Domain.Interfaces; |
||||
|
using MediatR; |
||||
|
|
||||
|
namespace CoreAgent.Application.Handlers.NetworkConfig.Queries |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置查询处理器
|
||||
|
/// </summary>
|
||||
|
public class GetAllNetworkConfigurationsQueryHandler : IRequestHandler<GetAllNetworkConfigurationsQuery, List<NetworkConfiguration>> |
||||
|
{ |
||||
|
private readonly INetworkConfigurationService _service; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="service">网络配置服务</param>
|
||||
|
public GetAllNetworkConfigurationsQueryHandler(INetworkConfigurationService service) |
||||
|
{ |
||||
|
_service = service; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 处理获取所有网络配置查询
|
||||
|
/// </summary>
|
||||
|
/// <param name="request">获取所有网络配置查询</param>
|
||||
|
/// <param name="cancellationToken">取消令牌</param>
|
||||
|
/// <returns>网络配置列表</returns>
|
||||
|
public Task<List<NetworkConfiguration>> Handle(GetAllNetworkConfigurationsQuery request, CancellationToken cancellationToken) |
||||
|
{ |
||||
|
return _service.GetAllAsync(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置查询处理器
|
||||
|
/// </summary>
|
||||
|
public class GetNetworkConfigurationByKeyQueryHandler : IRequestHandler<GetNetworkConfigurationByKeyQuery, NetworkConfiguration> |
||||
|
{ |
||||
|
private readonly INetworkConfigurationService _service; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="service">网络配置服务</param>
|
||||
|
public GetNetworkConfigurationByKeyQueryHandler(INetworkConfigurationService service) |
||||
|
{ |
||||
|
_service = service; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 处理根据配置键获取网络配置查询
|
||||
|
/// </summary>
|
||||
|
/// <param name="request">根据配置键获取网络配置查询</param>
|
||||
|
/// <param name="cancellationToken">取消令牌</param>
|
||||
|
/// <returns>网络配置</returns>
|
||||
|
public Task<NetworkConfiguration> Handle(GetNetworkConfigurationByKeyQuery request, CancellationToken cancellationToken) |
||||
|
{ |
||||
|
return _service.GetByConfigKeyAsync(request.ConfigKey); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
|
||||
|
namespace CoreAgent.Application.Interfaces |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 网络配置服务接口
|
||||
|
/// </summary>
|
||||
|
public interface INetworkConfigurationService |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 创建网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <param name="ragConfig">RAN配置文件路径</param>
|
||||
|
/// <param name="coreOrImsConfigs">核心网和IMS配置列表</param>
|
||||
|
/// <param name="apn">APN配置</param>
|
||||
|
/// <param name="band">频段配置</param>
|
||||
|
/// <param name="comment">配置说明</param>
|
||||
|
/// <returns>创建的网络配置</returns>
|
||||
|
Task<NetworkConfiguration> CreateAsync( |
||||
|
string configKey, |
||||
|
string ragConfig, |
||||
|
List<CoreImsConfig> coreOrImsConfigs, |
||||
|
string apn, |
||||
|
List<string> band, |
||||
|
string comment = null); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 保存网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">网络配置</param>
|
||||
|
Task SaveAsync(NetworkConfiguration configuration); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
Task DeleteAsync(string configKey); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置
|
||||
|
/// </summary>
|
||||
|
/// <returns>网络配置列表</returns>
|
||||
|
Task<List<NetworkConfiguration>> GetAllAsync(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <returns>网络配置</returns>
|
||||
|
Task<NetworkConfiguration> GetByConfigKeyAsync(string configKey); |
||||
|
} |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using MediatR; |
||||
|
|
||||
|
namespace CoreAgent.Application.Queries.NetworkConfig |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置查询
|
||||
|
/// </summary>
|
||||
|
public class GetAllNetworkConfigurationsQuery : IRequest<List<NetworkConfiguration>> |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置查询
|
||||
|
/// </summary>
|
||||
|
public class GetNetworkConfigurationByKeyQuery : IRequest<NetworkConfiguration> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 配置键值
|
||||
|
/// </summary>
|
||||
|
public string ConfigKey { get; set; } |
||||
|
} |
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Interfaces |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 网络配置仓储接口
|
||||
|
/// </summary>
|
||||
|
public interface INetworkConfigurationRepository |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 创建网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <param name="ragConfig">RAN配置文件路径</param>
|
||||
|
/// <param name="coreOrImsConfigs">核心网和IMS配置列表</param>
|
||||
|
/// <param name="apn">APN配置</param>
|
||||
|
/// <param name="band">频段配置</param>
|
||||
|
/// <param name="comment">配置说明</param>
|
||||
|
/// <returns>创建的网络配置</returns>
|
||||
|
Task<NetworkConfiguration> CreateAsync( |
||||
|
string configKey, |
||||
|
string ragConfig, |
||||
|
List<CoreImsConfig> coreOrImsConfigs, |
||||
|
string apn, |
||||
|
List<string> band, |
||||
|
string comment = null); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 保存网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">网络配置</param>
|
||||
|
Task SaveAsync(NetworkConfiguration configuration); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
Task DeleteAsync(string configKey); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置
|
||||
|
/// </summary>
|
||||
|
/// <returns>网络配置列表</returns>
|
||||
|
Task<List<NetworkConfiguration>> GetAllAsync(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <returns>网络配置</returns>
|
||||
|
Task<NetworkConfiguration> GetByConfigKeyAsync(string configKey); |
||||
|
} |
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Interfaces |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 网络配置服务接口
|
||||
|
/// </summary>
|
||||
|
public interface INetworkConfigurationService |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 创建网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <param name="ragConfig">RAN配置文件路径</param>
|
||||
|
/// <param name="coreOrImsConfigs">核心网和IMS配置列表</param>
|
||||
|
/// <param name="apn">APN配置</param>
|
||||
|
/// <param name="band">频段配置</param>
|
||||
|
/// <param name="comment">配置说明</param>
|
||||
|
/// <returns>创建的网络配置</returns>
|
||||
|
Task<NetworkConfiguration> CreateAsync( |
||||
|
string configKey, |
||||
|
string ragConfig, |
||||
|
List<CoreImsConfig> coreOrImsConfigs, |
||||
|
string apn, |
||||
|
List<string> band, |
||||
|
string comment = null); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 保存网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">网络配置</param>
|
||||
|
Task SaveAsync(NetworkConfiguration configuration); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
Task DeleteAsync(string configKey); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置
|
||||
|
/// </summary>
|
||||
|
/// <returns>网络配置列表</returns>
|
||||
|
Task<List<NetworkConfiguration>> GetAllAsync(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置
|
||||
|
/// </summary>
|
||||
|
/// <param name="configKey">配置键值</param>
|
||||
|
/// <returns>网络配置</returns>
|
||||
|
Task<NetworkConfiguration> GetByConfigKeyAsync(string configKey); |
||||
|
} |
||||
|
} |
@ -1,25 +0,0 @@ |
|||||
using CoreAgent.Domain.Models.Network; |
|
||||
|
|
||||
namespace CoreAgent.Domain.Interfaces.Network; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 网络配置器接口
|
|
||||
/// </summary>
|
|
||||
public interface INetworkConfigurator |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// 配置网络参数
|
|
||||
/// </summary>
|
|
||||
/// <param name="interfaceName">网络接口名称</param>
|
|
||||
/// <param name="config">网络配置</param>
|
|
||||
/// <returns>配置结果</returns>
|
|
||||
Task<bool> ConfigureAsync(string interfaceName, CellularNetworkConfig config); |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 设置发射功率
|
|
||||
/// </summary>
|
|
||||
/// <param name="interfaceName">网络接口名称</param>
|
|
||||
/// <param name="powerLevel">功率等级(0-100)</param>
|
|
||||
/// <returns>设置结果</returns>
|
|
||||
Task<bool> SetTransmitPowerAsync(string interfaceName, int powerLevel); |
|
||||
} |
|
@ -1,36 +0,0 @@ |
|||||
using CoreAgent.Domain.Models.Network; |
|
||||
|
|
||||
namespace CoreAgent.Domain.Interfaces.Network; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 网络状态提供者接口
|
|
||||
/// </summary>
|
|
||||
public interface INetworkStatusProvider |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// 获取网络状态
|
|
||||
/// </summary>
|
|
||||
/// <param name="interfaceName">网络接口名称</param>
|
|
||||
/// <returns>网络状态信息</returns>
|
|
||||
Task<NetworkStatus> GetNetworkStatusAsync(string interfaceName); |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 获取信号强度
|
|
||||
/// </summary>
|
|
||||
/// <param name="interfaceName">网络接口名称</param>
|
|
||||
/// <returns>信号强度信息</returns>
|
|
||||
Task<SignalStrength> GetSignalStrengthAsync(string interfaceName); |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 获取网络类型
|
|
||||
/// </summary>
|
|
||||
/// <param name="interfaceName">网络接口名称</param>
|
|
||||
/// <returns>网络类型信息</returns>
|
|
||||
Task<NetworkType> GetNetworkTypeAsync(string interfaceName); |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 获取全局状态
|
|
||||
/// </summary>
|
|
||||
/// <returns>全局状态信息</returns>
|
|
||||
Task<CellularNetworkGlobalStatus> GetGlobalStatusAsync(); |
|
||||
} |
|
@ -0,0 +1,175 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Text.Json; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using CoreAgent.Domain.Interfaces; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
|
|
||||
|
namespace CoreAgent.Infrastructure.Repositories |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 网络配置仓储实现
|
||||
|
/// </summary>
|
||||
|
public class NetworkConfigurationRepository : INetworkConfigurationRepository |
||||
|
{ |
||||
|
private static readonly object _lock = new object(); |
||||
|
private readonly string _configFilePath; |
||||
|
private static readonly JsonSerializerOptions _jsonOptions = new() |
||||
|
{ |
||||
|
PropertyNameCaseInsensitive = true, |
||||
|
WriteIndented = true, |
||||
|
AllowTrailingCommas = true, |
||||
|
ReadCommentHandling = JsonCommentHandling.Skip |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">配置</param>
|
||||
|
public NetworkConfigurationRepository(IConfiguration configuration) |
||||
|
{ |
||||
|
var basePath = AppDomain.CurrentDomain.BaseDirectory; |
||||
|
_configFilePath = Path.Combine(basePath, "Configurations", "NetworkConfig.json"); |
||||
|
|
||||
|
// 确保目录存在
|
||||
|
var directory = Path.GetDirectoryName(_configFilePath); |
||||
|
if (!Directory.Exists(directory)) |
||||
|
{ |
||||
|
Directory.CreateDirectory(directory); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 创建网络配置
|
||||
|
/// </summary>
|
||||
|
public async Task<NetworkConfiguration> CreateAsync( |
||||
|
string configKey, |
||||
|
string ragConfig, |
||||
|
List<CoreImsConfig> coreOrImsConfigs, |
||||
|
string apn, |
||||
|
List<string> band, |
||||
|
string comment = null) |
||||
|
{ |
||||
|
var config = NetworkConfiguration.Create( |
||||
|
configKey, |
||||
|
ragConfig, |
||||
|
coreOrImsConfigs, |
||||
|
apn, |
||||
|
band, |
||||
|
comment |
||||
|
); |
||||
|
|
||||
|
await SaveAsync(config); |
||||
|
return config; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 保存网络配置
|
||||
|
/// </summary>
|
||||
|
public async Task SaveAsync(NetworkConfiguration configuration) |
||||
|
{ |
||||
|
if (configuration == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException(nameof(configuration)); |
||||
|
} |
||||
|
|
||||
|
var configs = await GetAllAsync(); |
||||
|
var existingConfig = configs.FirstOrDefault(c => c.ConfigKey == configuration.ConfigKey); |
||||
|
|
||||
|
if (existingConfig != null) |
||||
|
{ |
||||
|
var index = configs.IndexOf(existingConfig); |
||||
|
configs[index] = configuration; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
configs.Add(configuration); |
||||
|
} |
||||
|
|
||||
|
await SaveConfigurationsAsync(configs); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置
|
||||
|
/// </summary>
|
||||
|
public async Task DeleteAsync(string configKey) |
||||
|
{ |
||||
|
if (string.IsNullOrEmpty(configKey)) |
||||
|
{ |
||||
|
throw new ArgumentException("配置键值不能为空", nameof(configKey)); |
||||
|
} |
||||
|
|
||||
|
var configs = await GetAllAsync(); |
||||
|
var config = configs.FirstOrDefault(c => c.ConfigKey == configKey); |
||||
|
|
||||
|
if (config != null) |
||||
|
{ |
||||
|
configs.Remove(config); |
||||
|
await SaveConfigurationsAsync(configs); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置
|
||||
|
/// </summary>
|
||||
|
public async Task<List<NetworkConfiguration>> GetAllAsync() |
||||
|
{ |
||||
|
if (!File.Exists(_configFilePath)) |
||||
|
{ |
||||
|
return new List<NetworkConfiguration>(); |
||||
|
} |
||||
|
|
||||
|
var json = await File.ReadAllTextAsync(_configFilePath); |
||||
|
try |
||||
|
{ |
||||
|
// 验证JSON格式
|
||||
|
using (JsonDocument.Parse(json)) |
||||
|
{ |
||||
|
// JSON格式正确
|
||||
|
} |
||||
|
|
||||
|
var result = JsonSerializer.Deserialize<NetworkConfigurationsWrapper>(json, _jsonOptions); |
||||
|
if (result == null) |
||||
|
{ |
||||
|
throw new JsonException("反序列化结果为null"); |
||||
|
} |
||||
|
return result.NetworkConfigurations ?? new List<NetworkConfiguration>(); |
||||
|
} |
||||
|
catch (JsonException ex) |
||||
|
{ |
||||
|
throw new InvalidOperationException($"JSON反序列化失败: {ex.Message}", ex); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置
|
||||
|
/// </summary>
|
||||
|
public async Task<NetworkConfiguration> GetByConfigKeyAsync(string configKey) |
||||
|
{ |
||||
|
if (string.IsNullOrEmpty(configKey)) |
||||
|
{ |
||||
|
throw new ArgumentException("配置键值不能为空", nameof(configKey)); |
||||
|
} |
||||
|
|
||||
|
var configs = await GetAllAsync(); |
||||
|
return configs.FirstOrDefault(c => c.ConfigKey == configKey); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 保存配置列表
|
||||
|
/// </summary>
|
||||
|
private async Task SaveConfigurationsAsync(List<NetworkConfiguration> configurations) |
||||
|
{ |
||||
|
if (configurations == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException(nameof(configurations)); |
||||
|
} |
||||
|
|
||||
|
var json = JsonSerializer.Serialize(new { networkConfigurations = configurations }, _jsonOptions); |
||||
|
await File.WriteAllTextAsync(_configFilePath, json); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -1,110 +0,0 @@ |
|||||
using CoreAgent.Domain.Interfaces.Network; |
|
||||
using CoreAgent.Domain.Interfaces.System.Command; |
|
||||
using CoreAgent.Domain.Models.Network; |
|
||||
using Microsoft.Extensions.Logging; |
|
||||
|
|
||||
namespace CoreAgent.Infrastructure.Services.Network; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 蜂窝网络配置器实现
|
|
||||
/// </summary>
|
|
||||
public class CellularNetworkConfigurator : INetworkConfigurator |
|
||||
{ |
|
||||
private readonly ILogger<CellularNetworkConfigurator> _logger; |
|
||||
private readonly ISystemCommandExecutor _commandExecutor; |
|
||||
|
|
||||
public CellularNetworkConfigurator( |
|
||||
ILogger<CellularNetworkConfigurator> logger, |
|
||||
ISystemCommandExecutor commandExecutor) |
|
||||
{ |
|
||||
_logger = logger; |
|
||||
_commandExecutor = commandExecutor; |
|
||||
} |
|
||||
|
|
||||
public async Task<bool> ConfigureAsync(string interfaceName, CellularNetworkConfig config) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
_logger.LogInformation("正在配置蜂窝网络接口: {InterfaceName}", interfaceName); |
|
||||
|
|
||||
// 1. 配置APN
|
|
||||
if (!string.IsNullOrEmpty(config.Apn)) |
|
||||
{ |
|
||||
var apnResult = await ExecuteCommandAsync($"netsh interface cellular set interface \"{interfaceName}\" apn=\"{config.Apn}\""); |
|
||||
if (!apnResult) |
|
||||
{ |
|
||||
_logger.LogError("配置APN失败"); |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 2. 配置认证信息
|
|
||||
if (!string.IsNullOrEmpty(config.Username) && !string.IsNullOrEmpty(config.Password)) |
|
||||
{ |
|
||||
var authResult = await ExecuteCommandAsync($"netsh interface cellular set interface \"{interfaceName}\" username=\"{config.Username}\" password=\"{config.Password}\""); |
|
||||
if (!authResult) |
|
||||
{ |
|
||||
_logger.LogError("配置认证信息失败"); |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 3. 配置网络类型
|
|
||||
//if (config.NetworkType != NetworkType.Auto)
|
|
||||
//{
|
|
||||
// var networkTypeResult = await ExecuteCommandAsync(
|
|
||||
// $"netsh interface cellular set networktype \"{interfaceName}\" " +
|
|
||||
// $"type={(int)config.NetworkType}");
|
|
||||
// if (!networkTypeResult)
|
|
||||
// {
|
|
||||
// _logger.LogError("配置网络类型失败");
|
|
||||
// return false;
|
|
||||
// }
|
|
||||
//}
|
|
||||
|
|
||||
_logger.LogInformation("蜂窝网络接口 {InterfaceName} 配置成功", interfaceName); |
|
||||
return true; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogError(ex, "配置蜂窝网络接口 {InterfaceName} 失败", interfaceName); |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public async Task<bool> SetTransmitPowerAsync(string interfaceName, int powerLevel) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
if (powerLevel < 0 || powerLevel > 100) |
|
||||
{ |
|
||||
_logger.LogWarning("发射功率设置无效: {PowerLevel}", powerLevel); |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
_logger.LogInformation("正在设置蜂窝网络发射功率: {InterfaceName}, {PowerLevel}", interfaceName, powerLevel); |
|
||||
|
|
||||
// 设置发射功率
|
|
||||
var result = await ExecuteCommandAsync($"netsh interface cellular set interface \"{interfaceName}\" power={powerLevel}"); |
|
||||
if (!result) |
|
||||
{ |
|
||||
_logger.LogError("设置蜂窝网络发射功率失败"); |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
_logger.LogInformation("蜂窝网络发射功率设置成功: {PowerLevel}", powerLevel); |
|
||||
return true; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogError(ex, "设置蜂窝网络发射功率失败: {InterfaceName}, {PowerLevel}", interfaceName, powerLevel); |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private async Task<bool> ExecuteCommandAsync(string command) |
|
||||
{ |
|
||||
var result = await _commandExecutor.ExecuteCommandAsync(command, new CancellationTokenSource()); |
|
||||
return result.IsSuccess; |
|
||||
} |
|
||||
} |
|
@ -1,244 +0,0 @@ |
|||||
using CoreAgent.Domain.Contexts; |
|
||||
using CoreAgent.Domain.Interfaces.Network; |
|
||||
using CoreAgent.Domain.Interfaces.System.Command; |
|
||||
using CoreAgent.Domain.Models.Network; |
|
||||
using CoreAgent.Domain.Models.System; |
|
||||
using Microsoft.Extensions.Logging; |
|
||||
|
|
||||
namespace CoreAgent.Infrastructure.Services.Network; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 蜂窝网络状态提供者实现
|
|
||||
/// </summary>
|
|
||||
public class CellularNetworkStatusProvider : INetworkStatusProvider |
|
||||
{ |
|
||||
private readonly ILogger<CellularNetworkStatusProvider> _logger; |
|
||||
private readonly ISystemCommandExecutor _commandExecutor; |
|
||||
private readonly CellularNetworkContext _context; |
|
||||
|
|
||||
public CellularNetworkStatusProvider( |
|
||||
ILogger<CellularNetworkStatusProvider> logger, |
|
||||
ISystemCommandExecutor commandExecutor) |
|
||||
{ |
|
||||
_logger = logger; |
|
||||
_commandExecutor = commandExecutor; |
|
||||
_context = CellularNetworkContext.Instance; |
|
||||
} |
|
||||
|
|
||||
public async Task<NetworkStatus> GetNetworkStatusAsync(string interfaceName) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
var result = await ExecuteCommandWithResultAsync($"netsh interface cellular show interfaces \"{interfaceName}\""); |
|
||||
if (!result.IsSuccess) |
|
||||
{ |
|
||||
_logger.LogError("获取蜂窝网络状态失败"); |
|
||||
return NetworkStatus.Unknown; |
|
||||
} |
|
||||
|
|
||||
var state = _context.GetOrCreateNetworkState(interfaceName); |
|
||||
var status = ParseNetworkStatus(result.Output); |
|
||||
state.UpdateStatus(status); |
|
||||
return status; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogError(ex, "获取蜂窝网络状态失败"); |
|
||||
return NetworkStatus.Unknown; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public async Task<SignalStrength> GetSignalStrengthAsync(string interfaceName) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
var result = await ExecuteCommandWithResultAsync($"netsh interface cellular show interfaces \"{interfaceName}\""); |
|
||||
if (!result.IsSuccess) |
|
||||
{ |
|
||||
_logger.LogError("获取蜂窝网络信号强度失败"); |
|
||||
return SignalStrength.NoSignal; |
|
||||
} |
|
||||
|
|
||||
var state = _context.GetOrCreateNetworkState(interfaceName); |
|
||||
var strength = ParseSignalStrength(result.Output); |
|
||||
state.UpdateSignalStrength(strength); |
|
||||
return strength; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogError(ex, "获取蜂窝网络信号强度失败"); |
|
||||
return SignalStrength.NoSignal; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public async Task<NetworkType> GetNetworkTypeAsync(string interfaceName) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
var result = await ExecuteCommandWithResultAsync($"netsh interface cellular show interfaces \"{interfaceName}\""); |
|
||||
if (!result.IsSuccess) |
|
||||
{ |
|
||||
_logger.LogError("获取蜂窝网络类型失败"); |
|
||||
return NetworkType.Unknown; |
|
||||
} |
|
||||
|
|
||||
var state = _context.GetOrCreateNetworkState(interfaceName); |
|
||||
var type = ParseNetworkType(result.Output); |
|
||||
state.UpdateNetworkType(type); |
|
||||
return type; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogError(ex, "获取蜂窝网络类型失败"); |
|
||||
return NetworkType.Unknown; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public async Task<CellularNetworkGlobalStatus> GetGlobalStatusAsync() |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
var result = await ExecuteCommandWithResultAsync("netsh interface cellular show interfaces"); |
|
||||
if (!result.IsSuccess) |
|
||||
{ |
|
||||
_logger.LogError("获取蜂窝网络全局状态失败"); |
|
||||
return new CellularNetworkGlobalStatus |
|
||||
{ |
|
||||
IsInitialized = false, |
|
||||
CurrentStatus = NetworkStatus.Unknown, |
|
||||
CurrentSignalStrength = SignalStrength.NoSignal, |
|
||||
CurrentNetworkType = NetworkType.Unknown, |
|
||||
CurrentTransmitPower = 0 |
|
||||
}; |
|
||||
} |
|
||||
|
|
||||
// 解析所有接口的状态
|
|
||||
var interfaces = ParseInterfaces(result.Output); |
|
||||
foreach (var iface in interfaces) |
|
||||
{ |
|
||||
var state = _context.GetOrCreateNetworkState(iface.Name); |
|
||||
state.UpdateStatus(iface.Status); |
|
||||
state.UpdateSignalStrength(iface.SignalStrength); |
|
||||
state.UpdateNetworkType(iface.NetworkType); |
|
||||
} |
|
||||
|
|
||||
// 聚合所有接口的状态
|
|
||||
var states = _context.GetAllNetworkStates(); |
|
||||
return new CellularNetworkGlobalStatus |
|
||||
{ |
|
||||
IsInitialized = states.Any(s => s.IsInitialized), |
|
||||
LastStartTime = states.Max(s => s.LastStartTime), |
|
||||
LastStopTime = states.Max(s => s.LastStopTime), |
|
||||
CurrentStatus = states.FirstOrDefault()?.CurrentStatus ?? NetworkStatus.Unknown, |
|
||||
CurrentSignalStrength = states.FirstOrDefault()?.CurrentSignalStrength ?? SignalStrength.NoSignal, |
|
||||
CurrentNetworkType = states.FirstOrDefault()?.CurrentNetworkType ?? NetworkType.Unknown, |
|
||||
CurrentTransmitPower = states.FirstOrDefault()?.CurrentTransmitPower ?? 0 |
|
||||
}; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogError(ex, "获取蜂窝网络全局状态失败"); |
|
||||
return new CellularNetworkGlobalStatus |
|
||||
{ |
|
||||
IsInitialized = false, |
|
||||
CurrentStatus = NetworkStatus.Unknown, |
|
||||
CurrentSignalStrength = SignalStrength.NoSignal, |
|
||||
CurrentNetworkType = NetworkType.Unknown, |
|
||||
CurrentTransmitPower = 0 |
|
||||
}; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private async Task<CommandExecutionResult> ExecuteCommandWithResultAsync(string command) |
|
||||
{ |
|
||||
return await _commandExecutor.ExecuteCommandAsync(command, new CancellationTokenSource()); |
|
||||
} |
|
||||
|
|
||||
private NetworkStatus ParseNetworkStatus(string output) |
|
||||
{ |
|
||||
if (output.Contains("已连接", StringComparison.OrdinalIgnoreCase)) |
|
||||
return NetworkStatus.Connected; |
|
||||
if (output.Contains("已断开", StringComparison.OrdinalIgnoreCase)) |
|
||||
return NetworkStatus.Disconnected; |
|
||||
return NetworkStatus.Unknown; |
|
||||
} |
|
||||
|
|
||||
private SignalStrength ParseSignalStrength(string output) |
|
||||
{ |
|
||||
if (output.Contains("信号强度: 强", StringComparison.OrdinalIgnoreCase)) |
|
||||
return SignalStrength.Strong; |
|
||||
if (output.Contains("信号强度: 中", StringComparison.OrdinalIgnoreCase)) |
|
||||
return SignalStrength.Medium; |
|
||||
if (output.Contains("信号强度: 弱", StringComparison.OrdinalIgnoreCase)) |
|
||||
return SignalStrength.Weak; |
|
||||
return SignalStrength.NoSignal; |
|
||||
} |
|
||||
|
|
||||
private NetworkType ParseNetworkType(string output) |
|
||||
{ |
|
||||
if (output.Contains("5G", StringComparison.OrdinalIgnoreCase)) |
|
||||
return NetworkType.G5; |
|
||||
if (output.Contains("4G", StringComparison.OrdinalIgnoreCase)) |
|
||||
return NetworkType.G4; |
|
||||
if (output.Contains("3G", StringComparison.OrdinalIgnoreCase)) |
|
||||
return NetworkType.G3; |
|
||||
if (output.Contains("2G", StringComparison.OrdinalIgnoreCase)) |
|
||||
return NetworkType.G2; |
|
||||
return NetworkType.Unknown; |
|
||||
} |
|
||||
|
|
||||
private class InterfaceInfo |
|
||||
{ |
|
||||
public string Name { get; set; } |
|
||||
public NetworkStatus Status { get; set; } |
|
||||
public SignalStrength SignalStrength { get; set; } |
|
||||
public NetworkType NetworkType { get; set; } |
|
||||
} |
|
||||
|
|
||||
private IEnumerable<InterfaceInfo> ParseInterfaces(string output) |
|
||||
{ |
|
||||
var interfaces = new List<InterfaceInfo>(); |
|
||||
var lines = output.Split('\n'); |
|
||||
InterfaceInfo currentInterface = null; |
|
||||
|
|
||||
foreach (var line in lines) |
|
||||
{ |
|
||||
if (line.Contains("接口名称:", StringComparison.OrdinalIgnoreCase)) |
|
||||
{ |
|
||||
if (currentInterface != null) |
|
||||
{ |
|
||||
interfaces.Add(currentInterface); |
|
||||
} |
|
||||
currentInterface = new InterfaceInfo |
|
||||
{ |
|
||||
Name = line.Split(':')[1].Trim(), |
|
||||
Status = NetworkStatus.Unknown, |
|
||||
SignalStrength = SignalStrength.NoSignal, |
|
||||
NetworkType = NetworkType.Unknown |
|
||||
}; |
|
||||
} |
|
||||
else if (currentInterface != null) |
|
||||
{ |
|
||||
if (line.Contains("状态:", StringComparison.OrdinalIgnoreCase)) |
|
||||
{ |
|
||||
currentInterface.Status = ParseNetworkStatus(line); |
|
||||
} |
|
||||
else if (line.Contains("信号强度:", StringComparison.OrdinalIgnoreCase)) |
|
||||
{ |
|
||||
currentInterface.SignalStrength = ParseSignalStrength(line); |
|
||||
} |
|
||||
else if (line.Contains("网络类型:", StringComparison.OrdinalIgnoreCase)) |
|
||||
{ |
|
||||
currentInterface.NetworkType = ParseNetworkType(line); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if (currentInterface != null) |
|
||||
{ |
|
||||
interfaces.Add(currentInterface); |
|
||||
} |
|
||||
|
|
||||
return interfaces; |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,13 @@ |
|||||
|
using CoreAgent.Domain.Interfaces.Network; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace CoreAgent.Infrastructure.Services.Network.Extensions; |
||||
|
|
||||
|
public static class ServiceCollectionExtensions |
||||
|
{ |
||||
|
public static IServiceCollection AddNetworkServices(this IServiceCollection services) |
||||
|
{ |
||||
|
services.AddScoped<ICellularNetworkService, CellularNetworkService>(); |
||||
|
return services; |
||||
|
} |
||||
|
} |
@ -0,0 +1,70 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using CoreAgent.Domain.Interfaces; |
||||
|
|
||||
|
namespace CoreAgent.Infrastructure.Services |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 网络配置服务实现
|
||||
|
/// </summary>
|
||||
|
public class NetworkConfigurationService : INetworkConfigurationService |
||||
|
{ |
||||
|
private readonly INetworkConfigurationRepository _repository; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 构造函数
|
||||
|
/// </summary>
|
||||
|
/// <param name="repository">网络配置仓储</param>
|
||||
|
public NetworkConfigurationService(INetworkConfigurationRepository repository) |
||||
|
{ |
||||
|
_repository = repository; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 创建网络配置
|
||||
|
/// </summary>
|
||||
|
public Task<NetworkConfiguration> CreateAsync( |
||||
|
string configKey, |
||||
|
string ragConfig, |
||||
|
List<CoreImsConfig> coreOrImsConfigs, |
||||
|
string apn, |
||||
|
List<string> band, |
||||
|
string comment = null) |
||||
|
{ |
||||
|
return _repository.CreateAsync(configKey, ragConfig, coreOrImsConfigs, apn, band, comment); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 保存网络配置
|
||||
|
/// </summary>
|
||||
|
public Task SaveAsync(NetworkConfiguration configuration) |
||||
|
{ |
||||
|
return _repository.SaveAsync(configuration); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除网络配置
|
||||
|
/// </summary>
|
||||
|
public Task DeleteAsync(string configKey) |
||||
|
{ |
||||
|
return _repository.DeleteAsync(configKey); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 获取所有网络配置
|
||||
|
/// </summary>
|
||||
|
public Task<List<NetworkConfiguration>> GetAllAsync() |
||||
|
{ |
||||
|
return _repository.GetAllAsync(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置键获取网络配置
|
||||
|
/// </summary>
|
||||
|
public Task<NetworkConfiguration> GetByConfigKeyAsync(string configKey) |
||||
|
{ |
||||
|
return _repository.GetByConfigKeyAsync(configKey); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue