SafeDispatch/AppServer/Config.cs

276 lines
11 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nini.Config;
using SafeMobileLib;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.IO;
namespace AppServer
{
public class Config
{
private IConfigSource source = null;
private string CFG_FILE = "appConfig.ini";
public string DB_IP, DB_schema, DB_user, DB_passwd, DB_port;
public string smtpServer = "";
public string pop3Server = "";
public Int32 pop3Port = 995;
public Int32 smtpPort = 465;
public bool popSSLState = true;
public bool smtpSSLState = true;
public bool enableEmailService = false;
public string emailAddress, emailPassword;
public Int64 lastEmailTime = 0;
public bool autoupdate = true;
public bool isDevelop = false;
public int regPort;
public int RecordingsPort;
public int RecordingsAudioPort;
public string msgBusIP;
public int msgBusPort;
public bool watchdog = false;
public bool enableRestarting = false;
public string gatewayType = "";
public int websocketTimer;
public string websocketUrl;
public string websocketUser;
public string websocketPass;
private Int32 _VoicePort = 17234;
public string LocalIPAddress { get; private set; } = null;
private void LoadConfig()
{
try
{
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)
{
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["Registration"] == null)
source.Configs.Add("Registration");
regPort = GetInt32Value("Registration", "port", 5680); // Convert.ToInt32(source.Configs["Registration"].Get("port"));
if (source.Configs["RecordingsServer"] == null)
source.Configs.Add("RecordingsServer");
RecordingsPort = GetInt32Value("RecordingsServer", "port", 5678); // Convert.ToInt32(source.Configs["RecordingsServer"].Get("port"));
RecordingsAudioPort = GetInt32Value("RecordingsServer", "portAudio", 15679); // Convert.ToInt32(source.Configs["RecordingsServer"].Get("portAudio"));
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");
msgBusIP = GetStringValue("MessageBus", "ip", "224.30.0.1"); // source.Configs["MessageBus"].Get("ip");
msgBusPort = GetInt32Value("MessageBus", "port", 17233); // Convert.ToInt32(source.Configs["MessageBus"].Get("port"));
if (source.Configs["VoiceMessageBus"] == null)
source.Configs.Add("VoiceMessageBus");
VoicePort = GetInt32Value("VoiceMessageBus", "port", 17234); //Int32.Parse(source.Configs["VoiceMessageBus"].Get("port"));
LocalIPAddress = GetStringValue("MessageBus", "localIP", "");
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);
if (source.Configs["Config"] == null)
source.Configs.Add("Config");
watchdog = GetBooleanValue("Config", "watchdog", false);
enableRestarting = GetBooleanValue("Config", "enableRestarting", false);
gatewayType = GetStringValue("Config", "Gateway", "");
if (source.Configs["Websocket"] == null)
source.Configs.Add("Websocket");
websocketTimer = GetInt32Value("Websocket", "WebsocketTimer_ms", 5000);
websocketUrl = GetStringValue("Websocket", "WebsocketUrl", "");
websocketUser = GetStringValue("Websocket", "WebsocketUser", "");
websocketPass = GetStringValue("Websocket", "WebsocketPassword", "");
DBemailServiceManager DB = new DBemailServiceManager(DB_IP, DB_schema, DB_user, DB_passwd, DB_port);
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
EmailService es = DB.GetEmailServiceSettings();
if (es != null)
{
enableEmailService = es.Enable;
emailAddress = es.EmailAddress;
emailPassword = es.Pass;
lastEmailTime = es.LastEmailTime;
pop3Server = es.PopServer;
smtpServer = es.SmtpServer;
pop3Port = es.PopPort;
smtpPort = es.SmtpPort;
popSSLState = es.IsPopSSL;
smtpSSLState = es.IsSmtpSSL;
}
if (enableEmailService)
SM.Debug("EmailService\n emailAddress=" + emailAddress + "\n " + pop3Server+":" + pop3Port + "\n " + smtpServer+":"+smtpPort);
else
SM.Debug("Email Service is not enabled");
MainForm.SendPOLLafterSMS = DB.GetPollSMSServiceSettings();
// Save the INI file
source.Save();
}
catch (Exception ex)
{
SM.Debug("LoadSetup() Exception: " + ex.Message);
}
SM.Debug("LoadSetup file OK !");
}
public Config()
{
LoadConfig();
}
public int VoicePort {
get { return _VoicePort; }
private set { _VoicePort = value; }
}
private void SaveCFGFile(string file)
{
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);
// Save the INI file
source.Save();
//UpdateFields();
}
catch (Exception ex)
{
SM.Debug("SaveCFGFile() Exception: " + ex.Message);
}
}
/// <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);
}
}
}