using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mesnac.Action.ChemicalWeighing.FinishBatch { /// /// 存盘业务辅助类 /// public class FinishBatchHelper { #region 冠合事件定义 public static object sender = null; public static event EventHandler GHOnUpdatePlanStateFromPlc; #endregion #region 小料称量数据存盘 /// /// 小料称量数据存盘 /// public static void SaveData() { try { #region 0 存盘信号读取 //if (BasePlcHelper.Instance.ChemicalWeighing_PLC_Request_Save_ShakeHand.NowValue.ToInt() == 1 && (BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack.NowValue.ToInt() <= 0 || BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack.NowValue.ToInt() == 65535 || BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack.NowValue.ToInt() == -1)) if (true) { #region 1 变量定义 int offset = 0; //保存偏移量 string reportHeadEquipRunName = String.Empty; //报表头基本信息设备变量别名 string materialEquipRunName = String.Empty; //物料信息设备变量别名 int[] reportHeaddataValue = null; //保存报表头基本信息设备变量别名的PLC数据 int[] materialDataValue = null; //保存物料信息设备变量别名的PLC数据 System.Reflection.PropertyInfo[] pis = null; //实体类型属性信息 Entity.LR_lot lRlotData = null; //报表头数据类 List lrWeighList = new List(); //物料数据类List #endregion #region 1.1 读取PlcSchema.xml中相应的存盘配置节,并根据配置节对应的设备变量别名读取PLC数据 List readerList = PlcSchemaHelper.Instance.GetCWSSPlcReader("WeighDataInfo"); if (readerList == null || readerList.Count == 0) { ICSharpCode.Core.LoggingService.Warn(String.Format("基础报表数据读取失败:PlcSchema.xml中没有对应WeighDataInfo的配置节!")); #region 保存异常,下传save=3 if (!BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 })) { BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 }); } #endregion return; } reportHeadEquipRunName = readerList[0].PlcFiledName; for (int i = 1; i < readerList.Count; i++) { if (reportHeadEquipRunName != readerList[i].PlcFiledName) { ICSharpCode.Core.LoggingService.Warn(String.Format("基础报表数据读取失败:PlcSchema.xml中相同配置节的equip={0}属性与第一个不相同!", readerList[i].PlcFiledName)); #region 保存异常,下传save=3 if (!BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 })) { BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 }); } #endregion return; } } List materialReaderList = PlcSchemaHelper.Instance.GetCWSSPlcReader("MaterialDataInfo"); if (materialReaderList == null || materialReaderList.Count == 0) { ICSharpCode.Core.LoggingService.Warn(String.Format("物料数据读取失败:PlcSchema.xml中没有对应MaterialDataInfo的配置节!")); #region 保存异常,下传save=3 if (!BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 })) { BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 }); } #endregion return; } materialEquipRunName = materialReaderList[0].PlcFiledName; for (int i = 1; i < materialReaderList.Count; i++) { if (materialEquipRunName != materialReaderList[i].PlcFiledName) { ICSharpCode.Core.LoggingService.Warn(String.Format("物料数据读取失败:PlcSchema.xml中相同配置节的equip={0}属性与第一个不相同!", readerList[i].PlcFiledName)); #region 保存异常,下传save=3 if (!BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 })) { BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 }); } #endregion return; } } #endregion #region 1.2 获取PLC品牌名称,处理西门子与其他PLC差异 foreach (PlcRecipeReader reader in readerList) { string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(reader.PlcFiledName); reader.EquipBrand = equipBrand; foreach (PlcRecipeReaderItem readerItem in reader.ItemList) { readerItem.EquipBrand = equipBrand; } } foreach (PlcRecipeReader materialReader in materialReaderList) { string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(materialReader.PlcFiledName); materialReader.EquipBrand = equipBrand; foreach (PlcRecipeReaderItem materialReaderItem in materialReader.ItemList) { materialReaderItem.EquipBrand = equipBrand; } } #endregion #region 2 存盘业务处理 #region 2.1 根据配置节对应的设备变量别名读取PLC基础报表存盘数据 if (!BasePlcHelper.Instance.PlcReadByRunName(reportHeadEquipRunName, out reportHeaddataValue)) { ICSharpCode.Core.LoggingService.Warn(String.Format("基础报表数据存盘失败:读取对应的PLC设备变量[{0}]数据失败!", reportHeadEquipRunName)); return; } else { ICSharpCode.Core.LoggingService.Debug(String.Format("基础报表数据存盘_读取PLC设备变量[{0}]成功,长度={1}", reportHeadEquipRunName, reportHeaddataValue.Length)); } #endregion #region 2.2 根据配置节对应的设备变量别名读取PLC物料存盘数据 if (!BasePlcHelper.Instance.PlcReadByRunName(materialEquipRunName, out materialDataValue)) { ICSharpCode.Core.LoggingService.Warn(String.Format("物料数据存盘失败:读取对应的PLC设备变量[{0}]数据失败!", materialEquipRunName)); return; } else { ICSharpCode.Core.LoggingService.Debug(String.Format("物料数据存盘_读取PLC设备变量[{0}]成功,长度={1}", materialEquipRunName, materialDataValue.Length)); } #endregion #region 2.3 把读取的PLC报表头基本信息数据进行实体映射处理并写入数据库 lRlotData = new Entity.LR_lot(); foreach (PlcRecipeReader reader in readerList) { ICSharpCode.Core.LoggingService.Debug(String.Format("targetTable={0}, runName={1}", reader.TargetTable, reader.PlcFiledName)); #region 3.1 数据处理 if (reader.TargetTable == "LR_lot") { if (reader.ThisCount != 1) { ICSharpCode.Core.LoggingService.Warn(String.Format("上辅机称量数据存盘失败:PlcSchema.xml中对应targetTable={0}的配置节count属性值不为1!", reader.TargetTable)); #region 保存异常,下传save=3 if (!BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 })) { BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 3 }); } #endregion return; } #region 3.2 配置项赋值 pis = typeof(Entity.LR_lot).GetProperties(); foreach (PlcRecipeReaderItem readerItem in reader.ItemList) { int[] value = new int[readerItem.ValueLen]; Array.Copy(reportHeaddataValue, offset, value, 0, value.Length); readerItem.SetValue = value; offset += value.Length; foreach (System.Reflection.PropertyInfo pi in pis) { if (pi.Name == readerItem.DataFieldName) { object objValue = null; try { if (readerItem.DataType.Equals("Int32", StringComparison.CurrentCultureIgnoreCase)) { if (readerItem.SetValue[0] > 32767) { readerItem.SetValue[0] = Math.Abs(readerItem.SetValue[0] - 65536); } if (readerItem.SetValue[1] > 32767) { readerItem.SetValue[1] = Math.Abs(readerItem.SetValue[1] - 65536); } } objValue = Mesnac.Basic.DataProcessor.ChangeType(readerItem.GetValue(), pi.PropertyType); if (readerItem.DataFieldName == "Prd_date") { objValue = DateTime.Now.ToString(); } } catch { objValue = readerItem.GetValue(); } pi.SetValue(lRlotData, objValue, null); } } } #endregion SaveHelper.LotSaveHelper.InsertLRLotData(lRlotData); break; } #endregion } ICSharpCode.Core.LoggingService.Debug("数据存盘_PLC报表基本数据进行实体映射处理完毕!"); #endregion #region 2.4 把读取的PLC物料信息数据进行实体映射处理并写入数据库 foreach (PlcRecipeReader reader in materialReaderList) { ICSharpCode.Core.LoggingService.Debug(String.Format("targetTable={0}, runName={1}", reader.TargetTable, reader.PlcFiledName)); if (reader.TargetTable == "LR_weigh") { offset = 0; pis = typeof(Entity.LR_weigh).GetProperties(); for (int i = 0; i < reader.ThisCount; i++) { Entity.LR_weigh tempLRweigh = new Entity.LR_weigh(); bool isCanAddFlag = false; foreach (PlcRecipeReaderItem readerItem in reader.ItemList) { int[] value = new int[readerItem.ValueLen]; Array.Copy(materialDataValue, offset, value, 0, value.Length); readerItem.SetValue = value; offset += value.Length; if (readerItem.PlcFiledName == "Read_BinNo" && readerItem.ValueLen == 1 && readerItem.SetValue[0] != 0) { isCanAddFlag = true; } } if (isCanAddFlag) { tempLRweigh.Dosing_id = 0; tempLRweigh.Plan_id = lRlotData.Plan_id; tempLRweigh.Equip_Code = lRlotData.Equip_Code; tempLRweigh.Serial_Num = lRlotData.Serial_Num; foreach (PlcRecipeReaderItem readerItem in reader.ItemList) { foreach (System.Reflection.PropertyInfo pi in pis) { if (pi.Name == readerItem.DataFieldName) { object objValue = null; try { objValue = Mesnac.Basic.DataProcessor.ChangeType(readerItem.GetValue(), pi.PropertyType); if (readerItem.DataFieldName == "Weight_Time") { objValue = DateTime.Now.ToString(); } } catch { objValue = readerItem.GetValue(); } pi.SetValue(tempLRweigh, objValue, null); } } } lrWeighList.Add(tempLRweigh); SaveHelper.LotSaveHelper.InsertLRweighData(tempLRweigh); } } } } ICSharpCode.Core.LoggingService.Debug("数据存盘_PLC物料数据写入数据库处理完毕!"); #endregion #region 2.5 反馈信号 bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Request_Save_FeedBack, new object[] { 1 }); if (!result) { ICSharpCode.Core.LoggingService.Debug("数据存盘后写入反馈信号失败!"); } #endregion #endregion } else { return; } #endregion } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("数据存盘异常:{1}", ex.Message), ex); } } #endregion #region 冠合存盘数据保存 /// /// 冠合存盘数据保存 /// public static void SaveGHData() { try { #region 存盘信号读取 if (BasePlcHelper.Instance.GH_PC_ConfirmSDReady.NowValue.ToInt() == 1) { Entity.RecordSaveDataInfo recordSaveDataInfo = new Entity.RecordSaveDataInfo(); bool PlanId = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SavePlanId, out int[] GH_PC_SavePlanId); bool SaveCode = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveCode, out int[] GH_PC_SaveCode); bool SaveFinishedNum = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveFinishedNum, out int[] GH_PC_SaveFinishedNum); bool SaveRow = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveRow, out int[] GH_PC_SaveRow); bool SaveCol = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveCol, out int[] GH_PC_SaveCol); bool SaveVCCSet = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveVCCSet, out int[] GH_PC_SaveVCCSet); bool SaveVCCAct = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveVCCAct, out int[] GH_PC_SaveVCCAct); bool SaveGFASet = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveGFASet, out int[] GH_PC_SaveGFASet); bool SaveGFAAct = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveGFAAct, out int[] GH_PC_SaveGFAAct); bool Save3thSet = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_Save3thSet, out int[] GH_PC_Save3thSet); bool Save3thAct = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_Save3thAct, out int[] GH_PC_Save3thAct); bool SaveLevel = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveLevel, out int[] GH_PC_SaveLevel); bool SaveFillTime = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveFillTime, out int[] GH_PC_SaveFillTime); bool SaveSpeed1 = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveSpeed1, out int[] GH_PC_SaveSpeed1); bool SaveSpeed2 = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveSpeed2, out int[] GH_PC_SaveSpeed2); bool SaveTime = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveTime, out int[] GH_PC_SaveTime); bool SaveVCC2thSet = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveVCC2thSet, out int[] GH_PC_SaveVCC2thSet); bool SaveVCC2thAct = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveVCC2thAct, out int[] GH_PC_SaveVCC2thAct); bool SaveGFA2thSet = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveGFA2thSet, out int[] GH_PC_SaveGFA2thSet); bool SaveGFA2thAct = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.GH_PC_SaveGFA2thAct, out int[] GH_PC_SaveGFA2thAct); recordSaveDataInfo.PlanId = Mesnac.Basic.DataProcessor.ToSiemensInt32(GH_PC_SavePlanId).ToString(); recordSaveDataInfo.SaveCode = GH_PC_SaveCode[0].ToString(); recordSaveDataInfo.SaveFinishedNum = GH_PC_SaveFinishedNum[0]; recordSaveDataInfo.SaveRow = GH_PC_SaveRow[0]; recordSaveDataInfo.SaveCol = GH_PC_SaveCol[0]; recordSaveDataInfo.SaveVCCSet = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveVCCSet); recordSaveDataInfo.SaveVCCAct = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveVCCAct); recordSaveDataInfo.SaveGFASet = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveGFASet); recordSaveDataInfo.SaveGFAAct = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveGFAAct); recordSaveDataInfo.Save3thSet = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_Save3thSet); recordSaveDataInfo.Save3thAct = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_Save3thAct); recordSaveDataInfo.SaveLevel = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveLevel); recordSaveDataInfo.SaveFillTime = GH_PC_SaveFillTime[0]; recordSaveDataInfo.SaveSpeed1 = GH_PC_SaveSpeed1[0]; recordSaveDataInfo.SaveSpeed2 = GH_PC_SaveSpeed2[0]; recordSaveDataInfo.SaveTime = GH_PC_SaveTime[0]; recordSaveDataInfo.RecordTime = DateTime.Now; recordSaveDataInfo.SaveVCC2thSet = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveVCC2thSet); recordSaveDataInfo.SaveVCC2thAct = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveVCC2thAct); recordSaveDataInfo.SaveGFA2thSet = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveGFA2thSet); recordSaveDataInfo.SaveGFA2thAct = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveGFA2thAct); //将实体保存至数据库 SaveHelper.LotSaveHelper.InsertGHSaveData(recordSaveDataInfo); #region 物料称量结果 try { List MaterInfolist = MaterialManage.MaterialHelper.getMaterialList(); Entity.LR_weigh tempLRweigh1 = new Entity.LR_weigh(); tempLRweigh1.Plan_id = Mesnac.Basic.DataProcessor.ToSiemensInt32(GH_PC_SavePlanId).ToString(); tempLRweigh1.Equip_Code = "01"; tempLRweigh1.Weight_ID = 1; tempLRweigh1.Serial_Num = GH_PC_SaveFinishedNum[0]; tempLRweigh1.Batch_Number = GH_PC_SaveCol[0].ToString(); //获取包含所有物料对象的集合 tempLRweigh1.Material_Code = MaterInfolist[0].materialId; tempLRweigh1.Real_Weight = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveVCCAct); tempLRweigh1.Real_Error = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveVCCSet) - tempLRweigh1.Real_Weight; tempLRweigh1.Waste_Time = GH_PC_SaveTime[0]; tempLRweigh1.Weight_Time = DateTime.Now.ToString(); tempLRweigh1.Recipe_code = GH_PC_SaveCode[0].ToString(); SaveHelper.LotSaveHelper.InsertLRweighData(tempLRweigh1); Entity.LR_weigh tempLRweigh2 = new Entity.LR_weigh(); tempLRweigh2.Plan_id = Mesnac.Basic.DataProcessor.ToSiemensInt32(GH_PC_SavePlanId).ToString(); tempLRweigh2.Equip_Code = "01"; tempLRweigh1.Weight_ID = 2; tempLRweigh2.Serial_Num = GH_PC_SaveFinishedNum[0]; tempLRweigh2.Batch_Number = GH_PC_SaveCol[0].ToString(); //获取包含所有物料对象的集合 tempLRweigh2.Material_Code = MaterInfolist[1].materialId; tempLRweigh2.Real_Weight = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveGFAAct); tempLRweigh2.Real_Error = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_SaveGFASet) - tempLRweigh1.Real_Weight; tempLRweigh2.Waste_Time = GH_PC_SaveTime[0]; tempLRweigh2.Weight_Time = DateTime.Now.ToString(); tempLRweigh2.Recipe_code = GH_PC_SaveCode[0].ToString(); SaveHelper.LotSaveHelper.InsertLRweighData(tempLRweigh2); Entity.LR_weigh tempLRweigh3 = new Entity.LR_weigh(); tempLRweigh3.Plan_id = Mesnac.Basic.DataProcessor.ToSiemensInt32(GH_PC_SavePlanId).ToString(); tempLRweigh3.Equip_Code = "01"; tempLRweigh1.Weight_ID = 3; tempLRweigh3.Serial_Num = GH_PC_SaveFinishedNum[0]; tempLRweigh3.Batch_Number = GH_PC_SaveCol[0].ToString(); //获取包含所有物料对象的集合 tempLRweigh3.Material_Code = MaterInfolist[2].materialId; tempLRweigh3.Real_Weight = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_Save3thAct); tempLRweigh3.Real_Error = Mesnac.Basic.DataProcessor.ToSiemensFloat(GH_PC_Save3thSet) - tempLRweigh1.Real_Weight; tempLRweigh3.Waste_Time = GH_PC_SaveTime[0]; tempLRweigh3.Weight_Time = DateTime.Now.ToString(); tempLRweigh3.Recipe_code = GH_PC_SaveCode[0].ToString(); SaveHelper.LotSaveHelper.InsertLRweighData(tempLRweigh3); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("物料称量存盘异常:{1}", ex.Message), ex); } #endregion //存盘数据保存完成,写入反馈信号 ICSharpCode.Core.LoggingService.Debug("数据存盘_PLC物料数据写入数据库处理完毕!"); bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_RpSaved, new object[] { 1 }); if (!result) { ICSharpCode.Core.LoggingService.Debug("数据存盘后写入反馈信号失败!"); } //将车次与计划表中的数量比对,更新计划数量中的完成数量 if (recordSaveDataInfo.PlanId != "") { Product.PptPlan.entity.Base_PlanInfo planInfo = Product.PptPlan.PlanHelper.getPlanInfoByPlanId(recordSaveDataInfo.PlanId); //不相等则更新计划中的完成数量 planInfo.real_Amount = recordSaveDataInfo.SaveFinishedNum * recordSaveDataInfo.SaveCol; planInfo.update_Time = DateTime.Now; if (planInfo.plan_Amount != recordSaveDataInfo.SaveFinishedNum * recordSaveDataInfo.SaveCol) { Product.PptPlan.PlanHelper.UpdatePlanInfo(planInfo); } else //本次计划完成 { planInfo.plan_endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); planInfo.plan_State = 2; Product.PptPlan.PlanHelper.UpdatePlanInfo(planInfo); } if (GHOnUpdatePlanStateFromPlc != null) { GHOnUpdatePlanStateFromPlc(sender, System.EventArgs.Empty); } } } #endregion } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("生产数据存盘异常:{1}", ex.Message), ex); } } #endregion } }