using Nini.Config;
using SafeMobileLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tetra_GW.Helpers
{
class ConfigHelper
{
private static IConfigSource source = null;
public static string CFG_FILE = "GWconfig.ini";
//DB
public static string APPLICATION_SERVER_IP, DB_IP, DB_schema, DB_user, DB_passwd, DB_port;
public static String messageBusIP = "224.30.0.1";
public static int messageBusPort = 17233;
public static int voicePort = 17234;
public static String localIP = "";
// Gateway Reg
public static Int16 regPort = 5680;
public static Int32 GW_ID = 1;
public static String GW_IP = "192.168.10.1";
public static String GatewayName = "Tetra";
public static String SerialPort = "";
public static String ReportType = "LONG";
public static Int32 BaudRate = 115200;
public static Int32 Area = 0;
public static bool GW_Autoconnect = true;
// audio index
public static String SpeakerDeviceName = "";
public static String MicDeviceName = "";
public static bool HasPresenceCheck = false;
// update
public static bool autoupdate = true;
public static bool isDevelop = false;
//Memory
public static bool RestartOnMemoryLeak = false;
public static long MaxMemoryAllowedMb = 512;
public static void LoadConfig()
{
try
{
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");
SerialPort = GetStringValue("Gateway", "serialPort", "");
GW_Autoconnect = GetBooleanValue("Gateway", "autoConnect", true);
localIP = GetStringValue("Gateway", "localIP", "");
BaudRate = GetInt32Value("Gateway", "BaudRate", 9600);
Area = GetInt32Value("Gateway", "Area", 0);
if (source.Configs["Audio"] == null)
source.Configs.Add("Audio");
SpeakerDeviceName = GetStringValue("Audio", "inDevice", "");
MicDeviceName = GetStringValue("Audio", "outDevice", "");
HasPresenceCheck = GetBooleanValue("Audio", "hasPresenceCheck", false);
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");
if (source.Configs["Server"] == null)
source.Configs.Add("Server");
APPLICATION_SERVER_IP = GetStringValue("Server", "IP", "127.0.0.1");
if (source.Configs["TYPE"] == null)
source.Configs.Add("TYPE");
ReportType = GetStringValue("TYPE", "ReportType", "");
string header = "Memory";
if (source.Configs[header] == null)
source.Configs.Add(header);
if (source.Configs[header].Contains("restartOnMemoryLeak"))
RestartOnMemoryLeak = source.Configs[header].GetBoolean("restartOnMemoryLeak");
if (source.Configs[header].Contains("maxMemoryAllowedMb"))
MaxMemoryAllowedMb = source.Configs[header].GetLong("maxMemoryAllowedMb");
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine("ConfigHelper [LoadConfig]: " + ex.ToString());
}
}
#region Save methods
public static void SaveSpeakerDevice(String deviceName)
{
SaveStringValue("Audio", "outDevice", deviceName);
}
public static void SaveMicDevice(String deviceName)
{
SaveStringValue("Audio", "inDevice", deviceName);
}
public static void SaveSerialPort(String serialPort)
{
SaveStringValue("Gateway", "serialPort", serialPort);
}
public static void SaveAutoConnect(bool isAutoConnect)
{
SaveStringValue("Gateway", "autoConnect", isAutoConnect ? "true" : "false");
}
public static void SaveReportType(String ReportType)
{
SaveStringValue("TYPE", "ReportType", ReportType);
}
private static void SaveStringValue(String header, String key, String value)
{
if (source.Configs[header] == null)
source.Configs.Add(header);
source.Configs[header].Set(key, value);
source.Save();
}
private static void SaveInt32Value(String header, String key, Int32 value)
{
if (source.Configs[header] == null)
source.Configs.Add(header);
source.Configs[header].Set(key, value);
source.Save();
}
#endregion
#region Get methods
///
/// 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 static Int32 GetInt32Value(String header, String key, Int32 defaultValue)
{
if (source.Configs[header] == null)
return 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 static String GetStringValue(String header, String key, String defaultValue)
{
if (source.Configs[header] == null)
return 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 static Boolean GetBooleanValue(String header, String key, Boolean defaultValue)
{
if (source.Configs[header] == null)
return defaultValue;
if (!source.Configs[header].Contains(key))
source.Configs[header].Set(key, defaultValue);
return source.Configs[header].GetBoolean(key);
}
#endregion
}
}