SafeDispatch/SafeMobileLIB_DLL/Helpers/CommandLineHelper.cs
2024-02-22 18:43:59 +02:00

184 lines
7.9 KiB
C#

using SafeMobileLib.Logs;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace SafeMobileLib.Helpers
{
public class CommandLineHelper
{
public static String LogFileName = "";
public AppType AppType = AppType.SAFEDISPATCH;
public RADIOTYPE ConfigurationType = RADIOTYPE.UNKNOWN;
public bool Console { get; }
public String ConsoleTitle { get; }
public bool ForceLogin { get; }
public bool Log { get; }
public int LogExpirationDays = 7;
public bool ErrorParsing = false;
public LogLevel LogLevel { get; }
public CommandLineHelper(string[] args, string consoleTitle)
{
this.ConsoleTitle = consoleTitle;
if (args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "-c" || args[i] == "--console" || args[i] == "console")
{
Console = true;
}
else if (args[i] == "-h" || args[i] == "--help")
GetHelp();
else if (args[i] == "-t" || args[i] == "--type")
{
if (i != args.Length - 1)
{
string nextArgs = args[i + 1];
if (nextArgs.Equals("safenet"))
AppType = AppType.SAFENET;
else if (nextArgs.Equals("safedispatch"))
AppType = AppType.SAFEDISPATCH;
}
}
else if (args[i] == "-l" || args[i] == "--log")
{
Log = true;
// check to see if the next argument is the number of expired days after which
// the logs should be deleted or if it's the log level name
while (i != args.Length - 1 && !args[i + 1].StartsWith("-"))
{
string nextArgs = args[i + 1];
int expirationDays = 0;
// try to parse the next argument as an int value for expiration days
if (Int32.TryParse(nextArgs, out expirationDays))
LogExpirationDays = expirationDays;
// verify if the next argument is the log level
else if (!nextArgs.StartsWith("-"))
{
LogLevel = LogLevel.GetLogLevel(nextArgs);
}
// move to the next argument
i++;
}
}
else if (args[i] == "forcelogin")
ForceLogin = true;
// get the type of the system in which the app runs
else if (args[i].TrimStart("-".ToCharArray()) == "motorola")
ConfigurationType = RADIOTYPE.MOTO;
else if (args[i].TrimStart("-".ToCharArray()) == "hytera")
ConfigurationType = RADIOTYPE.HYT;
else if (args[i].TrimStart("-".ToCharArray()) == "harris")
ConfigurationType = RADIOTYPE.HARRIS;
else if (args[i].TrimStart("-".ToCharArray()) == "atlas")
ConfigurationType = RADIOTYPE.ATLAS;
else if (args[i].TrimStart("-".ToCharArray()) == "tetra")
ConfigurationType = RADIOTYPE.TETRA;
else if (args[i].TrimStart("-".ToCharArray()) == "conplus")
ConfigurationType = RADIOTYPE.CONECTPLUS;
else if (args[i].TrimStart("-".ToCharArray()) == "repeater")
ConfigurationType = RADIOTYPE.REPEATER_TRBO;
else if (args[i].TrimStart("-".ToCharArray()) == "simoco")
ConfigurationType = RADIOTYPE.SIMOCO;
else if (args[i].TrimStart("-".ToCharArray()) == "excera")
ConfigurationType = RADIOTYPE.EXCERA;
else if (args[i].TrimStart("-".ToCharArray()) == "linx")
ConfigurationType = RADIOTYPE.LINX;
else if (args[i].TrimStart("-".ToCharArray()) == "linxb")
ConfigurationType = RADIOTYPE.LINXB;
else if (args[i].TrimStart("-".ToCharArray()) == "safenet")
ConfigurationType = RADIOTYPE.SAFENET;
else if (args[i].TrimStart("-".ToCharArray()) == "lisf")
ConfigurationType = RADIOTYPE.LISF;
}
}
// show or hide the console
setConsoleWindowVisibility(Console, ConsoleTitle);
// flag if console needs to be written or not
Utils.isConsoleVisible = Console;
// enable logs if required
if (Log)
{
LOGS.WRITE_LOGS = true;
LOGS l = new LOGS("");
LOGS.expirationDays = LogExpirationDays;
}
}
/// <summary>
/// Display the help in the console and then wait for the end user to press any to key to terminate
/// </summary>
private void GetHelp()
{
Utils.WriteLine(ConsoleTitle + " version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), ConsoleColor.Yellow);
Utils.WriteLine("\n Available option\n", ConsoleColor.Cyan);
Utils.WriteLine(" -h, --help Display informations about commands");
Utils.WriteLine(" -c, --console Display the console with desired output events and messages (default it will display all type of messages)");
Utils.WriteLine(" -l, --log [days] [loglevel] Store the console output to a log file places inside the gateway folder. The optional parameter allow to specify the number of days until automatically deleting the zip files. The log level parameter allows choosing the messages that will be added to the log file");
Utils.WriteLine(" -t, --type [safenet|safedispatch] Specify if the app will run under SafeNet system or SafeDisaptch system ");
Utils.WriteLine(" --forcelogin Old command used in previous SD systems");
Utils.WriteLine(" --sms : Display Text Messages related events and messages in the console");
Utils.WriteLine(" --gps : Display GPS related events and messages in the console");
Utils.WriteLine(" --xnl : Display XNL radio related events and messages in the console");
Utils.WriteLine("Press any key to exit...");
System.Console.ReadKey();
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void setConsoleWindowVisibility(bool visible, string title)
{
IntPtr hWnd = IntPtr.Zero;
while (hWnd == IntPtr.Zero)
{
hWnd = FindWindow(null, title);
if (hWnd != IntPtr.Zero)
{
if (!visible)
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
else
//Show window again
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}
Thread.Sleep(100);
}
}
}
}