You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.2 KiB
C#

using Microsoft.Extensions.Logging;
using SlnMesnac.Common;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
using SlnMesnac.Repository.service.Impl;
using SlnMesnac.TouchSocket;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography.Xml;
using System.Threading;
using TouchSocket.Core;
namespace SlnMesnac.Business
{
public class LogoBusiness
{
private ILogger<LogoBusiness> _logger;
private PlcAbsractFactory plc = null;
private CCameraHik hikHelper = CCameraHik.Instance;
private DebugConfig config = DebugConfig.Instance;
private IBaseMaterialService baseMaterialService;
private ILogoIdentifyService logoIdentifyService;
public PaddleOCRSharp.PaddleOCREngine engine = null;
private static LogoBusiness instance;
#region 委托定义 刷新界面扫描信息
public delegate void RefreshBoxInfo(string boxCode, string boxTime, string model, byte[] imageData, bool isSuccess);
public static event RefreshBoxInfo? RefreshBoxInfoEvent;
public delegate void RefreshMessage(string message, bool isWarning = false);
public static event RefreshMessage? RefreshMessageEvent;
#endregion
private LogoBusiness(IBaseMaterialService baseMaterialService,ILogoIdentifyService logoIdentifyService,PlcPool _plcPool)
{
TcpServer.RefreshMaterialCodeStrEvent += BarCodeHandler;
this.baseMaterialService = baseMaterialService;
this.logoIdentifyService = logoIdentifyService;
plc= _plcPool.GetPlcByKey("plc");
}
public static LogoBusiness GetInstance(IBaseMaterialService baseMaterialService, ILogoIdentifyService ocrVerfiyService, PlcPool _plcPool)
{
if (instance == null)
{
instance = new LogoBusiness(baseMaterialService, ocrVerfiyService, _plcPool);
}
return instance;
}
/// <summary>
/// 条码触发相机拍照校验
/// </summary>
/// <param name="materialCodeStr"></param>
/// <param name="ip"></param>
public void BarCodeHandler(string materialCodeStr, string ip)
{
try
{
bool judge = FoamtJudge(materialCodeStr);
if (!judge)
{
WarningAndStop($"箱体码{materialCodeStr}格式不正确,停线报警!");
return;
}
_logger.LogInformation($"扫描到箱体码:{materialCodeStr}");
// 1.触发相机拍照
bool TriggerFlag = hikHelper.TriggerGather();
if (!TriggerFlag)
{
WarningAndStop($"软触发相机拍照失败,停线报警");
return;
}
// 2.等待接收海康结果
bool flag = JudgeIsSuccess();
if (flag)
{
//校验成功下发放行
PlcPass();
RefreshMessageEvent?.Invoke("Logo识别成功下发放行");
_logger.LogInformation($"箱体码:{materialCodeStr}Logo识别成功下发放行");
}
else
{
//校验成功下发放行
WarningAndStop($"Logo识别失败禁止放行");
}
// 3.查询mes获取箱体信息
BaseMaterialInfo baseMaterialInfo = baseMaterialService.GetMaterialInfoByMaterialCode(materialCodeStr.Substring(7, 10));
string productName = GetSubstringBetweenCommas(baseMaterialInfo.MaterialName);
#region 回传MES信息和更新本地数据库
// TODO 回传MES信息
LogoIdentify record = new LogoIdentify();
record.BoxCode = materialCodeStr;
record.ProductModel = productName;
record.Result = flag ? "成功" : "失败";
record.RecordTime = DateTime.Now.ToString();
logoIdentifyService.InsertRecord(record);
#endregion
// TODO , 传入照片
byte[] ImageData = null ;
// 刷新界面、刷新图片
RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(),productName,ImageData,flag);
}
catch (Exception ex)
{
WarningAndStop($"BarCodeHandler异常,识别Logo失败,原因:{ex.Message},箱体条码:{materialCodeStr}");
}
}
/// <summary>
/// 判断Logo校验是否成功
/// </summary>
/// <returns></returns>
public bool JudgeIsSuccess()
{
bool result = false;
//TODO 等待接受海康的校验结果
return result;
}
/// <summary>
/// 箱体码格式校验
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
private bool FoamtJudge(string code)
{
if (!string.IsNullOrEmpty(code))
{
if(code.Substring(0,1)=="B" && code.Length == 21)
{
return true;
}
}
return false;
}
/// <summary>
/// 截取两个逗号之间的字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static string GetSubstringBetweenCommas(string input)
{
if (input == null) return null;
// 找到第一个逗号的位置
int firstCommaIndex = input.IndexOf(',');
if (firstCommaIndex != -1)
{
// 找到第二个逗号的位置
int secondCommaIndex = input.IndexOf(',', firstCommaIndex + 1);
if (secondCommaIndex != -1)
{
// 使用Substring截取第一个逗号和第二个逗号之间的字符
return input.Substring(firstCommaIndex + 1, secondCommaIndex - firstCommaIndex - 1);
}
else
{
return null;
}
}
else
{
return null;
}
}
#region PLC交互部分
//M100停止点位
public void PlcPass()
{
if(plc!=null && plc.IsConnected)
{
plc.writeInt16ByAddress("M100",0);
}
}
public void PlcStop()
{
if (plc != null && plc.IsConnected)
{
plc.writeInt16ByAddress("M100", 1);
}
}
#endregion
#region 记录日志刷新界面及下发PLC报警
public void WarningAndStop(string message)
{
_logger.LogError(message);
RefreshMessageEvent?.Invoke(message, true);
PlcStop();
}
#endregion
}
}