SafeNet/Hytera_SOC/Program.cs

319 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SafeNetLib;
using System.Threading;
using System.Diagnostics;
namespace Hytera_SOC
{
class Hytera_GW
{
public static DateTime lastEntry = DateTime.Now;
public static Config cfg;
private EmailHandler emailH;
public static bool unitsLoaded = false;
public static bool addressLoaded = false;
DBconnThread DBConnection = null;
Thread DBThreadobjARS = null;
Thread DBThreadobjGPS = null;
Thread DBThreadobjAux = null;
Thread DBThreadobjSMS = null;
Thread DBThreadobjSMS_conf = null;
Thread DBThreadobjAddr = null;
Thread DBThreadobjPOLL = null;
Thread CompAddrThread = null;
public static LOGS hLOG;
static void Main(string[] args)
{
//string test = Utils.Compute4digitALG(45.123443431312, -13.1200423423423);
//Console.WriteLine("test: " + test);
//start file loging!!!!!!!
hLOG = new LOGS();
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine("Version: " + v.ToString());
if (args.Length > 0)
{
if (args[0] == "-v")
{
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!!");
System.Environment.Exit(0);
}
}
Hytera_GW p = new Hytera_GW();
//load config
if (!p.LoadConfig())
{
Utils.ConsWrite(DebugMSG_Type.always, "EERROR loading configs!!!!\nPlease fix configs in DB/cfg file and restart the GW!!!");
return;
}
try
{
if (args.Length > 0 && args[0] != null && args[0] != "")
{
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Utils.ConsWrite(DebugMSG_Type.CFG, "GW started with param:" + args[0]);
Utils.ConsWrite(DebugMSG_Type.CFG, "Replacing config.ini GW_ID:" + cfg.gatewayID + " with:" + args[0]);
cfg.gatewayID = args[0];
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
return;
}
//start DB
p.StartDBService();
//wait
while (!Hytera_GW.unitsLoaded)
{
Utils.ConsWrite(DebugMSG_Type.DB, "Waiting for units to be fetched!!! sleep 5 secs!!!");
Thread.Sleep(5000);
}
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
Utils.ConsWrite(DebugMSG_Type.DB, "---- Unit load finished -----");
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
//start Radio COM
uint hyteraImei = Convert.ToUInt32(cfg.hyteraIMEI);
RadioHyt hyt = new RadioHyt(cfg.hyteraIP, cfg.subNet, hyteraImei);
if (hyt.Connect())
{
Utils.ConsWrite(DebugMSG_Type.always, "Connnection to hyt radio established.");
}
else
{
Utils.ConsWrite(DebugMSG_Type.always, "Failed to connect to hyt radio!!!!");
}
//address
//p.StartComputeAddr();- adress are done throw a bing script
//start ping thread
PingThread threadWorker = new PingThread((string)Hytera_GW.cfg.ar_radioIPs[0], Hytera_GW.cfg.ping_interval);
threadWorker.OnNewPingComplete += new PingThread.NewPingCompleteDEl(threadWorker_OnNewPingComplete);
threadWorker.OnRadioPing += new PingThread.RadioPingDEl(threadWorker_OnRadioPing);
Thread PingThreadObj = new Thread(new ThreadStart(threadWorker.Worker));
PingThreadObj.IsBackground = true;
PingThreadObj.Start();
while (true)
{
Thread.Sleep(10000);
Utils.ConsWrite(DebugMSG_Type.DB, "Last entry at:" + Hytera_GW.lastEntry);
TimeSpan span = DateTime.Now.Subtract(Hytera_GW.lastEntry);
if (span.TotalMinutes > 10)
{
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Utils.ConsWrite(DebugMSG_Type.always, "Nothing added to BD for 10 minutes.\nRestareting all DB threads.");
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
LOGS.LOG("Nothing added to BD for 10 minutes.");
Hytera_GW.lastEntry = DateTime.Now;
if (Hytera_GW.cfg.gw_restart)
{
p.RestartApp();
Utils.ConsWrite(DebugMSG_Type.DB, "Adding restart INFO to LOG file");
string format = "dd/MM/yyyy HH:mm";
string s_time = "[" + DateTime.Now.ToString(format) + "] ";
LOGS.LOG("@@@@@@@@@@@@@@@@@@@@@");
LOGS.LOG(s_time + "no GPS received for 10 minutes");
LOGS.LOG(s_time + "should restart");
LOGS.LOG("@@@@@@@@@@@@@@@@@@@@@");
}
else
{
p.StopDBService();
Thread.Sleep(10000);
p.StartDBService();
Utils.ConsWrite(DebugMSG_Type.DB, "Adding restart INFO to LOG file");
string format = "dd/MM/yyyy HH:mm";
string s_time = "[" + DateTime.Now.ToString(format) + "] ";
LOGS.LOG("@@@@@@@@@@@@@@@@@@@@@");
LOGS.LOG(s_time + "no GPS received for 10 minutes");
LOGS.LOG(s_time + "SHOULD restart DB thread");
LOGS.LOG("@@@@@@@@@@@@@@@@@@@@@");
}
}
}
}
static void threadWorker_OnRadioPing(bool status, string ip)
{
Utils.ConsWrite(DebugMSG_Type.DEV, "IP:" + ip + " status:" + status);
GWstatus gws = new GWstatus();
gws.gw_id = Hytera_GW.cfg.gatewayID;
gws.status = (status) ? 1 : 0;
SN_Queues.gwstatusQueue.PostItem(gws);
}
private static void threadWorker_OnNewPingComplete(bool status, string ip)
{
Utils.ConsWrite(DebugMSG_Type.DEV, "IP:" + ip + " status:" + status);
GWstatus gws = new GWstatus();
gws.gw_id = Hytera_GW.cfg.gatewayID;
gws.status = (status) ? 1 : 0;
SN_Queues.gwstatusQueue.PostItem(gws);
}
public static string MyConString;
private bool LoadConfig()
{
bool ret = false;
cfg = new Config();
MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
ret = true;
return ret;
}
void StartDBService()
{
try
{
//SOC_connection sc = new SOC_connection(Server_IP, Server_Port, gatewayID);
string MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
DBConnection = new DBconnThread(MyConString, cfg.gatewayID);
//Start Units DB thread
DBThreadobjAux = new Thread(new ThreadStart(DBConnection.HandleConnectionAux));
DBThreadobjAux.IsBackground = true;
DBThreadobjAux.Start();
Thread.Sleep(200);
//Start GPS DB thread
DBThreadobjGPS = new Thread(new ThreadStart(DBConnection.HandleConnectionGPS));
DBThreadobjGPS.IsBackground = true;
DBThreadobjGPS.Start();
Thread.Sleep(200);
//Start ARS DB thread
DBThreadobjARS = new Thread(new ThreadStart(DBConnection.HandleConnectionARS));
DBThreadobjARS.IsBackground = true;
DBThreadobjARS.Start();
Thread.Sleep(200);
//Start SMS DB thread
DBThreadobjSMS = new Thread(new ThreadStart(DBConnection.HandleConnectionSMS));
DBThreadobjSMS.IsBackground = true;
DBThreadobjSMS.Start();
Thread.Sleep(200);
//Start SMS DB thread
DBThreadobjSMS_conf = new Thread(new ThreadStart(DBConnection.HandleConnectionSMS_conf));
DBThreadobjSMS_conf.IsBackground = true;
DBThreadobjSMS_conf.Start();
Thread.Sleep(200);
//Start Addres insert thread
DBThreadobjAddr = new Thread(new ThreadStart(DBConnection.HandleConnectionAddr));
DBThreadobjAddr.IsBackground = true;
DBThreadobjAddr.Start();
Thread.Sleep(200);
//Start POLL thread
DBThreadobjPOLL = new Thread(new ThreadStart(DBConnection.HandleConnectionPOLL));
DBThreadobjPOLL.IsBackground = true;
DBThreadobjPOLL.Start();
Thread.Sleep(200);
}
catch (Exception e)
{
Utils.ConsWrite(DebugMSG_Type.always, "Could not intialize the connection with location server!\r\n" + e.Message + "\r\nPlease check your internet connection !!");
//System.Environment.Exit(1);
}
}
private void StopDBService()
{
if (DBConnection != null)
{
DBConnection.StopDB();
}
if (DBThreadobjARS != null)
{
DBThreadobjARS.Abort();
}
if (DBThreadobjGPS != null)
{
DBThreadobjGPS.Abort();
}
if (DBThreadobjAux != null)
{
DBThreadobjAux.Abort();
}
if (DBThreadobjSMS != null)
{
DBThreadobjSMS.Abort();
}
if (DBThreadobjSMS_conf != null)
{
DBThreadobjSMS_conf.Abort();
}
if (DBThreadobjAddr != null)
{
DBThreadobjAddr.Abort();
}
if (DBThreadobjPOLL != null)
{
DBThreadobjPOLL.Abort();
}
}
private void RestartApp()
{
Process oldProcess = Process.GetCurrentProcess();
oldProcess.WaitForExit(5000);
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.Environment.Exit(1);
}
void StartComputeAddr()
{
Address adr = new Address();
CompAddrThread = new Thread(new ThreadStart(adr.Compute));
CompAddrThread.IsBackground = true;
CompAddrThread.Start();
}
}
}