using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Quartz;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_Tasks.DBname;
|
using HslCommunication.WebSocket;
|
using Newtonsoft.Json;
|
using WIDESEAWCS_Tasks.Command;
|
using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_Model.Models.TaskInfo;
|
using WIDESEAWCS_Common.TaskEnum;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_Core;
|
using Autofac.Core;
|
using WIDESEAWCS_DTO.Stock;
|
using WIDESEAWCS_Tasks.DBName;
|
using WIDESEAWCS_Core.LogHelper;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
[DisallowConcurrentExecution]
|
public class StackerCraneJob : JobBase, IJob
|
{
|
public static int R_HeartBeat = 0;
|
public static string InorOut = "In";
|
//public static CommandData CommandData = new CommandData();
|
WebSocketServer _webSocketServer;
|
private readonly IRepository<Dt_Task> _taskRepository;
|
private readonly ITaskService _TaskService;
|
|
//存储信号
|
private readonly Commands comm = new Commands();
|
|
public StackerCraneJob(WebSocketServer webSocketServer, IRepository<Dt_Task> taskRepository, ITaskService TaskService)
|
{
|
_webSocketServer = webSocketServer;
|
_taskRepository = taskRepository;
|
_TaskService = TaskService;
|
}
|
|
public Task Execute(IJobExecutionContext context)
|
{
|
try
|
{
|
Thread.Sleep(3000);
|
//连接堆垛机
|
CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
|
if (commonStackerCrane != null)
|
{
|
//判断信号
|
int R_TaskPhases = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_TaskPhases);
|
int R_OrderNo = commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.R_OrderNo);
|
int R_ControlMode = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_ControlMode);
|
int R_TaskStatus = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_TaskStatus);
|
int R_LoadStatus = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_LoadStatus);
|
|
//Commands.CommandData.R_LoadStatus
|
//信号赋值
|
comm.R_TaskPhases((short)R_TaskPhases);
|
comm.R_OrderNo(R_OrderNo);
|
comm.R_ControlMode((short)R_ControlMode);
|
comm.R_TaskStatus((short)R_TaskStatus);
|
comm.R_LoadStatus((short)R_LoadStatus);
|
comm.R_FireStatus(commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_FireStatus));
|
comm.R_FaultCode(commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_FaultCode));
|
|
//任务完成
|
if (R_TaskPhases == 5)
|
{
|
//根据设备返回的任务号查询任务
|
Dt_Task tasks = _taskRepository.QueryFirst(x => x.TaskNum == R_OrderNo);
|
if (tasks != null)
|
{
|
|
Stock result = _TaskService.TaskAccomplish(R_OrderNo);
|
if (result.MaterialCode1 != null)
|
{
|
//连接输送线
|
OtherDevice CL = (OtherDevice)Storage.Devices.Find(v => v.DeviceName == "输送线");
|
if (CL.Communicator.IsConnected)
|
{
|
//发送物料编号
|
CL.SetValue(ConveyorLineName.W_NumberAD, result.MaterialCode1);
|
CL.SetValue(ConveyorLineName.W_NumberBD, result.MaterialCode2);
|
CL.SetValue(ConveyorLineName.W_NumberCD, result.MaterialCode3);
|
CL.SetValue(ConveyorLineName.W_NumberDD, result.MaterialCode4);
|
}
|
//commonStackerCrane.SetValue(StackerCraneDBName.W_Accomplish, true);
|
}
|
}
|
}
|
//修改任务状态
|
else if (R_TaskPhases == 1 || R_TaskPhases == 2 || R_TaskPhases == 4)
|
{
|
Dt_Task tasks = _TaskService.UpdateTaskStatus(R_OrderNo, R_TaskPhases);
|
if (tasks != null)
|
{
|
//同步WMS
|
string address = AppSettings.Get("WMSApiAddress");
|
if (!string.IsNullOrEmpty(address))
|
{
|
HttpHelper.Post($"{address}/api/Task/WCSTaskState", tasks.Serialize());
|
}
|
}
|
else
|
{
|
WriteInfo("堆垛机:", $"堆垛机修改状态找不到任务");
|
}
|
}
|
|
//判断是否为自动模式
|
if (R_ControlMode == 2 && R_TaskStatus == 2 && R_LoadStatus != 2 && (R_TaskPhases == 0 || R_TaskPhases == 5))
|
{
|
WriteInfo("堆垛机信号", $"{R_ControlMode+"||"+ R_TaskStatus + "||"+R_LoadStatus+"||"+R_TaskPhases}");
|
//判断是否有执行中的任务
|
Dt_Task tasking = _taskRepository.QueryFirst(x => (x.TaskState == (int)TaskStatusEnum.InNew && x.TaskState == (int)TaskStatusEnum.OutNew) || x.TaskState == (int)TaskStatusEnum.Task_Put || x.TaskState == (int)TaskStatusEnum.Task_fetch);
|
if (tasking == null)
|
{
|
//获取任务
|
Dt_Task? task = GetTask(InorOut);
|
if (task != null)
|
{
|
//任务实体转命令Model
|
StackerCraneCommand? StackerCommand = ConvertToStackerCommand(task);
|
if (StackerCommand != null)
|
{
|
//发送命令
|
commonStackerCrane.SetValue(StackerCraneDBName.W_SourceRow, StackerCommand.W_SourceRow);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_SourceColumn, StackerCommand.W_SourceColumn);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_SourceLayer, StackerCommand.W_SourceLayer);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_TargeRow, StackerCommand.W_TargeRow);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_TargeColumn, StackerCommand.W_TargeColumn);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_TargeLayer, StackerCommand.W_TargeLayer);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_TaskType, StackerCommand.W_TaskType);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_TaskMode, StackerCommand.W_TaskMode);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_DataCheckSum, StackerCommand.W_DataCheckSum);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_OrderNo, StackerCommand.W_OrderNo);
|
commonStackerCrane.SetValue(StackerCraneDBName.W_Start, StackerCommand.W_Start);
|
WriteInfo("堆垛机执行任务:", $"{StackerCommand.W_OrderNo}");
|
}
|
}
|
}
|
}
|
|
//心跳
|
if (R_HeartBeat == 0)
|
{
|
commonStackerCrane.SetValue(StackerCraneDBName.W_HearBeat, 1);
|
R_HeartBeat = 1;
|
}
|
else
|
{
|
commonStackerCrane.SetValue(StackerCraneDBName.W_HearBeat, 0);
|
R_HeartBeat = 0;
|
}
|
|
//WebSocket状态数据发送给前端
|
string ohtData = JsonConvert.SerializeObject(comm.CommandDatas());
|
_webSocketServer.PublishAllClientPayload(ohtData);
|
}
|
}
|
catch (Exception ex)
|
{
|
//WebSocket状态数据发送给前端
|
string ohtData = JsonConvert.SerializeObject(comm.CommandDatas());
|
_webSocketServer.PublishAllClientPayload(ohtData);
|
WriteError(nameof(StackerCraneJob), ex.Message);
|
}
|
return Task.CompletedTask;
|
}
|
|
//获取任务
|
public Dt_Task? GetTask(string InorOuts)
|
{
|
Dt_Task task;
|
task = _TaskService.TaskInorOut(InorOuts);
|
if (task != null)
|
{
|
//满桶出库
|
if (task.TaskType == (int)TaskTypeEnum.Outfull)
|
{
|
int pick = comm.GETR_PickD();
|
int status = comm.GETR_StatusD();
|
if (pick != 1 || status != 1)
|
{
|
WriteInfo("堆垛机执行满桶出库任务:", $"{"输送线出库信号异常" + pick + "//" + status}");
|
return null;
|
}
|
WriteInfo("堆垛机执行满桶出库任务:", $"{"输送线出库信号正常" + pick + "//" + status}");
|
}
|
//空桶出库
|
else if(task.TaskType == (int)TaskTypeEnum.OutEmpty)
|
{
|
int pick = comm.GETR_PickB();
|
int status = comm.GETR_StatusB();
|
if (pick != 1 || status != 1)
|
{
|
WriteInfo("堆垛机执行空桶出库任务:", $"{"输送线出库信号异常" + pick + "//" + status}");
|
return null;
|
}
|
WriteInfo("堆垛机执行空桶出库任务:", $"{"输送线出库信号正常" + pick + "//" + status}");
|
}
|
}
|
|
//一入一出
|
if (task != null && (task.TaskState == (int)TaskStatusEnum.InNew || task.TaskState == (int)TaskStatusEnum.OutNew))
|
{
|
if (task.TaskType == (int)TaskTypeEnum.InEmpty || task.TaskType == (int)TaskTypeEnum.Infull)
|
{
|
InorOut = "Out";
|
}
|
else
|
{
|
InorOut = "In";
|
}
|
}
|
else
|
{
|
if (InorOut.Equals("In"))
|
{
|
InorOut = "Out";
|
}
|
else
|
{
|
InorOut = "In";
|
}
|
}
|
return task;
|
}
|
|
//任务实体转命令Model
|
public StackerCraneCommand? ConvertToStackerCommand(Dt_Task task)
|
{
|
StackerCraneCommand stackerCraneCommand = new StackerCraneCommand();
|
string[] TargetAddress = task.TargetAddress.Split('-');
|
string[] SourceAddress = task.SourceAddress.Split('-');
|
//入库
|
if (task.TaskType == (int)TaskTypeEnum.InEmpty || task.TaskType == (int)TaskTypeEnum.Infull)
|
{
|
stackerCraneCommand.W_SourceRow = (short)int.Parse(SourceAddress[0]);
|
stackerCraneCommand.W_SourceColumn = (short)int.Parse(SourceAddress[1]);
|
stackerCraneCommand.W_SourceLayer = (short)int.Parse(SourceAddress[2]);
|
stackerCraneCommand.W_TargeRow = (short)int.Parse(TargetAddress[0]);
|
stackerCraneCommand.W_TargeColumn = (short)int.Parse(TargetAddress[1]);
|
stackerCraneCommand.W_TargeLayer = (short)int.Parse(TargetAddress[2]);
|
stackerCraneCommand.W_TaskType = 1;
|
stackerCraneCommand.W_TaskMode = 1;
|
//缺少货物大小
|
stackerCraneCommand.W_DataCheckSum = task.TaskNum + stackerCraneCommand.W_SourceRow + stackerCraneCommand.W_SourceColumn + stackerCraneCommand.W_SourceLayer + stackerCraneCommand.W_TargeRow + stackerCraneCommand.W_TargeColumn + stackerCraneCommand.W_TargeLayer + stackerCraneCommand.W_TaskType + stackerCraneCommand.W_TaskMode;
|
stackerCraneCommand.W_OrderNo = task.TaskNum;
|
}
|
//出库
|
else if (task.TaskType == (int)TaskTypeEnum.OutEmpty || task.TaskType == (int)TaskTypeEnum.Outfull)
|
{
|
stackerCraneCommand.W_SourceRow = (short)int.Parse(SourceAddress[0]);
|
stackerCraneCommand.W_SourceColumn = (short)int.Parse(SourceAddress[1]);
|
stackerCraneCommand.W_SourceLayer = (short)int.Parse(SourceAddress[2]);
|
stackerCraneCommand.W_TargeRow = (short)int.Parse(TargetAddress[0]);
|
stackerCraneCommand.W_TargeColumn = (short)int.Parse(TargetAddress[1]);
|
stackerCraneCommand.W_TargeLayer = (short)int.Parse(TargetAddress[2]);
|
stackerCraneCommand.W_TaskType = 1;
|
stackerCraneCommand.W_TaskMode = 1;
|
//缺少货物大小
|
stackerCraneCommand.W_DataCheckSum = task.TaskNum + stackerCraneCommand.W_SourceRow + stackerCraneCommand.W_SourceColumn + stackerCraneCommand.W_SourceLayer + stackerCraneCommand.W_TargeRow + stackerCraneCommand.W_TargeColumn + stackerCraneCommand.W_TargeLayer + stackerCraneCommand.W_TaskType + stackerCraneCommand.W_TaskMode;
|
stackerCraneCommand.W_OrderNo = task.TaskNum;
|
}
|
//确认下发信号
|
if (task.TaskState == (int)TaskStatusEnum.InNew || task.TaskState == (int)TaskStatusEnum.OutNew)
|
{
|
stackerCraneCommand.W_Start = 1;
|
}
|
else
|
{
|
stackerCraneCommand.W_Start = 0;
|
}
|
|
return stackerCraneCommand;
|
}
|
}
|
}
|