增加日志

This commit is contained in:
2025-04-01 18:15:30 +08:00
parent e08386333a
commit 409089e2ca
21 changed files with 1838 additions and 354 deletions

View File

@ -14,6 +14,7 @@ using DHSoftware.Utils;
using DHSoftware.Views;
using DVPCameraType;
using HalconDotNet;
using Microsoft.VisualBasic.Logging;
using Microsoft.Win32;
using OpenCvSharp;
using SqlSugar;
@ -33,6 +34,7 @@ using XKRS.UI.Device.Winform;
using static AntdUI.Math3D;
using static DH.Commons.Enums.EnumHelper;
using Camera = DHSoftware.Models.Camera;
using LogLevel = DH.Commons.Enums.EnumHelper.LogLevel;
using ResultState = DH.Commons.Base.ResultState;
namespace DHSoftware
@ -40,7 +42,7 @@ namespace DHSoftware
public partial class MainWindow : AntdUI.Window
{
private Dictionary<string, List<string>> _cameraRelatedDetectionDict = null;
public event Action<LogMsg> OnLog;
private string _loginName;
public string LoginName
@ -262,12 +264,15 @@ namespace DHSoftware
var cameraBase = ConfigModel.CameraBaseList[i];
if (cameraBase.CamType == EnumCamType.Do3think)
{
Do3ThinkCamera cam = new Do3ThinkCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled = cameraBase.IsEnabled;
Cameras.Add(cam);
cam.OnLog -= _visionEngine_OnLog;
cam.OnLog += _visionEngine_OnLog;
cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
@ -300,12 +305,14 @@ namespace DHSoftware
PLC.PLCName = plcBase.PLCName;
PLC.PLCItemList = plcBase.PLCItemList;
PLC.Port = plcBase.Port;
PLC.OnLog -= _visionEngine_OnLog;
PLC.OnLog += _visionEngine_OnLog;
PLC.PLCConnect();
}
}
}
}
FrmLog frmLog;
public void InitModel()
{
Dectection.Clear();
@ -323,6 +330,7 @@ namespace DHSoftware
detectionConfig.ModelHeight = detection.ModelHeight;
detectionConfig.In_lable_path = detection.In_lable_path;
detectionConfig.IsEnabled = detection.IsEnabled;
detectionConfig.ShowLocation.X = (i + 1) % 5 + (i + 1) / 5;
// detectionConfig.ShowLocation.X = detection.ShowLocation.X;
detectionConfig.ShowLocation.Y = (i + 1) / 5 + 1;
@ -351,22 +359,66 @@ namespace DHSoftware
});
string inferenceDevice = "CPU";
frmLog = new FrmLog();
frmLog.Dock = DockStyle.Fill;
pnlLog.Controls.Add(frmLog);
//
_visionEngine = new SimboVisionDriver();
_visionEngine.DetectionConfigs = DetectionConfigs;
_visionEngine.OnLog += _visionEngine_OnLog;
//初始化模型 加载模型
_visionEngine.Init();
CtrlVisionRunBase ctrlVisionRun = new CtrlVisionRunBase(_visionEngine);
ctrlVisionRun.Dock = DockStyle.Fill;
tabImgDisplay.Controls.Add(ctrlVisionRun);
}
private void _visionEngine_OnLog(LogMsg msg)
{
OnLog?.Invoke(msg);
LogDisplay(msg);
}
private void LogDisplay(LogMsg msg)
{
frmLog?.LogDisplay(msg);
}
public LoggerHelper LoggerHelper { get; set; } = new LoggerHelper();
public virtual void LogAsync(LogMsg msg)
{
//if (IConfig != null)
//{
// LoggerHelper.SetLogLevel(IConfig.DefaultLogLevel);
//}
//else
//{
// LoggerHelper.SetLogLevel(LogLevel.Assist);
//}
msg.ThreadId = Thread.CurrentThread.ManagedThreadId;
OnLog?.Invoke(msg);
//if (IConfig?.IsLogEnabled ?? true)
//{
LoggerHelper.LogAsync(msg);
//}
}
public virtual void LogAsync(DateTime dt, LogLevel logLevel, string msg)
{
LogAsync(new LogMsg(dt, logLevel, msg));
}
private void BindEventHandler()
{
btnAddProject.Click += BtnAddProject_Click;
btnDeleteProject.Click += BtnDeleteProject_Click;
btnLoadProject.Click += BtnLoadProject_Click;
LoggerHelper.LogPath = "D://";
LoggerHelper.LogPrefix = "Process";
OnLog -= LogDisplay;
OnLog += LogDisplay;
}
private void BtnDeleteProject_Click(object? sender, EventArgs e)
@ -579,7 +631,9 @@ namespace DHSoftware
private void HandleStartButton()
{
LogAsync(DateTime.Now, LogLevel.Information, "流程启动中,请稍候...");
StartProcess();
LogAsync(DateTime.Now, LogLevel.Action, "流程启动完成!");
}
@ -731,12 +785,10 @@ namespace DHSoftware
{
ProductData pData = new ProductData("", pieceNumber, ProductBaseCount);
_productLists[index][pieceNumber] = pData;
LogAsync(DateTime.Now, LogLevel.Action, $">> 轴{axisIndex}新产品{pieceNumber}加入队列{index}----板卡计数{PieceCount}");
}
string logStr = $"时间:{DateTime.Now} 轴{axisIndex}新产品{pieceNumber}加入队列{index}----入料计数{PieceCount}\n";
Task.Run(() =>
{
//this.BeginInvoke(new MethodInvoker(delegate () { richTextBox1.AppendText(logStr); }));
});
DateTime dtNow = DateTime.Now;
UpdateCT(null, (float)(dtNow - _ctTime).TotalSeconds);
_ctTime = dtNow;
@ -812,22 +864,7 @@ namespace DHSoftware
//LogAsync(DateTime.Now, LogLevel.Error, $"{camera.Name} 未找到产品,编号:{productNumber},队列{index}数量:{tmpDic.Count},列表:{pnStr}");
localImageSet.Dispose();
this.BeginInvoke(new MethodInvoker(delegate ()
{
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText(productNumber + "提前推出" + camera.CameraName);
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
}));
//重新生成实例 销毁之前的实例
using (StreamWriter sw = new StreamWriter("D://123log.txt", true, Encoding.UTF8))
{
sw.WriteLine(productNumber + "提前推出" + camera.CameraName);
}
return;
}
@ -836,20 +873,20 @@ namespace DHSoftware
if (!_cameraRelatedDetectionDict.ContainsKey(camera.CameraName))
{
localImageSet.Dispose();
this.BeginInvoke(new MethodInvoker(delegate ()
{
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
//this.BeginInvoke(new MethodInvoker(delegate ()
//{
// // int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText(productNumber + "提前推出" + camera.CameraName);
// // richTextBox1.AppendText(productNumber + "提前推出" + camera.CameraName);
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
}));
//重新生成实例 销毁之前的实例
// // 设置回原来的滚动位置
// // richTextBox1.SelectionStart = richTextBox1.TextLength;
// // richTextBox1.ScrollToCaret();
//}));
////重新生成实例 销毁之前的实例
// LogAsync(DateTime.Now, LogLevel.Warning, $"{camera.Name} 找到产品{productNumber}但是没有推理1");
LogAsync(DateTime.Now, LogLevel.Warning, $"{camera.CameraName} 找到产品{productNumber}但是没有推理1");
return;
}
@ -863,7 +900,7 @@ namespace DHSoftware
{
string detectionId = detectionDict[i];
// 1. 预处理
using (Mat inferenceImage = localImageSet.Clone()) // 仅在此处克隆,确保推理过程中 Mat 有独立副本
{
DetectStationResult temp1 = _visionEngine.RunInference(inferenceImage, detectionId);
@ -888,13 +925,13 @@ namespace DHSoftware
// CalculateOEE();
this.BeginInvoke(new MethodInvoker(delegate ()
{
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
//int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText($"统计结果成功,{productNumber} 吹气!\n");
//richTextBox1.AppendText($"统计结果成功,{productNumber} 吹气!\n");
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
// richTextBox1.SelectionStart = richTextBox1.TextLength;
// richTextBox1.ScrollToCaret();
}));
#region 6.
@ -935,13 +972,13 @@ namespace DHSoftware
$"当前队列产品数量:{tmpDic.Count}";
this.BeginInvoke(new MethodInvoker(delegate ()
{
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
// int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText(logStr);
// richTextBox1.AppendText(logStr);
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
// richTextBox1.SelectionStart = richTextBox1.TextLength;
//richTextBox1.ScrollToCaret();
}));
}
else
@ -953,13 +990,13 @@ namespace DHSoftware
$"当前队列产品数量:{tmpDic.Count}";
this.BeginInvoke(new MethodInvoker(delegate ()
{
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
//int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText(logStr);
// richTextBox1.AppendText(logStr);
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
// richTextBox1.SelectionStart = richTextBox1.TextLength;
//richTextBox1.ScrollToCaret();
}));
//重新生成实例 销毁之前的实例
var saveData = temp.GetProductData();