using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mesnac.Action.ChemicalWeighing.FinishBatch { /// /// 存盘业务辅助类 /// public class FinishBatchHelper { #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)) { #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 } }