Files
cms_thermo_active/Step.Config/StartupConfigController.cs
T
CMS4390\marantalu a05c3aeb85 Improved configuration
Added signalR auth
2017-12-01 17:18:18 +01:00

132 lines
4.9 KiB
C#

using System.Xml;
using System.Collections.Generic;
using System;
using System.Xml.Schema;
using System.Xml.Linq;
using System.Linq;
using static Step.Config.StartupConfig;
using Step.Model.ConfigModels;
using static Step.Config.Constants;
namespace Step.Config
{
public static class StartupConfigController
{
public static void ReadStartupConfig()
{
// Read validation file
XmlSchemaSet readerSettings = new XmlSchemaSet();
// Add Schema
readerSettings.Add(null, "startupValidator.xsd");
// Open file reader
XDocument xmlConfigFile = XDocument.Load("startupConfig.xml");
// Validate file
xmlConfigFile.Validate(readerSettings, ValidationHandler);
// Read nc Config with LINQ
ncConfig = xmlConfigFile
.Descendants(NC_CONFIG_KEY)
.Select(x => new NcConfigModel()
{
NcVendor = Convert.ToInt32(x.Element("ncVendor").Value),
NcIpAddress = x.Element("ncIpAddress").Value,
NcPort = Convert.ToInt32(x.Element("ncPort").Value)
}).FirstOrDefault();
// Read server config with LINQ and save into static config
serverConfig = xmlConfigFile
.Descendants(SERVER_CONFIG_KEY)
.Select(x => new ServerConfigModel()
{ // Set server config model data
Language = x.Element("language").Value,
ServerPort = Convert.ToInt32(x.Element("serverPort").Value),
EnableDirectoryBrowsing = Convert.ToBoolean(x.Element("enableDirectoryBrowsing").Value)
}).FirstOrDefault();
// Read areas config with LINQ
xmlConfigFile
.Descendants(AREAS_CONFIG_KEY) // Get areas config node
.Elements()
.ToList()
.ForEach(x => SetAreaValueByName(x)); // Loop through elements
}
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)
};
}
private static void ValidationHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
Console.Write("WARNING: ");
Console.WriteLine(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
Console.Write("ERROR: ");
Console.WriteLine(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;
default:
return false;
}
}
}
}