using FreeSql.Internal.Model;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Extensions;
using WIDESEA_Core.FreeDB;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.JobsPart.Common;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static FreeSql.Internal.GlobalFilter;
using static System.Collections.Specialized.BitVector32;
namespace WIDESEA_WCS
{
public class Gantry
{
#region 查询车轮数据
///
/// 查询车轮数据
///
///
public void QueryWheeldata(PLCClient client)
{
string namea = "";
try
{
VOLContext Context = new VOLContext();
Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
List names = new List() { "1单元辊道下料查询车轮", "2单元辊道下料查询车轮", "3单元辊道下料查询车轮", "辊道上料查询车轮" };
foreach (string name in names)
{
namea = name;
var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on").First(), client);
if ((byte)on == 1)
{
var wp_type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_type").First(), client);
var number = "1单元辊道下料查询订单";
if (name == "2单元辊道下料查询车轮") number = "2单元辊道下料查询订单";
if (name == "3单元辊道下料查询车轮") number = "3单元辊道下料查询订单";
if (name == "辊道上料查询车轮") number = "辊道上料查询订单";
var wp_id = client.ReadByOrder("R_o_wp_id", number);
if (name == "辊道上料查询车轮")
{
var wp_id1 = client.ReadByOrder("W_Wheel_id", "上料区");//车轮SN号
if (wp_id != wp_id1)
{
client.WriteByOrder("W_i_status", (byte)2, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n查询车轮SN:{wp_id}\n上料申请SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单信息");
continue;
}
}
var Mes_Work = workinfoRepository.Find(x => x.SN == wp_id && x.processCode == (name != "辊道上料查询车轮" ? "17" : "28")).OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (Mes_Work == null)
{
client.WriteByOrder("W_i_status", (byte)2, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单信息");
#endregion
continue;
}
var geometry = Pipeline.QueryMateriel(Mes_Work.drawingNo);
if (geometry == null)
{
//1-找到数据,2-未找到工件类型数据
client.WriteByOrder("W_i_status", (byte)2, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到机加工车轮信息");
#endregion
}
else
{
//1-找到数据,2-未找到工件类型数据
client.WriteByOrder("W_i_status", (byte)1, name);
client.WriteByOrder("W_i_parameter_a", (float)geometry.a, name);
client.WriteByOrder("W_i_parameter_b", (float)geometry.b, name);
client.WriteByOrder("W_i_parameter_c", (float)geometry.c, name);
client.WriteByOrder("W_i_parameter_d", (float)geometry.d, name);
client.WriteByOrder("W_i_parameter_e", (float)geometry.e, name);
client.WriteByOrder("W_i_parameter_f", (float)geometry.f, name);
client.WriteByOrder("W_i_parameter_g", (float)geometry.g, name);
client.WriteByOrder("W_i_parameter_h", (float)geometry.h, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
WriteLog.Write_Log(name, wp_id, "写入信息!", $"读取桁架信息:\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:1" + $"\n" +
$"a:{(float)geometry.a}\n" +
$"b:{(float)geometry.b}\n" +
$"c:{(float)geometry.c}\n" +
$"d:{(float)geometry.d}\n" +
$"e:{(float)geometry.e}\n" +
$"f:{(float)geometry.f}\n" +
$"g:{(float)geometry.g}\n" +
$"h:{(float)geometry.h}\n" +
$"R_oi_on:0");
#endregion
}
}
}
}
catch (Exception ex)
{
WriteLog.Write_Log("报错日志", namea, "错误信息!", $"错误信息:{ex.Message}");
}
}
#endregion
#region 查询订单,是否需要加工
///
/// 查询订单
///
///
public void QueryOrder(PLCClient client)
{
string namea = "";
try
{
VOLContext Context = new VOLContext();
Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
Idt_patternRepository patternRepository = new dt_patternRepository(Context);
var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
List names = new List() { "1单元辊道下料查询订单", "2单元辊道下料查询订单", "3单元辊道下料查询订单", "辊道上料查询订单" };
foreach (string name in names)
{
namea = name;
var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on").First(), client);
//var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on" && x.plcdetail_number == name).First(), client);
if ((byte)on == 1)
{
//var wp_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_id").First(), client).ToString();
var wp_id = client.ReadByOrder("R_o_wp_id", name);
if (string.IsNullOrEmpty(wp_id))
{
client.WriteByOrder("W_i_status", (byte)2, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
//WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n车轮SN号为空", "PCS");
WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n车轮SN号为空");
#endregion
continue;
}
var wp_type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_type").First(), client);
var Mes_Work = workinfoRepository.Find(x => x.SN == wp_id && x.processCode == (name != "辊道上料查询订单" ? "17" : "28")).OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (Mes_Work == null)
{
client.WriteByOrder("W_i_status", (byte)2, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单详情");
#endregion
continue;
}
var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == Mes_Work.jobID);
var geometry = Pipeline.QueryMateriel(Mes_Work.drawingNo);
if (geometry == null)
{
//1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
client.WriteByOrder("W_i_status", (byte)2, name);
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到机加工车轮信息");
#endregion
continue;
}
else
{
//1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
client.WriteByOrder("W_i_status", (byte)1, name);
client.WriteByOrder("W_i_job_id", Mes_Work.jobID, name);
client.WriteByOrder("W_i_drawing_id", Mes_Work.drawingNo, name);
client.WriteByOrder("W_i_heat_id", Mes_Work.heatID, name);
//client.WriteByOrder("W_i_sourceheat", "", name);
client.WriteByOrder("W_i_batch_id", Mes_Work.heatBatchID, name);
//屏蔽工艺(屏蔽视觉检测/屏蔽涂油)
if (name != "辊道上料查询订单")
{
client.WriteByOrder("W_i_skip_op_SJ1", name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_1, name);//屏蔽视觉检测设备1
client.WriteByOrder("W_i_skip_op_SJ2", name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_2, name);//屏蔽视觉检测设备2
client.WriteByOrder("W_i_skip_op_LT", mes_Head.skip_op_3, name);//屏蔽链条机
}
client.WriteByOrder("R_oi_on", (byte)0, name);
#region 日志记录
WriteLog.Write_Log(name, wp_id, "写入信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:1" +
$"\nW_i_job_id:{Mes_Work.jobID}\nW_i_drawing_id:{Mes_Work.drawingNo}\nW_i_heat_id:{Mes_Work.heatID}\nW_i_batch_id:{Mes_Work.heatBatchID}" +
$"\nW_i_skip_op_SJ1:{(name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_1)}\nW_i_skip_op_SJ2:{(name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_2)}" +
$"\nW_i_skip_op_LT:{mes_Head.skip_op_3}\nR_oi_on:0");
#endregion
}
}
}
}
catch (Exception ex)
{
//WriteDBLog.Error("查询车轮订单", $"错误信息:{ex.Message}", "PCS");
WriteLog.Write_Log("报错日志", namea, "错误信息!", $"错误信息:{ex.Message}");
}
}
#endregion
#region 下料位交互
///
/// 下料
///
///
public void Layofflevel(PLCClient client)
{
try
{
VOLContext Context = new VOLContext();
Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
Idt_patternRepository patternRepository = new dt_patternRepository(Context);
var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
var Pipelineplc = repository.FindFirst(x => x.plcinfo_name == Pipeline_client.PLCName);
List names = new List() { "1单元下料区", "2单元下料区", "3单元下料区" };
foreach (string name in names)
{
try
{
var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
var NGStation = "X01001003";
if (name == "2单元下料区") NGStation = "X02001003";
if (name == "3单元下料区") NGStation = "X03001002";
var finished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_load_finished").First(), client);//放料完成
var updatefinished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_Storage_update").First(), client);//货位状态更新
#region 货位状态更新复位
if (!finished && updatefinished)
{
client.WriteByOrder("W_Storage_update", false, name);//货位状态更新
#region 日志记录
//WriteDBLog.Success(name + "货位状态更新复位", $"读取桁架信息:\nR_load_finished:{finished}\n\n写入桁架信息:\nW_Storage_update:{false}", "PCS");
WriteLog.Write_Log(name + "放料完成", name + "货位状态更新信号复位", "成功!",
$"读取桁架信息:\n放料完成信号:{finished}\n\n写入桁架信息:\n货位状态更新信号:{false}");
#endregion
}
#endregion
if (finished && !updatefinished)
{
client.WriteByOrder("W_Enabl_Load", false, name);//是否允许
#region 读取桁架信息
var WheelType = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_Type").First(), client);//车轮类型
var Wheel_SN = client.ReadByOrder("R_Wheel_id", name);//车轮SN号
if (string.IsNullOrEmpty(Wheel_SN))
{
//WriteDBLog.Error(name + "放料完成", $"读取桁架信息:\n车轮SN号为空", "PCS");
WriteLog.Write_Log(name + "放料完成出错", "车轮SN号为空", "读取桁架信息:", $"车轮SN号:{Wheel_SN}为空");
continue;
}
//var Wheel_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_id").First(), client).ToString();
var Mes_Work = workinfoRepository.Find(x => x.SN == Wheel_SN && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (Mes_Work == null)
{
#region 日志记录
//WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN号:{Wheel_SN}\n\n未查询到工单详情", "PCS");
WriteLog.Write_Log(name + "放料完成出错", Wheel_SN, "读取桁架信息:", $"车轮SN号:{Wheel_SN}未查询到工单信息");
#endregion
continue;
}
var geometry = Pipeline.QueryMateriel(Mes_Work.drawingNo);
#endregion
var AreaNr = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_AreaNr").First(), client);
if (AreaNr == 0)
{
client.WriteByOrder("W_Storage_update", true, name);//货位状态更新
continue;
}
#region 判断区域号是否一致
var area = client.ReadByOrder("place_prog", name);//放料完成的区域号
if (AreaNr != area)
{
Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
//WriteDBLog.Error(name + "报警", $"PCS区域号:{AreaNr};桁架区域号:{area}错误信息:PCS区域号与桁架区域号不一致!", "PCS");
WriteLog.Write_Log(name + "放料完成出错", Wheel_SN, "判断区域号是否一致", $"PCS区域号:{AreaNr};\n桁架区域号:{area}\n错误信息:PCS区域号与桁架区域号不一致!");
continue;
}
#endregion
var areaCode = Pipeline.area_code(name); //根据下料口单元名获取区域代码
var station = stationinfoRepository.Find(x => x.area == areaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == AreaNr.ToString()).FirstOrDefault();
if (station != null)
{
if (station.quantity >= geometry.stackNoRange)
//if (station.quantity >= 5)
{
Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
WriteLog.Write_Log(name + "放料完成出错", Wheel_SN, "车轮数量已满!", $"区域号:{AreaNr};\n车轮数:{station.quantity}\n错误信息:区域号车轮数量已满!");
}
#region 判断车轮数量是否一致
var quantity = client.ReadByOrder("pos_prog", name);//桁架记录的车轮数量
if (quantity != station.quantity + 1)
{
Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
WriteLog.Write_Log(name + "放料完成出错", Wheel_SN, "判断车轮数量是否一致", $"区域号:{AreaNr};\n桁架车轮数:{quantity}\nPCS车轮数:{station.quantity + 1}\n错误信息:PCS区域号与桁架区域号不一致!");
continue;
}
#endregion
if (!string.IsNullOrEmpty(station.bindSN))
{
if (station.bindSN.Contains(Wheel_SN))
throw new Exception($"{station.stationCode}已存在车轮SN号:{Wheel_SN}");
}
#region 清空给桁架的下料信息
client.WriteByOrder("W_IndexNr", (Int16)0, name);//托盘上的第几个车轮
client.WriteByOrder("W_Storage_Type", (Int16)0, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
client.WriteByOrder("W_AreaNr", (Int16)0, name);//区域货位号
client.WriteByOrder("W_Storage_update", true, name);//货位状态更新
WriteLog.Write_Log(name + "放料完成", Wheel_SN, "成功!",
$"读取桁架信息:\n放料完成信号:{finished}\n车轮SN:{Wheel_SN}\n轮型ID:{WheelType}\n下料位号:{AreaNr}\n\n" +
$"处理写入桁架信息:\n区域货位号:{0}\n托盘上的第几个车轮:{0}\n托盘类型:{0}\n\n" +
$"写入桁架信息:\n货位状态更新信号:{true}\n是否允许下料:{false}");
//WriteDBLog.Success(name + "桁架下料完成",
// $"读取桁架信息:\nR_load_finished:{finished}\n车轮SN:{Wheel_SN}\n轮型ID:{WheelType}\n下料位号:{AreaNr}\n\n" +
// $"处理写入桁架信息:\nW_AreaNr:{0}\nW_IndexNr:{0}\nW_Storage_Type:{0}\n\n" +
// $"写入桁架信息:\nW_Storage_update:{true}\nW_Enabl_Load:{false}", "PCS");
#endregion
#region 货位状态更新
station.bindSN = string.IsNullOrEmpty(station.bindSN) ? Wheel_SN : station.bindSN + "," + Wheel_SN;
station.billetID = string.IsNullOrEmpty(station.billetID) ? Mes_Work.billetID.ToString() : station.billetID + "," + Mes_Work.billetID;
station.quantity++;
if (station.quantity == 1)
{
station.stationType = Mes_Work.drawingNo;
station.Number = Mes_Work.workOrder;
station.heatNumber = Mes_Work.heatID;
station.tray_status = "StrogeTray";
}
station.remark = "";
var count = stationinfoRepository.Update(station, true);
if (count < 1)
throw new Exception($"下料位信息更新失败!下料位编号:{station.stationCode}");
#endregion
#region 修改工单状态
var Detail = mes_DetailRepository.FindFirst(x => x.jobID == Mes_Work.jobID && x.SN == Mes_Work.SN);
Detail.Status = "下线";
Detail.FinishTime = DateTime.Now;
count = mes_DetailRepository.Update(Detail, x => new { x.Status, x.FinishTime }, true);
if (count < 1)
throw new Exception($"工单子表信息更新失败!车轮SN号:{Detail.SN}");
var Head = mes_HeadRepository.FindFirst(x => x.jobID == Detail.jobID);
Head.finishNum = Head.finishNum + 1;
if (Head.finishNum == Head.quantity) Head.FinishTime = DateTime.Now;
count = mes_HeadRepository.Update(Head, x => new { x.finishNum, x.FinishTime }, true);
if (count < 1)
throw new Exception($"工单头表信息更新失败!工单号:{Head.jobID}");
#endregion
}
}
var Stations = stationinfoRepository.Find(x => x.area == Pipeline.area_code(name)).OrderBy(x => x.lastUpdateTime).ToList();
#region 下料位是否为空状态同步给桁架
foreach (var station in Stations)
{
client.WriteByOrder(station.stationCode, station.location_state != LocationStateEnum.Stroge.ToString(), name);
}
#endregion
var Request = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Request_Load").First(), client);
if (!Request) continue;
#region 读取桁架信息
var Wheel_Type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_Type").First(), client);//车轮类型
var Wheel_id = client.ReadByOrder("R_Wheel_id", name);//车轮SN号
if (string.IsNullOrEmpty(Wheel_id))
{
WriteLog.Write_Log(name + "申请放料异常", "车轮SN号为空", "", $"读取桁架信息:\n车轮SN号:{Wheel_id}为空");
continue;
}
var Work = workinfoRepository.Find(x => x.SN == Wheel_id && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (Work == null)
{
client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
//WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{Wheel_id}\n\n未查询到工单信息", "PCS");
WriteLog.Write_Log(name + "申请放料异常", Wheel_id, "查询机加工工单", $"读取桁架信息:\n车轮SN号:{Wheel_id}未查询到工单信息\n写入桁架信息:\n是否允许下料:{false}");
continue;
}
#endregion
var wheel = Pipeline.QueryMateriel(Work.drawingNo);
//var wheel1 = Pipeline.QueryMateriel1(Work.drawingNo);
if (wheel == null /*&& wheel1 == null*/)
{
#region 日志记录
client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
//WriteDBLog.Error(name, $"未查询到车轮信息!", "PCS");
WriteLog.Write_Log(name + "申请放料异常", Wheel_id, $"查询{Work.drawingNo}车轮参数", $"读取桁架信息:\n车轮SN号:{Wheel_id}未查询到工单信息\n写入桁架信息:\n是否允许下料:{false}");
#endregion
continue;
}
#region 换图号,下料口其他图号托盘全部入库
var stations = Stations.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0 && x.stationType != Work.drawingNo).ToList();
foreach (var station in stations)
{
if (station.stationCode == NGStation || name == "3单元下料区")
agvtask(stationinfoRepository, station, Work, "TaskType_OutsourceInbound");
else
agvtask(stationinfoRepository, station, Work, "TaskType_Inbound");
}
#endregion
#region 托盘类型不一致需更换托盘
stations = Stations.Where(x => x.enable).ToList();
foreach (var Stationinfo in stations)
{
if (Stationinfo.tray_type != "LargeTray" && /*(wheel == null ? wheel1.e :)*/ wheel.e > 1100)
{
client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
Stationinfo.location_state = LocationStateEnum.Trayswitching.ToString();
Stationinfo.remark = "托盘类型不一致需更换托盘";
stationinfoRepository.Update(Stationinfo, x => new { x.location_state }, true);
}
}
#endregion
Stations = stationinfoRepository.Find(x => x.area == Pipeline.area_code(name)).OrderBy(x => x.lastUpdateTime).ToList();
var PartStatus = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PartStatus").First(), client);//1:ok;2:nok
var load_zone = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_load_zone").First(), client);
#region 日志记录
//WriteDBLog.Success(name + "申请放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:{PartStatus}\n是否进入下料区:{load_zone}", "PCS");
WriteLog.Write_Log(name + "申请放料", Wheel_id, "申请放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:{PartStatus}\n是否进入下料区:{load_zone}");
if (load_zone) continue;
#endregion
#region 合格
if ((Int16)PartStatus == 1)
{
dt_stationinfo Stationinfo = null;
if (Work.heatID != null)
{
Stationinfo = Stations
.Where(x => x.enable
&& x.stationCode != NGStation
&& x.location_state == LocationStateEnum.Stroge.ToString()
&& x.quantity < wheel.stackNoRange
//&& x.quantity < 5
&& x.stationType == Work.drawingNo
&& x.Number == Work.workOrder
&& x.heatNumber == Work.heatID)
.OrderByDescending(x => x.quantity)
.FirstOrDefault();
}
else
{
Stationinfo = Stations
.Where(x => x.enable
&& x.stationCode != NGStation
&& x.location_state == LocationStateEnum.Stroge.ToString()
&& x.quantity < wheel.stackNoRange
//&& x.quantity < 5
&& x.stationType == Work.drawingNo
&& x.Number == Work.workOrder)
.OrderByDescending(x => x.quantity)
.FirstOrDefault();
}
if (Stationinfo == null)
{
#region 触发NG任务
var NG = Stations.Where(x => x.enable && x.stationCode == NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.Number != Work.workOrder && x.quantity > 0).FirstOrDefault();
if (NG != null)
agvtask(stationinfoRepository, NG, Work, "TaskType_OutsourceInbound");
#endregion
Stationinfo = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
}
if (Stationinfo != null)
{
#region 换类型直接入库
//var stationinfo1 = Stations.Where(x => x.enable && x.stationCode != NGStation && x.stationCode != Stationinfo.stationCode && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0 && x.stationType != Work.drawingNo).FirstOrDefault();
//if (stationinfo1 != null)
// agvtask(stationinfoRepository, stationinfo1, Work, name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
#endregion
#region 托盘车轮堆垛第四个时下发另外一个炉号的入库任务
//if (Stationinfo.quantity + 1 >= 4)
if (Stationinfo.quantity + 1 >= wheel.stackNoRange - 1)
{
var stationinfo = Stations.Where(x => x.enable && x.stationCode != NGStation && x.stationCode != Stationinfo.stationCode && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).FirstOrDefault();
if (stationinfo != null)
agvtask(stationinfoRepository, stationinfo, Work, name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
}
#endregion
var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Stationinfo.stationCode).ToList();
var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), Pipeline_client);//读取托盘信号:1:有,2无
var MaterialSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), Pipeline_client);//读取货物信号:1:有,2无
if (PalletSignal == 1)
{
List list = new List();
if (Stationinfo.bindSN != null)
{
var SNS = Stationinfo.bindSN.Split(",");
foreach (var SN in SNS)
{
if (!string.IsNullOrEmpty(SN))
list.Add(SN);
}
}
#region 货位数量与光电不匹配,报警
//if (list.Count > 0 && MaterialSignal != 1)
//{
// client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
// Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
// Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
// Stationinfo.remark = "车轮数量与光电不匹配";
// stationinfoRepository.Update(Stationinfo, true);
// WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
// continue;
//}
//else if (list.Count < 1 && MaterialSignal == 1)
//{
// client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
// Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
// Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
// Stationinfo.remark = "车轮数量与光电不匹配";
// stationinfoRepository.Update(Stationinfo, true);
// WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
// continue;
//}
#endregion
//缓存架上车轮数量与SN号数量不一致
if (list.Count != Stationinfo.quantity)
{
Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
Stationinfo.remark = "车轮数量与SN号数量不一致";
stationinfoRepository.Update(Stationinfo, true);
continue;
//写入桁架报警信号
//throw new Exception("下料位车轮数量与SN号数量不一致,下料位编号:" + Stationinfo.stationCode);
}
var area = Convert.ToInt16(Stationinfo.stationCode.Substring(Stationinfo.stationCode.Length - 1, 1));
client.WriteByOrder("W_AreaNr", (Int16)area, name);//区域货位号
client.WriteByOrder("W_IndexNr", (Int16)(Stationinfo.quantity + 1), name);//托盘上的第几个车轮
client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
Stationinfo.remark = "桁架下料";
stationinfoRepository.Update(Stationinfo, true);
#region 日志记录
//WriteDBLog.Success(name + "允许放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:OK" +$"\n读取托盘光电信号:{PalletSignal}" +
// $"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Stationinfo.quantity + 1)}\n托盘类型:1\nW_Enabl_Load:true", "PCS");
WriteLog.Write_Log(name + "申请放料", Wheel_id, "允许放料",
$"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:合格" +
$"\n读取托盘光电信号:{PalletSignal}" +
$"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Stationinfo.quantity + 1)}\n托盘类型:1\n是否允许下料:true");
#endregion
continue;
}
}
else
{
#region 没托盘可用,触发入库任务
if (!Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state != LocationStateEnum.Stroge.ToString()).Any())
{
var station = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString()).OrderBy(x => x.lastUpdateTime).FirstOrDefault();
agvtask(stationinfoRepository, station, Work, name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
}
#endregion
}
}
#endregion
#region 不合格
else if ((Int16)PartStatus == 2)
{
dt_stationinfo Stationinfo = Stations.Where(x => x.enable
&& x.stationCode == NGStation
&& x.location_state == LocationStateEnum.Stroge.ToString()
&& x.quantity < wheel.stackNoRange
//&& x.quantity < 5
&& x.stationType == Work.drawingNo).FirstOrDefault();
if (Stationinfo == null)
{
#region 触发NG任务
var NG = Stations.Where(x => x.enable && x.stationCode == NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).FirstOrDefault();
if (NG != null)
{
agvtask(stationinfoRepository, NG, Work, "TaskType_OutsourceInbound");
continue;
}
#endregion
Stationinfo = Stations.Where(x => x.enable && x.stationCode == NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
}
if (Stationinfo != null)
{
var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Stationinfo.stationCode).ToList();
var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), Pipeline_client);//读取托盘信号:1:有,2无
var MaterialSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), Pipeline_client);//读取货物信号:1:有,2无
if (PalletSignal == 1)
{
List list = new List();
if (Stationinfo.bindSN != null)
{
var SNS = Stationinfo.bindSN.Split(",");
foreach (var SN in SNS)
{
if (!string.IsNullOrEmpty(SN))
list.Add(SN);
}
}
#region 货位数量与光电不匹配,报警
//if (list.Count > 0 && MaterialSignal != 1)
//{
// client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
// Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
// Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
// Stationinfo.remark = "车轮数量与光电不匹配";
// stationinfoRepository.Update(Stationinfo, true);
// WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
//}
//else if (list.Count < 1 && MaterialSignal == 1)
//{
// client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
// Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
// Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
// Stationinfo.remark = "车轮数量与光电不匹配";
// stationinfoRepository.Update(Stationinfo, true);
// WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
//}
#endregion
//缓存架上车轮数量与SN号数量不一致
if (list.Count != Stationinfo.quantity)
{
Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
Stationinfo.remark = "车轮数量与SN号数量不一致";
stationinfoRepository.Update(Stationinfo, true);
continue;
}
var area = Convert.ToInt16(Stationinfo.stationCode.Substring(Stationinfo.stationCode.Length - 1, 1));
client.WriteByOrder("W_AreaNr", (Int16)area, name);//区域货位号
client.WriteByOrder("W_IndexNr", (Int16)(Stationinfo.quantity + 1), name);//托盘上的第几个车轮
client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
Stationinfo.remark = "桁架下料";
stationinfoRepository.Update(Stationinfo, true);
#region 日志记录
//WriteDBLog.Success(name + "允许放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:NG" +$"\n读取托盘光电信号:{PalletSignal}" +
// $"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Stationinfo.quantity + 1)}\n托盘类型:1\nW_Enabl_Load:true", "PCS");
WriteLog.Write_Log(name + "申请放料", Wheel_id, "允许放料",
$"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:不合格" +
$"\n读取托盘光电信号:{PalletSignal}" +
$"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Stationinfo.quantity + 1)}\n托盘类型:1\n是否允许下料:true");
#endregion
continue;
}
}
}
#endregion
}
catch (Exception ex)
{
//WriteDBLog.Error(name, $"错误信息:{ex.Message}", "PCS");
WriteLog.Write_Log("报错日志", name, "错误信息!", $"错误信息:{ex.Message}");
}
}
}
catch (Exception ex)
{
//WriteDBLog.Error("下料区", $"错误信息:{ex.Message}", "PCS");
WriteLog.Write_Log("报错日志", "下料区", "错误信息!", $"错误信息:{ex.Message}");
}
}
#endregion
public void agvtask(Idt_stationinfoRepository stationinfoRepository, dt_stationinfo stationinfo, VV_Mes_Workinfo Work, string tasktype)
{
VOLContext Context = new VOLContext();
Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(Context);
IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
Work = workinfoRepository.Find(x => x.workOrder == stationinfo.Number && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
var task = agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).FirstOrDefault();
if (task != null) return;
if (!string.IsNullOrEmpty(Work.area))
tasktype = "TaskType_OutsourceInbound";
AGVTask.AddQueueTask(stationinfoRepository, agvtaskRepository, stationinfo, tasktype, Work);
}
}
}