using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.DBHelper;
using WIDESEA_Common.Tools;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Entity.DomainModels.Equipment;
using WIDESEA_WCS.Jobs;
using WIDESEA_WCS.JobsPart.Public;
using WIDESEA_WCS.WCSClient;
namespace WIDESEA_WCS
{
///
/// 提升机调度【西门子】
///
[DisallowConcurrentExecution]
public class StackerJob : JobBase, IJob
{
static List centerEqDBList;
static FreeDB freeDB = new FreeDB();
public StackerJob()
{
if (centerEqDBList == null || centerEqDBList.Count == 0)
{
string tsjJson = File.ReadAllText(AppContext.BaseDirectory + "/提升机.json", Encoding.UTF8);
centerEqDBList = JsonConvert.DeserializeObject>(tsjJson);
}
}
public Task Execute(IJobExecutionContext context)
{
//return null;//停用提升机调度
try
{
ExecuteJob(context, DoAction);
}
catch { }
return Task.FromResult(string.Empty);
}
private void DoAction(IJobExecutionContext context)
{
PLCClient client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
if (client==null)
{
return;
}
var stationList = centerEqDBList.FirstOrDefault(t => t.tiShenJiName == client.PLCName);
if (stationList != null)
{
foreach (var station in stationList.stationInfos)
{
var clientData = (byte[])client.ReadDBValue(station.eqDBList[0], 100);
TransferData(station, client, clientData);
try
{
//入口无料
if (station.stationType == "in" && clientData[93] == 0x00)
{
//提升机入口无料不主动叫料
//WCSCommon.CreateTask(station, true);
}
//出口有料
else if (station.stationType == "out" && clientData[93] == 0x01)
{
//物料类型,传任务号
var taskNo = DataTrans.GetStr(clientData, 26) + "_T";
WriteLog.GetLog().Write($"【info】任务{taskNo}请求任务完成。" + "\n", client.PLCName);
if (!string.IsNullOrEmpty(taskNo))
{
FinshTiShenJiTask(taskNo);
}
WCSCommon.CreateTask(station, false);
}
}
catch (Exception ex)
{
WriteLog.GetLog().Write($"【提升机任务请求异常】{client.PLCName},{station.stationCode}口。{ex.Message}" + "\n", client.PLCName);
}
}
}
}
///
/// 完成提升机任务
///
/// 提升机任务号
private void FinshTiShenJiTask(string taskNo)
{
var task = freeDB.Select().Where(t => t.agv_tasknum == taskNo).ToOne();
if (task != null)
{
freeDB.Add(new dt_agvtask_hty()
{
hty_pkid = Guid.NewGuid(),
agv_id = Guid.Empty,
agv_tasknum = task.agv_tasknum,
agv_materielid = task.agv_materielid,
agv_qty = task.agv_qty,
agv_createtime = task.agv_createtime,
agv_realesstime = task.agv_realesstime,
agv_executingBeginTime = task.agv_executingBeginTime,
agv_executingEndTime = task.agv_executingEndTime,
agv_completeBeginTime = task.agv_completeBeginTime,
agv_finishedtime = DateTime.Now,
agv_taskstate = AGVTaskStateEnum.Complete1.ToString(),
agv_tasktype = task.agv_tasktype,
agv_fromaddress = task.agv_fromaddress,
agv_toaddress = task.agv_toaddress,
agv_operatetype = string.Empty,
agv_compeletor = "wcs",
agv_completedate = DateTime.Now,
agv_grade = task.agv_grade,
agv_userid = "wcs",
agv_barcode = task.agv_barcode,
agv_code = task.agv_code,
agv_worktype = task.agv_worktype
});
freeDB.Remove(task);
}
}
///
/// 中转AGV对接数据
///
///
///
///
private void TransferData(StationInfo station, PLCClient tsj_Client, byte[] tsjData)
{
var agv_Client = WIDESEA_WCS.WCSService.Clients.FirstOrDefault(t => t.PLCName == station.agvCenterName);
if (agv_Client != null)
{
var agvData = (byte[])agv_Client.ReadDBValue(station.agvDBList[0], 200);
//90,流程步骤非0,开始对接
if (agvData[90 + 1] != 0x0 && station.stationType == "in")
{
//目的地址
agvData[96] = (byte)station.tiShenJiEndUniCode;
//托盘码
OperateDB.WriteData(agvData, 58, "null");
}
////tsj_Client.WriteDBValue(station.eqDBList[1], agvData);
/// agv_Client.WriteDBValue(station.agvDBList[1], tsjData);
}
else
{
WriteLog.GetLog().Write($"【异常】{station.agvCenterName},读取调度中心PLC失败" + "\n", tsj_Client.PLCName);
}
}
}
}