From 6d1c92f39cf9a188ed69a68dfc7b40ee0ce71da4 Mon Sep 17 00:00:00 2001 From: liuwf Date: Thu, 7 Mar 2024 15:25:51 +0800 Subject: [PATCH] =?UTF-8?q?change-=E7=81=8C=E6=B3=A8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Business/PerfusionCollection.cs | 99 +++++++++---------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/Aucma.Core.Tasks/Business/PerfusionCollection.cs b/Aucma.Core.Tasks/Business/PerfusionCollection.cs index 54208b87..e5a47928 100644 --- a/Aucma.Core.Tasks/Business/PerfusionCollection.cs +++ b/Aucma.Core.Tasks/Business/PerfusionCollection.cs @@ -22,14 +22,14 @@ namespace Aucma.Core.Tasks.Business public class PerfusionCollection { Semaphore semaphore = new Semaphore(1, 1); - protected readonly IPerfusionRecordServices? _perfusionRecordServices ; - protected readonly IPerfusionAlarmServices ?_perfusionAlarmServices; + protected readonly IPerfusionRecordServices? _perfusionRecordServices; + protected readonly IPerfusionAlarmServices? _perfusionAlarmServices; protected readonly IPerfusionDeviceStatusServices? _perfusionDeviceStatusServices; private static System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); - private Perfusion_Record record ; - private Perfusion_Record temprRecord ; + private Perfusion_Record record; + private Perfusion_Record temprRecord; private Perfusion_DeviceStatus perfusion_DeviceStatus = new Perfusion_DeviceStatus(); private Perfusion_Alarm perfusion_Alarm = new Perfusion_Alarm(); @@ -46,18 +46,25 @@ namespace Aucma.Core.Tasks.Business { Task.Run(() => { - while (true) - { - try + Thread.Sleep(3000); + Console.WriteLine("灌注数据采集服务开启"); + var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PerfusionPlc")); + if (obj != null) + { + while (true) { - Thread.Sleep(2000); - var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PerfusionPlc")); - if (obj != null && obj.plc.IsConnected) + try { + //采集数据 - record = ReadEquipSystem(obj,record, perfusion_DeviceStatus, perfusion_Alarm); - //更新设备状态 - Perfusion_DeviceStatus device = _perfusionDeviceStatusServices.FirstAsync().Result; + record = ReadEquipSystem(obj, record, perfusion_DeviceStatus, perfusion_Alarm); + if (string.IsNullOrEmpty(record.PerfusionBoxCode)) continue; + if (temprRecord == null) + { + temprRecord = _perfusionRecordServices.Query(d => d.PerfusionBoxCode.Equals(record.PerfusionBoxCode)).FirstOrDefault(); + } + #region 更新设备状态 + Perfusion_DeviceStatus device = _perfusionDeviceStatusServices.FirstAsync().Result; if (device == null) { _ = _perfusionDeviceStatusServices.AddAsync(perfusion_DeviceStatus).Result; @@ -67,53 +74,42 @@ namespace Aucma.Core.Tasks.Business device.PerfusionStatus = perfusion_DeviceStatus.PerfusionStatus; var rsult = _perfusionDeviceStatusServices.UpdateAsync(device).Result; } + #endregion - //// 更新报警信息 + #region 更新报警信息 if (perfusion_Alarm.PerfusionStatus > 0) { - int a = _perfusionAlarmServices.AddAsync(perfusion_Alarm).Result; + int a = _perfusionAlarmServices.AddAsync(perfusion_Alarm).Result; } - - //Perfusion_Alarm alarmRecord = _perfusionAlarmServices.FirstAsync(x=>x.PerfusionBoxcode==perfusion_Alarm.PerfusionBoxcode).Result; - //if (alarmRecord == null) - //{ + #endregion + - //} - //充注结束 if (record.PerfusionFinishStatus == "1") { - //缓存没数据查数据库 - if(temprRecord == null) + // 箱体码和上次不一样,或者箱体码一样但是充铸结果不一样 + if (temprRecord == null || temprRecord.PerfusionBoxCode != record.PerfusionBoxCode || (temprRecord.PerfusionBoxCode == record.PerfusionBoxCode && temprRecord.PerfusionResult != record.PerfusionResult)) { - Perfusion_Record lastRecord = _perfusionRecordServices.Query(d => d.PerfusionBoxCode.Equals(record.PerfusionBoxCode)).FirstOrDefault(); - // 数据库没有数据 - if (lastRecord == null && !string.IsNullOrEmpty(record.PerfusionBoxCode)) - { - _= _perfusionRecordServices.AddAsync(record).Result; - - return; + int result = _perfusionRecordServices.AddAsync(record).Result; + if (result > 0) + { + Console.WriteLine(record.ToJson()); + temprRecord = record; + } } - // 箱体码和上次不一样,或者箱体码一样但是充铸结果不一样 - else if(temprRecord.PerfusionBoxCode!=record.PerfusionBoxCode || (temprRecord.PerfusionBoxCode == record.PerfusionBoxCode && temprRecord.PerfusionResult!=record.PerfusionResult) ) - { - _ = _perfusionRecordServices.AddAsync(record).Result; - - } } - + + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + + Thread.Sleep(2000); } - catch (Exception ex) - { - Console.WriteLine(ex); - } - finally - { - temprRecord = record; - } - + } }); @@ -147,7 +143,7 @@ namespace Aucma.Core.Tasks.Business record.UpdatedBy = "equip"; record.UpdatedTime = DateTime.Now; #endregion - Console.WriteLine("灌注记录:" + record.ToJson()); + // Console.WriteLine("灌注记录:" + record.ToJson()); #region 设备状态赋值 deviceStatus.PerfusionDevicetype = "灌注"; deviceStatus.PerfusionStatus = short.Parse(StringChange.bytesToHexStr(info.Skip(50).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber); @@ -156,7 +152,7 @@ namespace Aucma.Core.Tasks.Business deviceStatus.UpdatedBy = "equip"; deviceStatus.UpdatedTime = DateTime.Now; #endregion - Console.WriteLine("设备状态:" + deviceStatus.ToJson()); + // Console.WriteLine("设备状态:" + deviceStatus.ToJson()); #region 设备报警赋值 alarmInfo.PerfusionBoxcode = record.PerfusionBoxCode; alarmInfo.PerfusionStatus = short.Parse(StringChange.bytesToHexStr(info.Skip(52).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber); @@ -167,8 +163,8 @@ namespace Aucma.Core.Tasks.Business alarmInfo.UpdatedBy = "equip"; alarmInfo.UpdatedTime = DateTime.Now; #endregion - Console.WriteLine("报警信息:" + alarmInfo.ToJson()); - Console.WriteLine("\n"); + // Console.WriteLine("报警信息:" + alarmInfo.ToJson()); + // Console.WriteLine("\n"); return record; } static string AlarmTrans(short code) @@ -176,6 +172,7 @@ namespace Aucma.Core.Tasks.Business string result = ""; switch (code) { + case 2: result = "充注枪真空不合格"; break; case 4: result = "充注故障"; break; case 8: result = "充注时真空度超限定"; break; @@ -251,7 +248,7 @@ namespace Aucma.Core.Tasks.Business Array.Reverse(list); result = BitConverter.ToSingle(list, 0); // 只保留两位小数 - // string str = result.ToString("0.00"); + // string str = result.ToString("0.00"); return result;