SafeDispatch/MotoRepeaterCore/Program.cs

187 lines
6.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;
using SafeMobileLib;
using SafeMobileLib.MessageDecoders;
namespace MotoRepeater
{
static class Program
{
public static string[] commandLineArgs;
static MainForm MainWindow = null;
static String filename = "";
public static LoginForm lf = null;
static void Main(string[] args)
{
commandLineArgs = args;
//error with this
//0xD 0x20 0x22 0x4 0x0 0x0 0x0 0xEE 0x34 0x1F 0x8C 0x82 0xC9 0xE1 0x69 0x32 0xE6 0x68 0x3C 0xBA 0xA4 0xDE 0x7A 0x82 0xF 0x54 0x6C 0x0 0x6 0x70 0x40 0x6 0x65 0x51
/*
String[] data = new string[] { "0D", "28", "22", "04", "00", "00", "00", "EE", "34", "1F", "8C", "82", "C9", "E1", "69", "32", "E6", "6B", "3C", "BA",
"A4", "DE", "7A", "82", "0F", "54", "6C", "00", "06", "70", "40", "06", "65", "51" };
LocationDecoder loc = new LocationDecoder("0", data.Select(s => Convert.ToByte(s, 16)).ToArray());
Console.ReadKey();
*/
//String[] data = new string[] { "07", "1A", "22", "04", "24", "68", "AC",
// "E0", "34", "1F", "4D", "BC", "77", "80", "51", "11", "8E", "CD", "8D", "11",
// "8A", "D4", "7B", "00", "63", "6C", "00", "06" };
//LocationDecoder loc = new LocationDecoder("0", data.Select(s => Convert.ToByte(s, 16)).ToArray());
//string data2 = "07 1A 22 04 24 68 AC E0 34 1F 4D BC 77 80 51 11 8E CD 8D 11 8A D4 7B 00 63 6C 00 06";
//LocationDecoder loc = new LocationDecoder("0", data2.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray());
//Console.ReadKey();
MotoRepeater_GW.consoleArgs = new ConsoleArgs();
MotoRepeater_GW.consoleArgs.isLE = true;
MotoRepeater_GW.consoleArgs.isWIRELINE = true;
MotoRepeater_GW.consoleArgs.isMB = true;
Console.Title = "MotoRepeater Gateway";
var cmdArgs = new CommandLineArgs();
if (args.Length > 0 && CommandLine.Parser.Default.ParseArguments(args, cmdArgs))
{
if (cmdArgs.Help)
{
Utils.WriteLine(cmdArgs.GetUsage());
Console.ReadKey();
}
if (!cmdArgs.Console)
{
// hide the console window
setConsoleWindowVisibility(false, Console.Title);
}
if(cmdArgs.Log)
{
LOGS.WRITE_LOGS = true;
}
if (cmdArgs.AppType != null && cmdArgs.AppType.Equals("safenet"))
{
MotoRepeater_GW.appType = AppType.SAFENET;
Console.Title = "SafeNet Direct Connect Gateway";
}
}
else if (args.Length == 0)
{
// no command line args specified
}
else
{
Utils.WriteLine("Unable to parse args", ConsoleColor.Red);
return;
}
/*
if (args.Length > 1)
{
if (args[1] != "-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)
{
Utils.WriteLine("Exception:" + ex.ToString(), ConsoleColor.Red);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//MainWindow = new MainForm();
if (MotoRepeater_GW.appType == AppType.SAFEDISPATCH)
Application.Run(new MainForm());
else
Application.Run(lf = new LoginForm());
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "ThreadException SDRgateway.Please restart the application.");
// here you can log the exception ...
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception SDRgateway.Please restart the application.");
// 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);
}
}
}
}