using Cowain.Bake.BLL; using Cowain.Bake.Common; using Cowain.Bake.Common.Core; using Cowain.Bake.Common.Enums; using Cowain.Bake.Common.Interface; using Cowain.Bake.Communication.Interface; using Cowain.Bake.Communication.MOM; using Cowain.Bake.Main.Common; using Cowain.Bake.Main.ViewModels; using Cowain.Bake.Model; using Cowain.Bake.Model.Models; using HslCommunication; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Unity; namespace Cowain.Bake.Main.Station { public class DataCollectStation : IServerManager { public string Name { get; set; } public IUnityContainer _unityContainer { get; set; } readonly List stoveConfs = null; readonly CancellationTokenSource cts = new CancellationTokenSource(); readonly List listStoveTemp = new List(); private Dictionary cacheStoveData { set; get; } = new Dictionary();//如果只有一个保存炉子的真空值,数据容易不及时,且冲突 //private Dictionary cacheElectricEnergyData { set; get; } = new Dictionary(); private readonly Prism.Events.IEventAggregator _eventAggregator; public DataCollectStation(IUnityContainer unityContainer, Prism.Events.IEventAggregator eventAggregator) { _unityContainer = unityContainer; _eventAggregator = eventAggregator; stoveConfs = _unityContainer.Resolve().GetConfig(EDeviceType.PLC, EStationType.Stove.ToString());// Start(); } public void Start() { string tempCycle = _unityContainer.Resolve() .GetValueByParaID(Cowain.Bake.Common.Enums.ESysSetup.DataCollectionCycle.ToString()); int tempCycleValue = int.Parse(tempCycle); Task.Run(async () => { await Task.Delay(20 * 1000); //等待去读PLC的数据 while (!cts.Token.IsCancellationRequested) { await Task.Delay(tempCycleValue * 1000); if (Global.AppExit) { return; } CollectTemp(); } }); } void CollectTemp() { bool isWork = false; float vacuum = 0; UInt16 workTime = 0; UInt16 totalWorkTime = 0; float[] temps = null; OperateResult resultTemp; listStoveTemp.Clear(); cacheStoveData.Clear(); foreach (var conf in stoveConfs) { IPLCDevice plc = _unityContainer.Resolve(conf.Name); if (null == plc || !plc.IsConnect) { continue; } var stoves = (from v in plc.Storages group v by v.StationId into a orderby a.Key select a).ToList(); //每个炉子都循环 foreach (var item in stoves) //一个PLC控制二台炉子(一台炉子) { //0 / 空闲 1 / 待机 2 / 停止 3 / 工作 4 / 保压 var workStatus = plc.GetValue(EStoveSignal.CavityStatus.ToString(), item.Key); if (!workStatus.IsSuccess || (int)EStoveWorkMode.Standby >= workStatus.Content) //系统状态寄存器 { isWork = false; } else { isWork = true; } if (item.Key == Global.DEW_STOVE_NUMBER) //IP:70 { //露点温度 var dewTemp = plc.GetValue(EStoveSignal.DewTemperature.ToString(), item.Key); if (!dewTemp.IsSuccess) { LogHelper.Instance.Error($"读取露点温度失败!{dewTemp.Message}"); _unityContainer.Resolve().SetTempData("-0"); } else { DealDewTemp(dewTemp.Content); _unityContainer.Resolve().SetTempData(Math.Round(dewTemp.Content,2).ToString()); } } //真空值 var result = plc.GetValue(EStoveSignal.Vacuum.ToString(), item.Key); if (!result.IsSuccess) { LogHelper.Instance.Error($"读取真空值失败!{result.Message}"); continue; } vacuum = (float)(result.Content); //工作时长 var workTimeResult = plc.GetValue(EStoveSignal.WorkTime.ToString(), item.Key); var totalTimeResult = plc.GetValue(EStoveSignal.TotalWorkTime.ToString(), item.Key); if (!workTimeResult.IsSuccess || !totalTimeResult.IsSuccess) { LogHelper.Instance.Error($"读取工作时长失败!{workTimeResult.Message},{totalTimeResult.Message}"); continue; } workTime = workTimeResult.Content; totalWorkTime = totalTimeResult.Content; //获取一个炉子的温度节点 var stoveTempNode = (from storage in plc.Storages from itemTemp in storage.VariableList where storage.StationId == item.Key && itemTemp.TagType == (int)ETagType.Temperature //&& itemTemp.Number == layer select itemTemp).ToList(); foreach (var tempParam in stoveTempNode) //每一层的温度 { int layer = tempParam.Number; int cavityId = _unityContainer.Resolve().GetCavityId(item.Key, layer); TPalletInfo palletInfo = _unityContainer.Resolve().GetPalletCode(cavityId); if (null == palletInfo || 0 == palletInfo.Id) //没有托盘 { continue; } if (palletInfo.BakingBeginTime == null //没有烘烤开始时间的不用上传 || 0 == palletInfo.VirtualId) //没有绑定电芯 { continue; } //PID var resultPID = plc.GetValue(EStoveSignal.PID.ToString() + layer, item.Key, layer); if (!resultPID.IsSuccess) { LogHelper.Instance.GetCurrentClassError("读取PID失败!"); continue; } //温度 resultTemp = plc.GetValue(tempParam.ParamName, item.Key, layer); //温度 if (!resultTemp.IsSuccess) { LogHelper.Instance.GetCurrentClassError("读取温度失败!"); continue; } if (resultTemp.Content.All(x => x == 0) || resultPID.Content.All(x => x == 0)) { //LogHelper.Instance.GetCurrentClassError("读取温度异常,全为0!"); continue; } temps = CommonFun.Instance.UShortToFloat(resultTemp.Content); cacheStoveData[cavityId] = new StoveDataModel() //少了两个条件,1.VirtualId为0,2.工作状态 { Temps = temps, Vacuum = vacuum, WorkTime = workTime, TotalWorkTime = totalWorkTime }; if (isWork) //add by lsm 20250926, 测试用 { string tempData = MakeJsonTemperature(tempParam.VarDesc, temps); string pid = MakeJsonPid(EStoveSignal.PID.ToString(), resultPID.Content); listStoveTemp.Add(MakeTStoveSctualPatrol(palletInfo.VirtualId, cavityId, palletInfo.PalletCode, vacuum, tempData, pid)); } } } } //UploadProcessData(cacheStoveData); //1.上传Mom //太多,太卡,不上传 _eventAggregator.GetEvent().Publish(cacheStoveData);//2.界面刷新,发布事件(发送消息) if (0 != listStoveTemp.Count) { _unityContainer.Resolve().Insert(listStoveTemp);//批量插入数据 } } static bool? failDev; void DealDewTemp(float value) { float target = float.Parse(_unityContainer.Resolve().GetValueByParaID(ESysSetup.DewTempAlarmTargetValue.ToString())); TAlarm alarm = new TAlarm() { StationId = (int)EAlarmStationId.DevTemp, Desc = "露点温度超标,请检查压缩空气!", StartTime = DateTime.Now, Status = EAlarmStatus.Alert.GetDescription(), }; if (value <= target) //正常 { if (false != failDev) { failDev = false; _eventAggregator.GetEvent().Publish(alarm); } } else //异常 { if (true != failDev) { failDev = true; _eventAggregator.GetEvent().Publish(alarm); } } } //上转Mom public void UploadProcessData(Dictionary datas) { if (int.Parse(_unityContainer.Resolve().GetValueByParaID(ESysSetup.MOMEnable.ToString())) == (int)EMOMEnable.Enable) { foreach (var item in datas) { string cavityName = _unityContainer.Resolve().CavityInfo.Find(x => x.Id == item.Key).Name; _unityContainer.Resolve().MESBakingParameter(item.Value.Vacuum, item.Value.Temps, cavityName); } } } TStoveSctualPatrol MakeTStoveSctualPatrol(int palletVirtualId, int cavityId, string palletCode, float vacuum, string temperature,string pid) { return new TStoveSctualPatrol() { PalletVirtualId = palletVirtualId, CavityId = cavityId, PalletCode = palletCode, Vacuum = vacuum, Temperature = temperature, CreateTime = DateTime.Now, PID = pid }; } public string MakeJsonTemperature(string headName, float[] value) { headName = Regex.Replace(headName, @"\d", ""); List listTemp = new List(); for (int i = 0; i < value.Length; i++) { listTemp.Add(new PallletTemp() { HeadName = $"{headName}{i + 1}", Value = value[i] }); } return JsonConvert.SerializeObject(listTemp); } public string MakeJsonPid(string headName, float[] value) { //headName = Regex.Replace(headName, @"\d", ""); List listTemp = new List(); for (int i = 1; i < value.Length; i++) //数组0不使用 { listTemp.Add(new PallletTemp() { HeadName = $"{headName}{i}", Value = value[i] }); } return JsonConvert.SerializeObject(listTemp); } public void Stop() { // 在需要取消任务的时候,调用以下代码: cts.Cancel(); } } }