using System;
|
using System.Collections.Generic;
|
using System.Drawing.Imaging;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
using Quartz;
|
using SqlSugar;
|
using StackExchange.Profiling.Internal;
|
using WIDESEA_ISerialPortRepository;
|
using WIDESEA_SerialPortRepository;
|
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,//parseok
|
GetOK = 1,//03
|
GetError = 2,//04
|
}
|
private readonly IPutakeRepository _putakeRepository;
|
private readonly IProcessRepository _processRepository;
|
private readonly ITorqueOpRepository _orqueOpRepository;
|
public SerialPortJob(IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository)
|
{
|
_putakeRepository = putakeRepository;
|
_processRepository = processRepository;
|
_orqueOpRepository = torqueOpRepository;
|
}
|
|
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)
|
{
|
|
//先查状态和组,按时间降序
|
string group = item.DeviceProDataBlock;
|
|
|
//这里判断组别三个看是那个,就查那个工艺表的值
|
if (group == "电气")
|
{
|
|
// 先看Dt_TorqueOp表,查询到现在这条数据的任务号和步骤
|
var take = _putakeRepository.QueryData(x => x.Pustatus == 2 && x.Grouptype == group).OrderBy(x => x.Dispatchtime).First();
|
if (take==null)
|
{
|
//等于空就停
|
}
|
|
//查到有该条数据了
|
var process = _processRepository.QueryData(x => x.CraftType == group).OrderBy(x => x.SetpNum).First();
|
|
|
|
float torqueone = process.TorqueOne;//读到值了
|
float torquetow = process.TorqueTwo;
|
if (torqueone != 0)
|
{
|
serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (torqueone * 1000).ToString().PadLeft(7, '0')) + "\r");
|
//这里给它设完值就 查存储值表 这个数据是否满足了,满足了就进行下一个值,不满足就不变一直给值
|
var num= _orqueOpRepository.QueryData(x => x.TakeId == take.Njtakeid && x.GroupOp == group && x.TorqueSize == torqueone).Count();
|
if (num!= process.TorqueOneQuantity)
|
{
|
|
}
|
|
}
|
if (torquetow != 0)
|
{
|
serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (torquetow * 1000).ToString().PadLeft(7, '0')) + "\r");
|
}
|
|
// // 计算当前步骤所需的 Torque 总数
|
int requiredTorqueCount = process.TorqueOneQuantity + process.TorqueTwoQuantity;
|
|
// 查询当前步骤的 TorqueOp 记录,匹配 ProcessSte和组,检查是否达到要求
|
int torqueCount = _orqueOpRepository.QueryData(x => x.ProcessSte == process.SetpNum && x.GroupOp == group).Count();
|
|
if (torqueCount < requiredTorqueCount)
|
{
|
// 如果数量不满足,停止读取
|
|
break;
|
}
|
#region
|
|
// //先看工艺表中步骤一中要扭的值有多少个 再查看Dt_TorqueOp表中是否有该任务号id和步骤总数是否满足工艺表
|
// // 查询所有工艺数据,按照步骤号 SetpNum 递增排序
|
// var processList = _processRepository.QueryData(x => x.CraftType == group)
|
// .OrderBy(x => x.SetpNum)
|
// .ToList();
|
|
// foreach (var process in processList)
|
// {
|
// float torqueone = process.TorqueOne;//读到值了
|
// float torquetow = process.TorqueTwo;
|
// if (torqueone!=0)
|
// {
|
// serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (torqueone * 1000).ToString().PadLeft(7, '0')) + "\r");
|
// }
|
// if (torquetow != 0)
|
// {
|
// serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (torquetow * 1000).ToString().PadLeft(7, '0')) + "\r");
|
// }
|
|
// // 计算当前步骤所需的 Torque 总数
|
// int requiredTorqueCount = process.TorqueOneQuantity + process.TorqueTwoQuantity;
|
|
// // 查询当前步骤的 TorqueOp 记录,匹配 ProcessSte和组,检查是否达到要求
|
// int torqueCount = _orqueOpRepository.QueryData(x => x.ProcessSte == process.SetpNum&& x.GroupOp== group).Count();
|
|
// if (torqueCount < requiredTorqueCount)
|
// {
|
// // 如果数量不满足,停止读取
|
|
// break;
|
// }
|
// //符合要求,继续读取下一步
|
|
}
|
|
//}
|
//if (group == "机械")
|
//{
|
|
//}
|
//if (group == "地沟")
|
//{
|
|
//}
|
#endregion
|
|
|
serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (5 * 1000).ToString().PadLeft(7, '0')) + "\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));
|
//parseok
|
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));
|
//03成功
|
|
if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
|
{
|
////这里成功之后,要存储到看Dt_TorqueOp(存任务号,组,值)
|
//string group = "";
|
//if (group == "电气")
|
//{
|
// var putake = _putakeRepository.QueryData(x => x.Pustatus == 2 && x.Grouptype == group).OrderByDescending(x => x.Dispatchtime).FirstOrDefault();//每次都拿最早的那条
|
// var punjid = putake.ID;
|
// var Nj = new Dt_TorqueOp()
|
// {
|
// TakeId = putake.Njtakeid,
|
// GroupOp = group,
|
// //ProcessSte= processList
|
// TorqueSize = receiveData
|
|
// };
|
// _orqueOpRepository.AddData(Nj);
|
|
//}
|
//if (group == "机械")
|
//{
|
|
//}
|
//if (group == "地沟")
|
|
|
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);
|
}
|
WriteDebug("CommonConveyorLineJob", "test");
|
return Task.CompletedTask;
|
}
|
}
|
}
|