using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Model.Model_New; using Aucma.Core.PLc; using log4net; using System.Timers; namespace Aucma.Core.Tasks { /// /// 钣金任务列表 /// public class AucamTaskService : IAucamTaskService { #region 钣金任务 /// /// 实时任务信息-每日产量 /// public delegate Task SmEverDayDelegate(); public static event SmEverDayDelegate SmEverDayDelegateEvent; /// /// 实时任务信息-展示 /// public delegate Task SmShowDelegate(ExecutePlanInfo info); public static event SmShowDelegate SmShowDelegateEvent; #endregion private static readonly log4net.ILog log = LogManager.GetLogger(typeof(AucamTaskService)); private readonly ISmTaskExecutionServices _smTaskExecutionServices; private readonly ISMProductionCompletionRecordServices _smProductionCompletionRecordServices; public AucamTaskService(ISmTaskExecutionServices smTaskExecutionServices, ISMProductionCompletionRecordServices smProductionCompletionRecordServices, IOldBoxFoamDataServices oldBoxFoamDataServices) { _smTaskExecutionServices= smTaskExecutionServices; _smProductionCompletionRecordServices = smProductionCompletionRecordServices; } System.Timers.Timer timer1 = new System.Timers.Timer(2000); System.Timers.Timer timer2 = new System.Timers.Timer(2000); System.Timers.Timer timer3 = new System.Timers.Timer(2000); bool qFlay=true;//前板标识 bool hFlay = true;//后板标识 public void AucamTaskAsync() { timer1.Elapsed += new System.Timers.ElapsedEventHandler(Run1); //到达时间的时候执行事件; timer1.AutoReset = true;//设置是执行一次(false)还是一直执行(true); timer1.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer1.Start();//timer.Start()的内部原理还是设置timer.Enabled = true; timer2.Elapsed += new System.Timers.ElapsedEventHandler(Run2); //到达时间的时候执行事件; timer2.AutoReset = true; timer2.Enabled = true; timer2.Start(); timer3.Elapsed += new System.Timers.ElapsedEventHandler(RealTimeTask); timer3.AutoReset = true; timer3.Enabled = true; timer3.Start(); } #region 钣金——前板 /// /// 钣金——前板 /// /// /// private void Run1(object? sender, ElapsedEventArgs e) { if (qFlay) { qFlay=false; //执行存盘操作 var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("前板Plc")); if (obj.plc.IsConnected) { var val = obj.plc.ReadInt16("M110");//任务结束信号存盘 if (val == 1) { SmTaskExecution smTask = _smTaskExecutionServices.FirstAsync(d=>d.IsExec==1).Result; SMProductionCompletionRecord sm=new SMProductionCompletionRecord(); sm.SmPlanid = smTask.TaskCode; sm.SmProductcode = smTask.MaterialCode; sm.SmProductname = smTask.MaterialName; sm.SmProductiontype = 0; sm.SmProductivetime =DateTime.Now; int result = _smProductionCompletionRecordServices.AddAsync(sm).Result; if (result>0) { bool saveDb = obj.plc.WriteInt16("M111", "1");//存盘完成信号 if (saveDb) { Console.WriteLine("前板完成"); qFlay = true; //刷新计划数、小时数据、型号统计数据 Console.WriteLine("执行刷新任务执行界面"); Console.WriteLine("执行每日刷新界面"); Console.WriteLine("执行型号刷新界面"); Console.WriteLine("执行实时任务刷新界面"); } } else { qFlay = true; } } } } } #endregion #region 钣金——后板 /// /// 钣金——后板 /// /// /// private void Run2(object? sender, ElapsedEventArgs e) { if (hFlay) { hFlay = false; //执行存盘操作 var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("后板Plc")); if (obj.plc.IsConnected) { var val = obj.plc.ReadInt16("M101");//任务结束信号存盘 if (val == 1) { SmTaskExecution smTask = _smTaskExecutionServices.FirstAsync(d => d.IsExec == 1).Result; SMProductionCompletionRecord sm = new SMProductionCompletionRecord(); sm.SmPlanid = smTask.TaskCode; sm.SmProductcode = smTask.MaterialCode; sm.SmProductname = smTask.MaterialName; sm.SmProductiontype = 0; sm.SmProductivetime = DateTime.Now; int result = _smProductionCompletionRecordServices.AddAsync(sm).Result; if (result > 0) { bool saveDb = obj.plc.WriteInt16("M102", "1");//存盘完成信号 if (saveDb) { Console.WriteLine("后板完成"); hFlay = true; //刷新计划数、小时数据、型号统计数据 Console.WriteLine("执行刷新任务执行界面"); Console.WriteLine("执行每日刷新界面"); Console.WriteLine("执行型号刷新界面"); Console.WriteLine("执行实时任务刷新界面"); } } else { hFlay = true; } } } } } #endregion #region 钣金—实时任务列表 /// /// 钣金—实时任务列表 /// /// /// private void RealTimeTask(object? sender, ElapsedEventArgs e) { var execTask = _smTaskExecutionServices.FirstAsync(d => d.IsExec == 0&&d.TaskSort==d.TaskSort).Result; if (execTask.FrontPlateAmount == execTask.RearPanelAmount) { execTask.IsExec = 2; execTask.CompleteAmount = execTask.FrontPlateAmount; bool r= _smTaskExecutionServices.UpdateAsync(execTask).Result; if (r) { var obj = _smTaskExecutionServices.QueryAsync(d => d.IsExec == 0).Result; int maxId = obj.Max(d => d.TaskSort); var task = obj.SingleOrDefault(d => d.IsExec == 0 && d.TaskSort == maxId); if (task == null) return; task.IsExec=1; bool exec= _smTaskExecutionServices.UpdateAsync(task).Result; //执行刷新任务执行界面 Console.WriteLine("执行刷新任务执行界面"); Console.WriteLine("执行每日刷新界面"); Console.WriteLine("执行型号刷新界面"); Console.WriteLine("执行实时任务刷新界面"); } } Console.WriteLine("实时任务列表"); } #endregion #region 实施当日产量和型号统计 public void Run() { //SmEverDayDelegateEvent?.Invoke(); } #endregion #region 实时计划显示 public void RealTimePlanRun() { //var model = _taskExecutionServices.FirstAsync(d => d.ExecuteStatus == 2).Result; //SmShowDelegateEvent?.Invoke(model); } #endregion #region 任务完成 public void CompleteRun() { var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("后板Plc")); if (obj == null) return; var val = obj.plc.ReadInt32("M100");//任务结束信号存盘 if (val == 1) { //1、存储当日完成记录表、计算节拍信息 //2、更新界面计划执行、当日产量、型号统计、计划列表 //3、更新日志记录 //4、更新数据统计列表 } } #endregion } }