using Microsoft.CodeAnalysis;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
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_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_Up = 0;
///
/// 入壳机上料
///
///
public static void RKSB_UpTask()
{
if (Interlocked.Exchange(ref _readRKSB_Up, 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 = "SL-RKSB" + RKSB.Replace("入壳机", "");//拼接入壳上料口
string equipType = "RKSB" + RKSB.Replace("入壳机", "");//设备
//获取当前入壳设备是否有上料AGV任务
var SBtask = agvRepository.FindFirst(f => f.agv_toaddress == SB_Up_Location);
//有结束本次循环
if (SBtask != null) { continue; }
//获取当前设备的上料请求
int requestRKUP = plc.ModbusPLCClient.ModbusTcpNetClient.ReadInt16("5306").Content;
if (requestRKUP==4)
{
//找到对应缓存架货位,并确认有料
dt_stationinfo location = stationinfoRepository.Find(x=>x.area== equipType && x.greenbutton == true
&& x.tpgd == true && x.wlgd == true && x.task == false).OrderBy(x=>x.greenbuttontime).FirstOrDefault();
if (location != null)
{
#region 记录上料请求信号
WriteLog.GetLog().Write($"{plc.PLCName}上料请求:{requestRKUP}---{DateTime.Now}", $"入壳机上料请求");
#endregion
//生成入壳机上料任务
dt_agvtask agvtask = new dt_agvtask()
{
agv_id = Guid.NewGuid(),
agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
agv_fromaddress = location.stationCode,
agv_toaddress = SB_Up_Location,
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;
int changeCount = locRepository.Update(location, d => new { d.task }, true);
if (changeCount == 1)
{
agvRepository.Add(agvtask, true);
}
break;
}
}
}
}
catch (Exception ex)
{
WriteLog.GetLog().Write($"入壳机上料:{DateTime.Now} --------------{ex}", $"入壳机上料调度Error");
}
finally
{
Interlocked.Exchange(ref _readRKSB_Up, 0);
}
});
}
}
}
}