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
{
public static object requestZJTBDown = new object();//requestZJJK
private static int _readZTBSignalsoZ_Down = 0;//_readZTBUPSignalso
private static int lock_TBXL_Z = 1;
///
/// 正极涂布下料工作逻辑
///
///
public static void Z_TBSB_DownTask()
{
if (Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 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" };
if (WCSService.Clients == null)
{
return;
}
//循环处理涂布设备下料请求
foreach (var TBSB in TBSBS)
{
PLCClient plc = WCSService.Clients.Find(v => v.PLCName == TBSB);
if (plc == null)
{
continue;
}
string SB_Up_Location = "ZJXL-TBSB" + TBSB.Replace("涂布机正极", "");//拼接涂布下料口
//查看当前涂布机设备是否有AGV任务
var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
//有结束本次循环
if (SBtask != null) { continue; }
//当前设备有没有下料请求
bool GYR_RFX = plc.OmronPLCClient.OmronFinsNetClient.ReadBool("D11293.00").Content;
//Convert.ToBoolean(plc.ReadValue("R_Feedingrequest".ToString(), SB_Up_Location));
if (GYR_RFX)
{
#region 记录下料请求信号
WriteLog.GetLog().Write($"{plc.PLCName}下料请求:{GYR_RFX}---{DateTime.Now}", $"正极涂布下料请求");
#endregion
//找到路由规则(以设备为起点的路由规则)
List Routing_Table = routingRepository.Find(d => d.route_began == SB_Up_Location);
base_ware_location location_TB = null;
if (Routing_Table.Count > 0)
{
foreach (var item in Routing_Table)
{
//找先找涂布区域缓存架
location_TB = locRepository.FindFirst(t => t.upper_code == item.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.tpgd == 0 && t.area == "ZJ-TBHCJ" && t.status == 1 && t.task == 2);
if (location_TB != null)
{
//location = location_TB.FirstOrDefault();//如果找到,赋值后退出循环
break;
}
}
//以上循环未找到合适货位,再找箔材库三号位缓存架
if (location_TB == null)
{
//定位铂材库路由,找到铂材库货位号
base_routing_table Loc_BCK = Routing_Table.FirstOrDefault(t => t.route_end.Contains("ZJ-BCHCJ"));
if (Loc_BCK != null)
{
//根据根据货位号找到铂材库位置并确认是否有料
location_TB = locRepository.FindFirst(t => t.upper_code == Loc_BCK.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.status == 1 && t.task == 2);
if (location_TB == null)
{
return;
}
}
}
}
if (location_TB != 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_TB.upper_code,
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_TB.update_time = DateTime.Now;
location_TB.task = 1;
var entry = locRepository.DbContext.ChangeTracker.Entries().FirstOrDefault();
if (entry != null)
{
entry.State = EntityState.Detached;
}
int x = locRepository.Update(location_TB, d => new { d.update_time, d.task }, true);
if (x == 1)
{
agvRepository.Add(agvtask, true);
//WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"生成任务:{agvtask.agv_tasknum},终点货位【{location_TB.upper_code}】状态:托盘光电检测:{location_TB.tpgd},状态:{location_TB.location_state},更新时间:{location_TB.wireless_time}", $"【{TBSB}】下料任务生成");
//WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"----------------任务生成成功 {DateTime.Now} -------------- {Environment.NewLine}", $"【{TBSB}】下料任务生成");
return content.OK();
}
else
{
return content.Error();
}
});
}
}
}
}
catch (Exception ex)
{
WriteLog.GetLog().Write($"正极涂布下料:{DateTime.Now} --------------{ex}", $"正极涂布下料");
}
finally
{
Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 0);
}
});
}
}
}
}