首次提交:添加src文件夹代码
This commit is contained in:
227
Cowain.Bake.Main/Station/UploadMesStation.cs
Normal file
227
Cowain.Bake.Main/Station/UploadMesStation.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Common.Enums;
|
||||
using Cowain.Bake.Common.Models;
|
||||
using Cowain.Bake.Model;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
using JSON = Newtonsoft.Json.JsonConvert;
|
||||
using Cowain.Bake.Main.ViewModels;
|
||||
using static Cowain.Bake.Common.Models.MESModel;
|
||||
using Cowain.Bake.Communication.MOM;
|
||||
using Cowain.Bake.Model.Models;
|
||||
|
||||
namespace Cowain.Bake.Main.Station
|
||||
{
|
||||
//自动上传模块
|
||||
public class UploadMesStation
|
||||
{
|
||||
public bool? OrdeIsMesOnline { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool haveWarn = true;
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
public string URL { get; set; }
|
||||
public IUnityContainer _unityContainer { get; set; }
|
||||
public int Id { get; set; }
|
||||
List<ProceParamList> listParams = new List<ProceParamList>();
|
||||
private readonly Prism.Events.IEventAggregator _eventAggregator;
|
||||
public UploadMesStation(IUnityContainer unityContainer, Prism.Events.IEventAggregator eventAggregator)
|
||||
{
|
||||
bool IsMesOnline = false;
|
||||
_unityContainer = unityContainer;
|
||||
TDeviceConfig dev = _unityContainer.Resolve<DeviceConfigService>().GetConfig(EDeviceType.MOM)[0];
|
||||
Id = dev.Id;
|
||||
Name = dev.Name;
|
||||
_eventAggregator = eventAggregator;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsMesOnline = false;
|
||||
if (int.Parse(_unityContainer.Resolve<SysSetupService>().GetValueByParaID(ESysSetup.MOMEnable.ToString())) == (int)EMOMEnable.Enable)
|
||||
{
|
||||
MESReturnCmdModel mesResult = _unityContainer.Resolve<MESProcess>().Alive();
|
||||
IsMesOnline = DealAlive(mesResult);
|
||||
}
|
||||
|
||||
if (OrdeIsMesOnline != IsMesOnline)
|
||||
{
|
||||
;
|
||||
OrdeIsMesOnline = IsMesOnline;
|
||||
_unityContainer.Resolve<DeviceConfigService>().UpdateStatus(this.Id, IsMesOnline);
|
||||
_unityContainer.Resolve<BasicInfoViewModel>().MesStatus = IsMesOnline;
|
||||
}
|
||||
|
||||
await Task.Delay(5000); //放这里的目的,是心跳后,再执行上传
|
||||
}
|
||||
});
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
await Task.Delay(3000);
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (int.Parse(_unityContainer.Resolve<SysSetupService>().GetValueByParaID(ESysSetup.MOMEnable.ToString())) == (int)EMOMEnable.Enable)
|
||||
{
|
||||
Run();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//MOM在回复心跳时通过KeyFlag= 2提醒设备需要调用MOM的工艺参数下发接口
|
||||
//MOM在回复心跳时通过KeyFlag= 2提醒设备需要调用MOM的工艺参数下发接口
|
||||
bool DealAlive(MESReturnCmdModel mesResult)
|
||||
{
|
||||
if (null == mesResult)
|
||||
{
|
||||
LogHelper.Instance.Warn($"MES不在线:{JSON.SerializeObject(mesResult)}");
|
||||
return false;
|
||||
}
|
||||
|
||||
else if (((int)EKeyFlag.NG).ToString() == mesResult.Info.KeyFlag)
|
||||
{
|
||||
TAlarm model = new TAlarm()
|
||||
{
|
||||
StationId = (int)EAlarmStationId.Mom,
|
||||
Desc = $"MOM心跳报警->{mesResult.Info.MOMMessage}", //会存在多次不同的报警
|
||||
StartTime = DateTime.Now,
|
||||
Status = EAlarmStatus.Alert.GetDescription(),
|
||||
};
|
||||
haveWarn = true;
|
||||
|
||||
_eventAggregator.GetEvent<AlarmAddEvent>().Publish(model);//2.界面刷新,发布事件(发送消息)
|
||||
_unityContainer.Resolve<AlarmService>().Insert(model);
|
||||
return true;
|
||||
}
|
||||
else if (((int)EKeyFlag.ProcessParam).ToString() == mesResult.Info.KeyFlag) //心跳时通过KeyFlag= 2提醒设备需要调用MOM的工艺参数下发接口
|
||||
{
|
||||
listParams.Clear();
|
||||
EqptParameterReturnCmd resultModel = _unityContainer.Resolve<MESProcess>().GetProcessParam();//获取返回的JOSN
|
||||
if (null == resultModel)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
TProductionInformation model = _unityContainer.Resolve<ProductionInformationService>().GetCurrentProductInfo();//实例化工单类
|
||||
|
||||
if (!_unityContainer.Resolve<ProcessParamService>().UpdateProcessParam(model.ProcessParamId, JSON.SerializeObject(resultModel.Info.ParameterInfo)))
|
||||
{
|
||||
LogHelper.Instance.Warn("保存工艺参数失败!");
|
||||
}
|
||||
}
|
||||
|
||||
if (haveWarn && ((int)EKeyFlag.NG).ToString() != mesResult.Info.KeyFlag)
|
||||
{
|
||||
_eventAggregator.GetEvent<AlarmStaionCancelEvent>().Publish((int)EAlarmStationId.Mom);//2.界面刷新,发布事件(发送消息)
|
||||
var alarms = _unityContainer.Resolve<AlarmService>().GetInAlarm((int)EAlarmStationId.Mom);
|
||||
foreach (var item in alarms)
|
||||
{
|
||||
_unityContainer.Resolve<AlarmService>().CancelAlarm(item);
|
||||
}
|
||||
haveWarn = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//public void GetJsonParam(string param)
|
||||
//{
|
||||
// dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
||||
// this.IpAddress = d.Ip;
|
||||
// this.Port = d.Port;
|
||||
// this.URL = d.URL;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
void Run()
|
||||
{
|
||||
if (int.Parse(_unityContainer.Resolve<SysSetupService>().GetValueByParaID(ESysSetup.MOMEnable.ToString())) != (int)EMOMEnable.Enable)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
List<TMesData> uploadItems = _unityContainer.Resolve<MesDataService>().GetUpLoadData();
|
||||
|
||||
foreach (var item in uploadItems)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject<dynamic>(item.CommandType);
|
||||
string service = d.Service;
|
||||
string func = d.Func;
|
||||
string url = d.UrlCmd;
|
||||
Type type = Type.GetType(MyPath.SIGNAL_TRIGGER + service);
|
||||
MethodInfo mi = this.GetType().GetMethod("ExecuteUpload").MakeGenericMethod(new Type[] { type }); //回复信息;
|
||||
|
||||
mi.Invoke(this, new object[]
|
||||
{
|
||||
func,
|
||||
url,
|
||||
item,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteUpload<T>(string func, string url, TMesData mesModel)
|
||||
{
|
||||
//取得实例
|
||||
var instnce = _unityContainer.Resolve<T>(); //上下料实例
|
||||
Type t = instnce.GetType();
|
||||
//取得方法
|
||||
MethodInfo mi = t.GetMethod(func);
|
||||
//调用方法
|
||||
mi.Invoke(instnce, new object[] //这里会去执行哪个方法上传,参数只有发送的信息
|
||||
{
|
||||
url,
|
||||
mesModel
|
||||
});
|
||||
}
|
||||
|
||||
public void UploadCommon(string url, TMesData mesModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
//这里就是调用上传的接口
|
||||
mesModel.SendTime = DateTime.Now;
|
||||
string result = HttpClientHelper.HttpPost(url, mesModel.Content);
|
||||
if (string.IsNullOrEmpty(result)) //请求失败
|
||||
{
|
||||
mesModel.SendFlag = (sbyte)EMesUpLoadStatus.Fail;
|
||||
}
|
||||
else //请求成功
|
||||
{
|
||||
mesModel.SendFlag = (sbyte)EMesUpLoadStatus.Success;
|
||||
mesModel.RecvContent = result;
|
||||
mesModel.RecvTime = DateTime.Now;
|
||||
_unityContainer.Resolve<MESProcess>().WriteRequestLog(mesModel.Content);
|
||||
_unityContainer.Resolve<MESProcess>().WriteResponseLog(result);
|
||||
}
|
||||
|
||||
_unityContainer.Resolve<MesDataService>().Update(mesModel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.GetCurrentClassError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user