SafeNet/.svn/pristine/19/19b07751b2656d497a5921f98ae4ea26d3c7d624.svn-base
2021-02-24 13:50:23 +02:00

214 lines
8.5 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Text;
using SafeNetLib;
using System.Threading;
using System.Diagnostics;
using System.IO;
namespace PAL_SOC
{
class Program
{
public static DateTime lastEntry = DateTime.Now;
public static Config cfg;
public static bool unitsLoaded = false;
public static bool addressLoaded = false;
public static LOGS hLOG;
LocationThread LocationConnection = null; // the threads for the 5 services we handle
DBconnThread DBConnection = null;
Thread LocationThreadobj = null;
Thread DBThreadobjGPS = null;
Thread DBThreadobjAddr = null;
Thread DBThreadobjAux = null;
//alerts
private static AlertHandler alertHandler;
static void Main(string[] args)
{
hLOG = new LOGS();
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine("Safemobile PAL Gateway for Safenet " + v.ToString() + ", Copyright 2012");
Console.WriteLine(" (www.Safemobile.com) ");
Console.WriteLine("-----------------------------------\n");
Console.WriteLine("Version: " + v.ToString());
if (args.Length > 0)
{
if (args[0] == "-v")
{
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!!");
System.Environment.Exit(0);
}
}
//creat PID file
Process currentProcess = Process.GetCurrentProcess();
int procid = currentProcess.Id;
if (File.Exists("pid.pid"))
{
File.Delete("pid.pid");
}
System.IO.File.WriteAllText("pid.pid", procid.ToString());
Program p = new Program();
//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 DEBUG
//SafeNetLib.Debug d = new SafeNetLib.Debug(cfg.debug_port);
p.StartDBService();
//wait
while (!Program.unitsLoaded)
{
Utils.ConsWrite(DebugMSG_Type.DB, "Waiting for units to be fetched!!! sleep 5 secs!!!");
Thread.Sleep(10000);
}
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, "------------------------------");
p.StartLocationService();
p.StartAlertService();
Utils.ConsWrite(DebugMSG_Type.DB, "###### Gateway started #######");
while (true)
{
Thread.Sleep(10000);
Utils.ConsWrite(DebugMSG_Type.DB, "Last entry at:" + Program.lastEntry + " Current time :" + DateTime.Now);
LOGS.LOG("Last entry at:" + Program.lastEntry + " Current time :" + DateTime.Now);
TimeSpan span = DateTime.Now.Subtract(Program.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.");
Program.lastEntry = DateTime.Now;
//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("@@@@@@@@@@@@@@@@@@@@@");
}
}
}
private bool LoadConfig()
{
bool ret = false;
cfg = new Config();
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 GPS DB thread
DBThreadobjGPS = new Thread(new ThreadStart(DBConnection.HandleConnectionGPS));
DBThreadobjGPS.IsBackground = true;
DBThreadobjGPS.Start();
//Start Units DB thread
DBThreadobjAux = new Thread(new ThreadStart(DBConnection.HandleConnectionAux));
DBThreadobjAux.IsBackground = true;
DBThreadobjAux.Start();
//Start Addres insert thread
DBThreadobjAddr = new Thread(new ThreadStart(DBConnection.HandleConnectionAddr));
DBThreadobjAddr.IsBackground = true;
DBThreadobjAddr.Start();
}
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);
}
}
void StartLocationService()
{
// create thread to listen UDP
LocationConnection = new LocationThread();
LocationConnection.port = cfg.locPort;
//LocationConnection.parent = this;
LocationThreadobj = new Thread(new ThreadStart(LocationConnection.HandleConnection));
LocationThreadobj.IsBackground = true;
LocationThreadobj.Start();
}
void StartAlertService()
{
string MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
DBhandle DB = new DBhandle(MyConString, cfg.gatewayID);
alertHandler = new AlertHandler(DB, cfg.alert_smtpServer, cfg.alert_smtpPort, cfg.alert_user, cfg.alert_psw,cfg.alert_sslState);
//alertHandler.TestSendEmail("gabriel.oprea@safemobile.com", "test");
}
private void RestartApp()
{
Process oldProcess = Process.GetCurrentProcess();
oldProcess.WaitForExit(5000);
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.Environment.Exit(1);
}
}
}