Files
cms_thermo_active/Step.Config/ServerConfigController.cs
T
2018-02-15 12:23:23 +01:00

189 lines
7.0 KiB
C#

using Step.Model.ConfigModels;
using Step.Utils;
using System;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Schema;
using static Step.Config.ServerConfig;
using static Step.Utils.Constants;
namespace Step.Config
{
public static class ServerConfigController
{
public static void ReadStartupConfig()
{
try
{
// Get server file handler
XDocument xmlConfigFile = GetXmlHandlerWithValidator(SERVER_CONFIG_SCHEMA_PATH, SERVER_CONFIG_PATH);
// Read nc Config with LINQ
NcConfig = xmlConfigFile
.Descendants(NC_CONFIG_KEY)
.Select(x => new NcConfigModel()
{
NcVendor = x.Element("ncVendor").Value,
showNcHMI = Convert.ToBoolean(x.Element("showNcHMI").Value),
NcIpAddress = x.Element("ncIpAddress").Value,
NcPort = Convert.ToUInt16(x.Element("ncPort").Value),
NcName = x.Element("machineModel").Value
}).FirstOrDefault();
// Read server config with LINQ and save into static config
ServerStartupConfig = xmlConfigFile
.Descendants(SERVER_CONFIG_KEY)
.Select(x => new ServerConfigModel()
{ // Set server config model data
Language = CultureInfo.CreateSpecificCulture(x.Element("language").Value),
ServerPort = Convert.ToInt32(x.Element("serverPort").Value),
EnableDirectoryBrowsing = Convert.ToBoolean(x.Element("enableDirectoryBrowsing").Value)
}).FirstOrDefault();
// Get Areas file handler
xmlConfigFile = GetXmlHandlerWithValidator(AREAS_CONFIG_SCHEMA_PATH, AREAS_CONFIG_PATH);
// Read areas config with LINQ
xmlConfigFile
.Descendants(AREAS_CONFIG_KEY) // Get areas config node
.Elements()
.ToList()
.ForEach(x => SetAreaValueByName(x)); // Loop through elements
// Get Maintenances file handler
xmlConfigFile = GetXmlHandlerWithValidator(MAINTENANCES_CONFIG_SCHEMA_PATH, MAINTENANCES_CONFIG_PATH);
MaintenancesConfig = xmlConfigFile
.Root
.Elements()
.ToList()
.Select(x =>
new MaintenanceConfigModel()
{
Id = Convert.ToInt32(x.Element("id").Value),
LocalizedNames = x.Element("localizedNames").Elements().ToDictionary( // Read names list
y => y.Attribute("langKey").Value, y => y.Value
),
Intervall = TimeSpan.FromMinutes(Convert.ToDouble(x.Element("interval").Value)),
Deadline = DateTime.Parse(x.Element("deadline").Value),
Type = x.Element("type").Value,
CouterId = Convert.ToInt32(x.Element("counterId").Value)
}
)
.ToList();
}
catch (Exception ex)
{
ExceptionManager.Manage(ex);
}
}
private static XDocument GetXmlHandlerWithValidator(string configSchemaFilePath, string configFilePath)
{
// Create new instance
XmlSchemaSet readerSettings = new XmlSchemaSet();
// Add Schema
readerSettings.Add(null, BASE_PATH + "\\" + configSchemaFilePath);
// Open file reader
XDocument xmlConfigFile = XDocument.Load(BASE_PATH + "\\" + configFilePath);
// Validate file
xmlConfigFile.Validate(readerSettings, ValidationHandler);
return xmlConfigFile;
}
private static void SetAreaValueByName(XElement element)
{
// Choose which area to be set
switch (element.Name.ToString())
{
case AREAS.PRODUCTION_KEY:
SetAreaValue(ref ProductionConfig, element);
break;
case AREAS.TOOLING_KEY:
SetAreaValue(ref ToolingConfig, element);
break;
case AREAS.REPORT_KEY:
SetAreaValue(ref ReportConfig, element);
break;
case AREAS.ALARMS_KEY:
SetAreaValue(ref AlarmsConfig, element);
break;
case AREAS.MAINTENANCE_KEY:
SetAreaValue(ref MaintenanceConfig, element);
break;
case AREAS.UTILITIES_KEY:
SetAreaValue(ref UtilitiesConfig, element);
break;
case AREAS.SCADA_KEY:
SetAreaValue(ref ScadaConfig, element);
break;
}
}
private static void SetAreaValue(ref AreasConfigModel areasConfig, XElement element)
{
// Set area model with xml data
areasConfig = new AreasConfigModel()
{
Name = element.Name.ToString(),
Enabled = Convert.ToBoolean(element.Element("enabled").Value),
AllowExternalBrowser = Convert.ToBoolean(element.Element("allowExternalBrowser").Value),
NcNeeded = Convert.ToBoolean(element.Element("ncNeeded").Value)
};
}
private static void ValidationHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
ExceptionManager.Manage(ERROR_LEVEL.WARNING, e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
ExceptionManager.Manage(ERROR_LEVEL.FATAL, e.Message);
}
}
public static bool CheckAreaStatus(string areaName)
{ // Get Area status ( enabled field) by name
switch (areaName)
{
case AREAS.PRODUCTION_KEY:
return ProductionConfig.Enabled;
case AREAS.TOOLING_KEY:
return ToolingConfig.Enabled;
case AREAS.REPORT_KEY:
return ProductionConfig.Enabled;
case AREAS.ALARMS_KEY:
return AlarmsConfig.Enabled;
case AREAS.MAINTENANCE_KEY:
return MaintenanceConfig.Enabled;
case AREAS.UTILITIES_KEY:
return UtilitiesConfig.Enabled;
case AREAS.SCADA_KEY:
return ScadaConfig.Enabled;
case AREAS.GENERAL_KEY:
return true;
default:
return false;
}
}
}
}