using System;
|
using System.Collections.Generic;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
|
using Quartz;
|
using StackExchange.Profiling.Internal;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_QuartzJob.DTO;
|
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
|
using WIDESEAWCS_Tasks.ConveyorLineJob;
|
using WIDESEAWCS_Tasks.StackerCraneJob;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
[DisallowConcurrentExecution]
|
public class SerialPortJob : JobBase, IJob, IDisposable
|
{
|
public void Dispose()
|
{
|
GC.SuppressFinalize(this);
|
}
|
|
public enum CommandType
|
{
|
None = 0,
|
Get = 1,
|
Set = 2,
|
}
|
|
public enum CommandResult
|
{
|
SetOK = 0,
|
GetOK = 1,
|
GetError = 2,
|
}
|
|
|
public Task Execute(IJobExecutionContext context)
|
{
|
try
|
{
|
SerialPortDevice serialPortDevice = (SerialPortDevice)context.JobDetail.JobDataMap.Get("JobParams");
|
if (serialPortDevice != null)
|
{
|
List<DeviceProDTO> deviceProDTOs = serialPortDevice.DeviceProDTOs;
|
foreach (var item in deviceProDTOs)
|
{
|
if (item.DeviceProParamName != CommandType.Get.ToString() && item.DeviceProParamName != CommandType.Set.ToString())
|
{
|
DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
|
if (deviceProtocolDetail != null)
|
{
|
serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
|
item.DeviceProParamName = CommandType.Set.ToString();
|
}
|
}
|
|
if (CommandType.Get.ToString() == item.DeviceProParamName)
|
{
|
DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Get));
|
if (deviceProtocolDetail != null)
|
{
|
serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
|
}
|
}
|
else if (CommandType.Set.ToString() == item.DeviceProParamName)
|
{
|
DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
|
if (deviceProtocolDetail != null)
|
{
|
serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
|
}
|
}
|
|
if (serialPortDevice.Communicator.Buffers.Count > 0)
|
{
|
string? receiveData = serialPortDevice.Communicator.ToString(Encoding.Default);
|
if (!string.IsNullOrEmpty(receiveData))
|
{
|
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + receiveData);
|
|
if (item.DeviceProParamName == CommandType.Set.ToString())
|
{
|
DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.SetOK));
|
if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
|
{
|
item.DeviceProParamName = CommandType.Get.ToString();
|
}
|
}
|
else if (item.DeviceProParamName == CommandType.Get.ToString())
|
{
|
DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetOK));
|
|
if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
|
{
|
item.DeviceProParamName = CommandType.Set.ToString();
|
}
|
}
|
else if (item.DeviceProParamName == CommandType.Get.ToString())
|
{
|
DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError));
|
if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
|
{
|
item.DeviceProParamName = CommandType.Set.ToString();
|
}
|
}
|
}
|
}
|
}
|
|
}
|
|
}
|
catch (Exception ex)
|
{
|
WriteError("CommonConveyorLineJob", "test", ex);
|
//Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
|
}
|
WriteDebug("CommonConveyorLineJob", "test");
|
return Task.CompletedTask;
|
}
|
}
|
}
|