pblbackend/DOAN.Service/PBL/MESInteractionServcie.cs

265 lines
11 KiB
C#
Raw Normal View History

2024-11-13 15:53:15 +08:00
using DOAN.Infrastructure.PLC;
2024-11-01 11:35:11 +08:00
using DOAN.Model.PBL;
2024-11-13 15:53:15 +08:00
using DOAN.Model.PBL.Dto;
2024-11-01 11:35:11 +08:00
using DOAN.Service.PBL.IService;
2025-01-16 10:13:50 +08:00
using DOAN.ServiceCore.Signalr;
2024-11-13 15:53:15 +08:00
using Infrastructure.Attribute;
2024-11-01 11:35:11 +08:00
using Mapster;
using Microsoft.AspNetCore.Authentication;
2025-01-16 10:13:50 +08:00
using Microsoft.AspNetCore.SignalR;
2024-11-13 15:53:15 +08:00
using Newtonsoft.Json.Linq;
2025-03-28 09:33:30 +08:00
using System;
using System.Security.Cryptography.X509Certificates;
2025-03-28 09:33:30 +08:00
using static System.Formats.Asn1.AsnWriter;
2024-11-01 11:35:11 +08:00
namespace DOAN.Service.PBL
{
/// <summary>
/// 料架表Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IMESInteractionServcie), ServiceLifetime = LifeTime.Transient)]
public class MESInteractionServcie : BaseService<Storagelocation>, IMESInteractionServcie
{
2025-01-24 11:44:27 +08:00
private readonly IHubContext<PBLhub> notificationHubContext;
2025-01-16 10:13:50 +08:00
2025-01-24 11:44:27 +08:00
public MESInteractionServcie(IHubContext<PBLhub> _notificationHubContext)
{
notificationHubContext = _notificationHubContext;
2025-01-16 10:13:50 +08:00
}
2025-01-24 11:44:27 +08:00
2024-11-13 15:53:15 +08:00
public bool TestPLc(string address, PLCTool pLCTool)
{
bool isSucesss = pLCTool.ReadBit(address);
return isSucesss;
}
2024-11-07 21:27:56 +08:00
2024-11-13 15:53:15 +08:00
/// <summary>
/// MES亮灯
/// </summary>
/// <param name="light"></param>
/// <param name="pLCTool"></param>
/// <returns></returns>
public bool MESLightUp(LightUpDto light, PLCTool pLCTool)
2024-11-01 11:35:11 +08:00
{
2025-03-28 09:33:30 +08:00
try
2024-11-13 15:53:15 +08:00
{
2025-03-28 09:33:30 +08:00
Context.Ado.BeginTran();
int result = 0;
// 1.记录MES交互记录
MES_Interation_Log item = light.Adapt<MES_Interation_Log>();
item.Id = XUEHUA;
item.CreatedTime = DateTime.Now;
Context.Insertable(item).ExecuteCommand();
// 2.根据总成零件号 ,版本 查询对应零件号,使得对应料架亮灯
List<Storagelocation> MirrorshellShelfList = Context
.Queryable<Storagelocation>()
.Where(it =>
it.Partnumber
== SqlFunc
.Subqueryable<Billofmaterials>()
.Where(It => It.Productcode == light.AssemblyPartNumber)
.Select(it => it.MirrorshellCode)
)
.ToList();
if (MirrorshellShelfList != null && MirrorshellShelfList.Count() > 0)
2024-11-13 15:53:15 +08:00
{
2025-03-28 09:33:30 +08:00
Storagelocation storagelocation = new();
// 是否合并料架
bool isMergeRack = MirrorshellShelfList.Count > 1;
if (isMergeRack)
2024-11-13 15:53:15 +08:00
{
2025-03-28 09:33:30 +08:00
Storagelocation leftShelf = MirrorshellShelfList[0];
Storagelocation rightShelf = MirrorshellShelfList[1];
// 料架今天是否有补料信息 (补料完成信号判定)
var today = DateTime.Today;
bool hasAlarm = Context.Queryable<AlarmLog>()
.Where(it => it.ActionTime != null &&
it.ActionTime >= today &&
it.ActionTime < today.AddDays(1))
.Where(it => it.StoragelocationId == leftShelf.Id || it.StoragelocationId == rightShelf.Id)
.OrderByDescending(it => it.ActionTime)
.Any();
int packageTotal = leftShelf.PackageNum.Value + rightShelf.PackageNum.Value;
//1. 在今日有补料信号的前提下 料架>6箱已补料,未消耗最新箱,先消耗旧箱,哪边存在报警补哪边)
if (packageTotal == 8 && hasAlarm)
{
if (leftShelf.IsLackAlarm == 1)
{
storagelocation = leftShelf;
}
else if (rightShelf.IsLackAlarm == 1)
{
storagelocation = rightShelf;
}
else
{
storagelocation = rightShelf;
}
}
else if (packageTotal == 7 && hasAlarm)
{
if (leftShelf.IsLackAlarm == 1)
{
storagelocation = leftShelf;
}
else if (rightShelf.IsLackAlarm == 1)
{
storagelocation = rightShelf;
}
else
{
storagelocation = rightShelf;
}
// 切换报警
DoChangeAlarmStatus(leftShelf, rightShelf);
}
else if (packageTotal <= 6 && packageTotal > 4 && hasAlarm)
{
2025-03-28 09:33:30 +08:00
if (leftShelf.IsLackAlarm == 1)
{
storagelocation = rightShelf;
}
else if (rightShelf.IsLackAlarm == 1)
{
storagelocation = leftShelf;
}
else
{
storagelocation = rightShelf;
}
}
2025-03-28 09:33:30 +08:00
else
{
// 默认情况
// 合并料架 判断 20250224 左先出原则
foreach (var shelf in MirrorshellShelfList)
{
// 第一个有箱子的料架
if (shelf.PackageNum > 0)
{
storagelocation = shelf;
break;
}
}
if (storagelocation.RackCode == null)
{
storagelocation = MirrorshellShelfList[^1];
}
}
}
else
{
// 单独料架
storagelocation = MirrorshellShelfList[0];
2024-11-13 15:53:15 +08:00
}
2025-03-28 09:33:30 +08:00
// 3.对应料架亮灯
bool isSucesss = pLCTool.WriteBit(storagelocation.PlcAddress, true);
if (isSucesss)
{
2025-03-28 09:33:30 +08:00
storagelocation.IsLight = 1;
result += Context.Updateable(storagelocation).ExecuteCommand();
}
2025-03-28 09:33:30 +08:00
//亮灯日志
Light_Log light_Log = new Light_Log();
light_Log.Id = XUEHUA;
light_Log.LightOperation = 1;
light_Log.Operationer = "PBL";
light_Log.CreatedTime = DateTime.Now;
light_Log.ShelfCode = storagelocation.RackCode;
light_Log.LayerNum = storagelocation.LayerNum;
light_Log.IsSuccess = isSucesss;
result += Context.Insertable(light_Log).ExecuteCommand();
}
else
{
2025-03-28 09:33:30 +08:00
// 发送socket 通知
string message =
$"MES产品编号{light.AssemblyPartNumber}或者版本{light.Version},在PBL中找不到。请维护PBL料架信息--{DateTime.Now}";
notificationHubContext.Clients.All.SendAsync("PBL_bom_except", message);
Context.Ado.RollbackTran();
return false;
2024-11-13 15:53:15 +08:00
}
2025-03-28 09:33:30 +08:00
notificationHubContext.Clients.All.SendAsync("PBL_storagelocation_change");
Context.Ado.CommitTran();
return result > 0;
2024-11-01 11:35:11 +08:00
}
2025-03-28 09:33:30 +08:00
catch (Exception)
2024-11-13 15:53:15 +08:00
{
2025-03-28 09:33:30 +08:00
Context.Ado.RollbackTran();
2024-11-13 15:53:15 +08:00
return false;
}
2025-03-28 09:33:30 +08:00
2024-11-01 11:35:11 +08:00
}
2024-11-08 08:42:33 +08:00
/// <summary>
2024-11-13 15:53:15 +08:00
/// MES灭灯
2024-11-08 08:42:33 +08:00
/// </summary>
/// <param name="scan_code"></param>
/// <returns></returns>
2024-11-13 15:53:15 +08:00
public bool MESLightDown(string scan_code, PLCTool pLCTool)
2024-11-01 11:35:11 +08:00
{
2024-11-13 15:53:15 +08:00
int result = 0;
2024-11-01 11:35:11 +08:00
// 1.记录MES交互记录
MES_Interation_Log item = new MES_Interation_Log();
item.Id = XUEHUA;
2024-11-07 21:27:56 +08:00
item.ScanCode = scan_code;
2024-11-01 11:35:11 +08:00
item.CreatedTime = DateTime.Now;
Context.Insertable(item).ExecuteCommand();
//2 找到对应的亮着的料架 进行灭灯
2025-01-24 11:44:27 +08:00
Storagelocation storagelocation = Context
.Queryable<Storagelocation>()
.Where(it => it.Partnumber.Contains(scan_code))
.Where(it => it.IsLight == 1)
2025-01-24 11:44:27 +08:00
.First();
if (storagelocation == null)
2024-11-13 15:53:15 +08:00
{
// 发送socket 通知
string message =
$"未找到{scan_code}对应的亮灯料架。请检查信息--{DateTime.Now}";
notificationHubContext.Clients.All.SendAsync("PBL_bom_except", message);
return false;
2024-11-08 08:42:33 +08:00
}
// TODO PLC 交互
bool isSuccess = pLCTool.WriteBit(storagelocation.PlcAddress, false);
if (isSuccess)
2024-11-07 21:27:56 +08:00
{
storagelocation.IsLight = 0;
result += Context.Updateable(storagelocation).ExecuteCommand();
2024-11-01 11:35:11 +08:00
}
//灭灯日志
Light_Log light_Log = new Light_Log
{
Id = XUEHUA,
LightOperation = 2,
LayerNum = storagelocation.LayerNum,
ShelfCode = storagelocation.RackCode,
IsSuccess = isSuccess,
Operationer = "PBL",
CreatedTime = DateTime.Now
};
result += Context.Insertable(light_Log).ExecuteCommand();
2025-03-28 09:33:30 +08:00
notificationHubContext.Clients.All.SendAsync("PBL_storagelocation_change");
2024-11-01 14:13:40 +08:00
return result > 0;
2024-11-01 11:35:11 +08:00
}
2025-03-28 09:33:30 +08:00
/// <summary>
/// 交换合并料架报警配置
/// </summary>
/// <param name="leftShelf"></param>
/// <param name="rightShelf"></param>
/// <returns></returns>
public int DoChangeAlarmStatus(Storagelocation leftShelf, Storagelocation rightShelf)
{
int result = 0;
// 都判断完后切换报警
leftShelf.IsLackAlarm = leftShelf.IsLackAlarm == 1 ? 0 : 1;
leftShelf.AlarmNum = leftShelf.IsLackAlarm == 1 ? rightShelf.AlarmNum : 0;
result += Context.Updateable(leftShelf).ExecuteCommand();
rightShelf.IsLackAlarm = rightShelf.IsLackAlarm == 1 ? 0 : 1;
rightShelf.AlarmNum = rightShelf.IsLackAlarm == 1 ? leftShelf.AlarmNum : 0;
result += Context.Updateable(rightShelf).ExecuteCommand();
return result;
}
2024-11-01 11:35:11 +08:00
}
2025-01-24 11:44:27 +08:00
}