205 lines
11 KiB
C#
205 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using UCS.ConfigFile.Attributes;
|
|
|
|
namespace UCS.ConfigFile
|
|
{
|
|
public static class CommunicationConfig
|
|
{
|
|
private static readonly NLog.Logger Errorlog = NLog.LogManager.GetLogger("ConfigFile");
|
|
|
|
public static event EventHandler<string> ErrorMessageEvent;
|
|
|
|
public static void ReadConfig(Object config, string xmlFilePath)
|
|
{
|
|
try
|
|
{
|
|
FileStream fs = new FileStream(xmlFilePath, FileMode.OpenOrCreate, 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 "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(null, "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(null, "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);
|
|
|
|
if (currentWorksheet == "Settings" && currentColumn == 2)
|
|
{
|
|
//Debug.WriteLine($"Currentrow = {currentRow}");
|
|
//Debug.WriteLine($"celldata = {cellData}");
|
|
|
|
PropertyInfo property = GetRowProperty(config.GetType(), currentRow);
|
|
|
|
if (property != null)
|
|
{
|
|
//Debug.WriteLine($"Property is {property}");
|
|
//Debug.WriteLine($"Property type {property.PropertyType}");
|
|
|
|
if (property.PropertyType == typeof(string))
|
|
{
|
|
property.SetValue(config, cellData);
|
|
}
|
|
else if (property.PropertyType == typeof(Boolean))
|
|
{
|
|
if (cellData.Contains("1"))
|
|
property.SetValue(config, true);
|
|
else
|
|
property.SetValue(config, false);
|
|
}
|
|
else if (property.PropertyType == typeof(UInt16))
|
|
{
|
|
if (Helpers.IsCellUInt16(cellData))
|
|
{
|
|
property.SetValue(config, Convert.ToUInt16(cellData));
|
|
}
|
|
else
|
|
{
|
|
ErrorMessageEvent?.Invoke(null, $"{xmlFilePath} Rij {currentRow} is geen nummer - {cellData}");
|
|
}
|
|
}
|
|
else if (property.PropertyType == typeof(UInt32))
|
|
{
|
|
if (Helpers.IsCellUInt32(cellData))
|
|
{
|
|
property.SetValue(config, Convert.ToUInt32(cellData));
|
|
}
|
|
else
|
|
{
|
|
ErrorMessageEvent?.Invoke(null, $"{xmlFilePath} Rij {currentRow} is geen nummer - {cellData}");
|
|
}
|
|
}
|
|
else if (property.PropertyType == typeof(Int32))
|
|
{
|
|
if (Helpers.IsCellInteger(cellData))
|
|
{
|
|
property.SetValue(config, Convert.ToInt32(cellData));
|
|
}
|
|
else
|
|
{
|
|
ErrorMessageEvent?.Invoke(null, $"{xmlFilePath} Rij {currentRow} is geen nummer - {cellData}");
|
|
}
|
|
}
|
|
else if (property.PropertyType == typeof(byte))
|
|
{
|
|
if (Helpers.IsCellByte(cellData))
|
|
{
|
|
property.SetValue(config, Convert.ToByte(cellData));
|
|
}
|
|
else
|
|
{
|
|
ErrorMessageEvent?.Invoke(null, $"{xmlFilePath} Rij {currentRow} is geen byte - {cellData}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine($"------------ IT IS {property.PropertyType}");
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
//Debug.WriteLine("Default");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Errorlog.Info($"loaded {xmlFilePath}");
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
Errorlog.Error(ex);
|
|
ErrorMessageEvent?.Invoke(null, $"ConfigFile kan niet worden gelezen (geen rechten), controleer de rechten in {xmlFilePath}");
|
|
}
|
|
catch (FileLoadException ex)
|
|
{
|
|
Errorlog.Error(ex);
|
|
ErrorMessageEvent?.Invoke(null, $"Het configfile bestand is mogelijk beschadigd, controleer het bestand in {xmlFilePath}");
|
|
}
|
|
catch (FileNotFoundException ex)
|
|
{
|
|
Errorlog.Error(ex);
|
|
ErrorMessageEvent?.Invoke(null, $"Geen configfile bestand gevonden bij het opstarten in {xmlFilePath}");
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
Errorlog.Error(ex);
|
|
ErrorMessageEvent?.Invoke(null, $"Kan {xmlFilePath} niet lezen - {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Errorlog.Error(ex);
|
|
ErrorMessageEvent?.Invoke(null, $"{xmlFilePath} fout - {ex.Message}");
|
|
}
|
|
}
|
|
|
|
|
|
private static PropertyInfo GetRowProperty(Type type, int row)
|
|
{
|
|
var properties = type.GetProperties().Where(m => m.GetCustomAttributes(typeof(ConfigRowAttribute), false).Length > 0).ToArray();
|
|
foreach (PropertyInfo item in properties)
|
|
{
|
|
ConfigRowAttribute attrib = (ConfigRowAttribute)item.GetCustomAttribute(typeof(ConfigRowAttribute));
|
|
if (attrib.Row == row)
|
|
{
|
|
return item;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |