Files
UCS_WinUI/UCS.ConfigFile/ConfigFile.cs
Martijn Scheepers a98dbf7e66 First commit
2021-05-07 11:31:58 +02:00

222 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace UCS.ConfigFile
{
public class ConfigFile
{
private static readonly NLog.Logger Errorlog = NLog.LogManager.GetLogger("ConfigFile");
public List<GraphicsIdListLine> Graphics_IDList = new List<GraphicsIdListLine>();
public List<RemoteIdListLine> Remote_IDList = new List<RemoteIdListLine>();
public List<GroupBoxLine> GroupBox_Data = new List<GroupBoxLine>();
public InitiationWorksheet Initiation_Data = new InitiationWorksheet();
public event EventHandler<string> ErrorMessageEvent;
public event EventHandler TelemetryConnectionStateUpdate;
public void ReadConfigFile(string xmlFilePath)
{
Initiation_Data.ErrorMessageEvent += this.ErrorMessageEvent;
try
{
using (FileStream fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (XmlReader reader = XmlReader.Create(fs))
{
string currentWorksheet = string.Empty;
int currentRow = 0;
int currentColumn = 0;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
//Debug.WriteLine(reader.NodeType + " " + reader.Name);
switch (reader.Name)
{
case "Worksheet":
//<Worksheet ss:Name="Graphics_IDList">
//Debug.WriteLine("Worksheet name = " + reader.GetAttribute("ss:Name"));
currentWorksheet = reader.GetAttribute("ss:Name");
break;
case "Table":
//<Table ss:ExpandedColumnCount="52" ss:ExpandedRowCount="409" x:FullColumns="1" x: FullRows = "1" ss: StyleID = "s63" ss: DefaultRowHeight = "12.5625" >
if ((int.TryParse(reader.GetAttribute("ss:ExpandedRowCount"), out int rowCount) == false))
ErrorMessageEvent?.Invoke(this, "Table ss:ExpandedRowCount heeft geen nummer.");
//Debug.WriteLine("Table Rows = " + rowCount);
switch (currentWorksheet)
{
case "Graphics_IDList":
{
for (var i = 0; i <= (rowCount - 2); i++)
{
this.Graphics_IDList.Add(new GraphicsIdListLine());
this.Graphics_IDList[i].LineNumber = i + 2;
this.Graphics_IDList[i].ErrorMessageEvent += this.ErrorMessageEvent;
}
break;
}
case "Remote_IDList":
{
for (var i = 0; i <= (rowCount - 2); i++)
{
this.Remote_IDList.Add(new RemoteIdListLine());
this.Remote_IDList[i].LineNumber = i + 2;
this.Remote_IDList[i].ErrorMessageEvent += this.ErrorMessageEvent;
this.Remote_IDList[i].TelemetryConnectionStateUpdate += this.TelemetryConnectionStateUpdate;
}
break;
}
case "Groupbox_Data":
{
for (var i = 0; i <= (rowCount - 2); i++)
{
this.GroupBox_Data.Add(new GroupBoxLine());
this.GroupBox_Data[i].LineNumber = i + 2;
this.GroupBox_Data[i].ErrorMessageEvent += this.ErrorMessageEvent;
}
break;
}
}
currentRow = 0;
break;
case "Row":
//<Row ss:Index="127" ss:AutoFitHeight="0">
if (reader.GetAttribute("ss:Index") != null)
{
if ((int.TryParse(reader.GetAttribute("ss:Index"), out currentRow) == false))
ErrorMessageEvent?.Invoke(this, "Row ss:Index heeft geen nummer.");
}
else
{
currentRow += 1;
}
currentColumn = 0;
//Debug.WriteLine("current Row Index = " + currentRow);
break;
case "Cell":
//<Cell ss:Index="5"><Data ss:Type="String">00000000</Data></Cell>
if (reader.GetAttribute("ss:Index") != null)
{
if ((int.TryParse(reader.GetAttribute("ss:Index"), out currentColumn) == false))
ErrorMessageEvent?.Invoke(this, "Cell ss:Index heeft geen nummer.");
}
else
{
currentColumn += 1;
}
//Debug.WriteLine("current column Index = " + currentColumn);
break;
case "Data":
//<Data ss:Type="String">00000000</Data>
string cellData = reader.ReadInnerXml();
//Debug.WriteLine("cellData = " + cellData);
if (String.IsNullOrEmpty(cellData))
continue;
cellData = Helpers.ReplaceSpecialChars(cellData);
switch (currentWorksheet)
{
case "Graphics_IDList":
if (currentRow > 1)
{
this.Graphics_IDList[currentRow - 2].StoreGraphicsIdListLine(currentColumn, currentRow, cellData);
}
break;
case "Remote_IDList":
if (currentRow > 1)
{
this.Remote_IDList[currentRow - 2].StoreRemoteIdListLine(currentColumn, currentRow, cellData);
}
break;
case "Groupbox_Data":
if (currentRow > 1)
{
this.GroupBox_Data[currentRow - 2].StoreGroupBoxDataLine(currentColumn, currentRow, cellData);
}
break;
case "Initiation_Data":
if (currentColumn == 2)
{
this.Initiation_Data.StoreInitiationDataLine(currentRow, cellData);
}
break;
}
break;
default:
//Debug.WriteLine("Default");
break;
}
}
}
}
}
Errorlog.Info($"loaded {xmlFilePath}");
}
catch (UnauthorizedAccessException ex)
{
Errorlog.Error(ex);
ErrorMessageEvent?.Invoke(this, $"ConfigFile.xml kan niet worden gelezen (geen rechten), controleer de rechten in {xmlFilePath}");
}
catch (FileLoadException ex)
{
Errorlog.Error(ex);
ErrorMessageEvent?.Invoke(this, $"Het configfile.xml bestand is mogelijk beschadigd, controleer het bestand in {xmlFilePath}");
}
catch (FileNotFoundException ex)
{
Errorlog.Error(ex);
ErrorMessageEvent?.Invoke(this, $"Geen configfile.xml bestand gevonden bij het opstarten in {xmlFilePath}");
}
catch (IOException ex)
{
Errorlog.Error(ex);
ErrorMessageEvent?.Invoke(this, $"Kan ConfigFile.xml niet lezen - {ex.Message}");
}
catch (Exception ex)
{
Errorlog.Error(ex);
ErrorMessageEvent?.Invoke(this, $"ConfigFile.xml fout - {ex.Message}");
}
this.Graphics_IDList.RemoveAll(t => t.Tag == null || t.ButtonEnable == false);
this.GroupBox_Data.RemoveAll(t => t.Tag == null);
//BACnet
this.Graphics_IDList.Where(x => x.TxComm == TxCommType.BAC).ToList().ForEach(s => s.TxString = s.TxString.ToUpper());
this.Graphics_IDList.Where(x => x.RxComm == RxCommType.BAC).ToList().ForEach(s => s.RxString = s.RxString.ToUpper());
//Stentofon
this.Graphics_IDList.Where(x => x.TxComm == TxCommType.STEN).ToList().ForEach(s => s.TxString = s.TxString.ToUpper());
this.Graphics_IDList.Where(x => x.RxComm == RxCommType.STEN).ToList().ForEach(s => s.RxString = s.RxString.ToUpper());
}
public bool UseBoschVideo()
{
return GroupBox_Data.Where(b => b.Type == GroupBoxType.VB).Any() || this.Graphics_IDList.Where(b => b.TxComm == TxCommType.VB).Any();
}
public bool UseBACnet()
{
return this.Graphics_IDList.Where(b => b.TxComm == TxCommType.BAC || b.RxComm == RxCommType.BAC).Any();
}
public bool UseStentofon()
{
return this.Graphics_IDList.Where(b => b.TxComm == TxCommType.STEN || b.RxComm == RxCommType.STEN).Any();
}
}
}