using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nini.Config;
using System.Windows.Forms;
using Telerik.WinControls;
using SafeMobileLib;
using System.Net;
namespace Safedispatch_4_0
{
public class Config
{
private IConfigSource source = null;
private string CFG_FILE = "config.ini";
//DB
public string APPLICATION_SERVER_IP, DB_IP, DB_schema, DB_user, DB_passwd, DB_port;
//Recording server
public string REC_IP, REC_port, REC_audio_port;
//Message bus
public string MSG_BUS_IP,MSg_BUS_port;
//Voice message buss
public string V_MSG_BUS_port;
//Login and Pass
public string Login,Pass,Lang;
//Regsitration port
public Int32 RegsPort;
public Int32 MapCallTimeout = 10;
public Boolean MapDebugConsole = false;
public Boolean MapRestart = false;
public Int64 MapRestartMinutes = 60;
public static string defaultOSM = "192.168.65.130";
public string ReverseGeoOSM = defaultOSM;
public int maxPositionsForMap = 300;
public int maxAlertsNumber = 25;
public bool autoupdate = true;
public bool isDevelope = false;
public bool hasFailover = false;
public bool appServerMonitoring = true;
public bool appServerPopUp = true;
public string LocalIP { get; private set; } = "";
public void LoadConfig()
{
try
{
source = new IniConfigSource(CFG_FILE);
APPLICATION_SERVER_IP = source.Configs["Server"].Get("IP");
//DB_schema = source.Configs["Database"].Get("Schema");
//DB_user = source.Configs["Database"].Get("User");
//DB_passwd = source.Configs["Database"].Get("Passwd");
//DB_port = source.Configs["Database"].Get("Port");
MainForm2.googlemaptype = source.Configs["Google"].Get("MapType").ToLower();
if ((MainForm2.googlemaptype == "") || (MainForm2.googlemaptype == " "))
MainForm2.googlemaptype ="map";
if (MainForm2.googlemaptype.Contains("map"))
MainForm2.MapHtmlName = "SfmbMapsMainMap.html";
else if (MainForm2.googlemaptype.Contains("hybrid"))
MainForm2.MapHtmlName = "SfmbMapsMain.html";
else if (MainForm2.googlemaptype.Contains("satellite"))
MainForm2.MapHtmlName = "SfmbMapsMainSatelitte.html";
REC_IP = source.Configs["Recordings"].Get("IP");
REC_port = source.Configs["Recordings"].Get("Port");
REC_audio_port = source.Configs["Recordings"].Get("AudioPort");
try
{
if (!source.Configs["Update"].Contains("autoupdate"))
source.Configs["Update"].Set("autoupdate", true);
autoupdate = source.Configs["Update"].GetBoolean("autoupdate");
if (source.Configs["Update"].Contains("develop"))
isDevelope = source.Configs["Update"].GetBoolean("develop");
}
catch (Exception ex)
{
Utils.WriteLine("AUTOUPDATE " + ex.ToString(), ConsoleColor.Red);
}
//MSG_BUS_IP = source.Configs["MessageBus"].Get("ip");
//MSg_BUS_port = source.Configs["MessageBus"].Get("port");
RegsPort = Convert.ToInt32(source.Configs["MessageBus"].Get("registport"));
MSg_BUS_port = GetStringValue("MessageBus", "port", "0");
if (source.Configs["Server"].Contains("localIP"))
LocalIP = source.Configs["Server"].Get("localIP");
try
{
MainForm2.uniqueID = Convert.ToInt32(source.Configs["MessageBus"].Get("uniqueid"));
if (source.Configs["MessageBus"].Contains("hasfailover"))
hasFailover = source.Configs["MessageBus"].GetBoolean("hasfailover");
}
catch
{
SM.Debug("Unable to read UniqueID");
MainForm2.uniqueID = 1;
}
try
{
MapCallTimeout = Convert.ToInt32(source.Configs["Google"].Get("mapCallTimeout"));
}
catch
{
SM.Debug("Unable to read mapCallTimeout");
MapCallTimeout = 10;
}
try
{
MapDebugConsole = Convert.ToBoolean(source.Configs["Google"].Get("mapDebugConsole"));
}
catch
{
SM.Debug("Unable to read mapDebugConsole");
MapDebugConsole = false;
}
try
{
MapRestart = Convert.ToBoolean(source.Configs["Google"].Get("mapRestart"));
MapRestartMinutes = Convert.ToInt64(source.Configs["Google"].Get("mapRestartMinutes"));
}
catch
{
SM.Debug("Unable to read mapRestart");
MapRestart = false;
MapRestartMinutes = 60;
}
try
{
ReverseGeoOSM = source.Configs["Google"].Get("ReverseGeoOSM");
IPAddress addr = null;
bool result = IPAddress.TryParse(ReverseGeoOSM, out addr);
if (ReverseGeoOSM.Length < 3)
ReverseGeoOSM = defaultOSM;
}
catch
{
SM.Debug("Unable to read ReverseGeoOSM");
ReverseGeoOSM = defaultOSM;
}
try
{
maxPositionsForMap = Convert.ToInt32(source.Configs["Google"].Get("maxPositionsForMap"));
if (maxPositionsForMap == 0)
maxPositionsForMap = 300;
}
catch
{
SM.Debug("Unable to read maxPositionsForMap");
maxPositionsForMap = 300;
}
try
{
maxAlertsNumber = Convert.ToInt32(source.Configs["User"].Get("maxAlertsNumber"));
if (maxAlertsNumber == 0)
maxAlertsNumber = 25;
}
catch
{
SM.Debug("Unable to read maxAlertsNumber");
maxAlertsNumber = 25;
}
V_MSG_BUS_port = source.Configs["VoiceMessageBus"].Get("port");
Login = source.Configs["User"].Get("Login");
Pass = source.Configs["User"].Get("Password");
Lang = source.Configs["User"].Get("Language");
try
{
if (source.Configs["User"].Contains("appServerMonitoring"))
appServerMonitoring = source.Configs["User"].GetBoolean("appServerMonitoring");
}
catch(Exception ex)
{
appServerMonitoring = true;
}
try
{
if (source.Configs["User"].Contains("appServerPopUp"))
appServerPopUp = source.Configs["User"].GetBoolean("appServerPopUp");
}
catch (Exception ex)
{
appServerMonitoring = true;
}
}
catch (Exception ex)
{
throw new ArgumentException("Error read config");
}
SM.Debug("LoadSetup file OK !");
}
public Config()
{
//LoadConfig();
}
public void SavePass(String login,String pass,String langidx)
{
source = new IniConfigSource(CFG_FILE);
source.Configs["User"].Set("Login",login);
source.Configs["User"].Set("Password", pass);
source.Configs["User"].Set("Language", langidx);
// Save the INI file
source.Save();
}
public void SaveMapType(String maptypename)
{
source = new IniConfigSource(CFG_FILE);
source.Configs["Google"].Set("MapType", maptypename);
// Save the INI file
source.Save();
}
public String GetCodeLogRemoveX(String code)
{
try
{
String codeStr = code;
StringBuilder inSb = new StringBuilder(codeStr);
StringBuilder outSb = new StringBuilder(codeStr.Length - 2);
int key = 145;
int key2 = 85;
char c;
for (int i = 1; i < codeStr.Length - 1; i++)
{
c = inSb[i];
if ((i - 1) % 2 == 0) c = (char)(c ^ key);
else c = (char)(c ^ key2);
outSb.Append(c);
}
return outSb.ToString();
}
catch (Exception ex)
{
SM.Debug("Error convert password:" + ex.ToString());
return "";
}
}
public String GetCodeLogPutX(String code)
{
try
{
String codeStr = code;
StringBuilder inSb = new StringBuilder(codeStr);
StringBuilder outSb = new StringBuilder(codeStr.Length + 2);
int key = 145;
int key2 = 85;
char c;
outSb.Append('X');
for (int i = 0; i < codeStr.Length; i++)
{
c = inSb[i];
if (i % 2 == 0) c = (char)(c ^ key);
else c = (char)(c ^ key2);
outSb.Append(c);
}
outSb.Append('X');
return outSb.ToString();
}
catch (Exception ex)
{
SM.Debug("Error convert password:" + ex.ToString());
return "";
}
}
///
/// 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 UpdateConfigParameter(string category, string option, string value)
{
try
{
if (source.Configs[category] == null)
source.Configs.Add(category);
if (source.Configs[category] != null)
source.Configs[category].Set(option, value);
// Save the INI file
source.Save();
}
catch (Exception ex)
{
Utils.WriteLine("Update Config Parameters: " + ex.ToString(), ConsoleColor.Red);
}
}
}
}