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 DataBlockHelper.DBHelpers; using DataBlockHelper; 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; _materialGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "lj_planning").FirstOrDefault(); //获取物料数据控件 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); if (MessageBox.Show("确定下传配方?", "下传确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } LoadingHelper.ShowLoadingScreen(); //int unit = Convert.ToInt32(dataGridViewRow.Cells["Unit"].Value); //DB2105Helper dB2105 = new DB2105Helper(); //bool isUnitOpen = false; //switch (unit) //{ // case 1: // isUnitOpen = dB2105.Status[0]; // break; // case 2: // isUnitOpen = dB2105.Status[1]; // break; // case 3: // isUnitOpen = dB2105.Status[2]; // break; // case 4: // isUnitOpen = dB2105.Status[3]; // break; // default: // MessageBox.Show("产线编号不正确!"); // LoadingHelper.CloseForm(); // return; //} //if (isUnitOpen == false) //{ // MessageBox.Show("产线未启动,无法下发!"); // LoadingHelper.CloseForm(); // return; //} 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 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) { PlcConnect.Instance.Write($"DB2104.53.6",true); } else { PlcConnect.Instance.Write($"DB2104.53.6", false); } var dataSilo1A = GetUnitSiloView(formulDetail, "A"); //1#糊化机使能 if (dataSilo1A.H2 != null && dataSilo1A.H2.Count > 0) { PlcConnect.Instance.Write($"DB2104.54.2", true); } else { PlcConnect.Instance.Write($"DB2104.54.2", false); } var dataSilo1B = GetUnitSiloView(formulDetail, "B"); // LjPlanningPlcHelp.DownG2Recipe(dataSilo1B.H1, dataSilo1B.H2, dataSilo1B.R1); //2#糊化机使能 if (dataSilo1B.H2 != null && dataSilo1B.H2.Count > 0) { PlcConnect.Instance.Write($"DB2104.54.3", true); } else { PlcConnect.Instance.Write($"DB2104.54.3", false); } // 1#湿混机使能 var dataWeter1A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM1Recipe(dataWeter1A.R2); if (dataWeter1A.R2 != null && dataWeter1A.R2.Count>0) { PlcConnect.Instance.Write($"DB2104.55.2", true); } else { PlcConnect.Instance.Write($"DB2104.55.2", false); } var dataWeter1B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM2Recipe(dataWeter1B.R2); if (dataWeter1B.R2 != null && dataWeter1B.R2.Count > 0) { PlcConnect.Instance.Write($"DB2104.55.3", true); } else { PlcConnect.Instance.Write($"DB2104.55.3", false); } PlcConnect.Instance.Write($"DB2105.0.0", Convert.ToUInt16(formulaId)); PlcConnect.Instance.Write($"DB2105.2.0", Convert.ToUInt16(ljPlanningView.Id)); PlcConnect.Instance.Write($"DB2105.4.0", Convert.ToUInt16(ljPlanningView.NumCar)); var sss= PlcConnect.Instance.Write($"DB2105.6.0", Convert.ToUInt16(1)); PlcConnect.Instance.Write($"DB2105.8.0", Convert.ToUInt16(0)); PlcConnect.Instance.Write($"DB2105.10.0", Convert.ToUInt16(0)); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); LjPlanningDb.UpdateBeginTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 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 && dataDry2.H1.Count>0) { PlcConnect.Instance.Write($"DB2104.53.7", true); } else { PlcConnect.Instance.Write($"DB2104.53.7", false); } //3#糊化机使能 var dataSilo2A = GetUnitSiloView(formulDetail, "A"); // LjPlanningPlcHelp.DownG3Recipe(dataSilo2A.H1, dataSilo2A.H2, dataSilo2A.R1); if (dataSilo2A.H1 != null && dataSilo2A.H1.Count>0) { PlcConnect.Instance.Write($"DB2104.54.4", true); } else { PlcConnect.Instance.Write($"DB2104.54.4", false); } var dataSilo2B = GetUnitSiloView(formulDetail, "B"); //4#糊化机使能 // LjPlanningPlcHelp.DownG4Recipe(dataSilo2B.H1, dataSilo2B.H2, dataSilo2B.R1); if (dataSilo2B.H1 != null && dataSilo2B.H1.Count>0) { PlcConnect.Instance.Write($"DB2104.54.5", true); } else { PlcConnect.Instance.Write($"DB2104.54.5", false); } var dataWeter2A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM3Recipe(dataWeter2A.R2); if (dataWeter2A.R2 != null && dataWeter2A.R2.Count>0) { PlcConnect.Instance.Write($"DB2104.55.4", true); } else { PlcConnect.Instance.Write($"DB2104.55.4", false); } var dataWeter2B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM4Recipe(dataWeter2B.R2); if (dataWeter2B.R2 != null && dataWeter2B.R2.Count > 0) { PlcConnect.Instance.Write($"DB2104.55.5", true); } else { PlcConnect.Instance.Write($"DB2104.55.5", false); } PlcConnect.Instance.Write($"DB2105.12.0", Convert.ToUInt16(formulaId)); PlcConnect.Instance.Write($"DB2105.14.0", Convert.ToUInt16(ljPlanningView.Id)); PlcConnect.Instance.Write($"DB2105.16.0", Convert.ToUInt16(ljPlanningView.NumCar)); PlcConnect.Instance.Write($"DB2105.18.0", Convert.ToUInt16(1)); PlcConnect.Instance.Write($"DB2105.20.0", Convert.ToUInt16(0)); PlcConnect.Instance.Write($"DB2105.22.0", Convert.ToUInt16(0)); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); LjPlanningDb.UpdateBeginTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 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 && dataDry3.H1.Count>0) { PlcConnect.Instance.Write($"DB2104.54.0", true); } else { PlcConnect.Instance.Write($"DB2104.54.0", false); } //5#糊化机使能 var dataSilo3A = GetUnitSiloView(formulDetail, "A"); LjPlanningPlcHelp.DownG5Recipe(dataSilo3A.H1, dataSilo3A.R1); if (dataSilo3A.H1 != null && dataSilo3A.H1.Count > 0) { PlcConnect.Instance.Write($"DB2104.54.6", true); } else { PlcConnect.Instance.Write($"DB2104.54.6", false); } //6#糊化机使能 var dataSilo3B = GetUnitSiloView(formulDetail, "B"); LjPlanningPlcHelp.DownG6Recipe(dataSilo3B.H1, dataSilo3B.R1); if (dataSilo3B.H1 != null && dataSilo3B.H1.Count > 0) { PlcConnect.Instance.Write($"DB2104.54.7", true); } else { PlcConnect.Instance.Write($"DB2104.54.7", false); } var dataWeter3A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM5Recipe(dataWeter3A.R2); if (dataWeter3A.R2 != null && dataWeter3A.R2.Count>0) { PlcConnect.Instance.Write($"DB2104.55.6", true); } else { PlcConnect.Instance.Write($"DB2104.55.6", false); } var dataWeter3B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM6Recipe(dataWeter3B.R2); if (dataWeter3B.R2 != null && dataWeter3B.R2.Count>0) { PlcConnect.Instance.Write($"DB2104.55.7", true); } else { PlcConnect.Instance.Write($"DB2104.55.7", false); } PlcConnect.Instance.Write($"DB2105.24.0", Convert.ToUInt16(formulaId)); PlcConnect.Instance.Write($"DB2105.26.0", Convert.ToUInt16(ljPlanningView.Id)); PlcConnect.Instance.Write($"DB2105.28.0", Convert.ToUInt16(ljPlanningView.NumCar)); PlcConnect.Instance.Write($"DB2105.30.0", Convert.ToUInt16(1)); PlcConnect.Instance.Write($"DB2105.32.0", Convert.ToUInt16(0)); PlcConnect.Instance.Write($"DB2105.34.0", Convert.ToUInt16(0)); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); LjPlanningDb.UpdateBeginTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 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 && dataDry4.H1.Count>0) { PlcConnect.Instance.Write($"DB2104.54.1", true); } else { PlcConnect.Instance.Write($"DB2104.54.1", false); } var dataSilo4A = GetUnitSiloView(formulDetail, "A"); LjPlanningPlcHelp.DownG7Recipe(dataSilo4A.H1, dataSilo4A.R1); if (dataSilo4A.H1 != null && dataSilo4A.H1.Count > 0) { PlcConnect.Instance.Write($"DB2104.55.0", true); } else { PlcConnect.Instance.Write($"DB2104.55.0", false); } // 8#糊化机使能 var dataSilo4B = GetUnitSiloView(formulDetail, "B"); LjPlanningPlcHelp.DownG8Recipe(dataSilo4B.H1, dataSilo4B.R1); if (dataSilo4B.H1 != null && dataSilo4B.H1.Count > 0) { PlcConnect.Instance.Write($"DB2104.55.1", true); } else { PlcConnect.Instance.Write($"DB2104.55.1", false); } var dataWeter4A = GetUnitWeterView(formulDetail, "A"); LjPlanningPlcHelp.DownM7Recipe(dataWeter4A.R2); if (dataWeter4A.R2 != null && dataWeter4A.R2.Count>0) { PlcConnect.Instance.Write($"DB2104.56.0", true); } else { PlcConnect.Instance.Write($"DB2104.56.0", false); } var dataWeter4B = GetUnitWeterView(formulDetail, "B"); LjPlanningPlcHelp.DownM8Recipe(dataWeter4B.R2); if (dataWeter4B.R2 != null && dataWeter4B.R2.Count>0) { PlcConnect.Instance.Write($"DB2104.56.1", true); } else { PlcConnect.Instance.Write($"DB2104.56.1", false); } PlcConnect.Instance.Write($"DB2105.36.0", Convert.ToUInt16(formulaId)); PlcConnect.Instance.Write($"DB2105.38.0", Convert.ToUInt16(ljPlanningView.Id)); PlcConnect.Instance.Write($"DB2105.40.0", Convert.ToUInt16(ljPlanningView.NumCar)); PlcConnect.Instance.Write($"DB2105.42.0", Convert.ToUInt16(1)); PlcConnect.Instance.Write($"DB2105.44.0", Convert.ToUInt16(0)); PlcConnect.Instance.Write($"DB2105.46.0", Convert.ToUInt16(0)); //把状态变成执行 LjPlanningDb.UpdateStatus(1, ljPlanningView.Id); LjPlanningDb.UpdateBeginTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ljPlanningView.Id); break; } DownToDatabase downToDatabase = new DownToDatabase(ljPlanningView.Id); } 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 List GetRecipePlcViews(int num) { List H1 = new List(); for (int i = 0; i < num; i++) { H1.Add(new RecipePlcView() { Bin = 0, Set = 0, Tolerance = 0 }); } return H1; } public List GetStep(int num) { List< Step > sto = new List< Step >(); for (int i = 0; i < num; i++) { sto.Add(new Step() { MixCode = 0, MixSpeed = 0, MixTemp = 0, MixTime = 0 }); } return sto; } public List GetMixStep(int num) { List< MixStep > sto = new List< MixStep >(); for (int i = 0; i < num; i++) { sto.Add(new MixStep() { MixCode = 0, MixSpeed = 0, MixTemp = 0, MixTime = 0 }); } return sto; } private void Clean(int unit) { if(unit == 1) { LjPlanningPlcHelp.DownD1Recipe(GetRecipePlcViews(4), GetStep(10)); LjPlanningPlcHelp.DownG1Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownG2Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownM1Recipe(GetMixStep(10)); LjPlanningPlcHelp.DownM2Recipe(GetMixStep(10)); } if (unit == 2) { LjPlanningPlcHelp.DownD2Recipe(GetRecipePlcViews(4), GetStep(10)); LjPlanningPlcHelp.DownG3Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownG4Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownM3Recipe(GetMixStep(10)); LjPlanningPlcHelp.DownM4Recipe(GetMixStep(10)); } if (unit == 3) { LjPlanningPlcHelp.DownD3Recipe(GetRecipePlcViews(4), GetStep(10)); LjPlanningPlcHelp.DownG5Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownG6Recipe(GetRecipePlcViews(2),GetStep(10)); LjPlanningPlcHelp.DownM5Recipe(GetMixStep(10)); LjPlanningPlcHelp.DownM6Recipe(GetMixStep(10)); } if (unit == 4) { LjPlanningPlcHelp.DownD4Recipe(GetRecipePlcViews(4), GetStep(10)); LjPlanningPlcHelp.DownG7Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownG8Recipe(GetRecipePlcViews(2), GetStep(10)); LjPlanningPlcHelp.DownM7Recipe(GetMixStep(10)); LjPlanningPlcHelp.DownM8Recipe(GetMixStep(10)); } } } }