Files
DP44/DataPRO/IService/Classes/TDAS/TDASConfig.cs
2026-04-17 14:55:32 -04:00

119 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using DTS.DASLib.Command.SLICE;
using DTS.Common.DAS.Concepts;
using DTS.Common.Utilities.DotNetProgrammingConstructs;
using DTS.Common.Utilities.Logging;
namespace DTS.DASLib.Service
{
public class TDASConfig : IXmlSerializable
{
private Dictionary<string, TDASModuleConfig> _modules = new Dictionary<string, TDASModuleConfig>();
public Dictionary<string, TDASModuleConfig> Modules { get { return _modules; } }
private string _fileName;
public string FileName { get { return _fileName; } }
public TDASConfig()
{
}
public TDASConfig(string fileName, bool deleteIfPresent)
{
const string dasConfigs = "DASConfigs";
var location = Path.Combine(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), dasConfigs), fileName);
_fileName = location; //fileName;
if (File.Exists(location))
{
try
{
if (deleteIfPresent)
{
try { File.Delete(_fileName); }
catch (Exception ex) { APILogger.Log("Problem deleting ", _fileName, ex); }
}
else { using (var sr = new StreamReader(_fileName)) { ReadXml(XmlReader.Create(sr)); } }
}
catch (Exception ex) { APILogger.Log("Problem reading ", _fileName, ex); }
}
}
public void SetModule(TDASModuleConfig module)
{
if (_modules.ContainsKey(module.SerialNumber)) { _modules[module.SerialNumber] = module; }
else { _modules.Add(module.SerialNumber, module); }
}
public TDASModuleConfig GetModule(TDASModuleConfig module)
{
if (_modules.ContainsKey(module.SerialNumber)) { return _modules[module.SerialNumber]; }
else
{
_modules.Add(module.SerialNumber, module);
return module;
}
}
public XmlSchema GetSchema()
{
return (null);
}
//
// Summary:
// Generates an object from its XML representation.
//
// Parameters:
// reader:
// The System.Xml.XmlReader stream from which the object is deserialized.
public void ReadXml(XmlReader reader)
{
reader.MoveToContent();
reader.ReadStartElement("TDASConfig");
reader.ReadStartElement("Modules");
while (reader.MoveToContent() == XmlNodeType.Element)
{
TDASModuleConfig tConfig = new TDASModuleConfig();
tConfig.ReadXml(reader);
SetModule(tConfig);
}
reader.ReadEndElement();
reader.ReadEndElement();
}
//
// Summary:
// Converts an object into its XML representation.
//
// Parameters:
// writer:
// The System.Xml.XmlWriter stream to which the object is serialized.
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("TDASConfig");
writer.WriteStartElement("Modules");
writer.Flush();
foreach (TDASModuleConfig module in _modules.Values)
{
module.WriteXml(writer);
writer.Flush();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
}
}
}