| | |
| | | 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 OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; |
| | | 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_QuartzJob.StackerCrane.Enum; |
| | | using WIDESEAWCS_Tasks.ConveyorLineJob; |
| | | using WIDESEAWCS_Tasks.StackerCraneJob; |
| | | using WIDESEAWCS_TaskInfoService; |
| | | using WIDESEAWCS_Tasks.SerialPort; |
| | | |
| | | namespace WIDESEAWCS_Tasks |
| | | { |
| | | [DisallowConcurrentExecution] |
| | | public class SerialPortJob : JobBase, IJob, IDisposable |
| | | public class SerialPortJob : JobBase, IJob,IDisposable |
| | | { |
| | | public void Dispose() |
| | | { |
| | |
| | | public enum CommandType |
| | | { |
| | | None = 0, |
| | | Get = 1, |
| | | Set = 2, |
| | | Get = 1,//åä¿¡å· |
| | | Set = 2,//è®¾å¼ |
| | | } |
| | | |
| | | public enum CommandResult |
| | | { |
| | | SetOK = 0, |
| | | GetOK = 1, |
| | | GetError = 2, |
| | | 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) |
| | | |
| | | 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) |
| | |
| | | DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set)); |
| | | if (deviceProtocolDetail != null) |
| | | { |
| | | //æè¦å端è¿åç»æä¸ä¸ªç»å«ï¼åä»»å¡id |
| | | string group = ""; |
| | | var putake = _putakeRepository.QueryFirst(x => x.Grouptype == group); |
| | | //è¿é夿ç»å«ä¸ä¸ªçæ¯é£ä¸ªï¼å°±æ¥é£ä¸ªå·¥èºè¡¨çå¼ |
| | | if (group=="çµæ°") |
| | | # region Setvalue |
| | | string group = item.DeviceProDataBlock; |
| | | if (group == "çµæ°") |
| | | { |
| | | //å
çå·¥èºè¡¨ä¸æ¥éª¤ä¸ä¸è¦æç弿å¤å°ä¸ª 忥çDt_TorqueOpè¡¨ä¸æ¯å¦æè¯¥ä»»å¡å·idåæ¥éª¤æ»æ°æ¯å¦æ»¡è¶³å·¥èºè¡¨ |
| | | var proces = _processRepository.QueryData(x => x.CraftType == 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); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | if (group == "æºæ¢°") |
| | | { |
| | | |
| | | } |
| | | if (group == "å°æ²") |
| | | { |
| | | |
| | | } |
| | | //gå·¥èºè¡¨çæåå¼ |
| | | |
| | | double num = 4.5;//0005000 |
| | | |
| | | serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (num* 1000).ToString().PadLeft(7,'0'))+"\r");//æå¼ä¸²å£æ¶å
è®¾å¼ |
| | | item.DeviceProParamName = CommandType.Set.ToString(); |
| | | #endregion |
| | | item.DeviceProParamName = CommandType.None.ToString(); |
| | | } |
| | | } |
| | | |
| | |
| | | if (deviceProtocolDetail != null) |
| | | { |
| | | serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//æå¼ä¸²å£æ¶å
è®¾å¼ |
| | | |
| | | WriteDebug("åå
¥", item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue); |
| | | } |
| | | } |
| | | 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); |
| | | Console.WriteLine("serialPortDevice:" + DateTime.Now.ToString("HH:mm:ss.fff") + receiveData); |
| | | |
| | | if (item.DeviceProParamName == CommandType.Set.ToString()) |
| | | 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 |
| | |
| | | { |
| | | 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ï¼åä»»å¡å·ï¼ç»ï¼å¼ï¼ |
| | | //æè¿æ¡æ°æ®æå
¥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.Set.ToString(); |
| | | |
| | | item.DeviceProParamName = CommandType.None.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)) |
| | | |
| | | 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.Set.ToString(); |
| | | |
| | | |
| | | |
| | | item.DeviceProParamName = CommandType.None.ToString(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | //建ä¸ä¸ªå¯¹è±¡å°å
¶ä¼ ç»å端 |
| | | //string data = JsonConvert.SerializeObject(serialPortDevice);//è¿éserialPortDeviceæ¯å设æè¿ä¸ªå¯¹è±¡ |
| | | //_webSocketContext.PublishAllClientPayload(data); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | catch (Exception 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"; |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | } |