using Microsoft.CodeAnalysis;
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
{
public static object requestFJTBUP = new object();//requestZJJK
private static int _readFTBSignalsoZ = 0;//_readZTBUPSignalso
///
/// 负极涂布上料工作逻辑
///
///
public static void F_TBSB_UpTask()
{
if (Interlocked.Exchange(ref _readFTBSignalsoZ, 1) == 0)
{
Task.Run(() =>
{
try
{
VOLContext Context = new VOLContext();
Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
List TBSBS = new List { "涂布机负极1", "涂布机负极2", "涂布机负极3", "涂布机负极4" };
//循环处理涂布设备上料请求
foreach (var TBSB in TBSBS)
{
string SB_Up_Location = "FJSL-TBSB" + TBSB.Replace("涂布机负极", "");//拼接涂布上料口
if (WCSService.Clients == null)
{
return;
}
PLCClient plc = WCSService.Clients.Find(v => v.PLCName == TBSB);
if (plc == null)
{
continue;
}
//当前设备有没有上料请求
bool GYR_RFX = plc.OmronPLCClient.OmronFinsNetClient.ReadBool("D11093.00").Content;
//Convert.ToBoolean(plc.ReadValue("R_Feedingrequest".ToString(), SB_Up_Location));
if (GYR_RFX)
{
//查看当前涂布机设备是否有AGV任务
var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location || f.agv_toaddress == SB_Up_Location);
//有结束本次循环
if (SBtask != null) { continue; }
#region 记录上料请求信号
WriteLog.GetLog().Write($"{plc.PLCName}上料请求:{GYR_RFX}---{DateTime.Now}", $"负极涂布上料请求");
#endregion
//找到对应箔材库货位(36号位置),并确认有料
base_ware_location location_36 = locRepository.FindFirst(t => t.upper_code == "FJ-BCHCJ36" && t.location_state == LoctionStateEnum.LocationState_Stored.ToString() && t.status == 1 && t.logic_col == 1 && t.task == 2);
if (location_36 != null)
{
dt_agvtask agvtask_36 = agvRepository.Find(t => t.agv_fromaddress == location_36.upper_code).FirstOrDefault();
if (agvtask_36 != null)
{
return;
}
//生成涂布机上料任务
dt_agvtask agvtask = new dt_agvtask()
{
agv_id = Guid.NewGuid(),
agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
agv_fromaddress = location_36.upper_code,
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 = "",
};
//WriteLog.GetLog($"任务生成记录").Write($"任务号:{agvtask.agv_tasknum},起点{location_36.upper_code}光电:{location_36.created_user}=>终点:{SB_Up_Location},任务已生成", "任务生成记录");
location_36.task = 1;
location_36.update_time = DateTime.Now;
int a = locRepository.Update(location_36, d => new { d.task, d.update_time }, true);
if (a == 1)
{
agvRepository.Add(agvtask, true);
}
//WriteLog.GetLog("设备呼叫").Write($"设备{TBSB},呼叫成功,任务{agvtask.agv_tasknum}生成", "设备呼叫");
break;
}
else
{
//WriteLog.GetLog("设备呼叫").Write($"设备 {TBSB} 上料呼叫,未找到满足搬运条件的货架,任务生成失败", "设备呼叫");
}
}
}
}
catch (Exception ex)
{
WriteLog.GetLog().Write($"负极棍分上料:{DateTime.Now} --------------{ex}", $"负极棍分上料调度");
}
finally
{
Interlocked.Exchange(ref _readFTBSignalsoZ, 0);
}
});
}
}
}
}