e043ae2181
Added Javascript TestPage in wwwroot
221 lines
7.1 KiB
C#
221 lines
7.1 KiB
C#
using Client.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
using System.Xml.Schema;
|
|
|
|
namespace Client.Config
|
|
{
|
|
public class ConfigController
|
|
{
|
|
|
|
public static void ReadStartupConfig()
|
|
{
|
|
|
|
// Read validation file
|
|
XmlSchemaSet readerSettings = new XmlSchemaSet();
|
|
|
|
// Add Schema
|
|
readerSettings.Add(null, Constants.STARTUP_CONFIG_SCHEMA_PATH);
|
|
// Open file reader
|
|
XDocument xmlConfigFile = XDocument.Load(Constants.STARTUP_CONFIG_PATH);
|
|
|
|
// Validate file
|
|
xmlConfigFile.Validate(readerSettings, ValidationHandler);
|
|
|
|
// Read XML Config
|
|
Config.ClientConfig = xmlConfigFile
|
|
.Descendants(Constants.CLIENT_CONFIG_KEY)
|
|
.Select(x => new SubModels.Client()
|
|
{
|
|
TranspColor = ValidateTranspColor(x.Element("TranspColor").Value)
|
|
}).FirstOrDefault();
|
|
|
|
Config.ConnectionConfig = xmlConfigFile
|
|
.Descendants(Constants.CONNECTION_CONFIG_KEY)
|
|
.Select(x => new SubModels.Connection()
|
|
{
|
|
Url = ValidateUrl(x.Element("Url").Value),
|
|
Id = ValidateClientID(x.Element("Id").Value),
|
|
DeleteCahceFolderOnStartup = ValidateDelCache(x.Element("DeleteCahceFolderOnStartup").Value),
|
|
}).FirstOrDefault();
|
|
|
|
Config.VendorHmiConfig = xmlConfigFile
|
|
.Descendants(Constants.VENDORHMI_CONFIG_KEY)
|
|
.Select(x => new SubModels.VendorHmi()
|
|
{
|
|
Enabled = ValidateOpenHmi(x.Element("Enabled").Value),
|
|
Type = ValidateNcType(x.Element("Type").Value)
|
|
}).FirstOrDefault();
|
|
|
|
Config.ExtSoftwaresConfig = xmlConfigFile
|
|
.Descendants(Constants.EXTSFT_CONFIG_KEY)
|
|
.Elements("Software")
|
|
.Select(x => new SubModels.Software()
|
|
{
|
|
Path = ValidateSFTPath(x.Element("Path").Value),
|
|
LongName = ValidateSFTLName(x.Element("LongName").Value),
|
|
ShortName = ValidateSFTSName(x.Element("ShortName").Value),
|
|
IconBase64 = ExtractBase64Icon(x.Element("Path").Value)
|
|
}).ToArray();
|
|
|
|
|
|
|
|
// Read ARGS Config
|
|
if (!String.IsNullOrWhiteSpace(Arguments.Url))
|
|
Config.ConnectionConfig.Url = ValidateArgumentUrl(Arguments.Url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ValidationHandler(object sender, ValidationEventArgs e)
|
|
{
|
|
throw new Exception(@"Configuration Error: " + e.Message);
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region PROPERTIES_VALIDATOR
|
|
|
|
private static String ValidateUrl(String value)
|
|
{
|
|
Uri NewUrl;
|
|
if (Uri.TryCreate(value, UriKind.Absolute, out NewUrl) && (NewUrl.Scheme == Uri.UriSchemeHttp || NewUrl.Scheme == Uri.UriSchemeHttps || NewUrl.Scheme == Uri.UriSchemeFile))
|
|
return value;
|
|
else
|
|
throw new Exception(@"Configuration Error: ""Connection - Url"" is not a valid URL");
|
|
}
|
|
|
|
|
|
private static String ValidateArgumentUrl(String value)
|
|
{
|
|
Uri NewUrl;
|
|
if (Uri.TryCreate(value, UriKind.Absolute, out NewUrl) && (NewUrl.Scheme == Uri.UriSchemeHttp || NewUrl.Scheme == Uri.UriSchemeHttps || NewUrl.Scheme == Uri.UriSchemeFile))
|
|
return value;
|
|
else
|
|
throw new Exception(@"Argument Url Error: is not a valid URL");
|
|
}
|
|
|
|
|
|
|
|
private static Color ValidateTranspColor(String value)
|
|
{
|
|
Color color;
|
|
try
|
|
{
|
|
color = ColorTranslator.FromHtml(value);
|
|
return color;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new Exception(@"Configuration Error: ""Client - TranspColor"" is not a valid Hex Color");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static Boolean ValidateOpenHmi(String value)
|
|
{
|
|
Boolean OpenHmi;
|
|
if (Boolean.TryParse(value, out OpenHmi))
|
|
return OpenHmi;
|
|
else
|
|
throw new Exception(@"Configuration Error: ""VendorHmi - Enabled"" is not a valid Boolean Type");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ushort ValidateNcType(String value)
|
|
{
|
|
ushort Nc;
|
|
|
|
if (ushort.TryParse(value, out Nc) && Nc <= 3)
|
|
return Nc;
|
|
else
|
|
throw new Exception(@"Configuration Error: ""VendorHmi - Type"" is not a valid NC Type");
|
|
}
|
|
|
|
|
|
|
|
private static ushort ValidateClientID(String value)
|
|
{
|
|
ushort Client;
|
|
if (ushort.TryParse(value, out Client))
|
|
return Client;
|
|
else
|
|
throw new Exception(@"Configuration Error: ""Connection - Id"" is not a valid Id of CMS-Client");
|
|
}
|
|
|
|
|
|
|
|
private static Boolean ValidateDelCache(String value)
|
|
{
|
|
Boolean DelCache;
|
|
if (Boolean.TryParse(value, out DelCache))
|
|
return DelCache;
|
|
else
|
|
throw new Exception(@"Configuration Error: ""Connection - DeleteCahceFolderOnStartup"" is not a valid Boolean Type");
|
|
|
|
}
|
|
|
|
|
|
private static String ValidateSFTPath(String value)
|
|
{
|
|
if(File.Exists(value))
|
|
return value;
|
|
else
|
|
throw new Exception(@"Configuration Error: File """+ value + @""" not found");
|
|
|
|
}
|
|
|
|
|
|
private static String ValidateSFTSName(String value)
|
|
{
|
|
if (!String.IsNullOrEmpty(value))
|
|
{
|
|
if (value.Count() <= 3)
|
|
return value;
|
|
else
|
|
throw new Exception(@"Configuration Error: Short Name """ + value + @""" cannot be over 3 Letters");
|
|
}
|
|
else
|
|
throw new Exception(@"Configuration Error: Software Short Name Must be setted");
|
|
|
|
}
|
|
|
|
|
|
private static String ValidateSFTLName(String value)
|
|
{
|
|
if (!String.IsNullOrEmpty(value))
|
|
return value;
|
|
else
|
|
throw new Exception(@"Configuration Error: Software Long Name Must be setted");
|
|
|
|
}
|
|
|
|
|
|
private static String ExtractBase64Icon(String value)
|
|
{
|
|
Image im = Icon.ExtractAssociatedIcon(value).ToBitmap();
|
|
MemoryStream m = new MemoryStream();
|
|
im.Save(m, ImageFormat.Png);
|
|
return "data:image/png;base64," + Convert.ToBase64String(m.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
} |