using System; using System.Collections.Generic; using System.Text; using Nini.Config; using SafeNetLib; using System.IO; namespace MotoTRBO_SOC { public class Config { private IConfigSource source = null; private string CFG_FILE = "config.ini"; public string ctrlIP; public int arsPort, locPort, smsPort, TALLYSMAN_PORT; public int locPort_r, smsPort_r; public string gw_id; public bool capPlus; public string masterRadioIP; public Int64 masterRadioID; public bool confSMS; public bool gw_restart; public int gw_restart_inactive_seconds; public bool locationStop = false; public int rep_inter_default; public int GPS_refresh; public bool autoupdate = false; public int debug_port; //email service public string smtpServer = ""; public string pop3Server = ""; public string pop3Port = ""; public string smtpPort = ""; public bool sslState = false; public bool enableEmailService = false; public string user, psw; //mysql public string SERVER; public string DATABASE; public string UID; public string PASSWORD; private Dictionary> configStructure = new Dictionary>() { {ConfigHeader.MOTOTRBO_PORTS, new List() {ConfigValues.LOCATION_PORT, ConfigValues.ARS_PORT, ConfigValues.SMS_PORT, ConfigValues.TALLYSMAN_PORT}}, {ConfigHeader.GATEWAY, new List() {ConfigValues.ID, ConfigValues.REPORT, ConfigValues.GPS_TRIGGER_REFRESH, ConfigValues.CAP_PLUS, ConfigValues.MASTER_RADIO_IP, ConfigValues.MASTER_RADIO_ID, ConfigValues.CONFIRM_SMS, ConfigValues.GW_RESTART, ConfigValues.GW_RESTART_INACTIVE_SECONDS, ConfigValues.AUTO_UPDATE, ConfigValues.LOCATION_STOP}}, {ConfigHeader.MYSQL, new List() {ConfigValues.SERVER, ConfigValues.DATABASE, ConfigValues.UID, ConfigValues.PASSWORD}}, {ConfigHeader.EMAIL_SERVICE, new List() {ConfigValues.ENABLE, ConfigValues.USER, ConfigValues.PASS, ConfigValues.POP3_SERVER, ConfigValues.SMTP_SERVER, ConfigValues.POP3_PORT, ConfigValues.SMTP_PORT, ConfigValues.ENABLE_SSL}} }; public Config() { //LoadConfig(); } public void LoadConfig() { if (!File.Exists(CFG_FILE)) { Utils.WriteLine("Config file didn't exist. Creating it...", ConsoleColor.Yellow); try { using (FileStream fs = File.Create(CFG_FILE)) { } } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!! " + CFG_FILE + " could not be created"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); System.Environment.Exit(0); } } FileInfo configFile = new FileInfo(CFG_FILE); try { // here the config file must exist source = new IniConfigSource(CFG_FILE); source.AutoSave = true; } catch (Exception ex) { Utils.WriteLine("Config File was not in the correct format![1] " + ex.ToString(), ConsoleColor.Red); try { // copy config file to a back-up one File.Copy(CFG_FILE, "config.ini_" + String.Format("{0:dd.MMM_HH.mm.ss}", DateTime.Now) + ".bak"); // delete config file File.Delete(CFG_FILE); // recreate file using (FileStream fs = File.Create(CFG_FILE)) { } if (OnConfigFileWasBroken != null) OnConfigFileWasBroken(); } catch (Exception ex2) { Utils.WriteLine("He's dead, Jim! " + ex2.ToString(), ConsoleColor.Red); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); System.Environment.Exit(0); } } try{ // here the config file must exist source = new IniConfigSource(CFG_FILE); source.AutoSave = true; foreach (KeyValuePair> pair in configStructure) { try { // check if the header exists if (source.Configs[pair.Key.ToString()] == null) { source.AddConfig(pair.Key.ToString()); foreach (ConfigValues cfg in pair.Value) source.Configs[pair.Key.ToString()].Set(cfg.ToString(), cfg.GetDefaultValue()); } else { foreach (ConfigValues cfg in pair.Value) { if (source.Configs[pair.Key.ToString()].Get(cfg.ToString()) == null) source.Configs[pair.Key.ToString()].Set(cfg.ToString(), cfg.GetDefaultValue()); } } } catch (Exception ex) { Utils.WriteLine("Could not write Config Header or value [" + pair.Key.ToString() + " ] " + ex.ToString(), ConsoleColor.Red); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); System.Environment.Exit(0); } } } catch (Exception ex) { Utils.WriteLine("Config File was not in the correct format[2] " + ex.ToString(), ConsoleColor.Red); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); System.Environment.Exit(0); } try { source = new IniConfigSource(CFG_FILE); gw_id = source.Configs["GATEWAY"].Get("id"); GPS_refresh = Convert.ToInt32(source.Configs["GATEWAY"].Get("GPS_trig_refresh")); rep_inter_default = Convert.ToInt32(source.Configs["GATEWAY"].Get("report")); capPlus = Convert.ToBoolean(source.Configs["GATEWAY"].Get("capPlus")); masterRadioIP = source.Configs["GATEWAY"].Get("MasterRadioIP"); masterRadioID = Convert.ToInt32(source.Configs["GATEWAY"].Get("MasterRadioID")); confSMS = Convert.ToBoolean(source.Configs["GATEWAY"].Get("confSMS")); gw_restart = Convert.ToBoolean(source.Configs["GATEWAY"].Get("gw_restart")); if (source.Configs["GATEWAY"].Get(ConfigValues.GW_RESTART_INACTIVE_SECONDS.ToString()) != null) gw_restart_inactive_seconds = Convert.ToInt32(source.Configs["GATEWAY"].Get(ConfigValues.GW_RESTART_INACTIVE_SECONDS.ToString())); else gw_restart_inactive_seconds = 60 * 10; locationStop = Convert.ToBoolean(source.Configs["GATEWAY"].Get("locationStop")); if (source.Configs["GATEWAY"].Get("autoupdate") == null) source.Configs["GATEWAY"].Set("autoupdate", false); else autoupdate = Convert.ToBoolean(source.Configs["GATEWAY"].Get("autoupdate")); pop3Server = source.Configs["EmailService"].Get("Pop3Server"); smtpServer = source.Configs["EmailService"].Get("SmtpServer"); pop3Port = source.Configs["EmailService"].Get("Pop3Port"); smtpPort = source.Configs["EmailService"].Get("SmtpPort"); sslState = Convert.ToBoolean(source.Configs["EmailService"].Get("EnableSSL")); enableEmailService = Convert.ToBoolean(source.Configs["EmailService"].Get("Enable")); user = source.Configs["EmailService"].Get("User"); psw = source.Configs["EmailService"].Get("Pass"); //MySQL connection SERVER = Encryption.Decrypt(source.Configs["MySQL"].Get("SERVER")); DATABASE = Encryption.Decrypt(source.Configs["MySQL"].Get("DATABASE")); UID = Encryption.Decrypt(source.Configs["MySQL"].Get("UID")); PASSWORD = Encryption.Decrypt(source.Configs["MySQL"].Get("PASSWORD")); //debug port is GWID with the first digit replage by 3. ex: 95001 -> 35001 string str_debug_port = "3" + gw_id.Substring(1); locPort = ushort.Parse(source.Configs["MotoTRBO_ports"].Get("LocationPort")); arsPort = ushort.Parse(source.Configs["MotoTRBO_ports"].Get("ARSPort")); smsPort = ushort.Parse(source.Configs["MotoTRBO_ports"].Get("SMSPort")); TALLYSMAN_PORT = ushort.Parse(source.Configs["MotoTRBO_ports"].Get("TALLYSMANPort")); debug_port = Int32.Parse(str_debug_port); } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, "LoadSetup() Exception: " + ex.ToString()); } Utils.ConsWrite(DebugMSG_Type.CTRL, "LoadSetup file OK !"); } /// /// Update a specific config file parameter from a desired category. /// /// Category in which the parameter is located /// Parameter which needs to be updated /// New value for the desired parameter public void UpdateConfigParameter(string category, string option, string value) { try { source = new IniConfigSource(CFG_FILE); //DataBase source.Configs[category].Set(option, value); // Save the INI file source.Save(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } public delegate void ConfigFileWasBroken(); public event ConfigFileWasBroken OnConfigFileWasBroken; } #region CONFIG STRING ENUMS public sealed class ConfigHeader { private readonly String name; private readonly int value; public static readonly ConfigHeader MOTOTRBO_PORTS = new ConfigHeader(1, "MotoTRBO_ports"); public static readonly ConfigHeader GATEWAY = new ConfigHeader(2, "GATEWAY"); public static readonly ConfigHeader MYSQL = new ConfigHeader(3, "MySQL"); public static readonly ConfigHeader EMAIL_SERVICE = new ConfigHeader(3, "EmailService"); private ConfigHeader(int value, String name) { this.name = name; this.value = value; } public override String ToString() { return name; } } public sealed class ConfigValues { private readonly String name; private readonly String defaultValue; private readonly int value; public static readonly ConfigValues LOCATION_PORT = new ConfigValues(11, "LocationPort", "4001"); public static readonly ConfigValues ARS_PORT = new ConfigValues(12, "ARSPort", "4005"); public static readonly ConfigValues SMS_PORT = new ConfigValues(13, "SMSPort", "4007"); public static readonly ConfigValues TALLYSMAN_PORT = new ConfigValues(14, "TALLYSMANPort", "4010"); public static readonly ConfigValues ID = new ConfigValues(21, "id", "666666"); public static readonly ConfigValues REPORT = new ConfigValues(22, "report", "60"); public static readonly ConfigValues GPS_TRIGGER_REFRESH = new ConfigValues(23, "GPS_trig_refresh", "8"); public static readonly ConfigValues CAP_PLUS = new ConfigValues(24, "capPlus", "true"); public static readonly ConfigValues MASTER_RADIO_IP = new ConfigValues(25, "MasterRadioIP", "192.168.10.1"); public static readonly ConfigValues MASTER_RADIO_ID = new ConfigValues(25, "MasterRadioID", "1"); public static readonly ConfigValues CONFIRM_SMS = new ConfigValues(26, "confSMS", "true"); public static readonly ConfigValues GW_RESTART = new ConfigValues(27, "gw_restart", "false"); public static readonly ConfigValues AUTO_UPDATE = new ConfigValues(28, "autoupdate", "true"); public static readonly ConfigValues GW_RESTART_INACTIVE_SECONDS = new ConfigValues(210, "gw_restart_inactive_seconds", "600"); public static readonly ConfigValues LOCATION_STOP = new ConfigValues(29, "locationStop", "false"); public static readonly ConfigValues SERVER = new ConfigValues(31, "SERVER", "ifqNConpwMO2SAGta2monK+/fXuIK0gG+yH/By/fKWTTSHvNzVOJB6WKfOLHGcA1R9csQWwX4kU="); public static readonly ConfigValues DATABASE = new ConfigValues(32, "DATABASE", "9F5Ufu/mgM9Btd7HWDylwg=="); public static readonly ConfigValues UID = new ConfigValues(33, "UID", "5wQPPYB9Xwg="); public static readonly ConfigValues PASSWORD = new ConfigValues(34, "PASSWORD", "YTXqw1P9AMJH1yxBbBfiRQ=="); public static readonly ConfigValues ENABLE = new ConfigValues(41, "Enable", "false"); public static readonly ConfigValues USER = new ConfigValues(42, "User", "bigutransfer@gmail.com"); public static readonly ConfigValues PASS = new ConfigValues(43, "Pass", "gigelamd01"); public static readonly ConfigValues POP3_SERVER = new ConfigValues(44, "Pop3Server", "pop.gmail.com"); public static readonly ConfigValues SMTP_SERVER = new ConfigValues(45, "SmtpServer", "smtp.gmail.com"); public static readonly ConfigValues ENABLE_SSL = new ConfigValues(46, "EnableSSL", "true"); public static readonly ConfigValues POP3_PORT = new ConfigValues(47, "Pop3Port", "995"); public static readonly ConfigValues SMTP_PORT = new ConfigValues(48, "SmtpPort", "587"); private ConfigValues(int value, String name, String defaultValue) { this.name = name; this.value = value; this.defaultValue = defaultValue; } public override String ToString() { return name; } public String GetDefaultValue() { return defaultValue; } } #endregion }