From 9613aa201bccbfa9f60ca2d0500975aba5167a3d Mon Sep 17 00:00:00 2001
From: root <295172551@qq.com>
Date: Fri, 1 Aug 2025 00:43:15 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E8=AE=BE=E5=A4=87=E7=BC=96?=
=?UTF-8?q?=E7=A0=81=20=E7=BD=91=E7=BB=9C=E8=BF=90=E8=A1=8C=E7=BC=96?=
=?UTF-8?q?=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Network/ICellularNetworkContext.cs | 7 +++++
.../Contexts/CellularNetworkContext.cs | 28 +++++++++++++++++++
.../Network/GeneralCellularNetworkService.cs | 2 ++
.../Network/NetworkProtocolLogObserver.cs | 10 +++++--
.../Models/MessageTransferProtocolLog.cs | 10 +++++++
5 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs b/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
index 7f4d63b..a82de91 100644
--- a/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
+++ b/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
@@ -20,6 +20,11 @@ public interface ICellularNetworkContext
///
bool IsInitialized { get; }
+ ///
+ /// 设备编码
+ ///
+ string DeviceCode { get; }
+
///
/// 网络IP端点管理器
///
@@ -41,6 +46,8 @@ public interface ICellularNetworkContext
/// 网络配置键
void Initialize(string neConfigKey);
+ void SetDeviceCode(string deviceCode);
+
///
/// 重置上下文状态
///
diff --git a/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs b/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs
index c3bc1bc..a81e885 100644
--- a/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs
+++ b/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 _logger;
@@ -33,6 +34,10 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
/// 是否已初始化
///
public bool IsInitialized => _isInitialized;
+ ///
+ /// 设备编码
+ ///
+ public string DeviceCode => _deviceCode;
///
/// 网络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}");
+ }
+ }
+
///
/// 更新网络配置类型
///
@@ -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();
diff --git a/CoreAgent.Infrastructure/Services/Network/GeneralCellularNetworkService.cs b/CoreAgent.Infrastructure/Services/Network/GeneralCellularNetworkService.cs
index 56ae847..0a2c3e4 100644
--- a/CoreAgent.Infrastructure/Services/Network/GeneralCellularNetworkService.cs
+++ b/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)
diff --git a/CoreAgent.Infrastructure/Services/Network/NetworkProtocolLogObserver.cs b/CoreAgent.Infrastructure/Services/Network/NetworkProtocolLogObserver.cs
index 89e3454..c05aab3 100644
--- a/CoreAgent.Infrastructure/Services/Network/NetworkProtocolLogObserver.cs
+++ b/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 _logger;
private readonly IMessageChannelManager _ChannelManager;
- public NetworkProtocolLogObserver(ILogger logger, IMessageChannelManager channelManager)
+ private readonly ICellularNetworkContext _context;
+ public NetworkProtocolLogObserver(ILogger logger, IMessageChannelManager channelManager, ICellularNetworkContext context)
{
this._logger = logger;
this._ChannelManager = channelManager;
+ this._context = context ?? throw new ArgumentNullException(nameof(context));
}
public void OnProtocolLogsReceived(IEnumerable 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());
// 尝试写入通道并跟踪结果
diff --git a/CoreAgent.WebSocketTransport/Models/MessageTransferProtocolLog.cs b/CoreAgent.WebSocketTransport/Models/MessageTransferProtocolLog.cs
index c1264f3..cbf0c5c 100644
--- a/CoreAgent.WebSocketTransport/Models/MessageTransferProtocolLog.cs
+++ b/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;
}
+ ///
+ /// 设备代码
+ ///
+ public string? DeviceCode { get; set; }
+
+ ///
+ /// 运行时代码
+ ///
+ public string RuntimeCode { get; set; } = null!;
}
}
\ No newline at end of file