166 lines
6.3 KiB
C#
166 lines
6.3 KiB
C#
using DOAN.Infrastructure.PLC;
|
|
using DOAN.Model.PBL;
|
|
using DOAN.Model.PBL.Dto;
|
|
using DOAN.Service.PBL.IService;
|
|
using DOAN.ServiceCore.Signalr;
|
|
using Infrastructure.Attribute;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
namespace DOAN.Service.PBL
|
|
{
|
|
/// <summary>
|
|
/// 料架表Service业务层处理
|
|
/// </summary>
|
|
[AppService(ServiceType = typeof(IMESInteractionServcie), ServiceLifetime = LifeTime.Transient)]
|
|
public class MESInteractionServcie : BaseService<Storagelocation>, IMESInteractionServcie
|
|
{
|
|
private readonly IHubContext<PBLhub> notificationHubContext;
|
|
|
|
public MESInteractionServcie(IHubContext<PBLhub> _notificationHubContext)
|
|
{
|
|
notificationHubContext = _notificationHubContext;
|
|
}
|
|
|
|
public bool TestPLc(string address, PLCTool pLCTool)
|
|
{
|
|
bool isSucesss = pLCTool.ReadBit(address);
|
|
return isSucesss;
|
|
}
|
|
|
|
/// <summary>
|
|
/// MES亮灯
|
|
/// </summary>
|
|
/// <param name="light"></param>
|
|
/// <param name="pLCTool"></param>
|
|
/// <returns></returns>
|
|
public bool MESLightUp(LightUpDto light, PLCTool pLCTool)
|
|
{
|
|
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)
|
|
{
|
|
Storagelocation storagelocation = new();
|
|
// 是否合并料架
|
|
bool isMergeRack = MirrorshellShelfList.Count > 1;
|
|
if (isMergeRack)
|
|
{
|
|
// 合并料架 判断先进先出
|
|
foreach(var shelf in MirrorshellShelfList)
|
|
{
|
|
// 第一个有箱子的
|
|
if(shelf.PackageNum > 0)
|
|
{
|
|
storagelocation = shelf;
|
|
continue;
|
|
}
|
|
}
|
|
if (storagelocation.RackCode == null)
|
|
{
|
|
storagelocation = MirrorshellShelfList[^1];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 单独料架
|
|
storagelocation = MirrorshellShelfList[0];
|
|
}
|
|
// 3.对应料架亮灯
|
|
bool isSucesss = pLCTool.WriteBit(storagelocation.PlcAddress, true);
|
|
if (isSucesss) {
|
|
storagelocation.IsLight = 1;
|
|
result += Context.Updateable(storagelocation).ExecuteCommand();
|
|
}
|
|
//亮灯日志
|
|
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
|
|
{
|
|
// 发送socket 通知
|
|
string message =
|
|
$"MES产品编号{light.AssemblyPartNumber}或者版本{light.Version},在PBL中找不到。请维护PBL料架信息--{DateTime.Now}";
|
|
notificationHubContext.Clients.All.SendAsync("PBL_bom_except", message);
|
|
|
|
return false;
|
|
}
|
|
return result > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// MES灭灯
|
|
/// </summary>
|
|
/// <param name="scan_code"></param>
|
|
/// <returns></returns>
|
|
public bool MESLightDown(string scan_code, PLCTool pLCTool)
|
|
{
|
|
int result = 0;
|
|
// 1.记录MES交互记录
|
|
MES_Interation_Log item = new MES_Interation_Log();
|
|
item.Id = XUEHUA;
|
|
item.ScanCode = scan_code;
|
|
item.CreatedTime = DateTime.Now;
|
|
Context.Insertable(item).ExecuteCommand();
|
|
//2 找到对应的亮着的料架 进行灭灯
|
|
Storagelocation storagelocation = Context
|
|
.Queryable<Storagelocation>()
|
|
.Where(it => it.Partnumber.Contains(scan_code))
|
|
.Where(it => it.IsLight == 1)
|
|
.First();
|
|
if (storagelocation == null)
|
|
{
|
|
// 发送socket 通知
|
|
string message =
|
|
$"未找到{scan_code}对应的亮灯料架。请检查信息--{DateTime.Now}";
|
|
notificationHubContext.Clients.All.SendAsync("PBL_bom_except", message);
|
|
return false;
|
|
}
|
|
// TODO PLC 交互
|
|
bool isSuccess = pLCTool.WriteBit(storagelocation.PlcAddress, false);
|
|
if (isSuccess)
|
|
{
|
|
storagelocation.IsLight = 0;
|
|
result += Context.Updateable(storagelocation).ExecuteCommand();
|
|
}
|
|
//灭灯日志
|
|
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();
|
|
return result > 0;
|
|
}
|
|
}
|
|
}
|