using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.LoctionEnum;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_WCS.WCSClient;
namespace WIDESEA_WCS
{
public partial class StackerExecutor
{
private static int _readRKSB_Down = 0;
///
/// 负极涂布下料工作逻辑
///
///
public static void RKSB_DownTask()
{
if (Interlocked.Exchange(ref _readRKSB_Down, 1) == 0)
{
Task.Run(() =>
{
try
{
VOLContext Context = new VOLContext();
Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
List RKSBS = new List { "入壳机1", "入壳机2" };
//循环处理涂布设备下料请求
foreach (var RKSB in RKSBS)
{
if (WCSService.Clients == null)
{
return;
}
PLCClient plc = WCSService.Clients.Find(v => v.PLCName == RKSB);
if (plc == null)
{
continue;
}
string SB_Up_Location = "XL-RKSB" + RKSB.Replace("入壳机", "");//拼接入壳下料口
string equipType = "RKSB" + RKSB.Replace("入壳机", "");//设备
//获取当前入壳设备是否有下料AGV任务
var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
//有结束本次循环
if (SBtask != null) { continue; }
//获取当前设备的下料请求
int requestRKDown = plc.ModbusPLCClient.ModbusTcpNetClient.ReadInt16("5308").Content;
if (requestRKDown==4)
{
#region 记录下料请求信号
WriteLog.GetLog().Write($"{plc.PLCName}下料请求:{requestRKDown}---{DateTime.Now}", $"入壳机下料请求");
#endregion
//找到对应缓存架货位,并确认空框
dt_stationinfo location = stationinfoRepository.Find(x => x.area == equipType && x.tpgd == false
&& x.wlgd == false && x.task == false).OrderBy(x => x.greenbuttontime).FirstOrDefault();
if (location != null)
{
WebResponseContent content = new WebResponseContent();
locRepository.DbContextBeginTransaction(() =>
{
//生成涂布机下料任务
dt_agvtask agvtask = new dt_agvtask()
{
agv_id = Guid.NewGuid(),
agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
agv_fromaddress = SB_Up_Location,//以设备起点
agv_toaddress = location.stationCode,
agv_code = "组装AGV调度",
agv_remark = "入壳下料任务",
agv_taskstate = AGVTaskStateEnum.Create.ToString(),
agv_tasktype = AGVTaskTypeEnum.TaskType_Outbound.ToString(),
agv_worktype = 1,//工作类型
agv_materielid = "",
agv_qty = 1,
agv_createtime = DateTime.Now,
agv_grade = 0,//任务优先级
agv_userid = "WCS",
agv_barcode = "",
};
location.task = true;
var entry = locRepository.DbContext.ChangeTracker.Entries().FirstOrDefault();
if (entry != null)
{
entry.State = EntityState.Detached;
}
int x = locRepository.Update(location, d => new { d.task}, true);
if (x == 1)
{
agvRepository.Add(agvtask, true);
return content.OK();
}
else
{
return content.Error();
}
});
}
}
}
}
catch (Exception ex)
{
WriteLog.GetLog().Write($"入壳机下料:{DateTime.Now} --------------{ex}", $"入壳机下料调度Error");
}
finally
{
Interlocked.Exchange(ref _readRKSB_Down, 0);
}
});
}
}
}
}