using Client.Utils; using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Xml.Linq; using System.Xml.Schema; namespace Client.Config { public class ConfigController { const String ChromeScheme = "chrome"; 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() { EnableTransparent = ValidateEnableTransparent(x.Element("EnableTransparent").Value), TranspColor = ValidateTranspColor(x.Element("TranspColor").Value), RenderingMethod = ValidateRendering(x.Element("RenderingMethod").Value), RunningOnSecondaryScreen = ValidateSecScreen(x.Element("RunningOnSecondaryScreen").Value), ShowVirtualKeyboard = ValidateVirtualKeyboard(x.Element("ShowVirtualKeyboard").Value), DeveloperMode = ValidateDeveloperMode(x.Element("DeveloperMode").Value), IsSCM = ValidateIsSCM(x.Element("IsSCM").Value) }).FirstOrDefault(); Config.ConnectionConfig = xmlConfigFile .Descendants(Constants.CONNECTION_CONFIG_KEY) .Select(x => new SubModels.Connection() { ServerUrl = ValidateServerUrl(x.Element("ServerUrl").Value), ServerPort = ValidateServerPort(x.Element("ServerPort").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() { FollowNcWindow = ValidateFollowNcWin(x.Element("FollowNcWindow").Value) }).FirstOrDefault(); Config.ProdSoftwareConfig = new SubModels.ProdSoftware(); Config.ExtSoftwaresConfig = new SubModels.Software[] { }; //ReadConfig Url compositing Config.ConnectionConfig.ReadConfigUrl = "http://" + Config.ConnectionConfig.ServerUrl + ":" + Config.ConnectionConfig.ServerPort + "/" + Constants.ConfigPage; Config.ConnectionConfig.StartingUrl = "http://" + Config.ConnectionConfig.ServerUrl + ":" + Config.ConnectionConfig.ServerPort + "/" + Constants.StartingPage; //ErrorPage content if (File.Exists(Constants.errorPageFile)) Config.ConnectionConfig.ErrorPage = File.ReadAllText(Constants.errorPageFile); else throw new Exception(@"File Error: """ + Constants.errorPageFile + @""" not found"); } private static void ValidationHandler(object sender, ValidationEventArgs e) { throw new Exception(@"Configuration Error: " + e.Message); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region PROPERTIES_VALIDATOR private static String ValidateServerUrl(String value) { if (!String.IsNullOrWhiteSpace(value)) return value; else throw new Exception(@"Configuration Error: ""Connection - ServerUrl"" is not a valid URL"); } private static ushort ValidateServerPort(String value) { if (ushort.TryParse(value, out ushort Port)) return Port; else throw new Exception(@"Configuration Error: ""Connection - ServerPort"" is not a valid Id of CMS-Client"); } 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 || NewUrl.Scheme == ChromeScheme)) 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 Constants.Rendering ValidateRendering(string value) { if (value.ToUpper().Equals("CPU")) return Constants.Rendering.CPU; else if (value.ToUpper().Equals("GPU")) return Constants.Rendering.GPU; else throw new Exception(@"Configuration Error: ""Client - RenderingMethod"" is not a valid Rendering Method"); } private static bool ValidateSecScreen(string value) { Boolean secScreen; if (Boolean.TryParse(value, out secScreen)) return secScreen; else throw new Exception(@"Configuration Error: ""Client - RunningOnSecondaryScreen"" is not a valid Boolean Type"); } private static bool ValidateIsSCM(string value) { Boolean IsSCM; if (Boolean.TryParse(value, out IsSCM)) return IsSCM; else throw new Exception(@"Configuration Error: ""Client - IsSCM"" is not a valid Boolean Type"); } private static bool ValidateVirtualKeyboard(string value) { Boolean keyboard; if (Boolean.TryParse(value, out keyboard)) return keyboard; else throw new Exception(@"Configuration Error: ""Client - ShowVirtualKeyboard"" is not a valid Boolean Type"); } private static bool ValidateDeveloperMode(string value) { Boolean DeveloperMode; if (Boolean.TryParse(value, out DeveloperMode)) return DeveloperMode; else throw new Exception(@"Configuration Error: ""Client - DeveloperMode"" is not a valid Boolean Type"); } private static bool ValidateEnableTransparent(string value) { Boolean DeveloperMode; if (Boolean.TryParse(value, out DeveloperMode)) return DeveloperMode; else throw new Exception(@"Configuration Error: ""Client - EnableTransparent"" is not a valid Boolean Type"); } private static Boolean ValidateFollowNcWin(String value) { Boolean FollowNc; if (Boolean.TryParse(value, out FollowNc)) return FollowNc; else throw new Exception(@"Configuration Error: ""VendorHmi - FollowNcWindow"" is not a valid Boolean 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 bool ValidateInMainMenuBar(String value) { if (!String.IsNullOrEmpty(value)) { return Boolean.Parse(value); } else throw new Exception(@"Configuration Error: In-Main-Menu-Bar 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 } }