|
@ -6,6 +6,8 @@ using CellularManagement.Domain.Repositories.Device; |
|
|
using CellularManagement.Domain.Repositories.Base; |
|
|
using CellularManagement.Domain.Repositories.Base; |
|
|
using CellularManagement.Domain.Services; |
|
|
using CellularManagement.Domain.Services; |
|
|
using X1.DynamicClientCore.Features; |
|
|
using X1.DynamicClientCore.Features; |
|
|
|
|
|
using CellularManagement.Domain.Repositories.NetworkProfile; |
|
|
|
|
|
using X1.DynamicClientCore.Models; |
|
|
|
|
|
|
|
|
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StartDeviceRuntime; |
|
|
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StartDeviceRuntime; |
|
|
|
|
|
|
|
@ -20,11 +22,13 @@ public class StartDeviceRuntimeCommandHandler : IRequestHandler<StartDeviceRunti |
|
|
private readonly IUnitOfWork _unitOfWork; |
|
|
private readonly IUnitOfWork _unitOfWork; |
|
|
private readonly ICurrentUserService _currentUserService; |
|
|
private readonly ICurrentUserService _currentUserService; |
|
|
private readonly IInstrumentProtocolClient _protocolClient; |
|
|
private readonly IInstrumentProtocolClient _protocolClient; |
|
|
|
|
|
private readonly INetworkStackConfigRepository _networkStackConfigRepository; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// 初始化命令处理器
|
|
|
/// 初始化命令处理器
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public StartDeviceRuntimeCommandHandler( |
|
|
public StartDeviceRuntimeCommandHandler( |
|
|
|
|
|
INetworkStackConfigRepository networkStackConfigRepository, |
|
|
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|
|
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|
|
ICellularDeviceRepository deviceRepository, |
|
|
ICellularDeviceRepository deviceRepository, |
|
|
ILogger<StartDeviceRuntimeCommandHandler> logger, |
|
|
ILogger<StartDeviceRuntimeCommandHandler> logger, |
|
@ -38,6 +42,7 @@ public class StartDeviceRuntimeCommandHandler : IRequestHandler<StartDeviceRunti |
|
|
_unitOfWork = unitOfWork; |
|
|
_unitOfWork = unitOfWork; |
|
|
_currentUserService = currentUserService; |
|
|
_currentUserService = currentUserService; |
|
|
_protocolClient= protocolClient; |
|
|
_protocolClient= protocolClient; |
|
|
|
|
|
_networkStackConfigRepository = networkStackConfigRepository; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
@ -64,103 +69,116 @@ public class StartDeviceRuntimeCommandHandler : IRequestHandler<StartDeviceRunti |
|
|
return OperationResult<StartDeviceRuntimeResponse>.CreateFailure("用户未登录"); |
|
|
return OperationResult<StartDeviceRuntimeResponse>.CreateFailure("用户未登录"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var deviceResults = new List<DeviceStartResult>(); |
|
|
var res = await _networkStackConfigRepository.GetNetworkStackConfigsByCodesAsync(request.DeviceRequests.Select(s => s.NetworkStackCode).ToArray(), cancellationToken); |
|
|
var successCount = 0; |
|
|
|
|
|
var failureCount = 0; |
|
|
|
|
|
|
|
|
//List<CellularNetworkConfiguration> Requests = new List<CellularNetworkConfiguration>();
|
|
|
// 批量处理设备启动
|
|
|
//foreach (var item in res)
|
|
|
foreach (var deviceRequest in request.DeviceRequests) |
|
|
//{
|
|
|
{ |
|
|
// Requests.Add(new CellularNetworkConfiguration
|
|
|
try |
|
|
// {
|
|
|
{ |
|
|
// DeviceCode= item.NetworkStackCode,
|
|
|
_logger.LogInformation("开始启动设备,设备编号: {DeviceCode}, 网络栈配置编号: {NetworkStackCode}", |
|
|
// RadioAccessNetworkConfiguration =item.RanName
|
|
|
deviceRequest.DeviceCode, deviceRequest.NetworkStackCode); |
|
|
// });
|
|
|
|
|
|
//}
|
|
|
// 获取设备运行时状态
|
|
|
|
|
|
var deviceRuntime = await _deviceRuntimeRepository.GetRuntimeByDeviceCodeAsync(deviceRequest.DeviceCode, cancellationToken); |
|
|
//var deviceResults = new List<DeviceStartResult>();
|
|
|
if (deviceRuntime == null) |
|
|
//var successCount = 0;
|
|
|
{ |
|
|
//var failureCount = 0;
|
|
|
_logger.LogWarning("设备运行时状态不存在,设备编号: {DeviceCode}", deviceRequest.DeviceCode); |
|
|
|
|
|
deviceResults.Add(new DeviceStartResult |
|
|
//// 批量处理设备启动
|
|
|
{ |
|
|
//foreach (var deviceRequest in request.DeviceRequests)
|
|
|
DeviceCode = deviceRequest.DeviceCode, |
|
|
//{
|
|
|
IsSuccess = false, |
|
|
// try
|
|
|
ErrorMessage = $"设备编号 {deviceRequest.DeviceCode} 的运行时状态不存在" |
|
|
// {
|
|
|
}); |
|
|
// _logger.LogInformation("开始启动设备,设备编号: {DeviceCode}, 网络栈配置编号: {NetworkStackCode}",
|
|
|
failureCount++; |
|
|
// deviceRequest.DeviceCode, deviceRequest.NetworkStackCode);
|
|
|
continue; |
|
|
|
|
|
} |
|
|
// // 获取设备运行时状态
|
|
|
|
|
|
// var deviceRuntime = await _deviceRuntimeRepository.GetRuntimeByDeviceCodeAsync(deviceRequest.DeviceCode, cancellationToken);
|
|
|
// 检查设备是否已经在运行
|
|
|
// if (deviceRuntime == null)
|
|
|
if (deviceRuntime.RuntimeStatus == DeviceRuntimeStatus.Running) |
|
|
// {
|
|
|
{ |
|
|
// _logger.LogWarning("设备运行时状态不存在,设备编号: {DeviceCode}", deviceRequest.DeviceCode);
|
|
|
_logger.LogWarning("设备已经在运行中,设备编号: {DeviceCode}", deviceRequest.DeviceCode); |
|
|
// deviceResults.Add(new DeviceStartResult
|
|
|
deviceResults.Add(new DeviceStartResult |
|
|
// {
|
|
|
{ |
|
|
// DeviceCode = deviceRequest.DeviceCode,
|
|
|
DeviceCode = deviceRequest.DeviceCode, |
|
|
// IsSuccess = false,
|
|
|
IsSuccess = false, |
|
|
// ErrorMessage = $"设备编号 {deviceRequest.DeviceCode} 的运行时状态不存在"
|
|
|
ErrorMessage = $"设备编号 {deviceRequest.DeviceCode} 已经在运行中" |
|
|
// });
|
|
|
}); |
|
|
// failureCount++;
|
|
|
failureCount++; |
|
|
// continue;
|
|
|
continue; |
|
|
// }
|
|
|
} |
|
|
|
|
|
|
|
|
// // 检查设备是否已经在运行
|
|
|
// 生成运行编码
|
|
|
// if (deviceRuntime.RuntimeStatus == DeviceRuntimeStatus.Running)
|
|
|
var runtimeCode = await GenerateRuntimeCodeAsync(deviceRequest.DeviceCode, cancellationToken); |
|
|
// {
|
|
|
|
|
|
// _logger.LogWarning("设备已经在运行中,设备编号: {DeviceCode}", deviceRequest.DeviceCode);
|
|
|
// 启动设备
|
|
|
// deviceResults.Add(new DeviceStartResult
|
|
|
deviceRuntime.Start(deviceRequest.NetworkStackCode, runtimeCode); |
|
|
// {
|
|
|
|
|
|
// DeviceCode = deviceRequest.DeviceCode,
|
|
|
// 更新到数据库
|
|
|
// IsSuccess = false,
|
|
|
_deviceRuntimeRepository.UpdateRuntime(deviceRuntime); |
|
|
// ErrorMessage = $"设备编号 {deviceRequest.DeviceCode} 已经在运行中"
|
|
|
|
|
|
// });
|
|
|
_logger.LogInformation("设备启动成功,设备编号: {DeviceCode}, 运行编码: {RuntimeCode}", |
|
|
// failureCount++;
|
|
|
deviceRuntime.DeviceCode, deviceRuntime.RuntimeCode); |
|
|
// continue;
|
|
|
|
|
|
// }
|
|
|
// 添加到结果列表
|
|
|
|
|
|
deviceResults.Add(new DeviceStartResult |
|
|
// // 生成运行编码
|
|
|
{ |
|
|
// var runtimeCode = await GenerateRuntimeCodeAsync(deviceRequest.DeviceCode, cancellationToken);
|
|
|
DeviceCode = deviceRuntime.DeviceCode, |
|
|
|
|
|
Id = deviceRuntime.Id, |
|
|
// // 启动设备
|
|
|
RuntimeStatus = deviceRuntime.RuntimeStatus.ToString(), |
|
|
// deviceRuntime.Start(deviceRequest.NetworkStackCode, runtimeCode);
|
|
|
NetworkStackCode = deviceRuntime.NetworkStackCode, |
|
|
|
|
|
UpdatedAt = deviceRuntime.UpdatedAt ?? DateTime.UtcNow, |
|
|
// // 更新到数据库
|
|
|
IsSuccess = true |
|
|
// _deviceRuntimeRepository.UpdateRuntime(deviceRuntime);
|
|
|
}); |
|
|
|
|
|
successCount++; |
|
|
// _logger.LogInformation("设备启动成功,设备编号: {DeviceCode}, 运行编码: {RuntimeCode}",
|
|
|
} |
|
|
// deviceRuntime.DeviceCode, deviceRuntime.RuntimeCode);
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
// // 添加到结果列表
|
|
|
_logger.LogError(ex, "启动设备失败,设备编号: {DeviceCode}", deviceRequest.DeviceCode); |
|
|
// deviceResults.Add(new DeviceStartResult
|
|
|
deviceResults.Add(new DeviceStartResult |
|
|
// {
|
|
|
{ |
|
|
// DeviceCode = deviceRuntime.DeviceCode,
|
|
|
DeviceCode = deviceRequest.DeviceCode, |
|
|
// Id = deviceRuntime.Id,
|
|
|
IsSuccess = false, |
|
|
// RuntimeStatus = deviceRuntime.RuntimeStatus.ToString(),
|
|
|
ErrorMessage = $"启动设备失败: {ex.Message}" |
|
|
// NetworkStackCode = deviceRuntime.NetworkStackCode,
|
|
|
}); |
|
|
// UpdatedAt = deviceRuntime.UpdatedAt ?? DateTime.UtcNow,
|
|
|
failureCount++; |
|
|
// IsSuccess = true
|
|
|
} |
|
|
// });
|
|
|
} |
|
|
// successCount++;
|
|
|
|
|
|
// }
|
|
|
// 保存所有更改
|
|
|
// catch (Exception ex)
|
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
// {
|
|
|
|
|
|
// _logger.LogError(ex, "启动设备失败,设备编号: {DeviceCode}", deviceRequest.DeviceCode);
|
|
|
// 构建响应
|
|
|
// deviceResults.Add(new DeviceStartResult
|
|
|
var response = new StartDeviceRuntimeResponse |
|
|
// {
|
|
|
{ |
|
|
// DeviceCode = deviceRequest.DeviceCode,
|
|
|
DeviceResults = deviceResults, |
|
|
// IsSuccess = false,
|
|
|
Summary = new BatchOperationSummary |
|
|
// ErrorMessage = $"启动设备失败: {ex.Message}"
|
|
|
{ |
|
|
// });
|
|
|
TotalCount = request.DeviceRequests.Count, |
|
|
// failureCount++;
|
|
|
SuccessCount = successCount, |
|
|
// }
|
|
|
FailureCount = failureCount |
|
|
//}
|
|
|
} |
|
|
|
|
|
}; |
|
|
//// 保存所有更改
|
|
|
|
|
|
//await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
|
_logger.LogInformation("批量启动设备完成,总数量: {TotalCount}, 成功: {SuccessCount}, 失败: {FailureCount}", |
|
|
|
|
|
response.Summary.TotalCount, response.Summary.SuccessCount, response.Summary.FailureCount); |
|
|
//// 构建响应
|
|
|
|
|
|
//var response = new StartDeviceRuntimeResponse
|
|
|
return OperationResult<StartDeviceRuntimeResponse>.CreateSuccess(response); |
|
|
//{
|
|
|
|
|
|
// DeviceResults = deviceResults,
|
|
|
|
|
|
// Summary = new BatchOperationSummary
|
|
|
|
|
|
// {
|
|
|
|
|
|
// TotalCount = request.DeviceRequests.Count,
|
|
|
|
|
|
// SuccessCount = successCount,
|
|
|
|
|
|
// FailureCount = failureCount
|
|
|
|
|
|
// }
|
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
|
|
//_logger.LogInformation("批量启动设备完成,总数量: {TotalCount}, 成功: {SuccessCount}, 失败: {FailureCount}",
|
|
|
|
|
|
// response.Summary.TotalCount, response.Summary.SuccessCount, response.Summary.FailureCount);
|
|
|
|
|
|
|
|
|
|
|
|
return OperationResult<StartDeviceRuntimeResponse>.CreateSuccess(null); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|