图像显示
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
this.imageBox.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.imageBox.Location = new System.Drawing.Point(0, 0);
|
||||
this.imageBox.Name = "imageBox";
|
||||
this.imageBox.Size = new System.Drawing.Size(150, 150);
|
||||
this.imageBox.Size = new System.Drawing.Size(512, 384);
|
||||
this.imageBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
|
||||
this.imageBox.TabIndex = 0;
|
||||
this.imageBox.TabStop = false;
|
||||
@@ -35,6 +35,7 @@
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.Controls.Add(this.imageBox);
|
||||
this.Name = "Camera";
|
||||
this.Size = new System.Drawing.Size(512, 384);
|
||||
this.Load += new System.EventHandler(this.Camera_Load);
|
||||
((System.ComponentModel.ISupportInitialize)(this.imageBox)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
@@ -23,12 +23,26 @@ namespace JoyD.Windows.CS.Toprie
|
||||
private System.Windows.Forms.Timer _errorDisplayTimer;
|
||||
|
||||
public Camera()
|
||||
{
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
// 将设计模式状态传递给DeviceManager
|
||||
DeviceManager.IsDesignMode = DesignMode;
|
||||
|
||||
// 只有在非设计模式下才初始化设备管理器和错误定时器
|
||||
if (!DesignMode)
|
||||
{
|
||||
// 清空现有日志
|
||||
try
|
||||
{
|
||||
string logFile = Path.Combine(Application.StartupPath, "camera.log");
|
||||
if (File.Exists(logFile))
|
||||
{
|
||||
File.WriteAllText(logFile, string.Empty);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
InitializeDeviceManager();
|
||||
InitializeErrorTimer();
|
||||
}
|
||||
@@ -60,13 +74,16 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// 初始化设备管理器
|
||||
/// </summary>
|
||||
private void InitializeDeviceManager()
|
||||
{
|
||||
{
|
||||
_deviceManager = new DeviceManager
|
||||
{
|
||||
{
|
||||
AutoReconnectEnabled = true,
|
||||
ReconnectInterval = 2000 // 2秒
|
||||
};
|
||||
|
||||
// 确保DeviceManager的设计模式状态与控件一致
|
||||
DeviceManager.IsDesignMode = DesignMode;
|
||||
|
||||
// 设置静态属性
|
||||
DeviceManager.MaxReconnectAttempts = 5;
|
||||
|
||||
|
||||
@@ -118,6 +118,9 @@ namespace JoyD.Windows.CS.Toprie
|
||||
|
||||
public class DeviceManager : IDisposable
|
||||
{
|
||||
// 设计模式标志,用于在设计模式下跳过实际的设备连接和初始化
|
||||
public static bool IsDesignMode { get; set; } = false;
|
||||
|
||||
// A8SDK实例
|
||||
private A8SDK _a8Sdk;
|
||||
// 设备ID列表
|
||||
@@ -209,20 +212,48 @@ namespace JoyD.Windows.CS.Toprie
|
||||
try
|
||||
{
|
||||
bool isAvailable = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
|
||||
Console.WriteLine($"网络可用性检查: {(isAvailable ? "可用" : "不可用")}");
|
||||
Log($"网络可用性检查: {(isAvailable ? "可用" : "不可用")}");
|
||||
return isAvailable;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"网络检查异常: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 捕获异常: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private TcpClient _imageTcpClient;
|
||||
private bool _isInfraredMode = true;
|
||||
private static readonly object _logLock = new object();
|
||||
|
||||
#region 私有方法
|
||||
|
||||
/// <summary>
|
||||
/// 记录日志到控制台和文件
|
||||
/// </summary>
|
||||
private void Log(string message)
|
||||
{
|
||||
// 输出到控制台
|
||||
Console.WriteLine(message);
|
||||
|
||||
// 输出到日志文件
|
||||
try
|
||||
{
|
||||
lock (_logLock)
|
||||
{
|
||||
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.txt");
|
||||
// 确保目录存在
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(logPath));
|
||||
// 写入日志,包含时间戳
|
||||
File.AppendAllText(logPath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}\r\n");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果写入日志文件失败,只输出到控制台
|
||||
Console.WriteLine($"日志写入失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从设备字符串中解析设备ID
|
||||
/// </summary>
|
||||
@@ -253,10 +284,11 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// <param name="exception">相关异常(如果有)</param>
|
||||
private void UpdateConnectionStatus(ConnectionStatus newStatus, string message = null, Exception exception = null)
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] UpdateConnectionStatus() - 尝试更新状态: {_connectionStatus} -> {newStatus}, 消息: {message}");
|
||||
// 检查对象是否已被释放
|
||||
if (_isDisposed)
|
||||
{
|
||||
Console.WriteLine("警告: 尝试在已释放对象上更新连接状态");
|
||||
Log("警告: 尝试在已释放对象上更新连接状态");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -264,6 +296,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
ConnectionStatus oldStatus = _connectionStatus;
|
||||
_connectionStatus = newStatus;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] UpdateConnectionStatus() - 状态已更新: {oldStatus} -> {newStatus}");
|
||||
|
||||
// 触发连接状态变更事件前再次检查对象是否已被释放
|
||||
if (!_isDisposed)
|
||||
@@ -274,18 +307,20 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"触发连接状态变更事件异常: {ex.Message}");
|
||||
Log($"触发连接状态变更事件异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 异常处理完成");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 连接检查正常完成");
|
||||
|
||||
Console.WriteLine($"连接状态变更: {oldStatus} -> {newStatus}");
|
||||
Log($"连接状态变更: {oldStatus} -> {newStatus}");
|
||||
if (!string.IsNullOrEmpty(message))
|
||||
{
|
||||
Console.WriteLine($"状态消息: {message}");
|
||||
Log($"状态消息: {message}");
|
||||
}
|
||||
if (exception != null)
|
||||
{
|
||||
Console.WriteLine($"异常信息: {exception.Message}");
|
||||
Log($"异常信息: {exception.Message}");
|
||||
}
|
||||
|
||||
// 如果断开连接且启用了自动重连,启动重连机制
|
||||
@@ -315,13 +350,28 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// <returns>是否初始化成功</returns>
|
||||
private bool Initialize()
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] Initialize() - 开始执行初始化");
|
||||
|
||||
// 在设计模式下,跳过实际初始化,直接返回成功
|
||||
if (IsDesignMode)
|
||||
{
|
||||
Log("设计模式下跳过实际初始化,模拟初始化成功");
|
||||
_isInitialized = true;
|
||||
_connectionStatus = ConnectionStatus.Connected;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 双重检查锁定模式,确保线程安全的初始化
|
||||
if (_isInitialized)
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] Initialize() - 已经初始化,直接返回成功");
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 清理旧资源(如果有)
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] Initialize() - 清理旧资源");
|
||||
CleanupResources();
|
||||
|
||||
// 初始化设备ID列表和相关变量
|
||||
@@ -332,12 +382,12 @@ namespace JoyD.Windows.CS.Toprie
|
||||
_connectionStatus = ConnectionStatus.Disconnected;
|
||||
_currentReconnectAttempt = 0;
|
||||
|
||||
Console.WriteLine("开始SDK初始化...");
|
||||
Log("开始SDK初始化...");
|
||||
|
||||
// 首先检查网络可用性
|
||||
if (!IsNetworkAvailable())
|
||||
{
|
||||
Console.WriteLine("网络不可用,初始化暂缓");
|
||||
Log("网络不可用,初始化暂缓");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -352,7 +402,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
if (retry > 0)
|
||||
{
|
||||
Console.WriteLine($"SDK初始化重试 {retry}/{maxInitRetries}");
|
||||
Log($"SDK初始化重试 {retry}/{maxInitRetries}");
|
||||
Thread.Sleep(300); // 重试前等待一小段时间
|
||||
}
|
||||
|
||||
@@ -362,36 +412,38 @@ namespace JoyD.Windows.CS.Toprie
|
||||
break; // 成功,跳出循环
|
||||
}
|
||||
|
||||
Console.WriteLine($"SDK静态初始化失败,返回值: {initResult}");
|
||||
Log($"SDK静态初始化失败,返回值: {initResult}");
|
||||
}
|
||||
catch (Exception initEx)
|
||||
{
|
||||
Console.WriteLine($"SDK初始化异常: {initEx.Message},堆栈: {initEx.StackTrace}");
|
||||
Log($"SDK初始化异常: {initEx.Message},堆栈: {initEx.StackTrace}");
|
||||
}
|
||||
}
|
||||
|
||||
if (initResult != 0)
|
||||
{
|
||||
Console.WriteLine($"SDK静态初始化失败,所有重试均未成功,返回值: {initResult}");
|
||||
Log($"SDK静态初始化失败,所有重试均未成功,返回值: {initResult}");
|
||||
_isInitialized = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
Console.WriteLine("SDK静态初始化成功");
|
||||
Log("SDK静态初始化成功");
|
||||
|
||||
// 设置默认配置参数
|
||||
_maxReconnectAttempts = 15; // 增加最大重连次数
|
||||
_isAutoReconnectEnabled = true;
|
||||
|
||||
_isInitialized = true;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] Initialize() - 初始化成功,_isInitialized设为true");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SDK初始化失败: {ex.Message},堆栈: {ex.StackTrace}");
|
||||
Log($"SDK初始化失败: {ex.Message},堆栈: {ex.StackTrace}");
|
||||
_isInitialized = false;
|
||||
OnConnectionException(new ConnectionExceptionEventArgs(ex, "初始化设备管理器失败"));
|
||||
return false;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] Initialize() - 初始化异常,_isInitialized保持false: {ex.Message}");
|
||||
OnConnectionException(new ConnectionExceptionEventArgs(ex, "初始化设备管理器失败"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,6 +452,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// </summary>
|
||||
private void CleanupResources()
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CleanupResources() - 开始清理资源");
|
||||
try
|
||||
{
|
||||
// 停止所有定时器
|
||||
@@ -413,13 +466,13 @@ namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("释放旧SDK实例...");
|
||||
Log("释放旧SDK实例...");
|
||||
// 注意:如果SDK提供了destroy方法,应该在这里调用
|
||||
// A8SDK.SDK_destroy();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"释放SDK实例异常: {ex.Message}");
|
||||
Log($"释放SDK实例异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,12 +483,13 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// _deviceIp = string.Empty; // 注释掉这行,避免IP地址在资源清理时被清空
|
||||
_currentReconnectAttempt = 0;
|
||||
_isInitialized = false;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CleanupResources() - _isInitialized已设为false");
|
||||
|
||||
Console.WriteLine("资源清理完成");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CleanupResources() - 资源清理完成");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"清理资源异常: {ex.Message}");
|
||||
Log($"清理资源异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -620,7 +674,8 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 如果初始化失败但显示已连接,尝试重新初始化
|
||||
if (!_isInitialized)
|
||||
{
|
||||
Console.WriteLine("警告: 显示已连接但初始化状态为false,尝试重新初始化...");
|
||||
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] 警告: 显示已连接但初始化状态为false,尝试重新初始化...");
|
||||
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] 当前状态: _connectionStatus={_connectionStatus}, _isInitialized={_isInitialized}, _a8Sdk={(_a8Sdk == null ? "null" : "已初始化")}, _currentDeviceId={_currentDeviceId}");
|
||||
if (!Initialize())
|
||||
{
|
||||
Console.WriteLine("重新初始化失败,确认连接已断开");
|
||||
@@ -644,7 +699,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"定时器回调异常: {ex.Message}");
|
||||
Log($"定时器回调异常: {ex.Message}");
|
||||
// 异常时如果是已连接状态,将其设为断开
|
||||
if (_connectionStatus == ConnectionStatus.Connected)
|
||||
{
|
||||
@@ -663,13 +718,13 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"创建连接检查定时器失败: {ex.Message}");
|
||||
Log($"创建连接检查定时器失败: {ex.Message}");
|
||||
_connectionCheckTimer = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"启动连接检查异常: {ex.Message}");
|
||||
Log($"启动连接检查异常: {ex.Message}");
|
||||
StopConnectionCheck();
|
||||
}
|
||||
}
|
||||
@@ -677,44 +732,52 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 连接检查的安全包装方法
|
||||
private void CheckConnectionWrapper()
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 开始连接检查");
|
||||
try
|
||||
{
|
||||
// 检查连接有效性
|
||||
bool isStillConnected = CheckConnectionValidity();
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - CheckConnectionValidity返回: {isStillConnected}");
|
||||
|
||||
// 连接状态变化时更新状态
|
||||
if (!isStillConnected && _connectionStatus == ConnectionStatus.Connected)
|
||||
{
|
||||
Console.WriteLine("检测到连接已断开");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 检测到连接已断开: _connectionStatus={_connectionStatus}, isStillConnected={isStillConnected}, _isInitialized={_isInitialized}");
|
||||
// 断开连接时重置初始化状态和SDK实例
|
||||
_isInitialized = false;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 重置初始化状态为false");
|
||||
_a8Sdk = null;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 释放SDK实例");
|
||||
UpdateConnectionStatus(ConnectionStatus.Disconnected, "连接已断开:设备离线");
|
||||
// 断开连接后自动启动重连
|
||||
if (_autoReconnectEnabled)
|
||||
{
|
||||
Console.WriteLine("连接断开,启动自动重连");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 连接断开,启动自动重连");
|
||||
StartAutoReconnect();
|
||||
}
|
||||
}
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 连接检查正常完成");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"检查连接状态异常: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 检查连接状态异常: {ex.Message}");
|
||||
// 检查异常时,将状态设为断开
|
||||
if (_connectionStatus == ConnectionStatus.Connected)
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 异常时连接状态为Connected,需要重置");
|
||||
// 异常时重置初始化状态和SDK实例
|
||||
_isInitialized = false;
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 异常时重置初始化状态为false");
|
||||
_a8Sdk = null;
|
||||
UpdateConnectionStatus(ConnectionStatus.Disconnected, "连接状态检查异常", ex);
|
||||
// 异常时启动自动重连
|
||||
if (_autoReconnectEnabled)
|
||||
{
|
||||
Console.WriteLine("检查异常,启动自动重连");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 检查异常,启动自动重连");
|
||||
StartAutoReconnect();
|
||||
}
|
||||
}
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 异常处理完成");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -723,27 +786,42 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// </summary>
|
||||
/// <returns>连接是否有效</returns>
|
||||
private bool CheckConnectionValidity()
|
||||
{
|
||||
{
|
||||
// 在设计模式下,跳过实际的连接有效性检查,直接返回连接有效
|
||||
if (IsDesignMode)
|
||||
{
|
||||
Log("设计模式下跳过实际的连接有效性检查,模拟连接有效");
|
||||
return true;
|
||||
}
|
||||
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 开始检查连接有效性");
|
||||
try
|
||||
{
|
||||
// 记录当前状态信息
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 当前状态: _currentDeviceId={_currentDeviceId}, _deviceIp={_deviceIp}, _a8Sdk={(null == _a8Sdk ? "null" : "存在")}");
|
||||
|
||||
// 重要修改:先检查设备ID有效性,避免后续无效检查
|
||||
if (_currentDeviceId == -1)
|
||||
{
|
||||
Console.WriteLine("当前设备ID无效");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 当前设备ID无效,返回false");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 优化1: 优先尝试ping设备IP地址,这是物理连接断开的最直接检测
|
||||
if (!PingDevice(_deviceIp))
|
||||
bool pingResult = PingDevice(_deviceIp);
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - ping检测结果: {pingResult}");
|
||||
if (!pingResult)
|
||||
{
|
||||
Console.WriteLine($"设备IP {_deviceIp} 不可达,物理连接可能已断开");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 设备IP {_deviceIp} 不可达,物理连接可能已断开,返回false");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 优化2: 系统网络接口检测作为辅助检查,不再作为首要条件
|
||||
if (!IsNetworkAvailable())
|
||||
bool networkAvailable = IsNetworkAvailable();
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 网络可用性检测结果: {networkAvailable}");
|
||||
if (!networkAvailable)
|
||||
{
|
||||
Console.WriteLine("警告: 系统网络连接不可用,但ping检测通过");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 警告: 系统网络连接不可用,但ping检测通过");
|
||||
}
|
||||
|
||||
// 优化3: 加强SDK连接状态验证,参考热像仪示例的实现方式
|
||||
@@ -752,8 +830,9 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 使用线程安全的SDK实例创建
|
||||
if (_a8Sdk == null)
|
||||
{
|
||||
Console.WriteLine("SDK实例不存在,重新初始化...");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK实例不存在,重新初始化...");
|
||||
_a8Sdk = new A8SDK(_deviceIp);
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK实例已创建");
|
||||
}
|
||||
|
||||
// 增强心跳检测重试机制,提高连接稳定性
|
||||
@@ -764,53 +843,55 @@ namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"SDK心跳检测...(尝试 {retry + 1}/{maxHeartbeatRetries + 1})");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK心跳检测...(尝试 {retry + 1}/{maxHeartbeatRetries + 1})");
|
||||
heartbeatResult = _a8Sdk.Heartbeat();
|
||||
|
||||
// 严格检查返回值,确保连接有效
|
||||
if (heartbeatResult == 0) // 参考Toprie项目,假设0表示成功
|
||||
{
|
||||
Console.WriteLine("SDK心跳检测成功");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK心跳检测成功");
|
||||
break;
|
||||
}
|
||||
else if (retry < maxHeartbeatRetries)
|
||||
{
|
||||
Console.WriteLine($"SDK心跳检测失败,返回代码: {heartbeatResult},等待500ms后重试...");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK心跳检测失败,返回代码: {heartbeatResult},等待500ms后重试...");
|
||||
Thread.Sleep(500); // 增加重试间隔到500ms
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("SDK心跳检测多次失败,尝试重建SDK连接...");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK心跳检测多次失败,尝试重建SDK连接...");
|
||||
// 安全释放旧的SDK实例
|
||||
A8SDK oldSdk = Interlocked.Exchange(ref _a8Sdk, null);
|
||||
// 尝试重建SDK连接
|
||||
Thread.Sleep(500);
|
||||
_a8Sdk = new A8SDK(_deviceIp);
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 已重建SDK连接,进行最后一次心跳检测");
|
||||
Thread.Sleep(500);
|
||||
heartbeatResult = _a8Sdk.Heartbeat();
|
||||
|
||||
if (heartbeatResult == 0)
|
||||
{
|
||||
Console.WriteLine("SDK重新连接成功");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK重新连接成功");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"SDK重新连接失败,返回代码: {heartbeatResult}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK重新连接失败,返回代码: {heartbeatResult}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"心跳检测异常: {ex.Message}");
|
||||
if (retry < maxHeartbeatRetries)
|
||||
{
|
||||
Thread.Sleep(500); // 增加重试间隔到500ms
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 心跳检测异常: {ex.Message}");
|
||||
if (retry < maxHeartbeatRetries)
|
||||
{
|
||||
Thread.Sleep(500); // 增加重试间隔到500ms
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (heartbeatResult != 0)
|
||||
{
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 心跳检测最终失败,返回false");
|
||||
// 心跳失败时,安全释放SDK实例
|
||||
if (_a8Sdk != null)
|
||||
{
|
||||
@@ -822,19 +903,20 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 添加额外的连接验证步骤
|
||||
try
|
||||
{
|
||||
Console.WriteLine("进行额外连接验证...");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 进行额外连接验证...");
|
||||
// 再次发送心跳包确保连接稳定
|
||||
int finalResult = _a8Sdk.Heartbeat();
|
||||
if (finalResult != 0)
|
||||
{
|
||||
Console.WriteLine($"最终验证失败,返回代码: {finalResult}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 最终验证失败,返回代码: {finalResult},返回false");
|
||||
_a8Sdk = null;
|
||||
return false;
|
||||
}
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 最终验证成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"连接验证异常: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 连接验证异常: {ex.Message},返回false");
|
||||
_a8Sdk = null;
|
||||
return false;
|
||||
}
|
||||
@@ -851,24 +933,25 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"获取设备名称异常,但心跳检测成功: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 获取设备名称异常,但心跳检测成功: {ex.Message}");
|
||||
// 设备名称获取失败不应导致整体连接失效
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SDK心跳检测失败: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - SDK心跳检测失败: {ex.Message},返回false");
|
||||
// 确保异常时SDK实例被释放
|
||||
_a8Sdk = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 所有检查通过,连接有效
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 所有检查通过,连接有效,返回true");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"连接有效性检查异常: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionValidity() - 连接有效性检查异常: {ex.Message},返回false");
|
||||
// 确保异常时SDK实例被释放
|
||||
_a8Sdk = null;
|
||||
return false;
|
||||
@@ -890,12 +973,12 @@ namespace JoyD.Windows.CS.Toprie
|
||||
timerToDispose.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
// 安全释放定时器资源
|
||||
timerToDispose.Dispose();
|
||||
Console.WriteLine("连接检查已停止");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] StopConnectionCheck() - 连接检查已停止");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"停止连接检查异常: {ex.Message}");
|
||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] StopConnectionCheck() - 停止连接检查异常: {ex.Message}");
|
||||
// 确保即使异常,引用也被清空
|
||||
_connectionCheckTimer = null;
|
||||
}
|
||||
@@ -1077,7 +1160,14 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// 开始接收图像数据
|
||||
/// </summary>
|
||||
public void StartImageReceiving()
|
||||
{
|
||||
{
|
||||
// 在设计模式下,跳过实际的图像接收
|
||||
if (IsDesignMode)
|
||||
{
|
||||
Log("设计模式下跳过实际的图像接收");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("开始使用HTTP方式接收图像数据");
|
||||
try
|
||||
{
|
||||
@@ -1981,7 +2071,18 @@ namespace JoyD.Windows.CS.Toprie
|
||||
public void ConnectDevice(int deviceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
{
|
||||
// 在设计模式下,跳过实际连接,直接模拟连接成功
|
||||
if (IsDesignMode)
|
||||
{
|
||||
Log("设计模式下跳过实际设备连接,模拟连接成功");
|
||||
_currentDeviceId = deviceId;
|
||||
_isConnected = true;
|
||||
_connectionStatus = ConnectionStatus.Connected;
|
||||
UpdateConnectionStatus(ConnectionStatus.Connected, "设计模式:设备模拟连接成功");
|
||||
return;
|
||||
}
|
||||
|
||||
// 取消之前的连接操作
|
||||
if (_connectCancellationTokenSource != null)
|
||||
{
|
||||
@@ -2665,7 +2766,14 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// 开始心跳检测
|
||||
/// </summary>
|
||||
private void StartHeartbeat()
|
||||
{
|
||||
{
|
||||
// 在设计模式下,跳过实际的心跳检测
|
||||
if (IsDesignMode)
|
||||
{
|
||||
Log("设计模式下跳过实际的心跳检测");
|
||||
return;
|
||||
}
|
||||
|
||||
// 停止现有的心跳定时器
|
||||
StopHeartbeat();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user