using Castle.Core.Internal;
|
using Newtonsoft.Json;
|
using System;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using WIDESEA_Common;
|
using WIDESEA_Common.Tools;
|
using WIDESEA_Core.EFDbContext;
|
using WIDESEA_Entity.DomainModels;
|
using WIDESEA_Services.IRepositories;
|
using WIDESEA_Services.Repositories;
|
using WIDESEA_WCS.EquipBaseInfo;
|
using WIDESEA_WCS.WCSClient;
|
|
namespace WIDESEA_WCS.SchedulerExecute.AGV
|
{
|
public partial class AGVSchedulerExecute
|
{
|
private static int _readSendAGVTaskSignalso = 0;
|
private static bool isTrue1 = false;
|
private static string name1 = "";
|
|
public static void SendAGVTask1(PLCClient plcClient)
|
{
|
//if (Interlocked.Exchange(ref _readSendAGVTaskSignalso, 1) == 0)
|
//{
|
try
|
{
|
if (plcClient==null)
|
{
|
return;
|
}
|
VOLContext context = new VOLContext();
|
Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
|
int TaskInteractive = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));//1收到
|
int TaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()));//0初始/1下发/2取消/3更改
|
int resetTaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveW.ToString()));
|
int resetTaskInteractiver = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveR.ToString()));
|
if (isTrue1 && name1 == plcClient.PLCName)
|
{
|
if (TaskInteractiveW != 0 || resetTaskInteractiveW != 0 || TaskInteractive != 0)
|
{
|
plcClient.WriteValue(TaskDBName.resetTaskInteractiveW.ToString(), 1);
|
Task.Delay(2000).Wait();
|
resetTaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveW.ToString()));
|
if (resetTaskInteractiveW == 1)
|
{
|
plcClient.WriteValue(TaskDBName.resetTaskInteractiveW.ToString(), 0);
|
plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
|
Task.Delay(2000).Wait();
|
}
|
return;
|
}
|
else
|
{
|
isTrue1 = false;
|
name1 = "";
|
}
|
}
|
if (resetTaskInteractiver == 1)
|
{
|
plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
|
for (int i = 0; i < 5; i++)
|
{
|
Thread.Sleep(300);
|
var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()));
|
if (agvnumber != 0)
|
{
|
plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
|
}
|
else
|
{
|
break;
|
}
|
}
|
}
|
if (0 == TaskInteractive && TaskInteractiveW == 0)
|
{
|
dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_taskstate == AGVTaskStateEnum.Create.ToString() && r.agv_code == plcClient.PLCName && r.agv_tasktype.Contains(AGVTaskTypeEnum.TaskType_Outbound.ToString())).OrderBy(r => r.agv_createtime).OrderByDescending(r => r.agv_grade).FirstOrDefault();
|
if (null != agvTask)
|
{
|
//写入任务信息
|
string rel = AGVBaseInfo.TaskWrite(plcClient, agvTask, AGVJobEnum.newTaskEnum);
|
/*2023.8.2新增 给驰众写入卷绕长度*/
|
if (agvTask?.agv_toaddress.Contains("JR") ?? false)
|
{
|
plcClient.SiemensPLCClient.SiemensS7NetClient.Write("DB1000.122", agvTask.size);
|
if (!string.IsNullOrEmpty(agvTask.JRunm))
|
{
|
plcClient.SiemensPLCClient.SiemensS7NetClient.Write("DB1000.78.0", agvTask.JRunm);
|
WriteLog.Info("SendAGVTask").Write("给AGV写入任务卷绕信息," + agvTask.size + agvTask.JRunm + DateTime.Now, "SendAGVTask");
|
}
|
else
|
{
|
WriteLog.Info("SendAGVTask").Write("给AGV写入任务卷绕信息,值为空" + DateTime.Now, "SendAGVTask");
|
}
|
|
}
|
#region
|
//if (agvTask.agv_fromaddress.Contains("JJK") && agvTask.agv_toaddress.Contains("TBHCJ"))
|
//{
|
// string barcode = "TB-" + agvTask.agv_materbarcode.Split('*')[0].Substring(agvTask.agv_materbarcode.Split('*')[0].Length - 1, 1);
|
// plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), barcode);
|
// WriteLog.Info("materBarcode1").Write(agvTask.agv_tasknum+barcode + DateTime.Now, "materBarcode1");
|
// for (int i = 0; i < 10; i++)
|
// {
|
// Thread.Sleep(1000);
|
// string newbarcode = plcClient.ReadBarcodeValue(TaskDBName.materBarcode.ToString()).ToString();
|
// if (newbarcode != barcode)
|
// {
|
// plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), barcode);
|
// WriteLog.Info("materBarcode2").Write(agvTask.agv_tasknum + barcode +newbarcode+ DateTime.Now, "materBarcode2");
|
// }
|
// else
|
// {
|
// break;
|
// }
|
// }
|
//}
|
//else
|
//{
|
// plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), "0");
|
// for (int i = 0; i < 10; i++)
|
// {
|
// Thread.Sleep(1000);
|
// string newbarcode = plcClient.ReadBarcodeValue(TaskDBName.materBarcode.ToString()).ToString();
|
// WriteLog.Info("materBarcode3").Write(agvTask.agv_tasknum + newbarcode + DateTime.Now, "materBarcode3");
|
// if (newbarcode!="0")
|
// {
|
// string barcode3= plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), "0").ToString();
|
// WriteLog.Info("materBarcode4").Write(agvTask.agv_tasknum + barcode3 + DateTime.Now, "materBarcode4");
|
// }
|
// else
|
// {
|
// break;
|
// }
|
// }
|
//}
|
#endregion
|
Thread.Sleep(1000);
|
string taskId = plcClient.ReadValue(TaskDBName.taskID.ToString()).ToString();
|
if (taskId != agvTask.agv_tasknum)
|
{
|
isTrue1 = true;
|
name1 = plcClient.PLCName;
|
WriteLog.Info("SendAGVTask").Write("给AGV写入任务失败," + agvTask.agv_tasknum + DateTime.Now, "SendAGVTask");
|
throw new Exception(string.Format("给AGV写入任务失败,任务号{0}", agvTask.agv_tasknum));
|
}
|
if (string.IsNullOrEmpty(rel))
|
{
|
//写入任务确认1
|
plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 1);
|
Thread.Sleep(1000);
|
//读取AGV信号
|
int ReadTask_1_OK = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));
|
if (ReadTask_1_OK == 1)
|
{
|
//写入WCS确认信号0
|
plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
|
Thread.Sleep(2000);
|
//读取AGV信号是否为0
|
int ReadTask_0_OK = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));
|
if (ReadTask_0_OK == 0)
|
{
|
agvTask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString();
|
agvTask.agv_realesstime = DateTime.Now;
|
agvtaskRepository.Update(agvTask, true);
|
// 上传AGV运行数据 by xiaoyang
|
SendMESTask.SendMesTask(agvTask, 0);
|
}
|
else
|
{
|
isTrue1 = true;
|
name1 = plcClient.PLCName;
|
throw new Exception(string.Format("给AGV写入任务确认0失败,任务号{0}", agvTask.agv_tasknum));
|
}
|
}
|
else if (ReadTask_1_OK == 11)
|
{
|
plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
|
Thread.Sleep(2000);
|
int ReadTask_0_Error = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));
|
if (ReadTask_0_Error == 0)
|
{
|
agvTask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString();
|
agvTask.agv_realesstime = DateTime.Now;
|
agvtaskRepository.Update(agvTask, true);
|
}
|
else
|
{
|
isTrue1 = true;
|
name1 = plcClient.PLCName;
|
throw new Exception(string.Format("给AGV写入任务异常确认0失败,任务号{0}", agvTask.agv_tasknum));
|
}
|
}
|
else
|
{
|
isTrue1 = true;
|
name1 = plcClient.PLCName;
|
throw new Exception(string.Format("给AGV写入任务确认1失败,任务号{0}", agvTask.agv_tasknum));
|
}
|
}
|
else
|
{
|
isTrue1 = true;
|
name1 = plcClient.PLCName;
|
throw new Exception(string.Format("给AGV写入任务失败,任务号{0}" + rel, agvTask.agv_tasknum));
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
StackTrace sta = new StackTrace(ex, true);
|
StackTrace st = new StackTrace(new StackFrame(true));
|
StackFrame sf = sta.GetFrame(0);
|
WriteLog.Info("SendAGVTask").Write(ex.Message+"行号"+ sf.GetFileLineNumber(), "SendAGVTask");
|
}
|
finally
|
{
|
// WriteLog.Info("SendAGVTask").Write(plcClient.PLCName+"\t"+DateTime.Now, "SendAGVTask");
|
Interlocked.Exchange(ref _readSendAGVTaskSignalso, 0);
|
}
|
}
|
}
|
}
|