using System;
|
using System.Collections.Generic;
|
using System.Drawing.Imaging;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Net.WebSockets;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
using System.Threading.Tasks;
|
using AutoMapper;
|
using HslCommunication.WebSocket;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
using Quartz;
|
using SqlSugar;
|
using StackExchange.Profiling.Internal;
|
using WIDESEA_ISerialPortRepository;
|
using WIDESEA_SerialPortRepository;
|
using WIDESEAWCS_Core;
|
using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_DTO.SerialPort;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_QuartzJob.DTO;
|
using WIDESEAWCS_TaskInfoService;
|
using WIDESEAWCS_Tasks.SerialPort;
|
|
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;
|
|
private WebSocketServer _webSocketContext;
|
private readonly IProcessServer _processServer;
|
private readonly IPutakeServer _putakeServer;
|
|
public SerialPortJob(IPutakeServer putakeServer,IProcessServer processServer, IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository, WebSocketServer webSocketContext)
|
{
|
_putakeRepository = putakeRepository;
|
_processRepository = processRepository;
|
_orqueOpRepository = torqueOpRepository;
|
_webSocketContext = webSocketContext;
|
_processServer = processServer;
|
_putakeServer = putakeServer;
|
}
|
|
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)
|
{
|
# region Setvalue
|
string group = item.DeviceProDataBlock;
|
if (group == "电气")
|
{
|
//获取当前任务号 //IPutakeServer里面的待执行的第一条任务;
|
var take = _putakeRepository.QueryData(x => x.Grouptype == group)
|
.OrderBy(x=>x.Dispatchtime)
|
.FirstOrDefault();
|
var takeid = take?.Njtakeid;//拿到任务号
|
|
//调用Getcircuit拿到当前步骤
|
var process = _processServer.Getcircuit(group,takeid);
|
|
if (process != null && process.Status)
|
{
|
var list = JsonConvert.SerializeObject(process.Data);
|
var data = JsonConvert.DeserializeObject<ProcessData>(list);
|
|
if (data?.proNow != null)
|
{
|
|
int setpNum = data.proNow.SetpNum;
|
int sum = data.proNow.TorqueSum;
|
double torqueone = data.proNow.TorqueOne;
|
int onequantity = data.proNow.TorqueOneQuantity;
|
int towquantity = data.proNow.TorqueTwoQuantity;
|
double torquetwo = data.proNow.TorqueTwo;
|
|
//判断op表中这个值是否有该条任务的数据
|
var op = _orqueOpRepository.QueryData(x => x.TakeId == takeid && x.GroupOp == group&&x.ProcessSte== setpNum);
|
if (op.Any())//判断是否有数据
|
{
|
//找到了就要对比现在op表中有多少条,是否和工艺表中的目标一致
|
var oponecount=op.Count(x=>x.TorqueSize== torqueone);//第一个扭的数量
|
var optowcount = op.Count(x => x.TorqueSize == torquetwo);//第二个扭的数量
|
sum=oponecount+optowcount;
|
//第一种,第一个扭力值没有扭完
|
if (oponecount< onequantity&& torqueone!=0)
|
{
|
//设值
|
//var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
|
// .Replace("[setNum]", (torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
|
var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
|
.Replace("[setNum]", Math.Round(torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
|
//格式化成整数
|
|
serialPortDevice.Communicator.Write(com);
|
WriteDebug("写入", com);
|
}
|
//第二种,第一个值扭完了,第二个没有扭完
|
if (oponecount==onequantity&& optowcount < towquantity&& torquetwo!=0)
|
{
|
//设值
|
var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
|
.Replace("[setNum]", Math.Round(torquetwo * 1000).ToString().PadLeft(7, '0')) + "\r";
|
serialPortDevice.Communicator.Write(com);
|
}
|
}
|
|
}
|
}
|
}
|
#endregion
|
item.DeviceProParamName = CommandType.None.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");//打开串口时先设值
|
|
WriteDebug("写入", item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue);
|
}
|
}
|
|
if (serialPortDevice.Communicator.Buffers.Count > 0)
|
{
|
string? receiveData = serialPortDevice.Communicator.ToString(Encoding.Default);
|
if (!string.IsNullOrEmpty(receiveData))
|
{
|
Console.WriteLine("serialPortDevice:" + DateTime.Now.ToString("HH:mm:ss.fff") + receiveData);
|
|
if (item.DeviceProParamName == CommandType.Set.ToString() || item.DeviceProParamName == CommandType.None.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))
|
{
|
//把这条数据插入op表中
|
string group = item.DeviceProDataBlock; // 设备所属组别
|
string devicecode = item.DeviceChildCode;//设备编号
|
string torqueValue = ExtractTorqueValue(receiveData);
|
|
if (group == "电气")
|
{
|
#region
|
var take = _putakeRepository.QueryData(x => x.Grouptype == group)
|
.OrderBy(x => x.Dispatchtime)
|
.FirstOrDefault();
|
var takeid = take?.Njtakeid;//拿到任务号
|
|
//调用Getcircuit拿到当前步骤
|
var process = _processServer.Getcircuit(group, takeid);
|
|
if (process != null && process.Status)
|
{
|
var list = JsonConvert.SerializeObject(process.Data);
|
var dataz = JsonConvert.DeserializeObject<ProcessData>(list);
|
|
if (dataz?.proNow != null)
|
{
|
|
int setpNum = dataz.proNow.SetpNum;
|
int sum = dataz.proNow.TorqueSum;
|
double torqueone = dataz.proNow.TorqueOne;
|
int onequantity = dataz.proNow.TorqueOneQuantity;
|
int towquantity = dataz.proNow.TorqueTwoQuantity;
|
double torquetwo = dataz.proNow.TorqueTwo;
|
Dt_TorqueOp Addop = new Dt_TorqueOp()
|
{
|
DeviceCode = devicecode,
|
TakeId = takeid,
|
GroupOp = group,
|
ProcessSte = setpNum,
|
TorqueSize = float.TryParse(torqueValue, out float torque) ? torque : 0.0f, // 这里进行字符串到float的转换
|
CreateDate = DateTime.Now,
|
|
};
|
}
|
}
|
|
|
#endregion
|
|
item.DeviceProParamName = CommandType.None.ToString();
|
}
|
|
}
|
|
DeviceProtocolDetailDTO? deviceProtocolDetail2 = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError));
|
if (deviceProtocolDetail2 != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail2.ProtocalDetailValue))
|
{
|
item.DeviceProParamName = CommandType.None.ToString();
|
}
|
}
|
}
|
|
|
//建一个对象将其传给前端
|
//string data = JsonConvert.SerializeObject(serialPortDevice);//这里serialPortDevice是假设有这个对象
|
//_webSocketContext.PublishAllClientPayload(data);
|
}
|
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
WriteError("CommonConveyorLineJob", "test", ex);
|
}
|
WriteDebug("CommonConveyorLineJob", "test");
|
return Task.CompletedTask;
|
}
|
|
/// <summary>
|
/// 将0004000转为4.0
|
/// 0004500转为4.5
|
/// </summary>
|
/// <param name="data"></param>
|
/// <returns></returns>
|
private string ExtractTorqueValue(string data)
|
{
|
Match match = Regex.Match(data, @"\b0*(\d{1,4})000\b");
|
return match.Success ? (double.Parse(match.Groups[1].Value) / 10.0).ToString("0.0") : "0.0";
|
}
|
|
|
|
}
|
}
|