using System; using System.Collections.Generic; using System.Net.NetworkInformation; using System.Text; using System.Linq; using System.Data; using System.Threading; using Mesnac.Action.Base; using Mesnac.Controls.Base; using System.Windows.Forms; using Mesnac.Codd.Session; using System.IO; using Mesnac.Action.Feeding.BasicInfo; using Mesnac.Action.Feeding.FeedingPlc; using Mesnac.Action.Feeding.Sys; namespace Mesnac.Action.Feeding.ProducingPlan { /// /// 执行计划 /// public class ExecutePlan : FeedingAction, IAction { #region 事件定义 /// /// 刷新计划事件 /// public static event EventHandler OnRefreshPlan; RuntimeParameter runtime; #endregion #region 业务入口 /// /// 执行计划业务入口 /// /// public void Run(RuntimeParameter runtime) { base.RunIni(runtime); this.runtime = runtime; base.LogDebug("执行计划..."); if (runtime.Sender is Control) { (runtime.Sender as Control).Enabled = false; } try { string msg = string.Empty; string lastPlanID = String.Empty; //最后一次执行的计划号 string currentPlanID = String.Empty; //保存当前要执行的计划号 #region 判断当班计划界面是否打开 if (PlanCommon.IsInit == false) { msg = "当班计划还为初始化,请先打开当班计划,再进行此操作!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion #region 控件验证 DbMCControl pptShiftControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptShift").FirstOrDefault(); DbMCControl pptClassControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptClass").FirstOrDefault(); DbMCControl planDateControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "[ppt_plan].[down_date]").FirstOrDefault(); if (planDateControl == null) { base.LogError("缺少日期控件...(日期控件为日历,数据源要绑定down_date)"); return; } if (pptShiftControl == null) { base.LogError("缺少班次控件...(班次控件为组合框,数据源要绑定PptShift)"); return; } #endregion #region 判断此功能可以使用的条件,只有满足才可使用本功能,若不满足则退出此功能 #region 1、判断上班最后一个计划是否已完成或终止,未完成或未终止,不能下传本班计划 //获取上班最后一个计划的计划号 if (PlanCommon.PlanLog != null) { DateTime planDate = Convert.ToDateTime(planDateControl.BaseControl.MCValue); int shiftID = 1; bool result = int.TryParse(pptShiftControl.BaseControl.MCValue.ToString(), out shiftID); if (!result) { msg = "请选择班次!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; //计划未完成,则终止 } if (PlanCommon.PlanExists(Basic.DataSourceFactory.MCDbType.Local, base.CurrEquipCode, PlanCommon.PlanLog.LastPlanID)) { lastPlanID = PlanCommon.PlanLog.LastPlanID; } else { //如果不存在此计划,则删除日志文件 //PlanCommon.PlanLog = null; //计划日志为null,会导致存盘失效 PlanCommon.PlanLog.LastPlanID = String.Empty; } } #region 1.1 如果合并一次法功能后,需要判断对应一次法的计划是否处于完成状态,如果未完成,则不能执行计划 if (base.GetConfigValue("IsCombinedOne", "0") == "1") { string processName = base.GetConfigValue("OneProcessName", String.Empty); if (!Mesnac.Basic.ProcessHelper.FindByProcessName(processName)) { //如果找不到一次法进程。 msg = "一次法应用没有运行,请先运行一次法应用程序,再执行计划!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); runtime.IsReturn = true; return; //0号开炼机还未完成,不能执行新计划! } // planFinishTag : -1代表计划不存在,2代表计划完成或终止 int planFinishTag = PlanCommon.GetPlanOneFinishTag(lastPlanID); if (planFinishTag != -1 && planFinishTag != 2) { msg = "计划号:{0},0号开炼机还未完成,不能执行新计划!"; msg = String.Format(msg, lastPlanID); ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); runtime.IsReturn = true; return; //0号开炼机还未完成,不能执行新计划! } } #endregion if (!String.IsNullOrEmpty(lastPlanID)) { DataRow planRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, lastPlanID); if (planRow != null) { if (Convert.ToInt32(planRow["PlanState"]) != (int)PlanStates.Completed && Convert.ToInt32(planRow["RealNum"]) < Convert.ToInt32(planRow["PlanNum"]) && (Convert.ToInt32(planRow["PlanState"]) == (int)PlanStates.Producting)) { msg = "计划号:{0},正在执行,不能执行新计划!"; msg = String.Format(msg, lastPlanID); ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; //计划未完成,则终止 } else// if (Convert.ToInt32(planRow["PlanState"]) == (int)PlanStates.Completed || Convert.ToInt32(planRow["RealNum"]) >= Convert.ToInt32(planRow["PlanNum"])) { //计划已完成 currentPlanID = String.Empty; //PlanCommon.PlanLog = null; //计划日志为null,会导致存盘失效 PlanCommon.PlanLog.LastPlanID = String.Empty; } } } #endregion #region 2、检查日期班次是否设置正确,不正确给出提示 DateTime plandate = DateTime.Now; int shiftitem = 0; if (!PlanCommon.PlanDateVerify(out plandate, out shiftitem, planDateControl.BaseControl.MCValue.ToString(), Convert.ToInt32(pptShiftControl.BaseControl.MCValue.ToString()), out msg)) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); planDateControl.BaseControl.MCValue = plandate; pptShiftControl.BaseControl.MCValue = shiftitem.ToString(); currentPlanID = String.Empty; //重置当前计划位空 return; } else { if (PlanCommon.PlanLog != null) { if (!((String.Format("{0:yyyyMMdd}", PlanCommon.PlanLog.LastSelectDate) == String.Format("{0:yyyyMMdd}", plandate)) && (PlanCommon.PlanLog.LastSelectShiftID != shiftitem))) { PlanCommon.PlanLog.LastSelectDate = plandate; PlanCommon.PlanLog.LastSelectShiftID = shiftitem; PlanCommon.PlanLog.LastClassID = 0; lastPlanID = String.Empty; PlanCommon.PlanLog.LastPlanID = String.Empty; } } } #endregion #region 3、若下位机正在处理报表,系统给出提示稍后再下传。下位机正在处理报表信号下位机可以提供 int readword = PlcData.Instance.FinishedBatch.LastValue.ToInt(); if (readword == 1) { msg = "下位机正在处理报表,稍后再执行计划!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion #region 4、判断本地是否存在可以执行的计划,若没有则退出 bool flag = false; //是否存在可以执行的计划 if (!String.IsNullOrEmpty(lastPlanID)) { //获取要执行的计划 DataRow planRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, lastPlanID); if (planRow != null && Convert.ToInt32(planRow["PlanState"]) == (int)PlanStates.Received && Convert.ToInt32(planRow["RealNum"]) == 0 && String.Format("{0:yyyyMMdd}", planRow["PlanDate"]) == String.Format("{0:yyyyMMdd}", Convert.ToDateTime(planDateControl.BaseControl.MCValue)) && Convert.ToInt32(planRow["ShiftID"]) == Convert.ToInt32(pptShiftControl.BaseControl.MCValue)) { //如果日志中记录的计划没有执行过,则把当前计划设置为日志中的计划 currentPlanID = lastPlanID; flag = true; } else { currentPlanID = PlanCommon.GetNextPlanID(lastPlanID, base.CurrEquipCode, Convert.ToDateTime(planDateControl.BaseControl.MCValue), Convert.ToInt32(pptShiftControl.BaseControl.MCValue)); if (!String.IsNullOrEmpty(currentPlanID)) { flag = true; } } } else { currentPlanID = PlanCommon.GetFirstPlanID(Basic.DataSourceFactory.MCDbType.Local, base.CurrEquipCode, Convert.ToDateTime(planDateControl.BaseControl.MCValue), Convert.ToInt32(pptShiftControl.BaseControl.MCValue)); if (!String.IsNullOrEmpty(currentPlanID)) { flag = true; } } if (!flag) { msg = "没有要执行的计划!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion #region 5、判断当前计划是否首次执行(完成数=0),若不是则退出。 DataRow planDataRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, currentPlanID); if (planDataRow == null) { msg = "没有要执行的计划!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (Convert.ToInt32(planDataRow["RealNum"]) != 0) { msg = "当前计划不是首次执行,不能执行计划!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion #region 6、若当前计划首次执行,则同时为网络版,则需要判断当前计划是否在网络上依然存在,若不存在则不进行当前计划的执行。 if (base.NetType == NetTypes.Net && PlanCommon.PlanExists(Basic.DataSourceFactory.MCDbType.Server, base.CurrEquipCode, currentPlanID) == false) { msg = "当前计划在网络库中已不存在,不能执行计划!"; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion #endregion #region 进行计划执行功能 DateTime lastSelectDate = DateTime.Now; int lastSelectShiftID = 0; int lastClassID = 0; DateTime.TryParse(planDateControl.BaseControl.MCValue.ToString(), out lastSelectDate); int.TryParse(pptShiftControl.BaseControl.MCValue.ToString(), out lastSelectShiftID); if (pptClassControl == null) { if (base.NetType == NetTypes.Net) { lastClassID = PlanCommon.GetClassID(Basic.DataSourceFactory.MCDbType.Server, 1, lastSelectDate, lastSelectShiftID); } } else { int.TryParse(pptClassControl.BaseControl.MCValue.ToString(), out lastClassID); } msg = Language(158); //"当前计划{0}设定{1}车,\r\n您确认要执行吗?"; msg = String.Format(msg, currentPlanID + "[" + Mesnac.Basic.DataProcessor.RowValue(planDataRow, "RecipeMaterialName", String.Empty) + "]", planDataRow["PlanNum"]); DialogResult dialogResult = MessageBox.Show(msg, Language(1), MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { #region 系统复位 new FeedingPlc.SystemReset().ExecuteReset(); #endregion this.Execute(currentPlanID, lastSelectDate, lastSelectShiftID, lastClassID, true); } else { runtime.IsReturn = true; return; } #endregion } catch (Exception ex) { base.LogError(ex.Message); base.LogError(ex.StackTrace); string msg = "计划执行失败:" + ex.Message; ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (runtime.Sender is Control) { (runtime.Sender as Control).Enabled = true; } } } #endregion #region 计划执行主业务 /// /// 计划执行 /// /// 要执行的计划号 /// 计划执行日期 /// 班次 /// 班组 /// 是否显示消息 public void Execute(string planID, DateTime selectedDate, int shiftID, int classID, bool isShowMsg) { List msgList = null; string msg = String.Empty; #region 0、记录日志 PlanLog planLog = new PlanLog(); planLog.LastSelectDate = selectedDate; planLog.LastSelectShiftID = shiftID; planLog.LastClassID = classID; planLog.LastPlanID = planID; PlanCommon.PlanLog = planLog; #endregion #region 1.0 下传时间到PLC DateTime dtNow = DateTime.Now; object[] buff = new object[] { dtNow.Year, dtNow.Month, dtNow.Day, dtNow.Hour, dtNow.Minute, dtNow.Second }; if (!PlcData.Instance.PlcWriteByDataKey(PlcData.Instance.MixingStartTime, buff)) { msg = "下传时间至PLC失败!"; if (isShowMsg) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } else { ICSharpCode.Core.LoggingService.Warn("执行计划:" + msg); } return; } else { ICSharpCode.Core.LoggingService.Debug("下传时间至PlC..."); } #endregion #region 1.1 下传配方(注:若生产过程中修改配方,则需要执行“配方重传”功能) int result = new BasicInfo.PLCRecipe().SendPlan(planID, out msgList); if (result != 1) { FrmNotify.Instance.Destory(); //销毁下传进度条对话框 base.LogError("配方下传失败..."); //ShowMsg(Language(138), Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); msgList.Add("配方下传失败..."); msg = PlanCommon.CombineMsgList(msgList); ICSharpCode.Core.LoggingService.Debug(msg); if (isShowMsg) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } return; } #endregion #region 1.2 把开始称量信号下传至PLC if (!new StartWeightToPlc().DownLoadStartWeight()) { msg = "把开始称量信号下传至PLC失败!"; if (isShowMsg) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } return; } #endregion #region 1.3 把开始密炼信号下传至PLC if (!new StartMixingToPlc().DownloadStartMixing()) { msg = "把开始密炼信号下传至PLC失败!"; if (isShowMsg) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } return; } #endregion #region 1.4 下传胶料秤传送控制 //RunShuSongHandler.DownloadRubShuSong(1, "下传配方"); //下传胶料秤传送控制 #endregion #region 1.5 更新计划状态,若为网络版,在网络数据库中把下达的计划状态更改为4(“正在生产”),同时更新计划执行时间RealStartTime为当前系统时间 if (base.NetType == NetTypes.Net) { //发送计划号 PlanCommon.UpdatedPlanStateAndRealStartTime(Basic.DataSourceFactory.MCDbType.Server, planID, 4, DateTime.Now); } PlanCommon.UpdatedPlanStateAndRealStartTime(Basic.DataSourceFactory.MCDbType.Local, planID, 4, DateTime.Now); //更新总重 double totalweight = PlanCommon.GetTotalWeightByPlanID(planID); PlanCommon.UpdateTotalWeight(planID, totalweight); #endregion #region 1.6 向本地库中增加批报表 if (!PlanCommon.AddPptGroupLot(planID)) { msg = "添加批报表失败!"; if (isShowMsg) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } return; } #endregion #region 1.7 如果合并一次发标志为1时,向一次法本地库增加一个计划 if (base.GetConfigValue("IsCombinedOne", "0") == "1") { PlanCommon.AddPlanOne(planID.Trim()); } #endregion #region 2、获取是否条码控制胶料秤标志。若当前系统为单机版,不进行锁秤。若为网络版,查询网络表中PmtRubWeightSetting当前机台是否需要锁秤。 //RubWeightSetting rubWeightSetting = null; //if (base.NetType == NetTypes.Net) //{ // //网络版 // bool isConnect = PlanCommon.PingIpOrDomainName(base.GetConfigValue("ServerIP", "127.0.0.1")); // if (!isConnect) // { // //网络不通 // if (isShowMsg) // { // ShowMsg(Language(33), Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); // } // else // { // ICSharpCode.Core.LoggingService.Warn(String.Format("计划[{0}]执行失败:{1}", planID, Language(33))); // } // return; // } // else // { // rubWeightSetting = PlanCommon.GetRubWeightSetting(base.CurrEquipCode); // if (rubWeightSetting != null && rubWeightSetting.WeightSettingCtrl != "0") // { // //进行锁秤处理 // } // } //} #endregion #region 3、若为网络版,向各客户端发送计划号。 if (base.NetType == NetTypes.Net) { //发送计划号 string netMsg = "{0}:{1}|{2}/"; //{0}执行计划协议头,{1}计划号,{2}设定数 netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveExecPlanNumber, planID, PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, planID)["PlanNum"]); Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg); } #endregion #region 4、若为网络版,清除本地扫描条码表数据pptShelfBar,清除网络条码扫描表中当前机台扫描数据。 PlanCommon.TruncatePptShelfBar(base.NetType, base.CurrEquipCode); #endregion #region 6、若为网络版,调用存储过程,更新是否出片标志 if (base.NetType == NetTypes.Net) { Dictionary parameters = new Dictionary(); parameters.Add("@Plan_id", planID); //当前计划号 parameters.Add("@Mater_code", PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, planID)["RecipeMaterialCode"]); //配方编码 base.ExecuteProcedure(Basic.DataSourceFactory.MCDbType.Server, "Proc_UpdatePlanCPFlag", parameters); } #endregion #region 7、若为网络版,为掺用配方增加下传计划标志 if (base.NetType == NetTypes.Net) { PlanCommon.SetExePlan(PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, planID)["RecipeMaterialCode"] as string); } #endregion #region 8、把当前计划列表发送给客户端 if (PlanCommon.PlanLog != null) { PlanCommon.SendInit(base.CurrEquipCode, PlanCommon.PlanLog.LastSelectDate, PlanCommon.PlanLog.LastSelectShiftID, PlanCommon.PlanLog.LastClassID, base.GetConfigValue("LastUserID", "0")); } else { string sbNetMsg = Global.ProtocalHeader.ReceivePlanIpNumber + ":/"; Mesnac.Communication.TcpService.Instance.NetSendMsg(sbNetMsg.ToString()); //发送网络消息 ICSharpCode.Core.LoggingService.Debug(String.Format("[{0}]没有计划日志!", planID)); } #endregion #region 9、触发刷新计划的事件 //触发刷新计划的事件 if (ExecutePlan.OnRefreshPlan != null) { ExecutePlan.OnRefreshPlan(null, System.EventArgs.Empty); } #endregion #region 10、消息提示 msg = "操作成功!"; msgList.Add(msg); msg = PlanCommon.CombineMsgList(msgList); ICSharpCode.Core.LoggingService.Debug(msg); if (isShowMsg) { Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); } #endregion } #endregion #region 处理Socket指令的计划执行 /// /// 处理Socket指令的计划执行 /// /// 班组 /// 班次 /// 日期 public void PlanExecute(string execclass, string execshift, string execdate) { try { string msg = string.Empty; string lastPlanID = String.Empty; //最后一次执行的计划号 string currentPlanID = String.Empty; //保存当前要执行的计划号 #region 判断此功能可以使用的条件,只有满足才可使用本功能,若不满足则退出此功能 #region 2、判断上班最后一个计划是否已完成或终止,未完成或未终止,不能下传本班计划 //获取上班最后一个计划的计划号 //PlanLog log = Global.Deserialize(PlanCommon.PlanLogFile); if (PlanCommon.PlanLog != null) { DateTime planDate = Convert.ToDateTime(execdate); int shiftID = Convert.ToInt32(execshift); if (PlanCommon.PlanExists(Basic.DataSourceFactory.MCDbType.Local, base.CurrEquipCode, PlanCommon.PlanLog.LastPlanID)) { lastPlanID = PlanCommon.PlanLog.LastPlanID; } else { //如果不存在此计划,则删除日志文件 //PlanCommon.PlanLog = null; //计划日志为null,会导致存盘失效 PlanCommon.PlanLog.LastPlanID = String.Empty; } } #region 1.1 如果合并一次法功能后,需要判断对应一次法的计划是否处于完成状态,如果未完成,则不能执行计划 if (base.GetConfigValue("IsCombinedOne", "0") == "1") { string processName = base.GetConfigValue("OneProcessName", String.Empty); if (!Mesnac.Basic.ProcessHelper.FindByProcessName(processName)) { //如果找不到一次法进程。 msg = "一次法应用没有运行,请先运行一次法应用程序,再执行计划!"; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; //0号开炼机还未完成,不能执行新计划! } // planFinishTag : -1代表计划不存在,2代表计划完成或终止 int planFinishTag = PlanCommon.GetPlanOneFinishTag(lastPlanID); if (planFinishTag != -1 && planFinishTag != 2) { msg = "计划号:{0},0号开炼机还未完成,不能执行新计划!"; msg = String.Format(msg, lastPlanID); //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; //0号开炼机还未完成,不能执行新计划! } } #endregion if (!String.IsNullOrEmpty(lastPlanID)) { //PlanStates planState = PlanCommon.GetPlanState(lastPlanID); DataRow planRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, lastPlanID); if (planRow != null) { if (Convert.ToInt32(planRow["PlanState"]) != (int)PlanStates.Completed && Convert.ToInt32(planRow["RealNum"]) < Convert.ToInt32(planRow["PlanNum"]) && (Convert.ToInt32(planRow["PlanState"]) == (int)PlanStates.Producting)) { msg = "计划号:{0},正在执行,不能执行新计划!"; msg = String.Format(msg, lastPlanID); //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; //计划未完成,则终止 } else// if (Convert.ToInt32(planRow["PlanState"]) == (int)PlanStates.Completed || Convert.ToInt32(planRow["RealNum"]) >= Convert.ToInt32(planRow["PlanNum"])) { //计划已完成 currentPlanID = String.Empty; //PlanCommon.PlanLog = null; //计划日志为null,会导致存盘失效 PlanCommon.PlanLog.LastPlanID = String.Empty; } } } #endregion #region 1、检查日期班次是否设置正确,不正确给出提示 DateTime plandate = DateTime.Now; int shiftitem = 0; if (!PlanCommon.PlanDateVerify(out plandate, out shiftitem, execdate, Convert.ToInt32(execshift), out msg)) { ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); //planDateControl.BaseControl.MCValue = plandate; //pptShiftControl.BaseControl.MCValue = shiftitem.ToString(); currentPlanID = String.Empty; //重置当前计划位空 return; } else { if (PlanCommon.PlanLog != null) { if (!((String.Format("{0:yyyyMMdd}", PlanCommon.PlanLog.LastSelectDate) == String.Format("{0:yyyyMMdd}", plandate)) && (PlanCommon.PlanLog.LastSelectShiftID != shiftitem))) { PlanCommon.PlanLog.LastSelectDate = plandate; PlanCommon.PlanLog.LastSelectShiftID = shiftitem; int classId = 0; int.TryParse(execclass, out classId); PlanCommon.PlanLog.LastClassID = classId; lastPlanID = String.Empty; PlanCommon.PlanLog.LastPlanID = String.Empty; } } } #endregion #region 3、若下位机正在处理报表,系统给出提示稍后再下传。下位机正在处理报表信号下位机可以提供 int readword = PlcData.Instance.FinishedBatch.LastValue.ToInt(); if (readword == 1) { msg = "下位机正在处理报表,稍后再执行计划!"; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; } #endregion #region 4、判断本地是否存在可以执行的计划,若没有则退出 bool flag = false; //是否存在可以执行的计划 if (!String.IsNullOrEmpty(lastPlanID)) { //获取要执行的计划 DataRow planRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, lastPlanID); if (planRow != null && Convert.ToInt32(planRow["PlanState"]) == (int)PlanStates.Received && Convert.ToInt32(planRow["RealNum"]) == 0 && String.Format("{0:yyyyMMdd}", planRow["PlanDate"]) == String.Format("{0:yyyyMMdd}", Convert.ToDateTime(execdate)) && Convert.ToInt32(planRow["ShiftID"]) == Convert.ToInt32(execshift)) { //如果日志中记录的计划没有执行过,则把当前计划设置为日志中的计划 currentPlanID = lastPlanID; flag = true; } else { currentPlanID = PlanCommon.GetNextPlanID(lastPlanID, base.CurrEquipCode, Convert.ToDateTime(execdate), Convert.ToInt32(execshift)); if (!String.IsNullOrEmpty(currentPlanID)) { flag = true; } } } else { currentPlanID = PlanCommon.GetFirstPlanID(Basic.DataSourceFactory.MCDbType.Local, base.CurrEquipCode, Convert.ToDateTime(execdate), Convert.ToInt32(execshift)); if (!String.IsNullOrEmpty(currentPlanID)) { flag = true; } } if (!flag) { msg = "没有要执行的计划!"; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; } #endregion #region 5、判断当前计划是否首次执行(完成数=0),若不是则退出。 DataRow planDataRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, currentPlanID); if (planDataRow == null) { msg = "没有要执行的计划!"; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; } if (Convert.ToInt32(planDataRow["RealNum"]) != 0) { msg = "当前计划不是首次执行,不能执行计划!"; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; } #endregion #region 6、若当前计划首次执行,则同时为网络版,则需要判断当前计划是否在网络上依然存在,若不存在则不进行当前计划的执行。 if (base.NetType == NetTypes.Net && PlanCommon.PlanExists(Basic.DataSourceFactory.MCDbType.Server, base.CurrEquipCode, currentPlanID) == false) { msg = "当前计划在网络库中已不存在,不能执行计划!"; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; return; } #endregion #endregion #region 进行计划执行功能 DateTime lastSelectDate = DateTime.Now; int lastSelectShiftID = 0; int lastClassID = 0; DateTime.TryParse(execdate, out lastSelectDate); int.TryParse(execshift, out lastSelectShiftID); if (execclass == null) { if (base.NetType == NetTypes.Net) { lastClassID = PlanCommon.GetClassID(Basic.DataSourceFactory.MCDbType.Server, 1, lastSelectDate, lastSelectShiftID); } } else { int.TryParse(execclass, out lastClassID); } #region 系统复位 new FeedingPlc.SystemReset().ExecuteReset(); #endregion this.Execute(currentPlanID, lastSelectDate, lastSelectShiftID, lastClassID, false); #endregion } catch (Exception ex) { base.LogError(ex.Message); base.LogError(ex.StackTrace); string msg = "计划执行失败:" + ex.Message; //ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); Mesnac.Basic.MessageBoxTimeOut.Show(msg, Language(1), 3000); runtime.IsReturn = true; } finally { //if (runtime.Sender is Control) //{ // (runtime.Sender as Control).Enabled = true; //} } } #endregion #region 执行下一个计划 /// /// 执行下一个计划 /// public void ExecuteNext() { //获取上班最后一个计划的计划号 PlanLog log = PlanCommon.PlanLog; if (log != null) { string nextPlanId = PlanCommon.GetNextPlanID(log.LastPlanID, base.CurrEquipCode, log.LastSelectDate, log.LastSelectShiftID); if (!String.IsNullOrEmpty(nextPlanId)) { this.Execute(nextPlanId, log.LastSelectDate, log.LastSelectShiftID, log.LastClassID, true); } } } #endregion } }