using DevExpress.Office.Drawing; using DevExpress.Office.Utils; using DevExpress.XtraEditors.Filtering.Templates; using HslCommunication.Profinet.Siemens; using Mesnac.Action.Base; using Mesnac.Action.ChemicalWeighing.ChemicalWeighingPlc; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Entity.PptPlan; using Mesnac.Action.ChemicalWeighing.Entity.station; using Mesnac.Action.ChemicalWeighing.Product.PptPlan; using Mesnac.Controls.Default; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing.Show { public class InitFormAction : ChemicalWeighingAction, IAction { #region 字段定义 public static bool IsFirstRun = true; //是否首次运行 private RuntimeParameter _runtime; //private DbMCControl _planDateControl = null; //计划日期 //private DbMCControl _pptShiftControl = null; //班次 private DbMCControl _clientGridControl = null; //本地计划控件 string planFlag= string.Empty; string planFlag2 = string.Empty; bool planFlag3 = true; #endregion System.Timers.Timer timer = new System.Timers.Timer(3000); System.Timers.Timer timer2 = new System.Timers.Timer(3000); public void Run(RuntimeParameter runtime) { base.RunIni(runtime); #region 首次执行,进行事件订阅 if (IsFirstRun) { ICSharpCode.Core.LoggingService.Debug("小料展示界面-窗体初始化业务..."); } #endregion DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_weigh").FirstOrDefault(); //获取本机台计划控件 if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Error("{首页展示}缺少本机台计划网格控件..."); return; } //StartServer1(); //StartServer2(); //InitData(); //1号地磅 timer.Elapsed += new ElapsedEventHandler(StartServer1); timer.AutoReset = true; timer.Enabled = true; timer.Start(); ////IsFirstRun = false; //////2号地磅 timer2.Elapsed += new ElapsedEventHandler(StartServer2); timer2.AutoReset = true; timer2.Enabled = true; timer2.Start(); //StartServer1(); //StartServer2(); #region 初始化 #region 获取界面控件 string source = string.Empty; //DbMCControl planDateControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "[xl_plan].[Plan_Date]").FirstOrDefault(); //DbMCControl pptShiftControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_Shiftime").FirstOrDefault(); //DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "xl_plan").FirstOrDefault(); //获取本机台计划网格控件 //if (planDateControl == null) //{ // ICSharpCode.Core.LoggingService.Error("{当班计划-窗体加载} 缺少日期选择控件..."); // return; //} //if (pptShiftControl == null) //{ // ICSharpCode.Core.LoggingService.Error("{当班计划-窗体加载} 缺少班次组合框控件..."); // return; //} //if (clientGridControl == null) //{ // ICSharpCode.Core.LoggingService.Error("{小料展示界面-窗体加载} 缺少计划列表网格控件..."); // return; //} //this._runtime = runtime; //this._clientGridControl = clientGridControl; #endregion #region 初始化操作 //DataGridView clientGrid = (clientGridControl.BaseControl as DataGridView); //this.InitGridViewEvent(clientGrid); //this.InitData(); #endregion #endregion } #region 方法定义 #region 网格事件处理方法 /// /// 初始化网格样式 /// /// DataGridView网格控件对象 private void InitGridViewEvent(DataGridView dgv) { } #endregion #region 初始化界面控件数据 /// /// 初始化界面控件数据 /// private void InitData() { DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_weigh").FirstOrDefault(); //获取本机台计划控件 if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Error("{首页展示}缺少本机台计划网格控件..."); return; } lock (String.Empty) { //本地计划 if (clientGridControl != null && clientGridControl.BaseControl != null) { //clientGridControl.BaseControl.BindDataSource = null; //clientGridControl.BaseControl.BindDataSource = dt; #region 根据计划状态处理背景色 DataGridView clientGrid = clientGridControl.BaseControl as DataGridView; SetBackColor(clientGrid); #endregion } else { ICSharpCode.Core.LoggingService.Warn("接在首页展示物料失败:本地控件为Null..."); } } } #endregion #endregion private void StartServer1(object sender, ElapsedEventArgs e) { DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_weigh").FirstOrDefault(); //获取本机台计划控件 if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Error("{首页展示}缺少本机台计划网格控件..."); return; } if (true) { planFlag3 = false; try { var lblPlanName = base.GetControlById("lblPlanName") as System.Windows.Forms.Label;//配方名称 if (lblPlanName == null) { timer.Stop(); timer.Close(); return; } var lblRecipeName = base.GetControlById("lblRecipeName") as System.Windows.Forms.Label;//配方名称 var lblTotalBatch = base.GetControlById("lblTotalBatch") as System.Windows.Forms.Label;//总批次 var lblMaterial = base.GetControlById("lblMaterial") as System.Windows.Forms.Label;//物料名称 var XProgressBar = base.GetControlById("XProgressBar") as System.Windows.Forms.ProgressBar;//进度条 var lblStandardWeight = base.GetControlById("lblStandardWeight") as System.Windows.Forms.Label;//标重 var lblStateMessage = base.GetControlById("lblStateMessage") as System.Windows.Forms.Label;//配方名称 var lblPlanStateMessage = base.GetControlById("lblPlanState") as System.Windows.Forms.Label;//计划状态 var rate = base.GetControlById("MCLabel2") as System.Windows.Forms.Label;//理论固含 DataTable table = PlanHelper.GetProPlan();//正在执行的任务 if (table == null) { lblStateMessage.Text = "状态信息"; lblMaterial.Text = "物料"; lblPlanName.Text = "计划号"; lblRecipeName.Text = "配方"; clientGridControl.BaseControl.BindDataSource = null; return; } SiemensS7Net plc = PlcConnect.Instance; int status01 = plc.ReadInt16("DB110.DBW1770").Content; //int status01 = BasePlcHelper.Instance.solvent_status01.NowValue.ToInt(); if (status01 == 1) { lblStateMessage.Text = "请加料!"; } int status02 = plc.ReadInt16("DB110.DBW1772").Content; //int status02 = BasePlcHelper.Instance.solvent_status02.NowValue.ToInt(); if (status02 == 1) { lblStateMessage.Text = "超重"; } int statusEnd = plc.ReadInt16("DB110.DBW1774").Content; int runState = plc.ReadInt16("DB110.DBW1780").Content; //int runState = BasePlcHelper.Instance.solvent_SystemRuning.NowValue.ToInt(); if (runState == 1 && status01 == 1&& statusEnd==0) { lblStateMessage.Text = "请加料!"; } int status04 = plc.ReadInt16("DB110.DBW1776").Content; //int status04 = BasePlcHelper.Instance.solvent_status04.NowValue.ToInt(); if (status04 == 1&& runState == 1) { lblStateMessage.Text = "请清空地磅,扫描之后放物料!"; } if (status04 == 1 && runState == 0) { lblStateMessage.Text = "请清空地磅,扫描之后放物料!"; } if (status04 == 0 && runState == 1 && status01 ==0) { lblStateMessage.Text = "请扫描拉缸条码,核对投料类型!"; } if (runState == 0 && status01 == 0 && status04 == 0 && status02 == 0) { lblStateMessage.Text = "状态信息"; } if (statusEnd ==1 ) { lblStateMessage.Text = "完成"; } List materialList = PlanHelper.DataTableToList(table); if (materialList.Count == 0) { return; } MetagePlan plan2 = null; var matlCode = BasePlcHelper.Instance.solvent_materialNo.NowValue.ToInt(); if (matlCode == 0) { plan2 = materialList.FirstOrDefault(d => d.MID == 1); } else { plan2 = materialList.FirstOrDefault(d => d.MID == matlCode); } if (plan2 == null) { return; } var matSerial = plan2.MID; MetagePlan plan = materialList.FirstOrDefault(d => d.MID == matSerial); if (plan2 != null) { lblPlanStateMessage.Text = plan2.Plan_StateText; } if (!string.IsNullOrEmpty(planFlag)) { if (planFlag == plan.Plan_Id) { return; } else { planFlag = plan.Plan_Id; } } else { planFlag = plan.Plan_Id; } if (plan != null) { lblPlanStateMessage.Text = plan2.Plan_StateText; var p = materialList.FirstOrDefault(); lblPlanName.Text = p.Plan_Id; lblRecipeName.Text = p.Recipe_Name; lblMaterial.Text = plan.Material_name;//正在执行的物料号 lblStandardWeight.Text = plan.Total_Weight.ToString("f2"); rate.Text = plan.TheoryRate.ToString(); } else { return; } lock (String.Empty) { //本地计划 if (clientGridControl != null && clientGridControl.BaseControl != null) { clientGridControl.BaseControl.BindDataSource = null; clientGridControl.BaseControl.BindDataSource = table; #region 根据计划状态处理背景色 DataGridView clientGrid = clientGridControl.BaseControl as DataGridView; SetBackColor(clientGrid); #endregion } else { ICSharpCode.Core.LoggingService.Warn("接在首页展示物料失败:本地计划控件为Null..."); } } } catch (Exception ex) { planFlag3 = true; Console.WriteLine(ex.Message); } finally { planFlag3 = true; } } } private void StartServer2(object sender, ElapsedEventArgs e) { DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "xl_weigh").FirstOrDefault(); //获取本机台计划控件 if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Error("{首页展示}缺少本机台计划网格控件..."); return; } var lblPlanName = base.GetControlById("lblPlanName2") as System.Windows.Forms.Label;//计划名称 if (lblPlanName == null) { timer.Stop(); timer.Close(); return; } var lblRecipeName = base.GetControlById("lblRecipeName2") as System.Windows.Forms.Label;//配方名称 var lblTotalBatch = base.GetControlById("lblTotalBatch2") as System.Windows.Forms.Label;//总批次 var lblMaterial = base.GetControlById("lblMaterial2") as System.Windows.Forms.Label;//物料名称 var XProgressBar = base.GetControlById("XProgressBar2") as System.Windows.Forms.ProgressBar;//进度条 var lblStandardWeight = base.GetControlById("lblStandardWeight2") as System.Windows.Forms.Label;//标重 var lblStateMessage = base.GetControlById("lblStateMessage2") as System.Windows.Forms.Label;//配方名称 var lblPlanStateMessage = base.GetControlById("lblPlanState2") as System.Windows.Forms.Label;//计划状态 var rate = base.GetControlById("lblRate") as System.Windows.Forms.Label;//理论固含 DataTable table = PlanHelper.GetProWeighbridge2Plan();//正在执行的任务 if (table == null) { lblStateMessage.Text = "状态信息"; lblMaterial.Text = "物料"; lblPlanName.Text = "计划号"; lblRecipeName.Text = "配方"; clientGridControl.BaseControl.BindDataSource = null; return; } SiemensS7Net plc = PlcConnect.Instance; int status01 = plc.ReadInt16("DB111.DBW1770").Content; //int status01 = BasePlcHelper.Instance.solvent_status01.NowValue.ToInt(); if (status01 == 1) { lblStateMessage.Text = "请加料!"; } int status02 = plc.ReadInt16("DB111.DBW1772").Content; //int status02 = BasePlcHelper.Instance.solvent_status02.NowValue.ToInt(); if (status02 == 1) { lblStateMessage.Text = "超重"; } int statusEnd = plc.ReadInt16("DB111.DBW1774").Content; int runState = plc.ReadInt16("DB111.DBW1780").Content; //int runState = BasePlcHelper.Instance.solvent_SystemRuning.NowValue.ToInt(); if (runState == 1 && status01 == 1 && statusEnd == 0) { lblStateMessage.Text = "请加料!"; } int status04 = plc.ReadInt16("DB111.DBW1776").Content; //int status04 = BasePlcHelper.Instance.solvent_status04.NowValue.ToInt(); if (status04 == 1 && runState == 1) { lblStateMessage.Text = "请清空地磅,扫描之后放物料!"; } if (status04 == 1 && runState == 0) { lblStateMessage.Text = "请清空地磅,扫描之后放物料!"; } if (status04 == 0 && runState == 1 && status01 == 0) { lblStateMessage.Text = "请扫描拉缸条码,核对投料类型!"; } if (runState == 0 && status01 == 0 && status04 == 0 && status02 == 0) { lblStateMessage.Text = "状态信息"; } if (statusEnd == 1) { lblStateMessage.Text = "完成"; } List materialList = PlanHelper.DataTableToList(table); if (materialList.Count == 0) { return; } MetagePlan plan2 = null; var matlCode = BasePlcHelper.Instance.solvent_materialNo.NowValue.ToInt(); if (matlCode == 0) { plan2 = materialList.FirstOrDefault(d => d.MID == 1); } else { plan2 = materialList.FirstOrDefault(d => d.MID == matlCode); } if (plan2 == null) { return; } var matSerial = plan2.MID; MetagePlan plan = materialList.FirstOrDefault(d => d.MID == matSerial); if (plan2 != null) { lblPlanStateMessage.Text = plan2.Plan_StateText; } if (!string.IsNullOrEmpty(planFlag2)) { if (planFlag2 == plan.Plan_Id) { return; } else { planFlag2 = plan.Plan_Id; } } else { planFlag2 = plan.Plan_Id; } if (plan != null) { lblPlanStateMessage.Text = plan2.Plan_StateText; var p = materialList.FirstOrDefault(); lblPlanName.Text = p.Plan_Id; lblRecipeName.Text = p.Recipe_Name; lblMaterial.Text = plan.Material_name;//正在执行的物料号 lblStandardWeight.Text = plan.Total_Weight.ToString("f2"); rate.Text = plan.TheoryRate.ToString(); } else { return; } lock (String.Empty) { //本地计划 if (clientGridControl != null && clientGridControl.BaseControl != null) { clientGridControl.BaseControl.BindDataSource = null; clientGridControl.BaseControl.BindDataSource = table; #region 根据计划状态处理背景色 DataGridView clientGrid = clientGridControl.BaseControl as DataGridView; SetBackColor(clientGrid); #endregion } else { ICSharpCode.Core.LoggingService.Warn("接在首页展示物料失败:本地计划控件为Null..."); } } } #region 设置(网格控件)计划状态背景色 /// /// 设置计划状态背景色 /// /// public static void SetBackColor(DataGridView grid) { lock (String.Empty) { try { if (grid == null || grid.Visible == false) { ICSharpCode.Core.LoggingService.Warn("设置背景色失败:网格控件为null或不可见"); return; } grid.ClearSelection(); //清空选中行 grid.ColumnHeadersHeight = 25; grid.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); grid.RowHeadersVisible = false; grid.RowTemplate.Height = 28; grid.RowsDefaultCellStyle.Font = new System.Drawing.Font("宋体", 12, System.Drawing.FontStyle.Bold); foreach (DataGridViewRow row in grid.Rows) { row.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(220, 220, 220); //灰色 } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("设置背景色失败:" + ex.Message); } } } #endregion } }