Browse Source

新建设备编码 网络运行编码

feature/protocol-log-Perfect
root 3 days ago
parent
commit
9613aa201b
  1. 7
      CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
  2. 28
      CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs
  3. 2
      CoreAgent.Infrastructure/Services/Network/GeneralCellularNetworkService.cs
  4. 10
      CoreAgent.Infrastructure/Services/Network/NetworkProtocolLogObserver.cs
  5. 10
      CoreAgent.WebSocketTransport/Models/MessageTransferProtocolLog.cs

7
CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs

@ -20,6 +20,11 @@ public interface ICellularNetworkContext
/// </summary>
bool IsInitialized { get; }
/// <summary>
/// 设备编码
/// </summary>
string DeviceCode { get; }
/// <summary>
/// 网络IP端点管理器
/// </summary>
@ -41,6 +46,8 @@ public interface ICellularNetworkContext
/// <param name="neConfigKey">网络配置键</param>
void Initialize(string neConfigKey);
void SetDeviceCode(string deviceCode);
/// <summary>
/// 重置上下文状态
/// </summary>

28
CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs

@ -20,6 +20,7 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
private CancellationTokenSource _token;
private bool _isDisposed;
private bool _isInitialized;
private string _deviceCode = string.Empty;
private readonly INetworkIPEndPointManager _networkIPEndPointManager;
private NetworkConfigType _currentConfigType;
private readonly ILogger<CellularNetworkContext> _logger;
@ -33,6 +34,10 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
/// 是否已初始化
/// </summary>
public bool IsInitialized => _isInitialized;
/// <summary>
/// 设备编码
/// </summary>
public string DeviceCode => _deviceCode;
/// <summary>
/// 网络IP端点管理器
@ -92,6 +97,28 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
}
}
public void SetDeviceCode(string deviceCode)
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(CellularNetworkContext));
}
if (!_isInitialized)
{
throw new InvalidOperationException("上下文未初始化");
}
if (string.IsNullOrEmpty(deviceCode))
{
throw new ArgumentNullException(nameof(deviceCode));
}
lock (_lock)
{
_deviceCode = deviceCode;
_logger.LogInformation($"设备代码已设置为: {deviceCode}");
}
}
/// <summary>
/// 更新网络配置类型
/// </summary>
@ -246,6 +273,7 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
_token?.Dispose();
_token = new CancellationTokenSource();
_neConfigKey = string.Empty;
_deviceCode =string.Empty;
_isInitialized = false;
_networkState = new CellularNetworkState(string.Empty);
_networkIPEndPointManager.Clear();

2
CoreAgent.Infrastructure/Services/Network/GeneralCellularNetworkService.cs

@ -100,6 +100,8 @@ namespace CoreAgent.Infrastructure.Services.Network
// 4. 初始化网络上下文
_context.Initialize(key);
_context.SetDeviceCode(cellular.DeviceCode);
// 5. 启动网络
var result = await StartNetworkAsync(cellular);
if (!result.IsSuccess)

10
CoreAgent.Infrastructure/Services/Network/NetworkProtocolLogObserver.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreAgent.Domain.Interfaces.Network;
using CoreAgent.ProtocolClient.Models;
using CoreAgent.ProtocolClient.ProtocolEngineCore;
using CoreAgent.WebSocketTransport.Interfaces;
@ -15,10 +16,12 @@ namespace CoreAgent.Infrastructure.Services.Network
{
private readonly ILogger<NetworkProtocolLogObserver> _logger;
private readonly IMessageChannelManager _ChannelManager;
public NetworkProtocolLogObserver(ILogger<NetworkProtocolLogObserver> logger, IMessageChannelManager channelManager)
private readonly ICellularNetworkContext _context;
public NetworkProtocolLogObserver(ILogger<NetworkProtocolLogObserver> logger, IMessageChannelManager channelManager, ICellularNetworkContext context)
{
this._logger = logger;
this._ChannelManager = channelManager;
this._context = context ?? throw new ArgumentNullException(nameof(context));
}
public void OnProtocolLogsReceived(IEnumerable<TransferProtocolLog> logDetails)
{
@ -60,7 +63,10 @@ namespace CoreAgent.Infrastructure.Services.Network
TimeMs = log.TimeMs,
Timestamp = log.Timestamp,
Info = log.Info,
Message = log.Message
Message = log.Message,
DeviceCode = _context.DeviceCode,
RuntimeCode = _context.GetNeConfigKey(),
});
ProtocolMessage message = new ProtocolMessage(webSocketLogs.ToArray());
// 尝试写入通道并跟踪结果

10
CoreAgent.WebSocketTransport/Models/MessageTransferProtocolLog.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -95,6 +96,15 @@ namespace CoreAgent.WebSocketTransport.Models
set => TimeMs = (long)value.TotalMilliseconds;
}
/// <summary>
/// 设备代码
/// </summary>
public string? DeviceCode { get; set; }
/// <summary>
/// 运行时代码
/// </summary>
public string RuntimeCode { get; set; } = null!;
}
}
Loading…
Cancel
Save