SafeDispatch/CPlus_GW/Program.cs

227 lines
8.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
using System.Net.Sockets;
using Nini.Config;
using System.Windows.Forms;
using System.IO;
using SafeMobileLib;
namespace CPlus_GW
{
static class Program
{
public static Config cfg;
static String filename = "";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread()]
static void Main(string[] args)
{
cfg = new Config();
Console.Title = "CPLUSgwConsole";
if (!HandleCommandLineArgs(args))
return;
/*
if (args.Length > 0)
{
if (args[0] != "-c")
{
// hide the console window
setConsoleWindowVisibility(false, Console.Title);
}
if ((args.Length > 2) && (args[1] == ">"))
filename = args[2].ToString().Trim();
}
else
{
setConsoleWindowVisibility(false, Console.Title);
}
//code for console stuff
try
{
filename = filename.Trim();
if (filename.Length > 1)
{
FileStream filestream = new FileStream(filename, FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
}
}
catch (Exception ex)
{
Console.WriteLine("Exeception:" + ex.ToString());
}
*/
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new MainForm());
}
/// <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;
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
SafeMobileLib.Utils.WriteLine("Application_ThreadException " + e.ToString());
MessageBox.Show(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)
{
SafeMobileLib.Utils.WriteLine("CurrentDomain_UnhandledException " + e.ToString());
MessageBox.Show((e.ExceptionObject as Exception).ToString(), "UI Exception Gateway.Please restart the application.");
Application.Exit();
// here you can log the exception ...
}
[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);
}
}
}
}