Commit ad4e221b 刘韬

优化监控相机重连机制

1 个父辈 958879e7
...@@ -345,6 +345,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -345,6 +345,7 @@ namespace OnlineStore.DeviceLibrary
runStatus = StoreRunStatus.Wait; runStatus = StoreRunStatus.Wait;
mainTimer.Enabled = false; mainTimer.Enabled = false;
CameraClose();
TimeSpan span = DateTime.Now - StartTime; TimeSpan span = DateTime.Now - StartTime;
LogInfo(",停止运行,总运行时间:" + span.ToString()); LogInfo(",停止运行,总运行时间:" + span.ToString());
} }
......
...@@ -20,6 +20,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -20,6 +20,7 @@ namespace OnlineStore.DeviceLibrary
public event EventHandler<Bitmap> camera_event; public event EventHandler<Bitmap> camera_event;
public string CameraDeviceName = "monitor1"; public string CameraDeviceName = "monitor1";
bool loop = true;
public void LoadCameraConfig(string id) public void LoadCameraConfig(string id)
{ {
string path = @".\StoreConfig\box_" + id + "_Camera.json"; string path = @".\StoreConfig\box_" + id + "_Camera.json";
...@@ -52,6 +53,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -52,6 +53,7 @@ namespace OnlineStore.DeviceLibrary
{ {
LogUtil.error(Name + $"监控相机打开失败"); LogUtil.error(Name + $"监控相机打开失败");
} }
loop = true;
camerathread = new Thread(new ThreadStart(startCamera)); camerathread = new Thread(new ThreadStart(startCamera));
camerathread.Start(); camerathread.Start();
GC.KeepAlive(camerathread); GC.KeepAlive(camerathread);
...@@ -60,7 +62,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -60,7 +62,7 @@ namespace OnlineStore.DeviceLibrary
int errortimes = 0; int errortimes = 0;
void startCamera() void startCamera()
{ {
while (true) while (loop)
{ {
try try
{ {
...@@ -77,15 +79,13 @@ namespace OnlineStore.DeviceLibrary ...@@ -77,15 +79,13 @@ namespace OnlineStore.DeviceLibrary
} }
else if (errortimes == 5) else if (errortimes == 5)
{ {
camera.Open(); camera.Close(CameraDeviceName);
errortimes++; Thread.Sleep(1000);
camera.Open(CameraDeviceName);
errortimes = 0;
LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}"); LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}");
} Thread.Sleep(5000);
else if (errortimes == 6) }
{
LogUtil.error(Name + $"相机连接失败, 相机线程退出,{errortimes}");
break;
}
Thread.Sleep(1000 / 7); Thread.Sleep(1000 / 7);
} }
catch catch
...@@ -93,9 +93,14 @@ namespace OnlineStore.DeviceLibrary ...@@ -93,9 +93,14 @@ namespace OnlineStore.DeviceLibrary
errortimes++; errortimes++;
} }
} }
camera.Close(); camera.Close(CameraDeviceName);
camera.Dispose(); camera.Dispose();
} }
public void CameraClose()
{
LogUtil.info(Name + "线程退出");
loop = false;
}
void CameraGrabOne(string filename) void CameraGrabOne(string filename)
{ {
try try
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!