295 lines
11 KiB
C#
295 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.IO;
|
|
using SafeMobileLib;
|
|
|
|
using Microsoft.VisualBasic.ApplicationServices;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
|
|
namespace MotoTrbo_GW
|
|
{
|
|
static class Program
|
|
{
|
|
|
|
public static bool isRunning = true;
|
|
public static bool isConsoleEnabled = false;
|
|
|
|
static Main MainForm = null;
|
|
|
|
static String filename = "";
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread()]
|
|
static void Main(string[] args)
|
|
{
|
|
Console.Title = "MotoTRBOgwConsole";
|
|
|
|
// register for console events in order to intercept the close event
|
|
handler = new ConsoleEventDelegate(ConsoleEventCallback);
|
|
|
|
setConsoleWindowVisibility(true, Console.Title);
|
|
|
|
/*
|
|
String message = "80050822042468AC005162";
|
|
message = message.Substring(2);
|
|
|
|
byte[] hex = Enumerable.Range(0, message.Length)
|
|
.Where(x => x % 2 == 0)
|
|
.Select(x => Convert.ToByte(message.Substring(x, 2), 16))
|
|
.ToArray();
|
|
SafeMobileLib.MessageDecoders.LocationDecoder loc = new SafeMobileLib.MessageDecoders.LocationDecoder("101", hex, false);
|
|
|
|
;*/
|
|
|
|
|
|
if (!HandleCommandLineArgs(args))
|
|
return;
|
|
|
|
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
|
|
//AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
|
|
|
|
// minimize the console
|
|
SendMessage(Process.GetCurrentProcess().MainWindowHandle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
|
|
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
SingleInstanceController controller = new SingleInstanceController();
|
|
controller.Run(args);
|
|
|
|
//MainForm = new Main();
|
|
//Application.Run(new Main());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check to see which Command Line Args were passed to the application
|
|
/// </summary>
|
|
/// <param name="args">String array containing all command line arguments</param>
|
|
/// <return>A value indicating if the application needs to be closed or not</return>
|
|
private static bool HandleCommandLineArgs(string[] args)
|
|
{
|
|
// reset the console arguments
|
|
SafeMobileLib.Utils.outputConsoleType = new List<ConsoleType>();
|
|
|
|
// hide the console window
|
|
setConsoleWindowVisibility(false, Console.Title);
|
|
|
|
if (args.Length > 0)
|
|
{
|
|
for (int i = 0; i < args.Length; i++)
|
|
{
|
|
if (args[i] == "-c")
|
|
{
|
|
// hide the console window
|
|
setConsoleWindowVisibility(true, Console.Title);
|
|
}
|
|
else if(args[i] == "-h")
|
|
{
|
|
setConsoleWindowVisibility(true, Console.Title);
|
|
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
Console.WriteLine(Console.Title + " version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
|
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
Console.WriteLine("\n Available option\n");
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.WriteLine(" -h : Display informations about commands");
|
|
Console.WriteLine(" -c : Display the console with desired output events and messages (default it will display all type of messages)");
|
|
Console.WriteLine(" -l : Store the console output to a log file places inside the gateway folder");
|
|
//Console.WriteLine("\n");
|
|
Console.WriteLine(" --sns : Display Text Messages related events and messages in the console");
|
|
Console.WriteLine(" --gps : Display GPS related events and messages in the console");
|
|
Console.WriteLine(" --xnl : Display XNL radio related events and messages in the console");
|
|
|
|
Console.WriteLine("Press any key to exit...");
|
|
Console.ReadKey();
|
|
}
|
|
else if (args[i] == "-l")
|
|
{
|
|
LOGS.WRITE_LOGS = true;
|
|
LOGS l = new LOGS("");
|
|
}
|
|
|
|
else if(args[i] == "--sms")
|
|
{
|
|
SafeMobileLib.Utils.outputConsoleType.Add(ConsoleType.SMS);
|
|
}
|
|
|
|
else if (args[i] == "--gps")
|
|
{
|
|
SafeMobileLib.Utils.outputConsoleType.Add(ConsoleType.GPS);
|
|
}
|
|
|
|
else if (args[i] == "--xnl")
|
|
{
|
|
SafeMobileLib.Utils.outputConsoleType.Add(ConsoleType.XNL);
|
|
}
|
|
}
|
|
/*
|
|
if ((args.Length > 2) && (args[1] == ">"))
|
|
filename = args[2].ToString().Trim();*/
|
|
}
|
|
|
|
// display all if nothing
|
|
if (SafeMobileLib.Utils.outputConsoleType.Count == 0)
|
|
SafeMobileLib.Utils.outputConsoleType.Add(ConsoleType.ALL);
|
|
|
|
//code for console stuff
|
|
try
|
|
{
|
|
filename = filename.Trim();
|
|
if (filename.Length > 1)
|
|
{
|
|
using (FileStream filestream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
|
|
{
|
|
using (var streamwriter = new StreamWriter(filestream))
|
|
{
|
|
streamwriter.AutoFlush = true;
|
|
Console.SetOut(streamwriter);
|
|
Console.SetError(streamwriter);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SafeMobileLib.Utils.WriteLine("Exeception:" + ex.ToString());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
public class SingleInstanceController : WindowsFormsApplicationBase
|
|
{
|
|
public static ReadOnlyCollection<string> CommandLineArgs = null;
|
|
|
|
public SingleInstanceController()
|
|
{
|
|
IsSingleInstance = true;
|
|
|
|
Startup += SingleInstanceController_Startup;
|
|
StartupNextInstance += this_StartupNextInstance;
|
|
}
|
|
|
|
void SingleInstanceController_Startup(object sender, StartupEventArgs e)
|
|
{
|
|
}
|
|
|
|
void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
|
|
{
|
|
if (MainForm.WindowState == FormWindowState.Minimized)
|
|
{
|
|
MainForm.Show();
|
|
MainForm.ShowInTaskbar = true;
|
|
MainForm.WindowState = FormWindowState.Normal;
|
|
// bring to front
|
|
MainForm.Activate();
|
|
}
|
|
}
|
|
|
|
protected override void OnCreateMainForm()
|
|
{
|
|
MainForm = new Main();
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
|
{
|
|
FeedbackRadMessageBox.ShowError(e.Exception.ToString(), "ThreadException Gateway.Please restart the application.");
|
|
Application.Exit();
|
|
// here you can log the exception ...
|
|
}
|
|
|
|
/*
|
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
FeedbackRadMessageBox.ShowError((e.ExceptionObject as Exception).ToString(), "UI Exception Gateway.Please restart the application.");
|
|
Application.Exit();
|
|
// here you can log the exception ...
|
|
}*/
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
internal static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
|
|
static Int32 WM_SYSCOMMAND = 0x0112;
|
|
static Int32 SC_MINIMIZE = 0x0F020;
|
|
|
|
|
|
|
|
[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)
|
|
{
|
|
isConsoleEnabled = visible;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
static bool ConsoleEventCallback(int eventType)
|
|
{
|
|
int CTRL_C_EVENT = 0;
|
|
int CTRL_BREAK_EVENT = 1;
|
|
int CTRL_CLOSE_EVENT = 2;
|
|
int CTRL_LOGOFF_EVENT = 5;
|
|
int CTRL_SHUTDOWN_EVENT = 6;
|
|
|
|
if (eventType == CTRL_C_EVENT || eventType == CTRL_BREAK_EVENT || eventType == CTRL_CLOSE_EVENT
|
|
|| eventType == CTRL_LOGOFF_EVENT || eventType == CTRL_SHUTDOWN_EVENT)
|
|
{
|
|
SafeMobileLib.Utils.WriteLine("Console window closing, death imminent");
|
|
|
|
SafeMobileLib.Utils.WriteEventLog("SafeMobile", "Gateway is closing, death imminent", EventLogEntryType.Warning, 21522);
|
|
|
|
// check if the main form was initiated and an invoke is required
|
|
if (MainForm != null && MainForm.InvokeRequired)
|
|
{
|
|
// call the Close event for the Main form using an invoke because it is called
|
|
// from another thread
|
|
MainForm.Invoke((MethodInvoker)delegate ()
|
|
{
|
|
MainForm.Close();
|
|
});
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
static ConsoleEventDelegate handler; // Keeps it from getting garbage collected
|
|
// Pinvoke
|
|
private delegate bool ConsoleEventDelegate(int eventType);
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
|
|
}
|
|
}
|