using DevExpress.Office.Drawing; using DevExpress.Office.Utils; 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 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; #endregion 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号地磅 System.Timers.Timer timer = new System.Timers.Timer(3000); timer.Elapsed += new ElapsedEventHandler(StartServer1); timer.AutoReset = true; timer.Enabled = true; timer.Start(); //IsFirstRun = false; ////2号地磅 System.Timers.Timer timer2 = new System.Timers.Timer(3000); 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; } var lblPlanName = base.GetControlById("lblPlanName") as System.Windows.Forms.Label;//配方名称 if (lblPlanName == null) { 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;//计划状态 DataTable table = PlanHelper.GetProPlan();//正在执行的任务 if (table == null) { lblStateMessage.Text = "状态信息"; lblMaterial.Text = "物料"; lblPlanName.Text = "计划号"; lblRecipeName.Text = "配方"; clientGridControl.BaseControl.BindDataSource = null; return; } int startBarCode = BasePlcHelper.Instance.solvent_status.NowValue.ToInt(); if (startBarCode == 1) { lblStateMessage.Text = "请扫描拉缸条码,核对投料类型!"; } int status04 = BasePlcHelper.Instance.solvent_status04.NowValue.ToInt(); if (status04 == 1) { lblStateMessage.Text = "请清空地磅,扫描之后放物料!"; } int status01 = BasePlcHelper.Instance.solvent_status01.NowValue.ToInt(); int setr = BasePlcHelper.Instance.solvent_Set_R.NowValue.ToInt(); if (status01 == 1 && setr == 250) { lblStateMessage.Text = "请加料!"; } int addmateral = BasePlcHelper.Instance.solvent_Set_R.NowValue.ToInt(); if (addmateral == 1) { lblStateMessage.Text = "称量完成!"; Thread.Sleep(5000); int runState = BasePlcHelper.Instance.solvent_SystemRuning.NowValue.ToInt(); if (runState == 1) { lblStateMessage.Text = "请扫描拉缸条码,核对投料类型!"; } //else //{ // if (status01 == 0 && setr == 0) // { // lblStateMessage.Text = ""; // } // lblStateMessage.Text = ""; //} } int status = BasePlcHelper.Instance.solvent_SystemRuning.NowValue.ToInt(); if (status01 == 0 && status04 == 0 && status != 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; } //if (plan2 != null) {lblPlanStateMessage.Text = plan2.Plan_StateText; } var matSerial = plan2.MID; //if (matSerial > 1&& matSerial != materialList.Count()) //{ // matSerial = matSerial - 1; //} MetagePlan plan = materialList.FirstOrDefault(d => d.MID == matSerial); if (plan2 != null) { lblPlanStateMessage.Text = plan2.Plan_StateText; } 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"); } else { return; } if (!string.IsNullOrEmpty(planFlag)) { if (planFlag == plan.Plan_Id) { return; } else { planFlag = plan.Plan_Id; } } else { planFlag = plan.Plan_Id; } 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..."); } } } private void StartServer2(object sender, ElapsedEventArgs e)//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) { 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;//计划状态 DataTable table = PlanHelper.GetProWeighbridge2Plan();//正在执行的任务 if (table == null) { lblStateMessage.Text = "状态信息"; lblMaterial.Text = "物料"; lblPlanName.Text = "计划号"; lblRecipeName.Text = "配方"; clientGridControl.BaseControl.BindDataSource = null; return; } int startBarCode = BasePlcHelper.Instance.solvent2_status.NowValue.ToInt(); if (startBarCode == 1) { lblStateMessage.Text = "请扫描拉缸条码,核对投料类型!"; } int status04 = BasePlcHelper.Instance.solvent2_status04.NowValue.ToInt(); if (status04 == 1) { lblStateMessage.Text = "请清空地磅,扫描之后放物料!"; } int status01 = BasePlcHelper.Instance.solvent2_status01.NowValue.ToInt(); int setr = BasePlcHelper.Instance.solvent2_Set_R.NowValue.ToInt(); if (status01 == 1 && setr == 250) { lblStateMessage.Text = "请加料!"; } int addmateral = BasePlcHelper.Instance.solvent2_Set_R.NowValue.ToInt(); if (addmateral == 1) { lblStateMessage.Text = "称量完成!"; Thread.Sleep(5000); int runState = BasePlcHelper.Instance.solvent2_SystemRuning.NowValue.ToInt(); if (runState == 1) { lblStateMessage.Text = "请扫描拉缸条码,核对投料类型!"; } else { //if (status01 == 0 && setr == 0) //{ // lblStateMessage.Text = ""; //} //lblStateMessage.Text = ""; } } int status = BasePlcHelper.Instance.solvent2_SystemRuning.NowValue.ToInt(); if (status01 == 0 && status04 == 0 && status != 1) { //lblStateMessage.Text = ""; } List materialList = PlanHelper.DataTableToList(table); if (materialList.Count == 0) { return; } MetagePlan plan2 = null; var matlCode = BasePlcHelper.Instance.solvent2_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; } if (plan2 != null) { var p = materialList.FirstOrDefault(); lblPlanName.Text = p.Plan_Id; lblRecipeName.Text = p.Recipe_Name; lblMaterial.Text = plan2.Material_name;//正在执行的物料号 lblStandardWeight.Text = plan2.Total_Weight.ToString("f2"); } else { return; } if (!string.IsNullOrEmpty(planFlag)) { if (planFlag == plan2.Plan_Id) { return; } else { planFlag = plan2.Plan_Id; } } else { planFlag = plan2.Plan_Id; } 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 } }