using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.IO;
|
using System.Drawing;
|
using Newtonsoft.Json;
|
using System.Threading;
|
using yunneiWCS.EnumDefine;
|
using yunneiWCS.ORM;
|
using yunneiWCS.ExtendFunction;
|
|
namespace yunneiWCS.data
|
{
|
public static class baseData
|
{
|
public static Mutex single = new Mutex();
|
//static HTTPService api = new HTTPService("http://192.168.0.2:6546/api/v2/");//AGVapi基地址
|
static HTTPService api = new HTTPService("http://192.168.0.3:6546/api/v2/");//AGVapi基地址
|
/// <summary>发送堆垛机任务
|
///
|
/// </summary>
|
/// <param name="taskId">任务ID</param>
|
/// <param name="sourcePlace">起始地址</param>
|
/// <param name="toPlace">目标地址</param>
|
/// <param name="isref">1=不是重发,2=重发</param>
|
public static void sendSrmTask(task _task, long taskId, string SourcePlace, string ToPlace, int isref)
|
{
|
if (single.WaitOne())//增加线程锁防止并发
|
{
|
try
|
{
|
//休眠800毫秒 【Editby shaocx,2024-10-09】
|
//再次校验
|
if (!MyExtendHelper.IsAllowSendNewTaskToSrm(800))
|
{
|
//必须抛异常
|
throw new Exception("当前堆垛机不满足下发任务的条件,任务号:" + taskId + ",起点:" + SourcePlace + ",终点:" + ToPlace);
|
}
|
|
string[] sourcePlace = SourcePlace.Split('-');
|
string[] toPlace = ToPlace.Split('-');
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_toPosX, Convert.ToInt16(toPlace[0]));//目标地址排
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_toPosY, Convert.ToInt16(toPlace[1]));//目标地址列
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_toPosZ, Convert.ToInt16(toPlace[2]));//目标地址层
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_command, Convert.ToInt16(1));//执行方式 搬运
|
if (_task == null)
|
{
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_TaskNo, Convert.ToInt32(taskId));//任务id
|
}
|
else
|
{
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_TaskNo, Convert.ToInt32(_task.PlcTaskNo));//此处任务号更改 【EditBy shaocx,2022-03-28】
|
}
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_sourcePosX, Convert.ToInt16(sourcePlace[0]));//起始地址排
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_sourcePosY, Convert.ToInt16(sourcePlace[1]));//起始地址排
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_sourcePosZ, Convert.ToInt16(sourcePlace[2]));//起始地址排
|
//记录任务记录
|
using (dbmodel mod = new dbmodel())
|
{
|
if (isref == 1)
|
{
|
taskRecord record = new taskRecord();
|
record.taskId = taskId;
|
record.sourcePlace = SourcePlace;
|
record.toPlace = ToPlace;
|
record.taskInfo = "堆垛机任务";
|
record.createTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
if (_task != null)
|
{//增加 【EditBy shaocx,2022-03-28】
|
record.AgvTaskNo = _task.AgvTaskNo;
|
record.PlcTaskNo = _task.PlcTaskNo;
|
}
|
|
mod.taskRecord.Add(record);
|
mod.SaveChanges();
|
}
|
|
}
|
int a = 0;
|
while (a < 5)//握手确认
|
{
|
if (Form1.srm.stb == "是")
|
{
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_ack, Convert.ToInt16(1));//ack确认
|
break;
|
}
|
a++;
|
Thread.Sleep(1000);
|
}
|
}
|
catch (Exception ex)
|
{
|
WZ.Useful.Commons.LogTextHelper.WriteLine("baseData", "sendSrmTask", ex.ToString());
|
throw ex;//必须抛出异常 【Editby shaocx,2024-10-09】
|
}
|
finally
|
{
|
single.ReleaseMutex();
|
}//一轮结束
|
}
|
|
}
|
/// <summary>发送AGV任务
|
///
|
/// </summary>
|
/// <param name="_task">任务对象</param>
|
/// <param name="taskId">任务ID</param>
|
/// <param name="sourcePlace">起始地址</param>
|
/// <param name="toPlace">目标地址</param>
|
/// <param name="isref">1=不是重发,2=重发</param>
|
public static string sendAgvTask(task _task, long taskId, string sourcePlace, string toPlace, int isref)
|
{
|
//注意:_task肯定不为NULL
|
string result = "失败";
|
if (single.WaitOne())//增加线程锁防止并发
|
{
|
try
|
{
|
var _agvTaskNo = _task.AgvTaskNo;
|
var _old_agvTaskNo = _task.AgvTaskNo;
|
if (isref == 2)
|
{
|
_agvTaskNo = TaskNoHelper.CreateTaskIdForAgvTask(_task.PlcTaskNo);//重发的时候,要重新生成任务号【Editby shaocx,2024-10-09】
|
}
|
var data = new
|
{
|
//id = taskId,
|
//id = _task.AgvTaskNo,//此处任务号更改 【EditBy shaocx,2022-03-28】
|
id = _agvTaskNo,//此处任务号更改 【EditBy shaocx,2022-03-28】
|
systemId = "WMS",
|
type = "LoadingAndUnloading",
|
source = sourcePlace,
|
destination = toPlace
|
};
|
string paprm = JsonConvert.SerializeObject(data);
|
Guid guid = System.Guid.NewGuid();
|
result = api.postContentForStringForAgv("orders", paprm, guid);
|
//记录AGV发送任务
|
if (result == "")
|
{
|
if (isref == 1)
|
{
|
using (dbmodel mod = new dbmodel())
|
{
|
taskRecord record = new taskRecord();
|
record.taskId = taskId;
|
record.sourcePlace = sourcePlace;
|
record.toPlace = toPlace;
|
record.taskInfo = "AGV任务";
|
record.createTime = DateTime.Now;
|
if (_task != null)
|
{//增加 【EditBy shaocx,2022-03-28】
|
record.AgvTaskNo = _task.AgvTaskNo;
|
record.PlcTaskNo = _task.PlcTaskNo;
|
}
|
|
mod.taskRecord.Add(record);
|
mod.SaveChanges();
|
}
|
}
|
else
|
{
|
//重发,需要更新AGV任务号 【Editby shaocx,2024-10-09】
|
using (dbmodel mod = new dbmodel())
|
{
|
var taskDb = mod.task.Where(t => t.taskId == _task.taskId).FirstOrDefault();
|
taskDb.AgvTaskNo = _agvTaskNo;
|
//子任务的agv任务号
|
var taskRecord = mod.taskRecord.Where(t => t.AgvTaskNo == _old_agvTaskNo).FirstOrDefault();
|
if (taskRecord == null)
|
{
|
result = "根据agv任务号" + _old_agvTaskNo + "没有找到agv任务";
|
return result;
|
}
|
taskRecord.AgvTaskNo = _agvTaskNo;
|
|
mod.SaveChanges();
|
}
|
}
|
|
}
|
else
|
{
|
AgvRetModel agvRet = JsonConvert.DeserializeObject<AgvRetModel>(result);
|
if (agvRet != null)
|
{
|
result = agvRet.message;
|
}
|
else
|
{
|
result = "AGV返回失败状态码";
|
}
|
|
WZ.Useful.Commons.LogTextHelper.WriteLine("baseData", "sendAgvTask", "发送AGV任务失败Taskid:" + taskId + " 发送结果: " + result + " 是否重发:" + (isref == 1 ? "不是" : "是"));
|
}
|
}
|
catch (Exception ex)
|
{
|
result = "AGV发送异常:" + ex.Message;
|
WZ.Useful.Commons.LogTextHelper.WriteLine("baseData", "sendAgvTask" + "发送AGV任务异常Taskid:" + taskId, ex.ToString());
|
}
|
|
finally { single.ReleaseMutex(); }//一轮结束
|
|
}
|
return result;
|
}
|
|
/// <summary>堆垛机ACK确认
|
///
|
/// </summary>
|
public static void sendSrmACK()
|
{
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_ack, Convert.ToInt16(1));//ack确认
|
|
}
|
/// <summary>堆垛机任务确认
|
///
|
/// </summary>
|
public static void srmConfirm()
|
{
|
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_Task_Finish_ACK, Convert.ToInt16(1));//确认任务
|
}
|
public static void srmAlarmReset()
|
{
|
Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_alarmAck, Convert.ToInt16(1));//确认任务
|
|
}
|
|
public static List<AgvStatus> readAgvs()
|
{
|
Guid guid = System.Guid.NewGuid();
|
string res = api.getContentForString("agvStatus", guid);
|
|
List<AgvStatus> result = JsonConvert.DeserializeObject<List<AgvStatus>>(res);
|
return result;
|
|
}
|
}
|
}
|