using Admin.Core.Common.Helper; using Admin.Core.IService; using Admin.Core.Model; using Aucma.Core.BoxFoam.Models; using Aucma.Core.BoxFoam.ViewModels; using Aucma.Core.HwPLc; using Consul; using log4net; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Logging; using NetTaste; using NPOI.Util; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Documents; using System.Windows.Forms; namespace Aucma.Core.BoxFoam.Business { /// /// 采集发泡机数据 /// public class CollectionFoamMachine { /// /// 刷新最后一枪DATAGRID /// public delegate void RefreshLastShotDataDelegate(BoxLastShotRecord record); public static event RefreshLastShotDataDelegate RefreshLastShotDataDelegateEvent; /// /// 刷新每一枪数据 /// public delegate void RefreshGunDataDelegate(Dictionary keys); public static event RefreshGunDataDelegate RefreshGunDataDelegateEvent; /// /// 刷新系统数据 /// public delegate void RefreshSystemDataDelegate(Dictionary keys); public static event RefreshSystemDataDelegate RefreshSystemDataDelegateEvent; private static System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); List foamMachinesList = new List(); Dictionary gunKeys = new Dictionary(); Dictionary tempKeys = new Dictionary(); // 系统参数 Dictionary systemKeys = new Dictionary(); private readonly IBoxLastShotRecordServices? _lastShotRecordServices = App.ServiceProvider.GetService(); private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(CollectionFoamMachine)); public CollectionFoamMachine() { startCollect(); } public void startCollect() { //采集注料记录 Task.Run(() => { Thread.Sleep(3000); while (true) { var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamMachinePlc")); ReadFillingRecord(obj); Thread.Sleep(200); } }); //采集系统数据 Task.Run(() => { while (true) { try { Thread.Sleep(3000); var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamMachinePlc")); if (obj != null) { ReadEquipSystem(obj, "DB50.388", systemKeys, "1"); //采集1系统 ReadEquipSystem(obj, "DB50.642", systemKeys, "2"); //采集2系统 RefreshSystemDataDelegateEvent?.Invoke(systemKeys); foreach (KeyValuePair kvPair in gunKeys) { //程序启动缓存没数据查数据库 if (!tempKeys.ContainsKey(kvPair.Key)) { BoxLastShotRecord record = _lastShotRecordServices.Query(x => x.System == kvPair.Value.System && x.ProductLineCode == kvPair.Value.ProductLineCode && x.GunCode == kvPair.Value.GunCode && x.StationNumber == "1005").OrderByDescending(x => x.CreateTime).FirstOrDefault(); if (record == null) { kvPair.Value.ProductLineCode = "CX_02"; kvPair.Value.StationNumber = "1005"; // 数据库也没数据 int a = _lastShotRecordServices.AddAsync(kvPair.Value).Result; RefreshLastShotDataDelegateEvent?.Invoke(kvPair.Value); continue; } tempKeys.Add(kvPair.Key, record); } // 设备状态变化,更新设备状态 if (kvPair.Value.SystemStatus!= tempKeys[kvPair.Key].SystemStatus) { BoxLastShotRecord record = _lastShotRecordServices.Query(x => x.System.Equals(kvPair.Value.System) && x.ProductLineCode.Equals(kvPair.Value.ProductLineCode) && x.GunCode.Equals(kvPair.Value.GunCode) && x.StationNumber == "1005").OrderByDescending(x => x.CreateTime).FirstOrDefault(); if (record != null) { record.SystemStatus = kvPair.Value.SystemStatus; bool flag = _lastShotRecordServices.UpdateAsync(record).Result; } } } } } catch (Exception ex) { logHelper.Info(ex.Message.ToString()); } finally { foreach (KeyValuePair kvp in gunKeys) { if (tempKeys.ContainsKey(kvp.Key)) { // 键已存在,更新值 tempKeys[kvp.Key] = kvp.Value; } else { // 键不存在,添加键值对 tempKeys.Add(kvp.Key, kvp.Value); } } } } }); } /// /// 读取注料结束信号 /// /// private void ReadFillingRecord(PlcModel obj_1) { try { if (obj_1 != null && obj_1.plc.IsConnected) { if (obj_1.plc.ReadBool("DB50.DBX983.0")) { logHelper.Info("读取到发泡机A1枪结束信号"); ReadGunData(obj_1, "DB50.896", gunKeys, "A1"); //A1枪数据 int a = _lastShotRecordServices.AddAsync(gunKeys["A1"]).Result; RefreshLastShotDataDelegateEvent?.Invoke(gunKeys["A1"]); RefreshGunDataDelegateEvent?.Invoke(gunKeys); Thread.Sleep(2000); } else if (obj_1.plc.ReadBool("DB50.DBX1171.0")) { logHelper.Info("读取到发泡机B1枪结束信号"); ReadGunData(obj_1, "DB50.1084", gunKeys, "B1"); //B1枪数据 int a = _lastShotRecordServices.AddAsync(gunKeys["B1"]).Result; RefreshLastShotDataDelegateEvent?.Invoke(gunKeys["B1"]); RefreshGunDataDelegateEvent?.Invoke(gunKeys); Thread.Sleep(2000); } else if (obj_1.plc.ReadBool("DB50.DBX1359.0")) { logHelper.Info("读取到发泡机A2枪结束信号"); ReadGunData(obj_1, "DB50.1272", gunKeys, "A2"); //A2枪数据 int a = _lastShotRecordServices.AddAsync(gunKeys["A2"]).Result; RefreshLastShotDataDelegateEvent?.Invoke(gunKeys["A2"]); RefreshGunDataDelegateEvent?.Invoke(gunKeys); Thread.Sleep(2000); } else if (obj_1.plc.ReadBool("DB50.DBX1547.0")) { logHelper.Info("读取到发泡机B2结束信号"); ReadGunData(obj_1, "DB50.1460", gunKeys, "B2"); //A2枪数据 int a = _lastShotRecordServices.AddAsync(gunKeys["B2"]).Result; RefreshLastShotDataDelegateEvent?.Invoke(gunKeys["B2"]); RefreshGunDataDelegateEvent?.Invoke(gunKeys); Thread.Sleep(2000); } } } catch (Exception ex) { logHelper.Error(ex.Message.ToString()); } } /// /// 采集设备系统参数 /// /// static void ReadEquipSystem(HwPLc.PlcModel obj, string startStr, Dictionary keys, string systemId) { if (obj == null || !obj.plc.IsConnected) return; if (obj.plc.IsConnected) { byte[] info = obj.plc.Read(startStr, 170); if (info == null) return; FoamMachinesModel model = new FoamMachinesModel(); model.systemId = systemId; model.Machine_Status_0 = judgeStatus(ByteArrayToBinary(info.Skip(0).Take(1).ToArray())).ToString(); model.Machine_Status_1 = judgeStatus(ByteArrayToBinary(info.Skip(1).Take(1).ToArray())).ToString(); model.POL_Level = byteToFloat(info.Skip(2).Take(4).ToArray()).ToString("0.00") ; model.POL_Temp = byteToFloat(info.Skip(14).Take(4).ToArray()).ToString("0.00"); model.POL_LP = byteToFloat(info.Skip(18).Take(4).ToArray()).ToString("0.00"); model.POL_HP = byteToFloat(info.Skip(22).Take(4).ToArray()).ToString("0.00"); model.POL_Vol = byteToFloat(info.Skip(34).Take(4).ToArray()).ToString("0.00"); model.POL_Usage = byteToFloat(info.Skip(38).Take(4).ToArray()).ToString("0.00"); model.POL_Freq_Speed = byteToFloat(info.Skip(42).Take(4).ToArray()).ToString("0.00"); model.ISO_Level = byteToFloat(info.Skip(82).Take(4).ToArray()).ToString("0.00"); model.ISO_Temp = byteToFloat(info.Skip(94).Take(4).ToArray()).ToString("0.00"); model.ISO_LP = byteToFloat(info.Skip(98).Take(4).ToArray()).ToString("0.00"); model.ISO_HP = byteToFloat(info.Skip(102).Take(4).ToArray()).ToString("0.00"); model.ISO_Vol = byteToFloat(info.Skip(114).Take(4).ToArray()).ToString("0.00"); model.ISO_Usage = byteToFloat(info.Skip(118).Take(4).ToArray()).ToString("0.00"); model.ISO_Freq_Speed = byteToFloat(info.Skip(122).Take(4).ToArray()).ToString("0.00"); model.Hydr_Press = byteToFloat(info.Skip(162).Take(4).ToArray()).ToString("0.00"); model.Hydr_Temp = byteToFloat(info.Skip(166).Take(4).ToArray()).ToString("0.00"); model.collectTime = DateTime.Now; if (keys.ContainsKey(systemId)) { // 键已存在,更新值 keys[systemId] = model; } else { // 键不存在,添加键值对 keys.Add(systemId, model); } } } /// /// 每一枪数据 ---A1,B1,A2,B2 /// /// static void ReadGunData(HwPLc.PlcModel obj, string startStr, Dictionary keys, string gunCode) { if (obj == null || !obj.plc.IsConnected) return; if (obj.plc.IsConnected) { byte[] info = obj.plc.Read(startStr, 88); if (info == null) return; BoxLastShotRecord lastShotRecord = new BoxLastShotRecord(); lastShotRecord.System = gunCode.Substring(1, 1); lastShotRecord.GunCode = gunCode; // 系统状态 if(lastShotRecord.System == "1") { if(obj.plc.ReadBool("DB50.388.0")) { lastShotRecord.SystemStatus = 1; } else { lastShotRecord.SystemStatus = 0; } } else { if (obj.plc.ReadBool("DB50.642.0")) { lastShotRecord.SystemStatus = 1; } else { lastShotRecord.SystemStatus = 0; } } lastShotRecord.ProductLineCode = "CX_02"; lastShotRecord.StationNumber = "1005"; lastShotRecord.PolTemp = byteToFloat(info.Skip(0).Take(4).ToArray()).ToString("0.00"); lastShotRecord.PolHp = byteToFloat(info.Skip(4).Take(4).ToArray()).ToString("0.00"); lastShotRecord.PolVol = byteToFloat(info.Skip(8).Take(4).ToArray()).ToString("0.00"); lastShotRecord.PolUsage = byteToFloat(info.Skip(12).Take(4).ToArray()).ToString("0.00"); lastShotRecord.IsoTemp = byteToFloat(info.Skip(16).Take(4).ToArray()).ToString("0.00"); lastShotRecord.IsoHp = byteToFloat(info.Skip(20).Take(4).ToArray()).ToString("0.00"); lastShotRecord.IsoVol = byteToFloat(info.Skip(24).Take(4).ToArray()).ToString("0.00"); lastShotRecord.IsoUsage = byteToFloat(info.Skip(28).Take(4).ToArray()).ToString("0.00"); lastShotRecord.PourNu = short.Parse(StringChange.bytesToHexStr(info.Skip(36).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber).ToString(); lastShotRecord.SetTime = int.Parse(StringChange.bytesToHexStr(info.Skip(38).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString(); lastShotRecord.SetWeight = byteToFloat(info.Skip(42).Take(4).ToArray()).ToString("0.00"); lastShotRecord.SetRatio = byteToFloat(info.Skip(46).Take(4).ToArray()).ToString("0.00"); ; lastShotRecord.PourWeight = byteToFloat(info.Skip(50).Take(4).ToArray()).ToString("0.00"); lastShotRecord.PourRatio = byteToFloat(info.Skip(54).Take(4).ToArray()).ToString("0.00"); lastShotRecord.MpTime = int.Parse(StringChange.bytesToHexStr(info.Skip(58).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString(); lastShotRecord.HpTime = int.Parse(StringChange.bytesToHexStr(info.Skip(62).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString(); lastShotRecord.HydrPress = byteToFloat(info.Skip(66).Take(4).ToArray()).ToString("0.00"); lastShotRecord.HydrTemp = byteToFloat(info.Skip(70).Take(4).ToArray()).ToString("0.00"); lastShotRecord.MixpistOn = int.Parse(StringChange.bytesToHexStr(info.Skip(74).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString(); lastShotRecord.MixpistOff = int.Parse(StringChange.bytesToHexStr(info.Skip(78).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString(); ; lastShotRecord.TotalYield = int.Parse(StringChange.bytesToHexStr(info.Skip(82).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString(); ; lastShotRecord.PourEnd = judgeStatus(ByteArrayToBinary(info.Skip(87).Take(1).ToArray())).ToString(); lastShotRecord.CreateTime = DateTime.Now; if (keys.ContainsKey(gunCode)) { // 键已存在,更新值 keys[gunCode] = lastShotRecord; } else { // 键不存在,添加键值对 keys.Add(gunCode, lastShotRecord); } } } #region 字符工具转换方法 /// byte[]转十进制ascii码 /// /// static string ByteArrayToString(byte[] bytes) { string result = string.Empty; foreach (byte b in bytes) { int decimalValue = Convert.ToInt32(b); // 将字节转换为十进制值 result += decimalValue; } return result; } /// /// byte[]转二进制 /// /// static string ByteArrayToBinary(byte[] bytes) { StringBuilder binaryString = new StringBuilder(); foreach (byte b in bytes) { binaryString.Append(Convert.ToString(b, 2).PadLeft(8, '0')); } // Console.WriteLine(binaryString); string reversedStr = new string(binaryString.ToString().Reverse().ToArray()); // Console.WriteLine(reversedStr); return reversedStr; } /// /// 根据二进制字符串确定夹具状态 1正常生产 2暂停生产 /// static int judgeStatus(string str1) { int item1 = 0; if (str1.Substring(0, 1) == "1") { item1 = 1; // } else if (str1.Substring(1, 1) == "1") { item1 = 2; // } return item1; } /// /// byte数组转换为float /// static float byteToFloat(byte[] list) { float result = 0.00f; Array.Reverse(list); result = BitConverter.ToSingle(list, 0); return result; } #endregion #region 2024-1-13 delete by liuwf 原始采数-根据关枪时间变化保存注料记录 //public void startCollect() //{ // Task.Run(() => // { // while (true) // { // try // { // Thread.Sleep(3000); // var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamMachinePlc")); // if (obj != null) // { // ReadEquipSystem(obj, "DB50.388", systemKeys, "1"); //采集1系统 // ReadEquipSystem(obj, "DB50.642", systemKeys, "2"); //采集2系统 // ReadGunData(obj, "DB50.896", gunKeys, "A1"); //A1枪数据 // ReadGunData(obj, "DB50.1084", gunKeys, "B1"); //B1枪数据 // ReadGunData(obj, "DB50.1272", gunKeys, "A2"); //A2枪数据 // ReadGunData(obj, "DB50.1460", gunKeys, "B2"); //A2枪数据 // RefreshSystemDataDelegateEvent?.Invoke(systemKeys); // RefreshGunDataDelegateEvent?.Invoke(gunKeys); // foreach (KeyValuePair kvPair in gunKeys) // { // //程序启动缓存没数据查数据库 // if (!tempKeys.ContainsKey(kvPair.Key)) // { // BoxLastShotRecord record = _lastShotRecordServices.Query(x => x.System == kvPair.Value.System && x.ProductLineCode == kvPair.Value.ProductLineCode && x.GunCode == kvPair.Value.GunCode && x.StationNumber == "1005").OrderByDescending(x => x.CreateTime).FirstOrDefault(); // if (record == null) // { // kvPair.Value.ProductLineCode = "CX_02"; // kvPair.Value.StationNumber = "1005"; // // 数据库也没数据 // int a = _lastShotRecordServices.AddAsync(kvPair.Value).Result; // RefreshLastShotDataDelegateEvent?.Invoke(kvPair.Value); // continue; // } // tempKeys.Add(kvPair.Key, record); // } // //有变化更新记录所有信息 // if (!kvPair.Value.MixpistOff.Equals(tempKeys[kvPair.Key].MixpistOff) && !kvPair.Value.MixpistOff.Equals("0")) // { // kvPair.Value.ProductLineCode = "CX_02"; // kvPair.Value.StationNumber = "1005"; // int b = _lastShotRecordServices.AddAsync(kvPair.Value).Result; // RefreshLastShotDataDelegateEvent?.Invoke(kvPair.Value); // } // // 设备状态变化,更新设备状态 // if (kvPair.Value.SystemStatus != tempKeys[kvPair.Key].SystemStatus) // { // BoxLastShotRecord record = _lastShotRecordServices.Query(x => x.System.Equals(kvPair.Value.System) && x.ProductLineCode.Equals(kvPair.Value.ProductLineCode) && x.GunCode.Equals(kvPair.Value.GunCode) && x.StationNumber == "1005").OrderByDescending(x => x.CreateTime).FirstOrDefault(); // if (record != null) // { // record.SystemStatus = kvPair.Value.SystemStatus; // bool flag = _lastShotRecordServices.UpdateAsync(record).Result; // } // } // } // } // } // catch (Exception ex) // { // logHelper.Info(ex.Message.ToString()); // } // finally // { // foreach (KeyValuePair kvp in gunKeys) // { // if (tempKeys.ContainsKey(kvp.Key)) // { // // 键已存在,更新值 // tempKeys[kvp.Key] = kvp.Value; // } // else // { // // 键不存在,添加键值对 // tempKeys.Add(kvp.Key, kvp.Value); // } // } // } // } // }); //} #endregion } }