diff --git a/SlnMesnac.Business/LogoBusiness.cs b/SlnMesnac.Business/LogoBusiness.cs index 646763c..d37f8ac 100644 --- a/SlnMesnac.Business/LogoBusiness.cs +++ b/SlnMesnac.Business/LogoBusiness.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using SlnMesnac.Common; using SlnMesnac.Config; using SlnMesnac.Model.domain; +using SlnMesnac.Model.dto; using SlnMesnac.Plc; using SlnMesnac.Repository.service; using SlnMesnac.Repository.service.Impl; @@ -37,6 +38,7 @@ namespace SlnMesnac.Business public PaddleOCRSharp.PaddleOCREngine engine = null; private GunHelper gunHelper = GunHelper.Instance; + private Queue consumeQueue = new Queue(); private static LogoBusiness instance; // 存放照片路径 @@ -45,7 +47,7 @@ namespace SlnMesnac.Business public static string LogPath = System.Environment.CurrentDirectory + "/Logs/"; /// - /// 存储海康相机识别结果 + /// 接收海康相机识别结果 /// private static bool HikCameraResult = false; #region 委托定义 刷新界面扫描信息 @@ -86,43 +88,29 @@ namespace SlnMesnac.Business } - public void test() - { - Task.Run(async() => - { - Thread.Sleep(5000); - TcpClient _tcpClient = new TcpClient(); - - _tcpClient.Setup(new TouchSocketConfig().SetRemoteIPHost($"127.0.0.1:7024")); - _tcpClient.Connect(); - - var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() - { - FilterFunc = response => - { - return true; - } - }); - byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 }; - var reciveBuffer =await waitClient.SendThenResponseAsync(release); - }); - - } - - + + /// + /// 接收光电模块信号 + /// + /// + /// private void SerialSignalPush(string ip, int flag) { // 光电触发拍照 if (flag == 1) { - + CameraHandler(); } - else //光电释放 + else //光电释放暂不使用 { } } + /// + /// 接受相机识别结果 + /// + /// private void ReceiveCameraResult(string result) { logger.LogInformation($"相机返回结果:{result}"); @@ -154,22 +142,17 @@ namespace SlnMesnac.Business /// public async void BarCodeHandler(string materialCodeStr, string ip) { - //打开灯光 - lightHelper.SendData("OPEN"); - #region 复位报警灯 - // 声光电报警复位 - gunHelper.SendData("OK"); - #endregion - logger.LogInformation("相机流程开始:"); - // 传入照片 - byte[] ImageData = null; - // 压缩后的图片 - byte[] compressedImageData = null; + try { + logger.LogInformation("扫码流程开始:"); #region 初始操作:复位海康上次结果,删除海康上次保存文件 HikCameraResult = false; FileHelper.DeleteAllPictures(config.CameraFilePath); + // 保证队列一条数据 + consumeQueue.Clear(); + // 声光电报警复位 + gunHelper.SendData("OK"); #endregion bool judge = FoamtJudge(materialCodeStr); @@ -180,10 +163,8 @@ namespace SlnMesnac.Business } logger.LogInformation($"扫描到箱体码:{materialCodeStr}"); - // 1.立即触发相机拍照 - tcpServer.SendCommand(config.CameraIP, "START"); - - //2.根据箱体码查询型号,根据型号判断是否需要校验LOGO + + //1.根据箱体码查询型号,根据型号判断是否需要校验LOGO ProductModel mode = logoConfigService.GetMaterialTypeByBarCode(materialCodeStr); if(mode == null) { @@ -192,85 +173,115 @@ namespace SlnMesnac.Business return; } LogoConfig logoConfig = logoConfigService.GetByMaterialType(mode.MaterialCode); - // 海康校验结果 - bool hikFlag = false; - + if (logoConfig.IsChecked == 0) { // 不需要校验 - hikFlag = true; - RefreshMessageEvent?.Invoke("LOGO+PCI无需识别,下发放行"); logger.LogInformation($"箱体码:{materialCodeStr},Logo无需识别,下发放行"); + RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(), logoConfig.MaterialName, true); + #region 更新数据库 + LogoIdentify record = new LogoIdentify(); + record.ProductCode = materialCodeStr; + record.MaterialType = logoConfig.MaterialType; + record.MaterialName = logoConfig.MaterialName; + record.isChecked = logoConfig.IsChecked; + record.Result = 1; + record.RecordTime = DateTime.Now; + record.ProductLine = config.ProductLine; + logoIdentifyService.InsertRecord(record); + #endregion + RefreshDataGridEvent?.Invoke(); } else { // 需要检验但是还没设置配方 if(logoConfig.CheckKind == 0) { - // 不需要校验 - hikFlag = false; - Warning($"箱体码:{materialCodeStr},LOGO+PCI需要识别,但是未设置PCI模版"); + return; } else { - #region 拍照处理流程 - Thread.Sleep(int.Parse(config.SleepStr)); - // 1.触发相机拍照 - tcpServer.SendCommand(config.CameraIP, logoConfig.CheckKind.ToString()); - // 需要校验 - // 2.等待接收海康结果 - hikFlag = await JudgeIsSuccessAsync(); - if (hikFlag) - { - //校验成功放行 - - RefreshMessageEvent?.Invoke("LOGO+PCI识别成功,下发放行"); - logger.LogInformation($"箱体码:{materialCodeStr},Logo识别成功,下发放行"); - } - else - { - //校验失败禁止放行 - WarningAndStop($"LOGO+PCI识别失败,禁止放行"); - } - #endregion + //需要检测 + //打开灯光 + lightHelper.SendData("OPEN"); + BarCodeModel barCodeModel = new BarCodeModel(); + barCodeModel.BarCode = materialCodeStr; + barCodeModel.logoConfig = logoConfig; + consumeQueue.Enqueue(barCodeModel); + logger.LogInformation($"箱体码:{materialCodeStr},入队等待识别"); } } + + } + catch (Exception ex) + { + Warning($"BarCodeHandler异常,识别Logo失败,原因:{ex.Message},箱体条码:{materialCodeStr}"); + } + + } + + + /// + /// 相机流程处理 + /// + /// + private async void CameraHandler() + { + try + { + logger.LogInformation("相机流程开始:"); + //照片 + byte[] ImageData = null; + // 压缩后的图片 + byte[] compressedImageData = null; + + //从队列取出条码 + if (consumeQueue.Count < 0) return; + bool hikFlag = false; + BarCodeModel barCodeModel = consumeQueue.Dequeue(); - + // Thread.Sleep(int.Parse(config.SleepStr)); + // 1.触发相机拍照 + tcpServer.SendCommand(config.CameraIP, barCodeModel.logoConfig.CheckKind.ToString()); + + // 2.等待接收海康结果 + hikFlag = await JudgeIsSuccessAsync(); + if (hikFlag) + { + RefreshMessageEvent?.Invoke("LOGO+PCI识别成功,下发放行"); + logger.LogInformation($"箱体码:{barCodeModel.BarCode},Logo识别成功,下发放行"); + } + else + { + WarningAndStop($"LOGO+PCI识别失败,禁止放行"); + } // 刷新界面、刷新图片,照片按照日期存储本地 - RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(), logoConfig.MaterialName, hikFlag); - - //需要检测并且设置过配方的条码等待照片 - if(logoConfig.IsChecked == 1 && logoConfig.CheckKind != 0) + #region + RefreshBoxInfoEvent?.Invoke(barCodeModel.BarCode, DateTime.Now.ToString(), barCodeModel.logoConfig.MaterialName, hikFlag); + await Task.Run(() => { - await Task.Run(() => + Thread.Sleep(int.Parse(config.PictureSleep)); + ImageData = FileHelper.FindPhoto(config.CameraFilePath); + if (ImageData != null) { - Thread.Sleep(int.Parse(config.PictureSleep)); - ImageData = FileHelper.FindPhoto(config.CameraFilePath); - if (ImageData != null) - { - // 图片压缩 - compressedImageData = FileHelper.CompressImageData(ImageData, 20); - FileHelper.SaveImage(compressedImageData, materialCodeStr + ".jpg", PicturePath); - RefreshPictureEvent?.Invoke(ImageData); - } - else - { - // WarningAndStop($"读取相机保存照片文件夹失败,请检查文件夹:{config.CameraFilePath}是否存在"); - } + // 图片压缩 + compressedImageData = FileHelper.CompressImageData(ImageData, 20); + FileHelper.SaveImage(compressedImageData, barCodeModel.BarCode + ".jpg", PicturePath); + RefreshPictureEvent?.Invoke(ImageData); + } - }); - } + }); + #endregion #region 更新数据库 LogoIdentify record = new LogoIdentify(); - record.ProductCode = materialCodeStr; - record.MaterialType = logoConfig.MaterialType; - record.MaterialName = logoConfig.MaterialName; - record.isChecked = logoConfig.IsChecked; - record.Result = hikFlag ? 1 : 0; + record.ProductCode = barCodeModel.BarCode; + record.MaterialType = barCodeModel.logoConfig.MaterialType; + record.MaterialName = barCodeModel.logoConfig.MaterialName; + record.isChecked = barCodeModel.logoConfig.IsChecked; + record.Result = hikFlag == true ? 1 : 0; record.RecordTime = DateTime.Now; record.ProductLine = config.ProductLine; if (compressedImageData != null) @@ -280,23 +291,21 @@ namespace SlnMesnac.Business logoIdentifyService.InsertRecord(record); #endregion RefreshDataGridEvent?.Invoke(); - - } - catch (Exception ex) + }catch(Exception ex) { - Warning($"BarCodeHandler异常,识别Logo失败,原因:{ex.Message},箱体条码:{materialCodeStr}"); + Warning($"相机流程异常,原因:{ex.Message}"); } finally { - lightHelper.SendData("CLOSE"); - #region 复位海康上次结果,删除海康上次保存文件 + + #region 复位海康上次结果,删除海康上次保存文件、关闭灯光 HikCameraResult = false; FileHelper.DeleteAllPictures(config.CameraFilePath); + lightHelper.SendData("CLOSE"); #endregion } - } - + } /// @@ -319,7 +328,7 @@ namespace SlnMesnac.Business { return true; } - // 检查是否超过两秒 + // 检查是否超时 if (stopwatch.ElapsedMilliseconds > 2500) { return false; @@ -555,5 +564,35 @@ namespace SlnMesnac.Business } #endregion + + + #region 测试用例 + /// + /// 模拟光电发送byte,测试接收处理 + /// + public void testPostByte() + { + Task.Run(async () => + { + Thread.Sleep(5000); + TcpClient _tcpClient = new TcpClient(); + + _tcpClient.Setup(new TouchSocketConfig().SetRemoteIPHost($"127.0.0.1:7024")); + _tcpClient.Connect(); + + var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() + { + FilterFunc = response => + { + return true; + } + }); + byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 }; + var reciveBuffer = await waitClient.SendThenResponseAsync(release); + }); + + } + + #endregion } } diff --git a/SlnMesnac.Model/domain/LogoConfig.cs b/SlnMesnac.Model/domain/LogoConfig.cs index 7580741..f597aca 100644 --- a/SlnMesnac.Model/domain/LogoConfig.cs +++ b/SlnMesnac.Model/domain/LogoConfig.cs @@ -38,7 +38,7 @@ namespace SlnMesnac.Model.domain public int IsChecked { get; set; } /// - /// 是否需要检测;0不需要;1需要 + /// 检测类型 /// [SugarColumn(ColumnName = "CHECK_KIND")] public int CheckKind { get; set; } diff --git a/SlnMesnac.Model/dto/BarCodeModel.cs b/SlnMesnac.Model/dto/BarCodeModel.cs new file mode 100644 index 0000000..0c74f52 --- /dev/null +++ b/SlnMesnac.Model/dto/BarCodeModel.cs @@ -0,0 +1,15 @@ +using SlnMesnac.Model.domain; +using System; + +namespace SlnMesnac.Model.dto +{ + public class BarCodeModel + { + + //扫描到的条码 + public string? BarCode { get; set; } + + //条码对应的识别配置 + public LogoConfig? logoConfig { get; set; } + } +} \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index 6dd47e2..bb46d36 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -195,30 +195,36 @@ namespace SlnMesnac.TouchSocket /// public int ChangeSerialSignal(byte[] data,int len) { - if (data == null || len < 0) + try { - return -1; - } - len = Math.Min(len, data.Length); - byte[] CheckByte = new byte[len]; - Array.Copy(data, 0, CheckByte, 0, len); + if (data == null || len < 0) + { + return -1; + } + len = Math.Min(len, data.Length); + byte[] CheckByte = new byte[len]; + Array.Copy(data, 0, CheckByte, 0, len); - byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 }; - byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 }; + byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 }; + byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 }; - if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release)) - { + if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release)) + { - return 0; - } - else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger)) - { + return 0; + } + else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger)) + { - return 1; - } + return 1; + } - // 异常 - return -1; + // 异常 + return -1; + }catch(Exception ex) + { + return -1; + } } }