SafeDispatch/SDRgateway/Config.cs

294 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nini.Config;
using System.Drawing;
using SafeMobileLib;
using System.IO;
using static SDRgateway.LIP;
namespace SDRgateway
{
public class Config
{
private IConfigSource source = null;
private string CFG_FILE = "GWconfig.ini";
public string APPLICATION_SERVER_IP;
// Gateway Reg
public Int16 regPort;
public Int32 GW_ID;
public String GW_IP;
public String GatewayName;
public string DB_IP, DB_schema, DB_user, DB_passwd, DB_port;
public String messageBusIP;
public int messageBusPort;
public string SDR_IP;
public int SDR_port;
public Int32 SDR_ISSI;
public string SDR_pass;
public string GPS_type;
public int SDR_ping;
public int SDR_TimeOut = 120;
public int SDR_StoreTM = 0;
public Int32 SDR_SFServer = 0;
public int SDR_CallOut = 1;
public bool SDR_Autoconnect = true;
public PositionError PositionErrorTolerance = PositionError.Unknown;
// update
public bool autoupdate = true;
public bool isDevelop = false;
//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;
public string domain;
public Config()
{
LoadConfig();
}
public 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["GATEWAY"] == null)
source.Configs.Add("GATEWAY");
regPort = (Int16)GetInt32Value("GATEWAY", "regPort", 5680);
GW_ID = GetInt32Value("GATEWAY", "id", -1);
GW_IP = GetStringValue("GATEWAY", "thisIP", "127.0.0.1");
GatewayName = GetStringValue("GATEWAY", "name", "Tetra SDR");
if (source.Configs["Server"] == null)
source.Configs.Add("Server");
APPLICATION_SERVER_IP = GetStringValue("Server", "IP", "127.0.0.1");
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["MessageBus"] == null)
source.Configs.Add("MessageBus");
messageBusIP = GetStringValue("MessageBus", "ip", "224.30.0.1"); // source.Configs["MessageBus"].Get("ip");
messageBusPort = GetInt32Value("MessageBus", "port", 17233); // Convert.ToInt32(source.Configs["MessageBus"].Get("port"));
if (source.Configs["SDR"] == null)
source.Configs.Add("SDR");
SDR_IP = GetStringValue("SDR", "SDR_IP", "127.0.0.1"); // source.Configs["SDR"].Get("SDR_IP");
SDR_port = GetInt32Value("SDR", "SDR_port", 6006); // Convert.ToInt32(source.Configs["SDR"].Get("SDR_port"));
SDR_ISSI = GetInt32Value("SDR", "SDR_ISSI", 101100); // Convert.ToUInt32(source.Configs["SDR"].Get("SDR_ISSI"));
SDR_pass = GetStringValue("SDR", "SDR_pass", "127.0.30.20"); // source.Configs["SDR"].Get("SDR_pass");
GPS_type = GetStringValue("SDR", "GPS_type", "LIP"); // source.Configs["SDR"].Get("GPS_type");
SDR_ping = GetInt32Value("SDR", "SDR_ping", 1); //Convert.ToInt32(source.Configs["SDR"].Get("SDR_ping"));
SDR_TimeOut = GetInt32Value("SDR", "SDR_TimeOut", 120); // Convert.ToInt32(source.Configs["SDR"].Get("SDR_TimeOut"));
SDR_StoreTM = GetInt32Value("SDR", "SDR_StoreTM", 0);
SDR_SFServer = GetInt32Value("SDR", "SDR_SFServer", 13999999);
SDR_CallOut = GetInt32Value("SDR", "SDR_CallOut", 0);
PositionErrorTolerance = PositionError.GetPositionError(GetStringValue("SDR", "Position_Error_Tolerance", "111"));
;
/*
if (source.Configs["SDR"].Get("SDR_StoreTM") != null)
SDR_StoreTM = Convert.ToInt32(source.Configs["SDR"].Get("SDR_StoreTM"));
if (source.Configs["SDR"].Get("SDR_SFServer") != null)
SDR_SFServer = Convert.ToUInt32(source.Configs["SDR"].Get("SDR_SFServer"));
if (source.Configs["SDR"].Get("SDR_CallOut") != null)
SDR_CallOut = Convert.ToInt32(source.Configs["SDR"].Get("SDR_CallOut"));
SDR_Autoconnect = Convert.ToBoolean(source.Configs["SDR"].Get("SDR_Autoconnect"));
*/
SDR_Autoconnect = GetBooleanValue("SDR", "SDR_Autoconnect", true);
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");
GetBooleanValue("Update", "Enable", true);
// Save the INI file
source.Save();
}
catch (Exception ex)
{
Utils.WriteLine("LoadSetup() Exception: " + ex.Message, ConsoleColor.Red);
}
Utils.WriteLine("LoadSetup file OK !");
}
public void SaveCFGFile()
{
try
{
source = new IniConfigSource(CFG_FILE);
//DataBase
source.Configs["Database"].Set("IP", DB_IP);
source.Configs["Database"].Set("Schema", DB_schema);
source.Configs["Database"].Set("User", DB_user);
source.Configs["Database"].Set("Passwd", DB_passwd);
source.Configs["Database"].Set("Port", DB_port);
//message buss
source.Configs["MessageBus"].Set("ip", messageBusIP);
source.Configs["MessageBus"].Set("port", messageBusPort);
//SDR
source.Configs["SDR"].Set("SDR_IP", SDR_IP);
source.Configs["SDR"].Set("SDR_port", SDR_port);
source.Configs["SDR"].Set("SDR_ISSI", SDR_ISSI);
source.Configs["SDR"].Set("SDR_pass", SDR_pass);
source.Configs["SDR"].Set("GPS_type", GPS_type);
source.Configs["SDR"].Set("SDR_StoreTM", SDR_StoreTM);
source.Configs["SDR"].Set("SDR_SFServer", SDR_SFServer);
source.Configs["SDR"].Set("SDR_CallOut", SDR_CallOut);
source.Configs["SDR"].Set("SDR_Autoconnect", SDR_Autoconnect);
source.Configs["SDR"].Set("Position_Error_Tolerance", PositionErrorTolerance.ToString());
// Save the INI file
source.Save();
}
catch (Exception ex)
{
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
}
Utils.WriteLine("SaveFile OK");
}
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)
{
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
}
Utils.WriteLine("SaveConfigOption OK");
}
/// <summary>
/// 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
/// </summary>
/// <param name="header">Header unde which the desired config parameter is placed</param>
/// <param name="key">The Key that designates the desired config parameter</param>
/// <param name="defaultValue">A default value that will be used and returned in case the
/// config file doesn't have a value for the parameter</param>
/// <returns>Parameter value from the config file, or the default value in case it doesn't
/// exist</returns>
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);
}
/// <summary>
/// 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
/// </summary>
/// <param name="header">Header unde which the desired config parameter is placed</param>
/// <param name="key">The Key that designates the desired config parameter</param>
/// <param name="defaultValue">A default value that will be used and returned in case the
/// config file doesn't have a value for the parameter</param>
/// <returns>Parameter value from the config file, or the default value in case it doesn't
/// exist</returns>
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);
}
/// <summary>
/// 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
/// </summary>
/// <param name="header">Header unde which the desired config parameter is placed</param>
/// <param name="key">The Key that designates the desired config parameter</param>
/// <param name="defaultValue">A default value that will be used and returned in case the
/// config file doesn't have a value for the parameter</param>
/// <returns>Parameter value from the config file, or the default value in case it doesn't
/// exist</returns>
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);
}
}
}