232 lines
8.3 KiB
C#
232 lines
8.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Xml;
|
|
|
|
namespace SCMA
|
|
{
|
|
public class DataModel
|
|
{
|
|
|
|
#region oggetti di base
|
|
|
|
public class ouData
|
|
{
|
|
|
|
public string SymbolicName { get; set; }
|
|
|
|
public string BrowseName { get; set; }
|
|
|
|
public string DataType { get; set; }
|
|
|
|
public string ValueRank { get; set; }
|
|
|
|
|
|
}
|
|
|
|
public class Property : ouData
|
|
{
|
|
public string Value { get; set; }
|
|
}
|
|
|
|
public class Variable : ouData
|
|
{
|
|
public string Units { get; set; }
|
|
public string CmsDataType { get; set; } = "";
|
|
public string CmsDataIndex { get; set; } = "";
|
|
public string CmsDataOpt { get; set; } = "";
|
|
public int CmsDataScale { get; set; } = 1;
|
|
}
|
|
public class Condition : ouData
|
|
{
|
|
public string Type { get; set; }
|
|
public string Units { get; set; }
|
|
}
|
|
|
|
public class Component
|
|
{
|
|
public string Name { get; set; }
|
|
public string BrowseName { get; set; }
|
|
}
|
|
|
|
public List<Property> Properties;
|
|
public List<Variable> Variables;
|
|
public List<Component> Components;
|
|
public List<Condition> Conditions;
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// init base
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
public DataModel()
|
|
{
|
|
Properties = new List<Property>();
|
|
Variables = new List<Variable>();
|
|
Components = new List<Component>();
|
|
Conditions = new List<Condition>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deserializza modello a partire da classe base
|
|
/// </summary>
|
|
/// <param name="xmlOrig"></param>
|
|
public DataModel Deserialize(string xmlOrig)
|
|
{
|
|
DataModel answ = new DataModel();
|
|
var xmlDocument = new XmlDocument();
|
|
xmlDocument.LoadXml(xmlOrig);
|
|
string baseNS = "Machine";
|
|
var itemRef = xmlDocument.GetElementsByTagName(baseNS);
|
|
// aggiungo i child...
|
|
var xmlNodeList = itemRef[0]?.ChildNodes;
|
|
addNodes(baseNS, xmlNodeList);
|
|
|
|
// salvo proprietà, variabili, condizioni, ...
|
|
answ.Properties = Properties;
|
|
answ.Variables = Variables;
|
|
answ.Components = Components;
|
|
answ.Conditions = Conditions;
|
|
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Deserializza modello a partire da classe base
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
public DataModel DeserializeFile(string fileName)
|
|
{
|
|
DataModel answ = new DataModel();
|
|
var xmlDocument = new XmlDocument();
|
|
xmlDocument.Load(fileName);
|
|
string baseNS = "Machine";
|
|
var itemRef = xmlDocument.GetElementsByTagName(baseNS);
|
|
// aggiungo i child...
|
|
var xmlNodeList = itemRef[0]?.ChildNodes;
|
|
addNodes(baseNS, xmlNodeList);
|
|
return answ;
|
|
}
|
|
|
|
|
|
private void addNodes(string baseNS, XmlNodeList xmlNodeList)
|
|
{
|
|
// se ho child processo---
|
|
if (xmlNodeList.Count > 0)
|
|
{
|
|
foreach (XmlElement xmlElement in xmlNodeList)
|
|
{
|
|
saveMemItem(xmlElement, baseNS);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica elemento e lo inserisce in memoria
|
|
/// </summary>
|
|
/// <param name="currElement"></param>
|
|
private void saveMemItem(XmlElement currElement, string baseNS)
|
|
{
|
|
if (currElement.Name != "Component" && currElement.Name != "Conditions")
|
|
{
|
|
switch (currElement.Name)
|
|
{
|
|
case "Property":
|
|
AddProperty(currElement, baseNS);
|
|
break;
|
|
case "Condition":
|
|
AddCondition(currElement, baseNS);
|
|
break;
|
|
default:
|
|
AddVariable(currElement, baseNS);
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var varName = currElement.Attributes["Name"].Value;
|
|
if (currElement.Name == "Conditions")
|
|
{
|
|
// CreateConditions(folder, stringFolderName + "/" + varName, varName, xmlElement, stringFolderName, folderInstanceState, nameSpaceIndex);
|
|
}
|
|
else
|
|
{
|
|
AddComponent(currElement, baseNS);
|
|
}
|
|
}
|
|
}
|
|
private void AddComponent(XmlElement currElement, string baseNS)
|
|
{
|
|
Component newItem = new Component
|
|
{
|
|
Name = currElement.Attributes["Name"].Value,
|
|
BrowseName = string.Format(baseNS + ":" + currElement.Attributes["Name"].Value)
|
|
};
|
|
Components.Add(newItem);
|
|
// controllo se ha dei child... nel caso aggiungo i child...
|
|
var xmlNodeList = currElement?.ChildNodes;
|
|
if (xmlNodeList.Count > 0)
|
|
{
|
|
addNodes(newItem.BrowseName, xmlNodeList);
|
|
}
|
|
}
|
|
private void AddProperty(XmlElement currElement, string baseNS)
|
|
{
|
|
Property newItem = new Property
|
|
{
|
|
SymbolicName = currElement.Attributes["SymbolicName"].Value,
|
|
BrowseName = string.Format(baseNS + ":" + currElement.Attributes["SymbolicName"].Value),
|
|
DataType = currElement.Attributes["DataType"].Value,
|
|
ValueRank = currElement.Attributes["ValueRank"].Value,
|
|
Value = currElement.Attributes["Value"].Value
|
|
};
|
|
Properties.Add(newItem);
|
|
// controllo se ha dei child... nel caso aggiungo i child...
|
|
var xmlNodeList = currElement?.ChildNodes;
|
|
if (xmlNodeList.Count > 0)
|
|
{
|
|
addNodes(newItem.BrowseName, xmlNodeList);
|
|
}
|
|
}
|
|
private void AddVariable(XmlElement currElement, string baseNS)
|
|
{
|
|
Variable newItem = new Variable
|
|
{
|
|
SymbolicName = currElement.Attributes["SymbolicName"].Value,
|
|
BrowseName = string.Format(baseNS + ":" + currElement.Attributes["SymbolicName"].Value),
|
|
DataType = currElement.Attributes["DataType"].Value,
|
|
ValueRank = currElement.Attributes["ValueRank"].Value,
|
|
Units = currElement.Attributes["Units"].Value,
|
|
CmsDataIndex = currElement.Attributes["CmsDataIndex"] == null ? "" : currElement.Attributes["CmsDataIndex"].Value,
|
|
CmsDataOpt = currElement.Attributes["CmsDataOpt"] == null ? "" : currElement.Attributes["CmsDataOpt"].Value,
|
|
CmsDataType = currElement.Attributes["CmsDataType"] == null ? "" : currElement.Attributes["CmsDataType"].Value,
|
|
CmsDataScale = currElement.Attributes["CmsDataScale"] == null ? 1 : XmlConvert.ToInt32(currElement.Attributes["CmsDataScale"].Value)
|
|
};
|
|
Variables.Add(newItem);
|
|
// controllo se ha dei child... nel caso aggiungo i child...
|
|
var xmlNodeList = currElement?.ChildNodes;
|
|
if (xmlNodeList.Count > 0)
|
|
{
|
|
addNodes(newItem.BrowseName, xmlNodeList);
|
|
}
|
|
}
|
|
private void AddCondition(XmlElement currElement, string baseNS)
|
|
{
|
|
Condition newItem = new Condition
|
|
{
|
|
SymbolicName = currElement.Attributes["SymbolicName"].Value,
|
|
BrowseName = string.Format(baseNS + ":" + currElement.Attributes["SymbolicName"].Value),
|
|
DataType = currElement.Attributes["DataType"].Value,
|
|
ValueRank = currElement.Attributes["ValueRank"].Value,
|
|
Type = currElement.Attributes["Type"].Value,
|
|
Units = currElement.Attributes["Units"].Value
|
|
};
|
|
Conditions.Add(newItem);
|
|
// controllo se ha dei child... nel caso aggiungo i child...
|
|
var xmlNodeList = currElement?.ChildNodes;
|
|
if (xmlNodeList.Count > 0)
|
|
{
|
|
addNodes(newItem.BrowseName, xmlNodeList);
|
|
}
|
|
}
|
|
}
|
|
}
|