Files
6098/Cowain.Bake.Main/Station/DataCollectStation.cs

313 lines
13 KiB
C#
Raw Normal View History

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<TDeviceConfig> stoveConfs = null;
readonly CancellationTokenSource cts = new CancellationTokenSource();
readonly List<TStoveSctualPatrol> listStoveTemp = new List<TStoveSctualPatrol>();
private Dictionary<int, StoveDataModel> cacheStoveData { set; get; } = new Dictionary<int, StoveDataModel>();//如果只有一个保存炉子的真空值,数据容易不及时,且冲突
//private Dictionary<int, int> cacheElectricEnergyData { set; get; } = new Dictionary<int, int>();
private readonly Prism.Events.IEventAggregator _eventAggregator;
public DataCollectStation(IUnityContainer unityContainer, Prism.Events.IEventAggregator eventAggregator)
{
_unityContainer = unityContainer;
_eventAggregator = eventAggregator;
stoveConfs = _unityContainer.Resolve<DeviceConfigService>().GetConfig(EDeviceType.PLC, EStationType.Stove.ToString());//
Start();
}
public void Start()
{
string tempCycle = _unityContainer.Resolve<Cowain.Bake.BLL.SysSetupService>()
.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<Int16[]> resultTemp;
listStoveTemp.Clear();
cacheStoveData.Clear();
foreach (var conf in stoveConfs)
{
IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(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<Int16>(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<float>(EStoveSignal.DewTemperature.ToString(), item.Key);
if (!dewTemp.IsSuccess)
{
LogHelper.Instance.Error($"读取露点温度失败!{dewTemp.Message}");
_unityContainer.Resolve<BasicInfoViewModel>().SetTempData("-0");
}
else
{
DealDewTemp(dewTemp.Content);
_unityContainer.Resolve<BasicInfoViewModel>().SetTempData(Math.Round(dewTemp.Content,2).ToString());
}
}
//真空值
var result = plc.GetValue<int>(EStoveSignal.Vacuum.ToString(), item.Key);
if (!result.IsSuccess)
{
LogHelper.Instance.Error($"读取真空值失败!{result.Message}");
continue;
}
vacuum = (float)(result.Content);
//工作时长
var workTimeResult = plc.GetValue<UInt16>(EStoveSignal.WorkTime.ToString(), item.Key);
var totalTimeResult = plc.GetValue<UInt16>(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<CavityInfoService>().GetCavityId(item.Key, layer);
TPalletInfo palletInfo = _unityContainer.Resolve<PalletInfoService>().GetPalletCode(cavityId);
if (null == palletInfo
|| 0 == palletInfo.Id) //没有托盘
{
continue;
}
if (palletInfo.BakingBeginTime == null //没有烘烤开始时间的不用上传
|| 0 == palletInfo.VirtualId) //没有绑定电芯
{
continue;
}
//PID
var resultPID = plc.GetValue<float[]>(EStoveSignal.PID.ToString() + layer, item.Key, layer);
if (!resultPID.IsSuccess)
{
LogHelper.Instance.GetCurrentClassError("读取PID失败");
continue;
}
//温度
resultTemp = plc.GetValue<Int16[]>(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为02.工作状态
{
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<StoveDataEvent>().Publish(cacheStoveData);//2.界面刷新,发布事件(发送消息)
if (0 != listStoveTemp.Count)
{
_unityContainer.Resolve<StoveSctualPatrolService>().Insert(listStoveTemp);//批量插入数据
}
}
static bool? failDev;
void DealDewTemp(float value)
{
float target = float.Parse(_unityContainer.Resolve<SysSetupService>().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<AlarmCancelEvent>().Publish(alarm);
}
}
else //异常
{
if (true != failDev)
{
failDev = true;
_eventAggregator.GetEvent<AlarmAddEvent>().Publish(alarm);
}
}
}
//上转Mom
public void UploadProcessData(Dictionary<int, StoveDataModel> datas)
{
if (int.Parse(_unityContainer.Resolve<SysSetupService>().GetValueByParaID(ESysSetup.MOMEnable.ToString())) == (int)EMOMEnable.Enable)
{
foreach (var item in datas)
{
string cavityName = _unityContainer.Resolve<MemoryDataProvider>().CavityInfo.Find(x => x.Id == item.Key).Name;
_unityContainer.Resolve<MESProcess>().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<PallletTemp> listTemp = new List<PallletTemp>();
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<PallletTemp> listTemp = new List<PallletTemp>();
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();
}
}
}