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.
154 lines
5.4 KiB
154 lines
5.4 KiB
using CoreAgent.Domain.Interfaces;
|
|
using CoreAgent.Domain.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Diagnostics;
|
|
|
|
namespace CoreAgent.Infrastructure.Services;
|
|
|
|
/// <summary>
|
|
/// 蜂窝网络服务实现
|
|
/// </summary>
|
|
public class CellularNetworkService : ICellularNetworkService
|
|
{
|
|
private readonly ILogger<CellularNetworkService> _logger;
|
|
|
|
public CellularNetworkService(ILogger<CellularNetworkService> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<bool> StartAsync(string interfaceName, CellularNetworkConfig config)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("正在启动蜂窝网络接口: {InterfaceName}", interfaceName);
|
|
|
|
// 启用网络接口
|
|
var enableCommand = $"netsh interface set interface \"{interfaceName}\" admin=ENABLED";
|
|
await ExecuteCommandAsync(enableCommand);
|
|
|
|
// 配置网络接口
|
|
var configCommand = $"netsh interface cellular set profile name=\"{config.Apn}\" interface=\"{interfaceName}\"";
|
|
await ExecuteCommandAsync(configCommand);
|
|
|
|
_logger.LogInformation("蜂窝网络接口启动成功: {InterfaceName}", interfaceName);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "启动蜂窝网络接口失败: {InterfaceName}", interfaceName);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> StopAsync(string interfaceName)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("正在停止蜂窝网络接口: {InterfaceName}", interfaceName);
|
|
|
|
var command = $"netsh interface set interface \"{interfaceName}\" admin=DISABLED";
|
|
await ExecuteCommandAsync(command);
|
|
|
|
_logger.LogInformation("蜂窝网络接口停止成功: {InterfaceName}", interfaceName);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "停止蜂窝网络接口失败: {InterfaceName}", interfaceName);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<CellularNetworkStatus> GetStatusAsync(string interfaceName)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("正在获取蜂窝网络接口状态: {InterfaceName}", interfaceName);
|
|
|
|
// 获取接口状态
|
|
var statusCommand = $"netsh interface show interface \"{interfaceName}\"";
|
|
var statusResult = await ExecuteCommandAsync(statusCommand);
|
|
|
|
// 获取信号强度
|
|
var signalCommand = $"netsh interface cellular show interfaces \"{interfaceName}\"";
|
|
var signalResult = await ExecuteCommandAsync(signalCommand);
|
|
|
|
var status = new CellularNetworkStatus
|
|
{
|
|
IsEnabled = statusResult.Contains("已启用", StringComparison.OrdinalIgnoreCase),
|
|
ConnectionState = ParseConnectionState(statusResult),
|
|
SignalStrength = ParseSignalStrength(signalResult),
|
|
CarrierName = ParseCarrierName(signalResult)
|
|
};
|
|
|
|
_logger.LogInformation("获取蜂窝网络接口状态成功: {InterfaceName}", interfaceName);
|
|
return status;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取蜂窝网络接口状态失败: {InterfaceName}", interfaceName);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private async Task<string> ExecuteCommandAsync(string command)
|
|
{
|
|
var process = new Process
|
|
{
|
|
StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "cmd.exe",
|
|
Arguments = $"/c {command}",
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
}
|
|
};
|
|
|
|
process.Start();
|
|
var output = await process.StandardOutput.ReadToEndAsync();
|
|
var error = await process.StandardError.ReadToEndAsync();
|
|
await process.WaitForExitAsync();
|
|
|
|
if (process.ExitCode != 0)
|
|
{
|
|
throw new Exception($"命令执行失败: {error}");
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
private string ParseConnectionState(string statusResult)
|
|
{
|
|
if (statusResult.Contains("已连接", StringComparison.OrdinalIgnoreCase))
|
|
return "已连接";
|
|
if (statusResult.Contains("已断开", StringComparison.OrdinalIgnoreCase))
|
|
return "已断开";
|
|
return "未知";
|
|
}
|
|
|
|
private int ParseSignalStrength(string signalResult)
|
|
{
|
|
// 这里需要根据实际输出格式进行解析
|
|
// 示例实现,实际使用时需要根据具体输出格式调整
|
|
if (signalResult.Contains("信号强度: 强", StringComparison.OrdinalIgnoreCase))
|
|
return 4;
|
|
if (signalResult.Contains("信号强度: 中", StringComparison.OrdinalIgnoreCase))
|
|
return 3;
|
|
if (signalResult.Contains("信号强度: 弱", StringComparison.OrdinalIgnoreCase))
|
|
return 2;
|
|
return 1;
|
|
}
|
|
|
|
private string ParseCarrierName(string signalResult)
|
|
{
|
|
// 这里需要根据实际输出格式进行解析
|
|
// 示例实现,实际使用时需要根据具体输出格式调整
|
|
var carrierLine = signalResult.Split('\n')
|
|
.FirstOrDefault(line => line.Contains("运营商:", StringComparison.OrdinalIgnoreCase));
|
|
|
|
return carrierLine?.Split(':').LastOrDefault()?.Trim() ?? "未知";
|
|
}
|
|
}
|