Browse Source

更新系统配置和网络服务相关代码

master
root 4 days ago
parent
commit
0f387ceea9
  1. 2
      CoreAgent.API/Program.cs
  2. 3
      CoreAgent.API/Startup.cs
  3. 3
      CoreAgent.API/appsettings.json
  4. 12
      CoreAgent.Domain/Models/System/AppSettings.cs
  5. 65
      CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
  6. 13
      CoreAgent.Infrastructure/Services/Network/Extensions/ServiceCollectionExtensions.cs

2
CoreAgent.API/Program.cs

@ -1,4 +1,5 @@
using CoreAgent.API;
using CoreAgent.Domain.Models.System;
using CoreAgent.Infrastructure.Logging;
using Serilog;
using System;
@ -11,6 +12,7 @@ try
// 第一步:添加所有配置
builder.AddConfigurations();
// 第二步:创建Startup实例
var startup = builder.CreateStartup();

3
CoreAgent.API/Startup.cs

@ -90,6 +90,9 @@ namespace CoreAgent.API
var networkCommandConfig = builder.Configuration.GetSection("networkCommand").Get<NetworkCommandConfig>();
CellularNetworkContext.Instance.SetNetworkCommandConfig(networkCommandConfig);
// 添加配置
builder.Services.Configure<AppSettings>(builder.Configuration);
return builder;
}

3
CoreAgent.API/appsettings.json

@ -13,5 +13,8 @@
"Url": "http://0.0.0.0:11003"
}
}
},
"AppSettings": {
"TempDirectory": "/tmp/"
}
}

12
CoreAgent.Domain/Models/System/AppSettings.cs

@ -0,0 +1,12 @@
namespace CoreAgent.Domain.Models.System;
/// <summary>
/// 应用程序配置
/// </summary>
public class AppSettings
{
/// <summary>
/// 临时目录路径
/// </summary>
public string TempDirectory { get; set; } = "/tmp/";
}

65
CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs

@ -7,6 +7,8 @@ using CoreAgent.Domain.Interfaces.System.Command;
using CoreAgent.Domain.Models.Network;
using CoreAgent.Domain.Models.System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.IO;
namespace CoreAgent.Infrastructure.Services.Network;
@ -19,6 +21,7 @@ public class CellularNetworkService : ICellularNetworkService
private readonly ISystemCommandExecutor _commandExecutor;
private readonly INetworkConfigurationService _configService;
private readonly CellularNetworkContext _context;
private readonly AppSettings _appSettings;
private static readonly SemaphoreSlim _startLock = new(1, 1);
private const int MaxConnectionAttempts = 30;
private const int ConnectionCheckDelayMs = 1000;
@ -27,11 +30,13 @@ public class CellularNetworkService : ICellularNetworkService
public CellularNetworkService(
ILogger<CellularNetworkService> logger,
ISystemCommandExecutor commandExecutor,
INetworkConfigurationService configService)
INetworkConfigurationService configService,
IOptions<AppSettings> appSettings)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_commandExecutor = commandExecutor ?? throw new ArgumentNullException(nameof(commandExecutor));
_configService = configService ?? throw new ArgumentNullException(nameof(configService));
_appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings));
_context = CellularNetworkContext.Instance;
}
@ -211,6 +216,13 @@ public class CellularNetworkService : ICellularNetworkService
{
try
{
// 复制配置值到临时目录并更新配置路径
if (!await CopyConfigValuesToTempAsync(networkConfig))
{
_logger.LogError("复制配置值到临时目录失败");
return false;
}
var commands = _context.GetNetworkCommandConfig();
var startCommand = commands.NetworkCommands.FirstOrDefault(s => s.Type == NetworkCommandType.Start);
if (startCommand == null)
@ -243,6 +255,57 @@ public class CellularNetworkService : ICellularNetworkService
}
}
/// <summary>
/// 将配置值复制到临时目录并更新配置路径
/// </summary>
private async Task<bool> CopyConfigValuesToTempAsync(NetworkConfiguration networkConfig)
{
try
{
// 确保临时目录存在
if (!Directory.Exists(_appSettings.TempDirectory))
{
Directory.CreateDirectory(_appSettings.TempDirectory);
}
// 复制 RAG 配置值并更新路径
if (!string.IsNullOrEmpty(networkConfig.RagConfig))
{
var ragFileName = Path.GetFileName(networkConfig.RagConfig);
var ragTempPath = $"{_appSettings.TempDirectory}{ragFileName}";
await File.WriteAllTextAsync(ragTempPath, networkConfig.RagConfig);
networkConfig.RagConfig = ragTempPath;
}
// 复制核心网络配置值和IMS配置值并更新路径
foreach (var config in networkConfig.CoreOrImsConfigs)
{
if (!string.IsNullOrEmpty(config.CoreNetworkConfig))
{
var coreFileName = Path.GetFileName(config.CoreNetworkConfig);
var coreTempPath = $"{_appSettings.TempDirectory}{coreFileName}";
await File.WriteAllTextAsync(coreTempPath, config.CoreNetworkConfig);
config.CoreNetworkConfig = coreTempPath;
}
if (!string.IsNullOrEmpty(config.ImsConfig))
{
var imsFileName = Path.GetFileName(config.ImsConfig);
var imsTempPath = $"{_appSettings.TempDirectory}{imsFileName}";
await File.WriteAllTextAsync(imsTempPath, config.ImsConfig);
config.ImsConfig = imsTempPath;
}
}
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "复制配置值到临时目录时发生错误");
return false;
}
}
private bool HasBothRagAndCoreConfigs(NetworkConfiguration config)
{
return !string.IsNullOrWhiteSpace(config.RagConfig) && config.CoreOrImsConfigs.Any();

13
CoreAgent.Infrastructure/Services/Network/Extensions/ServiceCollectionExtensions.cs

@ -1,13 +0,0 @@
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;
}
}
Loading…
Cancel
Save