using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using DevExpress.Office.Utils; using DevExpress.XtraRichEdit.Import.OpenXml; using Mesnac.Action.Base; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.LjMixManager; using Mesnac.Action.ChemicalWeighing.LjPlanning.Db; using Mesnac.Action.ChemicalWeighing.LjPlanning.Entity; using Mesnac.Action.ChemicalWeighing.LjProdcutLine; using Mesnac.PlcUtils; namespace Mesnac.Action.ChemicalWeighing.LjPlanning { public class DownloadAction : ChemicalWeighingAction, IAction { private DbMCControl _materialGridControl = null; //物料列表控件 private RuntimeParameter _runtime; public static event EventHandler OnDown; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); //必须调用 this._runtime = runtime; DbMCControl materialGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "lj_planning") .FirstOrDefault(); //获取物料数据控件 this._materialGridControl = materialGridControl; DataGridView clientGridView = this._materialGridControl.BaseControl as DataGridView; //验证是否选中某物料 if (clientGridView.SelectedRows.Count != 1) { MessageBox.Show("请选择要下发的数据", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); this._runtime.IsReturn = true; return; } var dataGridViewRow = clientGridView.SelectedRows[0]; var id = Convert.ToInt32(dataGridViewRow.Cells["Id"].Value); var ljPlanningView = LjPlanningDb.GetById(id); LoadingHelper.ShowLoadingScreen(); var watchDog = LjHelp.WatchDog; if (watchDog == 0) { LoadingHelper.CloseForm(); MessageBox.Show("PLC通讯失败"); return; } if (ljPlanningView != null) { if (ljPlanningView.Status !="0") { MessageBox.Show("状态不是新建 禁止下发"); return; } //List recipePlcViews = new List(); //List recipePlcViews2 = new List(); //List recipeSteps = new List(); //List mixSteps = new List(); //配方名称 var formulaId = ljPlanningView.FormulaId; var unit = ljPlanningView.Unit; var plc = BasePlcHelper.Instance; var formulDetail = LjFormulaDb.GetDetail(formulaId); Clean(unit); //单元 switch (unit) { case 1: //单元1 干混1 糊化1 2 湿混 1 2 var dataDry1 = GetUnitDryView(formulDetail); LjPlanningPlcHelp.DownD1Recipe(dataDry1.H1, dataDry1.R1); if (dataDry1.H1 != null && dataDry1.H1.Count>0) { WriteEnbale(plc.EnableDryD1); } else { WriteCanel(plc.EnableDryD1); } var dataSilo1A = GetUnitSiloView(formulDetail, "A"); LjPlanningPlcHelp.DownG1Recipe(dataSilo1A.H1, dataSilo1A.H2, dataSilo1A.R1); if (dataSilo1A.H1 != null && dataSilo1A.H1.Count > 0) { WriteEnbale(plc.EnableDryG1); } else { WriteCanel(plc.EnableDryG1); } var dataSilo1B = GetUnitSiloView(formulDetail, "B"); LjPlanningPlcHelp.DownG2Recipe(dataSilo1B.H1, dataSilo1B.H2, dataSilo1B.R1); if (dataSilo1B.H1 != null && dataSilo1B.H1.Count > 0) { WriteEnbale(plc.EnableDryG2); } else { WriteCanel(plc.EnableDryG2); } var dataWeter1A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM1Recipe(dataWeter1A.R2); if (dataWeter1A.R2 != null && dataWeter1A.R2.Count>0) { WriteEnbale(plc.EnableDryM1); } else { WriteCanel(plc.EnableDryM1); } var dataWeter1B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM2Recipe(dataWeter1B.R2); if (dataWeter1B.R2 != null && dataWeter1B.R2.Count > 0) { WriteEnbale(plc.EnableDryM2); } else { WriteCanel(plc.EnableDryM2); } plc.PlcWriteByDataKey(plc.Plan0_RecipeCode, new object[] { formulaId }); plc.PlcWriteByDataKey(plc.Plan0_PlanCode, new object[] { ljPlanningView.Id }); plc.PlcWriteByDataKey(plc.Plan0_Batch, new object[] { ljPlanningView.NumCar }); plc.PlcWriteByDataKey(plc.Plan0_Start, new object[] { 1 }); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); break; case 2: //单元2 干混2 糊化3 4 湿混 3 4 var dataDry2 = GetUnitDryView(formulDetail); LjPlanningPlcHelp.DownD2Recipe(dataDry2.H1, dataDry2.R1); if (dataDry2.H1 != null) { WriteEnbale(plc.EnableDryD2); } else { WriteCanel(plc.EnableDryD2); } var dataSilo2A = GetUnitSiloView(formulDetail, "A"); LjPlanningPlcHelp.DownG3Recipe(dataSilo2A.H1, dataSilo2A.H2, dataSilo2A.R1); if (dataSilo2A.H1 != null) { WriteEnbale(plc.EnableDryG3); } else { WriteCanel(plc.EnableDryG3); } var dataSilo2B = GetUnitSiloView(formulDetail, "B"); LjPlanningPlcHelp.DownG4Recipe(dataSilo2B.H1, dataSilo2B.H2, dataSilo2B.R1); if (dataSilo2B.H1 != null) { WriteEnbale(plc.EnableDryG4); } else { WriteCanel(plc.EnableDryG4); } var dataWeter2A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM3Recipe(dataWeter2A.R2); if (dataWeter2A.R2 != null) { WriteEnbale(plc.EnableDryM3); } var dataWeter2B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM4Recipe(dataWeter2B.R2); if (dataWeter2B.R2 != null) { WriteEnbale(plc.EnableDryM4); } plc.PlcWriteByDataKey(plc.Plan1_RecipeCode, new object[] { formulaId }); plc.PlcWriteByDataKey(plc.Plan1_RecipeCode, new object[] { ljPlanningView.Id }); plc.PlcWriteByDataKey(plc.Plan1_Batch, new object[] { ljPlanningView.NumCar }); plc.PlcWriteByDataKey(plc.Plan1_Start, new object[] { 1 }); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); break; case 3: //单元3 干混3 糊化5 6 湿混 5 6 var dataDry3 = GetUnitDryView(formulDetail); LjPlanningPlcHelp.DownD3Recipe(dataDry3.H1, dataDry3.R1); if (dataDry3.H1 != null) { WriteEnbale(plc.EnableDryD3); } var dataSilo3A = GetUnitSiloView(formulDetail, "A"); LjPlanningPlcHelp.DownG5Recipe(dataSilo3A.H1, dataSilo3A.H2, dataSilo3A.R1); if (dataSilo3A.H1 != null) { WriteEnbale(plc.EnableDryG5); } var dataSilo3B = GetUnitSiloView(formulDetail, "B"); LjPlanningPlcHelp.DownG6Recipe(dataSilo3B.H1, dataSilo3B.H2, dataSilo3B.R1); if (dataSilo3B.H1 != null) { WriteEnbale(plc.EnableDryG6); } var dataWeter3A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM5Recipe(dataWeter3A.R2); if (dataWeter3A.R2 != null) { WriteEnbale(plc.EnableDryM5); } var dataWeter3B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM6Recipe(dataWeter3B.R2); if (dataWeter3B.R2 != null) { WriteEnbale(plc.EnableDryM6); } plc.PlcWriteByDataKey(plc.Plan2_RecipeCode, new object[] { formulaId }); plc.PlcWriteByDataKey(plc.Plan2_RecipeCode, new object[] { ljPlanningView.Id }); plc.PlcWriteByDataKey(plc.Plan2_Batch, new object[] { ljPlanningView.NumCar }); plc.PlcWriteByDataKey(plc.Plan2_Start, new object[] { 1 }); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); break; case 4: //单元4 干混4 糊化7 8 湿混 7 8 var dataDry4 = GetUnitDryView(formulDetail); LjPlanningPlcHelp.DownD4Recipe(dataDry4.H1, dataDry4.R1); if (dataDry4.H1 != null) { WriteEnbale(plc.EnableDryD4); } var dataSilo4A = GetUnitSiloView(formulDetail, "A"); LjPlanningPlcHelp.DownG7Recipe(dataSilo4A.H1, dataSilo4A.H2, dataSilo4A.R1); if (dataSilo4A.H1 != null) { WriteEnbale(plc.EnableDryG7); } var dataSilo4B = GetUnitSiloView(formulDetail, "B"); LjPlanningPlcHelp.DownG8Recipe(dataSilo4B.H1, dataSilo4B.H2, dataSilo4B.R1); if (dataSilo4B.H1 != null) { WriteEnbale(plc.EnableDryG8); } var dataWeter4A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM7Recipe(dataWeter4A.R2); if (dataWeter4A.R2 != null) { WriteEnbale(plc.EnableDryM7); } var dataWeter4B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM8Recipe(dataWeter4B.R2); if (dataWeter4B.R2 != null) { WriteEnbale(plc.EnableDryM8); } plc.PlcWriteByDataKey(plc.Plan3_RecipeCode, new object[] { formulaId }); plc.PlcWriteByDataKey(plc.Plan3_RecipeCode, new object[] { ljPlanningView.Id }); plc.PlcWriteByDataKey(plc.Plan3_Batch, new object[] { ljPlanningView.NumCar }); plc.PlcWriteByDataKey(plc.Plan3_Start, new object[] { 1 }); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); break; } } else { MessageBox.Show("数据下发失败"); } LoadingHelper.CloseForm(); MessageBox.Show("数据下发成功"); if (OnDown != null) { OnDown(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty); } } public void WriteEnbale(DataKeyValue key) { var plc = BasePlcHelper.Instance; bool b= plc.PlcWriteByDataKey(key, new object[] { 1 }); } public void WriteCanel(DataKeyValue key) { var plc = BasePlcHelper.Instance; plc.PlcWriteByDataKey(key, new object[] {0}); } public UnitView GetUnitDryView(List formulDetail) { UnitView unitView = new UnitView(); //获取干混机1 var dataDry1C = formulDetail.Where(x => x.MachineType == "Dry" && x.FormulaType == 1 && x.Machine == "A").ToList(); var dataDry1CPlcView = dataDry1C.Select(x => new RecipePlcView() { Bin = x.BinNo, Set = x.Weight, TolErance = x.Tolerance }).ToList(); unitView.H1 = dataDry1CPlcView; var dataDry1H = formulDetail.Where(x => x.MachineType == "Dry" && x.FormulaType == 2 && x.Machine == "A").ToList(); var dataDry1HPlcView = dataDry1H.Select(x => new Step() { MixCode = x.ActionCode, MixSpeed = x.Speed, MixTemp = x.Temp, MixTime = x.TimeInfo }).ToList(); unitView.R1 = dataDry1HPlcView; return unitView; } public UnitView GetUnitSiloView(List formulDetail, string machine) { UnitView unitView = new UnitView(); // 糊化1 水 称量 称量 var dataSilo1C = formulDetail.Where(x => x.MachineType == "Silo" && x.FormulaType == 1 && x.Machine == machine).ToList(); var dataSilo1CPlcView = dataSilo1C.Select(x => new RecipePlcView() { Bin = x.BinNo, Set = x.Weight, TolErance = x.Tolerance }).ToList(); unitView.H1 = dataSilo1CPlcView; var dataSilo1CWater = formulDetail.Where(x => x.MachineType == "Silo_Water" && x.FormulaType == 1 && x.Machine == machine).ToList(); var dataSilo1CWaterPlcView = dataSilo1CWater.Select(x => new RecipePlcView() { Bin = x.BinNo, Set = x.Weight, TolErance = x.Tolerance }).ToList(); unitView.H2 = dataSilo1CWaterPlcView; var dataSilo1H = formulDetail.Where(x => x.MachineType == "Silo" && x.FormulaType == 2 && x.Machine == machine).ToList(); var dataSilo1HPlcView = dataSilo1H.Select(x => new Step() { MixCode = x.ActionCode, MixSpeed = x.Speed, MixTemp = x.Temp, MixTime = x.TimeInfo }).ToList(); unitView.R1 = dataSilo1HPlcView; return unitView; } public UnitView GetUnitWeterView(List formulDetail, string machine) { //单元1 干混1 糊化1 2 湿混 1 2 UnitView unitView = new UnitView(); // 糊化2 水 称量 称量 var dataSilo2C = formulDetail.Where(x => x.MachineType == "Weter" && x.FormulaType == 1 && x.Machine == machine).ToList(); var dataSilo2CPlcView = dataSilo2C.Select(x => new RecipePlcView() { Bin = x.BinNo, Set = x.Weight, TolErance = x.Tolerance }).ToList(); unitView.H1 = dataSilo2CPlcView; var dataSilo2H = formulDetail.Where(x => x.MachineType == "Weter" && x.FormulaType == 2 && x.Machine == machine).ToList(); var dataSilo2HPlcView = dataSilo2H.Select(x => new MixStep() { MixCode = x.ActionCode, MixSpeed = x.Speed, MixTemp = x.Temp, MixTime = x.TimeInfo, SetValue = x.SetValue, SetTolerance = x.SetTolerance }).ToList(); unitView.R2 = dataSilo2HPlcView; return unitView; } public void Clean(int unit) { List H1 = new List(); List< Step > sto = new List< Step >(); List < MixStep > mixSteps=new List (); for (int i = 0; i < 12; i++) { H1.Add(new RecipePlcView() { Bin = 0, Set = 0, TolErance = 0 }); sto.Add(new Step() { MixCode = 0, MixSpeed = 0, MixTemp = 0, MixTime = 0 }); mixSteps.Add(new MixStep() { MixCode = 0, MixSpeed = 0, MixTemp = 0, MixTime = 0, SetTolerance = 0, SetValue = 0 }); } if(unit == 1) { LjPlanningPlcHelp.DownD1Recipe(H1, sto); LjPlanningPlcHelp.DownG1Recipe(H1, H1, sto); LjPlanningPlcHelp.DownG2Recipe(H1, H1, sto); LjPlanningPlcHelp.DownM1Recipe(mixSteps); LjPlanningPlcHelp.DownM2Recipe(mixSteps); } if (unit == 2) { LjPlanningPlcHelp.DownD2Recipe(H1, sto); LjPlanningPlcHelp.DownG3Recipe(H1, H1, sto); LjPlanningPlcHelp.DownG4Recipe(H1, H1, sto); LjPlanningPlcHelp.DownM3Recipe(mixSteps); LjPlanningPlcHelp.DownM4Recipe(mixSteps); } if (unit == 3) { LjPlanningPlcHelp.DownD3Recipe(H1, sto); LjPlanningPlcHelp.DownG5Recipe(H1, H1, sto); LjPlanningPlcHelp.DownG6Recipe(H1, H1, sto); LjPlanningPlcHelp.DownM5Recipe(mixSteps); LjPlanningPlcHelp.DownM6Recipe(mixSteps); } if (unit == 4) { LjPlanningPlcHelp.DownD4Recipe(H1, sto); LjPlanningPlcHelp.DownG7Recipe(H1, H1, sto); LjPlanningPlcHelp.DownG8Recipe(H1, H1, sto); LjPlanningPlcHelp.DownM7Recipe(mixSteps); LjPlanningPlcHelp.DownM8Recipe(mixSteps); } } } }