173 lines
4.7 KiB
C#
173 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace SCMA
|
|
{
|
|
/// <summary>
|
|
/// Classe per serializzazione / deserializzazione del datamodel x le informazioni necessarie x ADAPTER OPC-UA 2.1+
|
|
///
|
|
/// ref:
|
|
/// - https://stackoverflow.com/questions/37198689/deserialization-of-complex-object-c-sharp
|
|
/// - https://stackoverflow.com/questions/15907357/deserialization-of-xml-file-by-using-xmlarray
|
|
/// - http://www.cambiaresearch.com/articles/33/how-can-i-easily-manage-an-xml-configuration-file-in-dotnet
|
|
/// </summary>
|
|
[Serializable]
|
|
[XmlRoot("ModelDesign")]
|
|
public class DataModelXML
|
|
{
|
|
|
|
// rivedere con
|
|
// - https://docs.microsoft.com/it-it/dotnet/standard/serialization/controlling-xml-serialization-using-attributes
|
|
// - https://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm
|
|
// RIPENSARE OBJ GERARCHICO IN TOTO...
|
|
// ModelDesign > MAchine > List<Property> + List<Array> + List<Component> ognuno coi suoi attributi...
|
|
|
|
|
|
[XmlArray("Machine")]
|
|
[XmlArrayItem("Property", Type = typeof(Property))]
|
|
[XmlArrayItem("Variable", Type = typeof(Variable))]
|
|
[XmlArrayItem("Component", Type = typeof(Component))]
|
|
public ouData[] PVData { get; set; }
|
|
|
|
//[XmlArray("Machine")]
|
|
//[XmlArrayItem("Property")]
|
|
//[XmlArrayItem("Property", Type = typeof(Property))]
|
|
//[XmlArrayItem("Variable", Type = typeof(Variable))]
|
|
//[XmlArrayItem("Component", Type = typeof(Component))]
|
|
//public cData[] CData { get; set; }
|
|
|
|
/// <summary>
|
|
/// Serializzazione XML dell'oggetto conf dell'adapter
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <param name="c"></param>
|
|
public static void Serialize(string file, DataModelXML c)
|
|
{
|
|
// prima provo a creare il file vuoto...
|
|
if (!File.Exists(file))
|
|
{
|
|
string dirPath = file.Substring(0, file.LastIndexOf('\\'));
|
|
// verifico directory
|
|
if (!Directory.Exists(dirPath))
|
|
{
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
}
|
|
// salvo effettivamente file...
|
|
XmlSerializer xs = new XmlSerializer(c.GetType());
|
|
StreamWriter writer = File.CreateText(file);
|
|
xs.Serialize(writer, c);
|
|
writer.Flush();
|
|
writer.Close();
|
|
}
|
|
/// <summary>
|
|
/// deserializzazione oggetto conf adapter
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
public static DataModelXML Deserialize(string file)
|
|
{
|
|
XmlSerializer xs = new XmlSerializer(typeof(DataModelXML));
|
|
StreamReader reader = File.OpenText(file);
|
|
DataModelXML c = (DataModelXML)xs.Deserialize(reader);
|
|
reader.Close();
|
|
return c;
|
|
}
|
|
/// <summary>
|
|
/// restitusice forma XML grezza del file
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
public static string rawXml(string file)
|
|
{
|
|
string answ = "";
|
|
XmlSerializer xs = new XmlSerializer(typeof(DataModelXML));
|
|
StreamReader reader = File.OpenText(file);
|
|
answ = reader.ReadToEnd();
|
|
reader.Close();
|
|
return answ;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class Modules
|
|
{
|
|
[XmlElement]
|
|
public Channel channel { get; set; }
|
|
|
|
[XmlArray("Machine")]
|
|
//[XmlArrayItem("Property")]
|
|
//[XmlArrayItem("Property", Type = typeof(Property))]
|
|
//[XmlArrayItem("Variable", Type = typeof(Variable))]
|
|
//[XmlArrayItem("Component", Type = typeof(Component))]
|
|
public ouData[] PVData { get; set; }
|
|
|
|
|
|
//public List<Property> Properties { get; set; }
|
|
}
|
|
|
|
[Serializable]
|
|
[XmlInclude(typeof(Property))]
|
|
[XmlInclude(typeof(Variable))]
|
|
[XmlInclude(typeof(Component))]
|
|
public class ouData
|
|
{
|
|
[XmlAttribute]
|
|
public string Name { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string SymbolicName { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string BrowseName { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string DataType { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string ValueRank { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string Value { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string Units { get; set; }
|
|
}
|
|
|
|
[Serializable]
|
|
public class Property : ouData { }
|
|
|
|
[Serializable]
|
|
public class Variable : ouData { }
|
|
|
|
[Serializable]
|
|
public class Component : ouData { }
|
|
|
|
|
|
[Serializable]
|
|
public class Channel
|
|
{
|
|
[XmlArray("resources")]
|
|
[XmlArrayItem("resource")]
|
|
public List<Resources> resources { get; set; }
|
|
}
|
|
|
|
[Serializable]
|
|
public class Resources
|
|
{
|
|
[XmlAttribute]
|
|
public string name { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string url { get; set; }
|
|
|
|
[XmlAttribute]
|
|
public string refresh_interval { get; set; }
|
|
|
|
[XmlText]
|
|
public string someText { get; set; }
|
|
}
|
|
}
|