Files
CMS-MTConn/MTC_Adapter/SCMA/ModelDesignConf.cs
T

522 lines
10 KiB
C#

using System;
using System.IO;
using System.Xml.Serialization;
namespace SCMA
{
#region -- ModelDesign Class --
/// <summary>
/// Classe per serializzazione / deserializzazione del datamodel x le informazioni necessarie x ADAPTER OPC-UA 2.1+
///
/// ref: http://www.cambiaresearch.com/articles/33/how-can-i-easily-manage-an-xml-configuration-file-in-dotnet
/// </summary>
[Serializable]
[XmlRoot("ModelDesign")]
public class ModelDesign
{
cMachine mMachine;
#if false
string sNomeAdapt;
int nVers;
double tContOreMaccOn;
double tContOreMaccLav;
double tContSlittaTast;
int[] _ContGiriElettrom;
float[] _ContKmMovAssi;
tipoAdapter etipoAdapt;
element[] _VacuumPump;
element[] _VacuumAct;
element[] _Lubro;
element[] _SlittaMag;
element[] _ProtMag;
element[] _Cooler;
element[] _Press;
element[] _Temp;
element[] _Path;
element[] _UnOp;
element[] _Axis;
element[] _MemArea;
#endif
/// <summary>
/// init conf adapter
/// </summary>
public ModelDesign()
{
#if false
sNomeAdapt = "";
etipoAdapt = tipoAdapter.DEMO;
#endif
}
/// <summary>
/// Classe di base oggetti OPC-UA in Datamodel da deserializzare
/// </summary>
public class ouBaseObj
{
//[XmlAttribute("SymbolicName")]
[XmlAttribute]
public string SymbolicName { get; set; }
[XmlAttribute("BrowseName")]
public string BrowseName { get; set; }
[XmlAttribute("DataType")]
public string DataType { get; set; }
[XmlAttribute("ValueRank")]
public string ValueRank { get; set; }
}
public class Property : ouBaseObj
{
[XmlAttribute("Value")]
public string Value { get; set; }
}
public class ouVariable : ouBaseObj
{
[XmlAttribute("Units")]
public string Units { get; set; }
}
/// <summary>
/// Oggetto più esterno Machine
/// </summary>
[Serializable]
public class cMachine
{
public Property Model { get; set; }
}
public cMachine Machine
{
get
{
return mMachine;
}
set
{
mMachine = value;
}
}
#if false
public int nVacuumPump
{
get
{
int answ = 0;
if (VacuumPump != null)
{
try
{
answ = Convert.ToInt32(VacuumPump.Length);
}
catch
{ }
}
return answ;
}
}
public int nVacuumAct
{
get
{
int answ = 0;
if (VacuumAct != null)
{
try
{
answ = Convert.ToInt32(VacuumAct.Length);
}
catch
{ }
}
return answ;
}
}
public int nLubro
{
get
{
int answ = 0;
if (Lubro != null)
{
try
{
answ = Convert.ToInt32(Lubro.Length);
}
catch
{ }
}
return answ;
}
}
public int nSlittaMag
{
get
{
int answ = 0;
if (SlittaMag != null)
{
try
{
answ = Convert.ToInt32(SlittaMag.Length);
}
catch
{ }
}
return answ;
}
}
public int nProtMag
{
get
{
int answ = 0;
if (ProtMag != null)
{
try
{
answ = Convert.ToInt32(ProtMag.Length);
}
catch
{ }
}
return answ;
}
}
public int nCooler
{
get
{
int answ = 0;
if (Cooler != null)
{
try
{
answ = Convert.ToInt32(Cooler.Length);
}
catch
{ }
}
return answ;
}
}
public int nPress
{
get
{
int answ = 0;
if (Press != null)
{
try
{
answ = Convert.ToInt32(Press.Length);
}
catch
{ }
}
return answ;
}
}
public int nTemp
{
get
{
int answ = 0;
if (Temp != null)
{
try
{
answ = Convert.ToInt32(Temp.Length);
}
catch
{ }
}
return answ;
}
}
public int nPath
{
get
{
int answ = 0;
if (Path != null)
{
try
{
answ = Convert.ToInt32(Path.Length);
}
catch
{ }
}
return answ;
}
}
public int nUnOp
{
get
{
int answ = 0;
if (UnOp != null)
{
try
{
answ = Convert.ToInt32(UnOp.Length);
}
catch
{ }
}
return answ;
}
}
public int nAxis
{
get
{
int answ = 0;
if (Axis != null)
{
try
{
answ = Convert.ToInt32(Axis.Length);
}
catch
{ }
}
return answ;
}
}
public int nMemArea
{
get
{
int answ = 0;
if (MemArea != null)
{
try
{
answ = Convert.ToInt32(MemArea.Length);
}
catch
{ }
}
return answ;
}
}
#endif
/// <summary>
/// Serializzazione XML dell'oggetto conf dell'adapter
/// </summary>
/// <param name="file"></param>
/// <param name="c"></param>
public static void Serialize(string file, ModelDesign 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 ModelDesign Deserialize(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(ModelDesign));
StreamReader reader = File.OpenText(file);
ModelDesign c = (ModelDesign)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(ModelDesign));
StreamReader reader = File.OpenText(file);
answ = reader.ReadToEnd();
reader.Close();
return answ;
}
#if false
public int Version
{
get { return nVers; }
set { nVers = value; }
}
public string NomeAdapt
{
get { return sNomeAdapt; }
set { sNomeAdapt = value; }
}
public tipoAdapter TipoAdapt
{
get { return etipoAdapt; }
set { etipoAdapt = value; }
}
public double ContOreMaccOn
{
get { return tContOreMaccOn; }
set { tContOreMaccOn = value; }
}
public double ContOreMaccLav
{
get { return tContOreMaccLav; }
set { tContOreMaccLav = value; }
}
public double ContSlittaTast
{
get { return tContSlittaTast; }
set { tContSlittaTast = value; }
}
public int[] ContGiriElettrom
{
get { return _ContGiriElettrom; }
set { _ContGiriElettrom = value; }
}
public float[] ContKmMovAssi
{
get { return _ContKmMovAssi; }
set { _ContKmMovAssi = value; }
}
public element[] VacuumPump
{
get { return _VacuumPump; }
set { _VacuumPump = value; }
}
public element[] VacuumAct
{
get { return _VacuumAct; }
set { _VacuumAct = value; }
}
public element[] Lubro
{
get { return _Lubro; }
set { _Lubro = value; }
}
public element[] SlittaMag
{
get { return _SlittaMag; }
set { _SlittaMag = value; }
}
public element[] ProtMag
{
get { return _ProtMag; }
set { _ProtMag = value; }
}
public element[] Cooler
{
get { return _Cooler; }
set { _Cooler = value; }
}
public element[] Press
{
get { return _Press; }
set { _Press = value; }
}
public element[] Temp
{
get { return _Temp; }
set { _Temp = value; }
}
public element[] Path
{
get { return _Path; }
set { _Path = value; }
}
public element[] UnOp
{
get { return _UnOp; }
set { _UnOp = value; }
}
public element[] Axis
{
get { return _Axis; }
set { _Axis = value; }
}
public element[] MemArea
{
get { return _MemArea; }
set { _MemArea = value; }
}
#endif
}
#if false
[Serializable]
[XmlType(TypeName = "dataRef")]
public struct DataRefItem<K, V>
{
public K Key { get; set; }
public V Value { get; set; }
public DataRefItem(K k, V v) : this() { Key = k; Value = v; }
}
/// <summary>
/// classe elemento base in cui salvare i dati di conf x recupero dati adapters
/// </summary>
public class element
{
/// <summary>
/// identificativo univoco x classe di elemento
/// </summary>
public string ident;
/// <summary>
/// Elenco riferimento dati x recupero (es posizioni memoria separate da #)
/// </summary>
public List<DataRefItem<string, string>> dataRefList;
/// <summary>
/// init empty
/// </summary>
public element()
{
ident = "";
dataRefList = new List<DataRefItem<string, string>>();
}
/// <summary>
/// init element con dati
/// </summary>
/// <param name="Idx">Identificativo univoco</param>
/// <param name="DataRef">Parametri x recupero dati in forma dictionary</param>
public element(string Idx, List<DataRefItem<string, string>> DataRef)
{
ident = Idx;
dataRefList = DataRef;
}
}
#endif
#endregion
}