using Autofac.Core;
using HslCommunication;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Eventing.Reader;
using System.DirectoryServices.Protocols;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Reflection.Metadata;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Model.Models.System;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_TaskInfoService;
using WIDESEAWCS_Tasks.StackerCraneJob;
namespace WIDESEAWCS_Tasks
{
[DisallowConcurrentExecution]
public class CommonStackerCraneJob : IJob
{
private readonly ITaskService _taskService;
private readonly ITaskExecuteDetailService _taskExecuteDetailService;
private readonly ITaskRepository _taskRepository;
private readonly IRouterService _routerService;
private readonly IDt_PlatformStationService _DtPlatformStationService;
public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IDt_PlatformStationService dt_PlatformStationService)
{
_taskService = taskService;
_taskExecuteDetailService = taskExecuteDetailService;
_taskRepository = taskRepository;
_routerService = routerService;
_DtPlatformStationService= dt_PlatformStationService;
}
public Task Execute(IJobExecutionContext context)
{
try
{
CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
if (commonStackerCrane != null)
{
UpdateHandShake(commonStackerCrane.CurrentTaskNum, commonStackerCrane.StackerCraneWorkStatusDes);
//任务完成StackerCraneWorkStatus
if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.WorkCompleted)
{
//判断当前任务是否是盘点
Dt_Task? currtask = _taskService.CurrtaskInfo(commonStackerCrane.CurrentTaskNum);
if(currtask != null)
{
string PalletCode = "";
//判断任务类型
if (currtask.TaskType== (int)TaskTypeEnum.Inventorybound)
{
//读取条码,进行配对
DeviceProDTO? deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == "SCBarcode");
byte[] bytes = commonStackerCrane.Communicator.Read(deviceProDTO.DeviceProAddress, 64);
//读取的托盘条码
PalletCode = Encoding.ASCII.GetString(bytes).Replace(" ", "");
}
WebResponseContent webResponse = _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum, PalletCode);
if (webResponse.Status)
{
WriteLog.Write_Log("堆垛机任务上报完成信息", "堆垛机任务上报完成信息", $"【{commonStackerCrane.DeviceCode}】库堆垛机上报任务号:【{commonStackerCrane.CurrentTaskNum}】完成信号!");
bool ddjfk = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
}
}
}
WriteLog.Write_Log("堆垛机当前状态", "堆垛机当前状态信息", $"堆垛机设备地址:【DB105.2.0】,读取的数据:【{commonStackerCrane.StackerCraneAutoStatusDes}】\n" +
$"堆垛机工作模式地址:【DB105.3.0】,读取的数据:【{commonStackerCrane.StackerCraneStatusDes}】\n" +
$"堆垛机作业状态地址:【DB105.22.0】,读取的数据:【{commonStackerCrane.StackerCraneWorkStatusDes}】\n" +
$"堆垛机当前任务执行中地址:【DB105.18.0】,读取的数据:【{commonStackerCrane.CurrentTaskNum}】\n");
if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal)
{
if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
{
Dt_Task? task = GetTask(commonStackerCrane);
if (task != null)
{
StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
if (stackerCraneTaskCommand != null)
{
bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
if (sendFlag)
{
commonStackerCrane.Communicator.Write("DB105.52", 1);
WebResponseContent webResponseContent=_taskService.UpdateTaskStatusToNext(task.TaskNum);
}
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
WriteLog.Write_Log("堆垛机报错信息", "堆垛机报错信息", $"堆垛机报错信息:【{ex.ToString()}】");
}
return Task.CompletedTask;
}
///
/// 任务完成事件订阅的方法
///
///
///
private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
{
CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
if (commonStackerCrane != null)
{
if (commonStackerCrane.GetValue(StackerCraneDBName.WorkType) != 5 && e.TaskNum !=0)
{
//Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
//_taskService.StackCraneTaskCompleted(e.TaskNum,);
commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
}
}
}
///
/// 获取任务
///
/// 堆垛机对象
///
private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
{
try
{
//判断当前堆垛机是否有在执行的任务
if (_taskService.CutStackerCraneTask(commonStackerCrane.DeviceCode) != null) return null;
List task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
if (task.Count > 0)
{
foreach (Dt_Task taskItem in task)
{
if (taskItem.TaskType == (int)TaskTypeEnum.Outbound)
{
if (OutTaskStationIsOccupied(taskItem.NextAddress,taskItem.Roadway))
{
return taskItem;
}
}
else if (taskItem.TaskType == (int)TaskTypeEnum.Inbound || taskItem.TaskType == (int)TaskTypeEnum.Inventorybound)
{
InTaskStationIsOccupied(taskItem.SourceAddress, taskItem.Roadway);
return taskItem;
}
}
}
return null;
}
catch (Exception ex)
{
WriteLog.Write_Log("任务查找", "任务查错误", "错误原因", new { 信息 = ex.Message });
return null;
}
}
///
/// 出库任务判断出库站台是否被占用
///
/// 任务实体
/// 如果未被占用,返回传入的任务信息,否则,返回null
private bool OutTaskStationIsOccupied(string NextAddress,string Roadway)
{
try
{
if (Roadway == "C")
{
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1004");
if (device == null) return false;
CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationNum");
DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "HandShake");
if (deviceProDTO == null || HandShakeprodto == null) return false;
int StationNum = conveyorLine.Communicator.Read(deviceProDTO.DeviceProAddress);
int HandShake = conveyorLine.Communicator.Read(HandShakeprodto.DeviceProAddress);
conveyorLine.Communicator.Write(HandShakeprodto.DeviceProAddress, 1);
WriteLog.Write_Log("出库库口状态信息", "出库库口状态信息", $"库口是【{NextAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
$"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
if (StationNum == 0 && HandShake == 0 && Roadway == "C")
{
return true;
}
}
else
{
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1003");
if (device == null) return false;
CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationNum");
DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "HandShake");
if (deviceProDTO == null || HandShakeprodto == null) return false;
int StationNum = conveyorLine.Communicator.Read(deviceProDTO.DeviceProAddress);
int HandShake = conveyorLine.Communicator.Read(HandShakeprodto.DeviceProAddress);
WriteLog.Write_Log("出库库口状态信息", "出库库口状态信息", $"库口是【{NextAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
$"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
if ((StationNum == 1 && HandShake == 0 && Roadway == "B") || (StationNum == 0 && HandShake == 0 && Roadway != "B"))
{
return true;
}
}
}
catch (Exception ex)
{
Console.WriteLine("读取站台信息错误:"+ex.Message);
}
return false;
}
///
/// 入库任务库口有货无货状态
///
private void InTaskStationIsOccupied(string SourceAddress, string Roadway)
{
try
{
if (Roadway == "C")
{
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1004");
CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "StationNum");
DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "HandShake");
int StationNum = conveyorLine.Communicator.Read(deviceProDTO.DeviceProAddress);
int HandShake = conveyorLine.Communicator.Read(HandShakeprodto.DeviceProAddress);
WriteLog.Write_Log("入库库口状态信息", "入库库口状态信息", $"库口是【{SourceAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
$"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
}
else
{
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1003");
CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "StationNum");
DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "HandShake");
int StationNum = conveyorLine.Communicator.Read(deviceProDTO.DeviceProAddress);
int HandShake = conveyorLine.Communicator.Read(HandShakeprodto.DeviceProAddress);
WriteLog.Write_Log("入库库口状态信息", "入库库口状态信息", $"库口是【{SourceAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
$"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
}
}
catch (Exception ex)
{
Console.WriteLine("读取站台信息错误:" + ex.Message);
}
}
///
/// 任务实体转换成命令Model
///
/// 任务实体
///
///
public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
{
StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
stackerCraneTaskCommand.TaskNum = task.TaskNum;
stackerCraneTaskCommand.WorkType = 1;
stackerCraneTaskCommand.TrayType = 0;
if (task.Roadway == "B" || task.Roadway == "C")
{
//判断 task.PalletCode 是不是一个有效的 GUID
stackerCraneTaskCommand.Barcode = stackerCraneTaskCommand.Barcode = Guid.TryParse(task.PalletCode, out var guid) ? guid.ToString("N")[0..6] : task.PalletCode;
}
else if (task.Roadway == "G" )
{
if (Guid.TryParse(task.PalletCode, out var palletGuid))
{
stackerCraneTaskCommand.Barcode = palletGuid.ToString("N")[0..6];
// 标准GUID就是36位,所以直接判断长度即可
stackerCraneTaskCommand.TrayType = (short)(task.PalletCode.Length == 36 ? 1 : 2);
}
else
{
stackerCraneTaskCommand.Barcode = task.PalletCode;
stackerCraneTaskCommand.TrayType = 2;
}
}
if (task.TaskType == (int)TaskTypeEnum.Inbound)//判断是否是入库任务
{
//获取站台编号
Dt_PlatformStation dt_Platform = _DtPlatformStationService.getStatiomInfo(task.SourceAddress);
string[] targetCodest = dt_Platform.Station_storey.Split("-");
if (targetCodest.Length == 3)
{
string StartRow = targetCodest[0].Substring(1);
stackerCraneTaskCommand.StartRow = Convert.ToInt16(StartRow);
stackerCraneTaskCommand.StartColumn = Convert.ToInt16(targetCodest[1]);
stackerCraneTaskCommand.StartLayer = Convert.ToInt16(targetCodest[2]);
}
else
{
//数据配置错误
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库起点错误,起点:【{task.SourceAddress}】");
return null;
}
string[] EndCodest = task.NextAddress.Split("-");
if (EndCodest.Length == 3)
{
string EndRow = EndCodest[0].Substring(1);
string SCEndRow = LaneModification(task.Roadway, int.Parse(EndRow));
stackerCraneTaskCommand.EndRow = Convert.ToInt16(SCEndRow);
stackerCraneTaskCommand.EndColumn = Convert.ToInt16(EndCodest[1]);
stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
}
else
{
//数据配置错误
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
return null;
}
}
else if (task.TaskType == (int)TaskTypeEnum.Outbound)
{
string[] sourceCodes = task.CurrentAddress.Split("-");
if (sourceCodes.Length == 3)
{
string StartRow = sourceCodes[0].Substring(1);
string SCStartRow = LaneModification(task.Roadway, int.Parse(StartRow));
stackerCraneTaskCommand.StartRow = Convert.ToInt16(SCStartRow);
stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
}
else
{
//数据配置错误
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
return null;
}
//获取站台编号
Dt_PlatformStation dt_Platform = _DtPlatformStationService.getStatiomInfo(task.NextAddress);
string[] EndCodest = dt_Platform.Station_storey.Split("-");
if (EndCodest.Length == 3)
{
string EndRow = EndCodest[0].Substring(1);
stackerCraneTaskCommand.EndRow = Convert.ToInt16(EndRow);
stackerCraneTaskCommand.EndColumn = Convert.ToInt16(EndCodest[1]);
stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
}
else
{
//数据配置错误
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,起点:【{task.TargetAddress}】");
return null;
}
}
else if (task.TaskType == (int)TaskTypeEnum.Inventorybound)
{
stackerCraneTaskCommand.WorkType = 6;
stackerCraneTaskCommand.StartRow = Convert.ToInt16(0);
stackerCraneTaskCommand.StartColumn = Convert.ToInt16(0);
stackerCraneTaskCommand.StartLayer = Convert.ToInt16(0);
string[] EndCodest = task.NextAddress.Split("-");
if (EndCodest.Length == 3)
{
string EndRow = EndCodest[0].Substring(1);
string SCEnRow = LaneModification(task.Roadway, int.Parse(EndRow));
stackerCraneTaskCommand.EndRow = Convert.ToInt16(SCEnRow);
stackerCraneTaskCommand.EndColumn = (short)(Convert.ToInt16(EndCodest[1]) + 1);
stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
}
else
{
//数据配置错误
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
return null;
}
}
return stackerCraneTaskCommand;
}
public string LaneModification(string Roadway,int SCRow)
{
if (Roadway == "G")
{
return SCRow == 1 ? "3" : "4";
}else if(Roadway == "C")
{
return SCRow == 1 ? "5" : "6";
}
else
{
return SCRow.ToString();
}
}
///
/// 监测G库、C库出库放货时屏蔽光栅
///
///
///
public void UpdateHandShake(int taskNum, string stackerCraneWorkStatusDes)
{
// 1. 查询任务,空值直接返回
Dt_Task? dtTask = _taskRepository.QueryFirst(x => x.TaskNum == taskNum);
if (dtTask == null) return;
// 2. 定义固定地址
string[] targetAddresses = { "G03", "G04", "C02", "C01" };
// 3. 条件判断 + 确定写入值
short writeValue = -1;
if (dtTask.TaskType == 100 && targetAddresses.Contains(dtTask.TargetAddress))
{
writeValue = stackerCraneWorkStatusDes switch
{
"放货中" => 1,
"任务完成" => 0,
_ => -1
};
}
// 4. 获取设备编码
string deviceCode = dtTask.CurrentAddress.StartsWith("C") ? "1004" : "1003";
// 5. 获取设备,空值直接返回
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == deviceCode);
if (device != null && writeValue != -1)
{
CommonConveyorLine? conveyorLine = (CommonConveyorLine)device;
// 6. 获取握手点位,空值直接返回
DeviceProDTO? handShake = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == dtTask.TargetAddress && x.DeviceProParamName == "HandShake");
if (handShake == null)
{
WriteLog.Write_Log("出库库口光栅状态错误信息", "出库库口光栅状态错误信息", $"WCS未找到库口信息是【{dtTask.TargetAddress}】,任务号:【{dtTask.TaskNum}】,堆垛机作业状态:【{stackerCraneWorkStatusDes}】");
return;
}
// 7. 写入值
conveyorLine.Communicator.Write(handShake.DeviceProAddress, writeValue);
WriteLog.Write_Log("出库库口光栅状态正常信息", "出库库口光栅状态正常信息", $"库口是【{dtTask.TargetAddress}】,申请状态地址:【{handShake.DeviceProAddress}】,读取的数据:【{conveyorLine.Communicator.Read(handShake.DeviceProAddress)}】\n" +
$"任务号:【{dtTask.TaskNum}】,堆垛机作业状态:【{stackerCraneWorkStatusDes}】");
return;
}
else
{
WriteLog.Write_Log("出库库口光栅状态错误信息", "出库库口光栅状态错误信息", $"库口是【{dtTask.TargetAddress}】,任务号:【{dtTask.TaskNum}】,堆垛机作业状态:【{stackerCraneWorkStatusDes}】");
return;
}
}
}
}