using System; using System.Collections.Generic; using System.Text; using Nini.Config; using SafeMobileLib; using System.IO; namespace CPlus_GW { public class Config { private IConfigSource source = null; private string CFG_FILE = "GWconfig.ini"; public string DB_IP, DB_schema, DB_user, DB_passwd, DB_port; public string ctrlIP; public string multi_IP; public int multi_port; public int arsPort, locPort, smsPort; public int locPort_r, smsPort_r; public string gatewayID; public string report; public Boolean autoReconnect = false; // update public bool autoupdate = true; public bool isDevelop = false; //test public int suid, dialogid; public bool ping = true; //error reporting public string errorMSG = ""; public int tallysmanPort = 4010; //Constructor public Config() { LoadConfig(); } private void LoadConfig() { try { if (!File.Exists(CFG_FILE)) { SafeMobileLib.Utils.WriteLine("Config file didn't exist. Creating it...", ConsoleColor.Yellow); try { using (FileStream fs = File.Create(CFG_FILE)) { } } catch (Exception) { SafeMobileLib.Utils.WriteLine("Application ended!!!! " + CFG_FILE + " could not be created"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); System.Environment.Exit(0); } } source = new IniConfigSource(CFG_FILE); if (source.Configs["Database"] == null) source.Configs.Add("Database"); DB_IP = GetStringValue("Database", "IP", "127.0.0.1");// source.Configs["Database"].Get("IP"); DB_schema = GetStringValue("Database", "Schema", "safedispatchdb"); // source.Configs["Database"].Get("Schema"); DB_user = GetStringValue("Database", "User", "postgres"); // source.Configs["Database"].Get("User"); DB_passwd = GetStringValue("Database", "Passwd", "wizdemo26"); //source.Configs["Database"].Get("Passwd"); DB_port = GetStringValue("Database", "Port", "5432"); // source.Configs["Database"].Get("Port"); if (source.Configs["Controller"] == null) source.Configs.Add("Controller"); ctrlIP = GetStringValue("Controller", "IP", "127.0.0.1"); // source.Configs["Controller"].Get("IP"); arsPort = GetInt32Value("Controller", "ARSport", 4005); // Convert.ToInt32(source.Configs["Controller"].Get("ARSport")); locPort = GetInt32Value("Controller", "LocationPort", 4001); // Convert.ToInt32(source.Configs["Controller"].Get("LocationPort")); smsPort = GetInt32Value("Controller", "SMSport", 4007); // Convert.ToInt32(source.Configs["Controller"].Get("SMSport")); tallysmanPort = GetInt32Value("Controller", "TallysmanPort", 4010); locPort_r = GetInt32Value("Controller", "LocationPort_r", 50001); // Convert.ToInt32(source.Configs["Controller"].Get("LocationPort_r")); smsPort_r = GetInt32Value("Controller", "SMSport_r", 50002); // Convert.ToInt32(source.Configs["Controller"].Get("SMSport_r")); if (source.Configs["MessageBus"] == null) source.Configs.Add("MessageBus"); multi_IP = GetStringValue("MessageBus", "ip", "224.30.0.1"); //source.Configs["MessageBus"].Get("ip"); multi_port = GetInt32Value("MessageBus", "port", 17233); // Convert.ToInt32(source.Configs["MessageBus"].Get("port")); if (source.Configs["GATEWAY"] == null) source.Configs.Add("GATEWAY"); gatewayID = GetStringValue("GATEWAY", "id", "95001"); //source.Configs["GATEWAY"].Get("id"); report = GetStringValue("GATEWAY", "report", "30"); //source.Configs["GATEWAY"].Get("report"); autoReconnect = GetBooleanValue("GATEWAY", "autoReconnect", false); Console.WriteLine("Controller IP:" + ctrlIP); Console.WriteLine("Controller PN port:" + arsPort); if (source.Configs["Update"] == null) source.Configs.Add("Update"); autoupdate = GetBooleanValue("Update", "autoupdate", true); if (source.Configs["Update"].Contains("develop")) isDevelop = source.Configs["Update"].GetBoolean("develop"); GetStringValue("Update", "Version", "1.0.0.2"); GetBooleanValue("Update", "Enable", true); //Console.WriteLine("Test subscriber ID:" + suid); //Console.WriteLine("Test dialogID:" + dialogid); // Save the INI file source.Save(); } catch (Exception ex) { Console.WriteLine("LoadSetup() Exception: " + ex.ToString()); errorMSG = ex.ToString(); } Console.WriteLine("LoadSetup file OK !"); } public static void SaveConfig(string ctrlIP, string arsPort, string locPort, string smsPort, string tallysmanPort, string locPort_r, string smsPort_r, string multi_IP, string multi_port, string gatewayID, string report) { IniConfigSource source = new IniConfigSource("GWconfig.ini"); source.Configs["Controller"].Set("IP", ctrlIP); source.Configs["Controller"].Set("ARSport", arsPort); source.Configs["Controller"].Set("LocationPort", locPort); source.Configs["Controller"].Set("SMSport", smsPort); source.Configs["Controller"].Set("TallysmanPort", tallysmanPort); source.Configs["Controller"].Set("LocationPort_r", locPort_r); source.Configs["Controller"].Set("SMSport_r", smsPort_r); source.Configs["MessageBus"].Set("ip", multi_IP); source.Configs["MessageBus"].Set("port", multi_port); source.Configs["GATEWAY"].Set("id", gatewayID); source.Configs["GATEWAY"].Set("report", report); source.Save(); SM.Debug("Configs saved to file \"GWconfig.ini\""); } /// /// Get an int value from the configuration file. The desired value is identifiec by /// the key and the header under which is placed is pointed by the header. Also a default /// value can be set in case the value doesn't exist and needs to be created before /// beeing returned /// /// Header unde which the desired config parameter is placed /// The Key that designates the desired config parameter /// A default value that will be used and returned in case the /// config file doesn't have a value for the parameter /// Parameter value from the config file, or the default value in case it doesn't /// exist private Int32 GetInt32Value(String header, String key, Int32 defaultValue) { if (!source.Configs[header].Contains(key)) source.Configs[header].Set(key, defaultValue); return source.Configs[header].GetInt(key); } /// /// Get a string value from the configuration file. The desired value is identifiec by /// the key and the header under which is placed is pointed by the header. Also a default /// value can be set in case the value doesn't exist and needs to be created before /// beeing returned /// /// Header unde which the desired config parameter is placed /// The Key that designates the desired config parameter /// A default value that will be used and returned in case the /// config file doesn't have a value for the parameter /// Parameter value from the config file, or the default value in case it doesn't /// exist private String GetStringValue(String header, String key, String defaultValue) { if (!source.Configs[header].Contains(key)) source.Configs[header].Set(key, defaultValue); return source.Configs[header].GetString(key); } /// /// Get a boolean value from the configuration file. The desired value is identifiec by /// the key and the header under which is placed is pointed by the header. Also a default /// value can be set in case the value doesn't exist and needs to be created before /// beeing returned /// /// Header unde which the desired config parameter is placed /// The Key that designates the desired config parameter /// A default value that will be used and returned in case the /// config file doesn't have a value for the parameter /// Parameter value from the config file, or the default value in case it doesn't /// exist private Boolean GetBooleanValue(String header, String key, Boolean defaultValue) { if (!source.Configs[header].Contains(key)) source.Configs[header].Set(key, defaultValue); return source.Configs[header].GetBoolean(key); } public void SaveConfigOption(String header, String property, String value) { try { source = new IniConfigSource(CFG_FILE); //DataBase source.Configs[header].Set(property, value); // Save the INI file source.Save(); } catch (Exception ex) { SafeMobileLib.Utils.WriteLine(ex.ToString(), ConsoleColor.Red); } SafeMobileLib.Utils.WriteLine("SaveConfigOption OK"); } } }