using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.IO; namespace Mesnac.Action.ChemicalWeighing { /// /// PLCSchema.xml配置文件解析辅助类 /// public class PlcSchemaHelper { #region 单例实现 private static PlcSchemaHelper _instance = null; private PlcSchemaHelper() { } /// /// PLCSchema.xml配置文件解析类 /// public static PlcSchemaHelper Instance { get { if (_instance == null) { _instance = new PlcSchemaHelper(); } return _instance; } } #endregion #region 获取PlcSchema.xml文件路径 /// /// 获取PLCSchema.xml文件路径 /// /// private string getPlcSchemaPath() { string path = System.Windows.Forms.Application.StartupPath; return Path.Combine(path, "Data\\MCProject\\PlcSchema.xml"); } #endregion #region 读取PLCSchema.xml配置文件 #region 解析XML元素的属性值 /// /// 解析XML元素的属性值 /// /// XML元素节点 /// 要获取值的属性名称 /// 默认值 /// 解析成功返回对应的属性值,解析失败返回默认值 - String类型属性 private string ToValue(XmlNode node, string key, string defaultValue) { foreach (XmlAttribute a in node.Attributes) { if (a.Name.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { return a.Value; } } return defaultValue; } /// /// 解析XML元素的属性值(重载方法) /// /// XML元素节点 /// 要获取值的属性名称 /// 默认值 /// 解析成功返回对应的属性值,解析失败返回默认值 - Int类型属性 private int ToValue(XmlNode node, string key, int defaultValue) { int Result = 0; string value = ToValue(node, key, string.Empty); if (!string.IsNullOrWhiteSpace(value) && int.TryParse(value, out Result)) { return Result; } return defaultValue; } /// /// 解析XML元素的属性值(重载方法) /// /// XML元素节点 /// 要获取值的属性名称 /// 默认值 /// 解析成功返回对应的属性值,解析失败返回默认值 - bool类型属性 private bool ToValue(XmlNode node, string key, bool defaultValue) { bool Result = false; string value = ToValue(node, key, string.Empty); if (!string.IsNullOrWhiteSpace(value) && bool.TryParse(value, out Result)) { return Result; } return defaultValue; } #endregion #region 解析PlCSchema.xml文件中的存盘项 /// /// 解析PlCSchema.xml文件中的存盘项 /// /// 元素名 - 对应存盘项节点 /// 返回存盘数据项集合 public List GetCWSSPlcReader(string tableName) { lock (String.Empty) { List result = new List(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(getPlcSchemaPath()); foreach (XmlNode configNode in xmlDocument) { //跳过注释 if (configNode.NodeType == XmlNodeType.Comment) { continue; } if (!configNode.Name.Equals("configuration", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode readNode in configNode.ChildNodes) { //跳过注释 if (readNode.NodeType == XmlNodeType.Comment) { continue; } if (!readNode.Name.Equals("CWSSPlcSaveRead", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode serviceNode in readNode.ChildNodes) { //跳过注释 if (serviceNode.NodeType == XmlNodeType.Comment) { continue; } if (!serviceNode.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase)) { continue; } PlcRecipeReader reader = new PlcRecipeReader(); reader.TargetTable = ToValue(serviceNode, "targetTable", string.Empty); reader.PlcFiledName = ToValue(serviceNode, "equip", string.Empty); reader.ThisCount = ToValue(serviceNode, "count", 1); reader.ThisType = ToValue(serviceNode, "type", 0); foreach (XmlNode node in serviceNode.ChildNodes) { //跳过注释 if (node.NodeType == XmlNodeType.Comment) { continue; } PlcRecipeReaderItem plc = new PlcRecipeReaderItem(); plc.DataFieldName = ToValue(node, "field", string.Empty); plc.PlcFiledName = ToValue(node, "equip", string.Empty); plc.PlcShifting = ToValue(node, "shifting", 0); plc.DefaultValue = ToValue(node, "default", 0); plc.ValueLen = ToValue(node, "len", 1); plc.DataType = ToValue(node, "datatype", string.Empty); plc.Multiply = ToValue(node, "multiply", 1); reader.ItemList.Add(plc); } result.Add(reader); } } } return result; } } #endregion #region 解析PlCSchema.xml文件中的计划的配方头数据的下传PLC项 /// /// 解析PlCSchema.xml文件中的计划的配方头数据的下传PLC项 /// /// 元素名 - 对应下传数据项节点 /// 返回称量参数下传PLC数据项集合 public List GetPlcWriter(string tableName) { List result = new List(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(getPlcSchemaPath()); foreach (XmlNode configNode in xmlDocument) { //跳过注释 if (configNode.NodeType == XmlNodeType.Comment) { continue; } if (!configNode.Name.Equals("configuration", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode writeNode in configNode.ChildNodes) { //跳过注释 if (writeNode.NodeType == XmlNodeType.Comment) { continue; } if (!writeNode.Name.Equals("PlcPlanAndRecipeWrite", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode serviceNode in writeNode.ChildNodes) { //跳过注释 if (serviceNode.NodeType == XmlNodeType.Comment) { continue; } if (!serviceNode.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase)) { continue; } PlcWriter writer = new PlcWriter(); writer.EquipRunName = ToValue(serviceNode, "equip", string.Empty); writer.DataFieldName = ToValue(serviceNode, "field", string.Empty); writer.ThisCount = ToValue(serviceNode, "count", 1); writer.ThisType = ToValue(serviceNode, "type", 0); foreach (XmlNode node in serviceNode.ChildNodes) { //跳过注释 if (node.NodeType == XmlNodeType.Comment) { continue; } PlcWriteItem plc = new PlcWriteItem(); plc.DataFieldName = ToValue(node, "field", string.Empty); if (string.IsNullOrWhiteSpace(plc.DataFieldName)) { plc.DataFieldName = writer.DataFieldName; } plc.EquipRunName = ToValue(node, "equip", string.Empty); if (string.IsNullOrWhiteSpace(plc.EquipRunName)) { plc.EquipRunName = writer.EquipRunName; } plc.PlcShifting = ToValue(node, "shifting", 0); plc.DefaultValue = ToValue(node, "default", 0); plc.ValueLen = ToValue(node, "len", 1); plc.DataType = ToValue(node, "datatype", string.Empty); plc.Multiply = ToValue(node, "multiply", 1); writer.SchemaList.Add(plc); } result.Add(writer); } } } return result; } #endregion #region 解析PlCSchema.xml文件中的计划的物料信息数据的下传PLC项 /// /// 解析PlCSchema.xml文件中的计划的物料信息数据的下传PLC项 /// /// 元素名 - 对应下传数据项节点 /// 返回称量参数下传PLC数据项集合 public List GetMaterialPlcWriter(string tableName) { List result = new List(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(getPlcSchemaPath()); foreach (XmlNode configNode in xmlDocument) { //跳过注释 if (configNode.NodeType == XmlNodeType.Comment) { continue; } if (!configNode.Name.Equals("configuration", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode writeNode in configNode.ChildNodes) { //跳过注释 if (writeNode.NodeType == XmlNodeType.Comment) { continue; } if (!writeNode.Name.Equals("PlcPlanAndRecipeWrite", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode serviceNode in writeNode.ChildNodes) { //跳过注释 if (serviceNode.NodeType == XmlNodeType.Comment) { continue; } if (!serviceNode.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase)) { continue; } PlcWriter writer = new PlcWriter(); writer.EquipRunName = ToValue(serviceNode, "equip", string.Empty); writer.DataFieldName = ToValue(serviceNode, "field", string.Empty); writer.ThisCount = ToValue(serviceNode, "count", 1); writer.ThisType = ToValue(serviceNode, "type", 0); foreach (XmlNode node in serviceNode.ChildNodes) { //跳过注释 if (node.NodeType == XmlNodeType.Comment) { continue; } PlcWriteItem plc = new PlcWriteItem(); plc.DataFieldName = ToValue(node, "field", string.Empty); if (string.IsNullOrWhiteSpace(plc.DataFieldName)) { plc.DataFieldName = writer.DataFieldName; } plc.EquipRunName = ToValue(node, "equip", string.Empty); if (string.IsNullOrWhiteSpace(plc.EquipRunName)) { plc.EquipRunName = writer.EquipRunName; } plc.PlcShifting = ToValue(node, "shifting", 0); plc.DefaultValue = ToValue(node, "default", 0); plc.ValueLen = ToValue(node, "len", 1); plc.DataType = ToValue(node, "datatype", string.Empty); plc.Multiply = ToValue(node, "multiply", 1); writer.SchemaList.Add(plc); } result.Add(writer); } } } return result; } #endregion #endregion } }