SafeNet/ConnectPlus_SOC/Program.cs
2021-02-24 13:50:23 +02:00

463 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Net.Sockets;
using Nini.Config;
using SafeNetLib;
using System.Net;
using System.IO;
namespace ConnectPlus_SOC
{
public class ConnectPlus_GW
{
public static Boolean isRunning = true;
public static DateTime lastEntry = DateTime.Now;
public static DebugMSG_Type debugMSG_type = DebugMSG_Type.DEV;
public static Config cfg;
public static UdpClient smsUDPclient = null;
public static bool unitsLoaded = false;
public static bool addressLoaded = false;
public static DateTime lastActivityTime = DateTime.Now;
LocationThread LocationConnection = null; // the threads for the 5 services we handle
static DBconnThread DBConnection = null;
ReceiveSMSThread ReceiveSMSthr = null;
SendSMSThread SendSMSthr = null;
//ars thread is watchserver
Thread LocationThreadobj = null;
Thread DBThreadobjARS = null;
Thread DBThreadobjGPS = null;
Thread DBThreadobjAux = null;
Thread DBThreadobjSMS = null;
Thread DBThreadobjAddr = null;
Thread DBThreadobjPOLL = null;
Thread ReceiveSMSThreadobj = null;
Thread SendSMSThreadobj = null;
Thread CompAddrThread = null;
EmailHandler emailH;
public static LOGS hLOG;
static void Main(string[] args)
{
StartUp(args);
string str = Utils.Compute4digitALG(1.23, 145.2);
Console.WriteLine(str);
//Console.ReadKey();
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine("Version: " + v.ToString());
//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());
//Console.ReadKey();
//start file loging!!!!!!!
//hLOG = new LOGS(Config.folder_path);
//start gateway
ConnectPlus_GW p = new ConnectPlus_GW();
//load config
if (!p.LoadConfig())
{
Utils.WriteLine("ERROR loading configs!!!!\nPlease fix configs in DB/cfg file and restart the GW!!!", ConsoleColor.Red);
Console.WriteLine("Press any key to continue...");
Thread.Sleep(3000);
System.Environment.Exit(0);
}
//TestGPS test1 = new TestGPS(ConnectPlus_GW.cfg.testFreq, ConnectPlus_GW.cfg.testLat, ConnectPlus_GW.cfg.testLng);
//Console.ReadKey();
//start DEBUG
//SafeNetLib.Debug d = new SafeNetLib.Debug(cfg.debug_port);
smsUDPclient = new UdpClient(ConnectPlus_GW.cfg.smsPort_r);
p.StartDBService();
while (!ConnectPlus_GW.unitsLoaded)
{
Utils.ConsWrite(DebugMSG_Type.DB, "Waiting for units to be fetched...");
Thread.Sleep(1000);
while (!DBConnection.DB.DBconnected && ConnectPlus_GW.isRunning)
{
{
DBConnection.DB.StopDB();
Thread.Sleep(500);
//Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to DB!!! <<HandleConnectionGPS>> ");
Utils.WriteLine("Database is not connected!!! Restarting it...", ConsoleColor.Red);
DBConnection.DB.StartDB();
}
Thread.Sleep(500);
}
}
Utils.WriteLine("»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»««««««««««««««««««««««««««««««", ConsoleColor.Yellow);
Utils.WriteLine(String.Format("»»»»»»»»»»»»»»»» Finished loading {0:D4} UNITS «««««««««««««««", SN_Queues.ht_SUInfo.Count), ConsoleColor.Yellow);
Utils.WriteLine("»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»««««««««««««««««««««««««««««««", ConsoleColor.Yellow);
if (ConnectPlus_GW.cfg.testActive)
{
//LOGS.active = false;
Utils.ConsWrite(DebugMSG_Type.DEV, "TEST mode active. GW in TEST mode.Logs Deactivated");
Utils.ConsWrite(DebugMSG_Type.DEV, "TEST mode active. GW in TEST mode.Logs Deactivated");
Utils.ConsWrite(DebugMSG_Type.DEV, "TEST mode active. GW in TEST mode.Logs Deactivated");
TestGPS test = new TestGPS(ConnectPlus_GW.cfg.testFreq, ConnectPlus_GW.cfg.testLat, ConnectPlus_GW.cfg.testLng);
while (true)
{
Utils.ConsWrite(DebugMSG_Type.DEV, "TEST mode active. GW in TEST mode.Logs Deactivated");
Utils.ConsWrite(DebugMSG_Type.DEV, "TEST mode active. GW in TEST mode.Logs Deactivated");
Utils.ConsWrite(DebugMSG_Type.DEV, "TEST mode active. GW in TEST mode.Logs Deactivated");
Thread.Sleep(20000);
}
}
Utils.ConsWrite(DebugMSG_Type.DB, "Done loading ");
p.StartARSService();
p.StartLocationService();
p.StartSMSService();
if (ConnectPlus_GW.cfg.enableEmailService)
{
p.StartEmailService();//new
}
//p.StartComputeAddr(); - adress are done throw a bing script
while (true)
{
Thread.Sleep(10000);
Utils.ConsWrite(DebugMSG_Type.DB, "Last entry at: " + ConnectPlus_GW.lastEntry);
//LOGS.LOG("Last entry at:" + ConnectPlus_GW.lastEntry + " Current time :" + DateTime.Now);
TimeSpan span = DateTime.Now.Subtract(ConnectPlus_GW.lastEntry);
if (span.TotalMinutes > 5)
{
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.");
//LOGS.LOG("******\nNothing added to BD for 10 minutes.\nRestareting all DB threads.\n********");
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
ConnectPlus_GW.lastEntry = DateTime.Now;
if (ConnectPlus_GW.cfg.gw_restart)
{
//p.RestartApp();
//ADD restart... to LOG
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) + "] ";
}
else
{
Utils.ConsWrite(DebugMSG_Type.DB, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
}
}
}
static void StartUp(string[] args)
{
if (args.Length > 0)
{
//version
if (args[0] == "-v")
{
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!!");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
System.Environment.Exit(0);
}
else if (args[0] == "-h" || args[0] == "help")
{
Console.WriteLine("\nOptions:\n");
Console.WriteLine("-d [string to decrypt] [key]\t Decrypts a string using the specified key");
Console.WriteLine("-e [string to encrypt] [key]\t Encrypts a string using the specified key");
Console.WriteLine("\nEncryption key is - safemobileTriple");
Console.ReadKey();
System.Environment.Exit(0);
}
//startup path
else if (args[0] == "-p")
{
if (args.Length < 1)
{
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!! Cfg path missing");
Console.WriteLine("Press any key to exit...");
Thread.Sleep(3000);
System.Environment.Exit(0);
}
Utils.ConsWrite(DebugMSG_Type.always, "Application started with param:" + args[1]);
if (!Directory.Exists(args[1]))
{
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!! \"" + args[1] + "\" -> folder not found.");
Console.WriteLine("Press any key to exit...");
Thread.Sleep(3000);
System.Environment.Exit(0);
}
Config.folder_path = args[1];
}
else if (args[0] == "-d" && args[1] != null && args[2] != null)
{
try
{
//Console.WriteLine("Decrypt required for " + args[1]);
Console.WriteLine("" + Encryption.Decrypt(args[1], args[2]));
}
catch (Exception ex)
{
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
System.Environment.Exit(0);
}
}
else if (args[0] == "-e" && args[1] != null && args[2] != null)
{
try
{
//Console.WriteLine("Encrypt required for " + args[1]);
Console.WriteLine("" + Encryption.Encrypt(args[1], args[2]));
}
catch (Exception ex)
{
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
System.Environment.Exit(0);
}
}
}
}
private void RestartApp()
{
Process oldProcess = Process.GetCurrentProcess();
oldProcess.WaitForExit(5000);
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.Environment.Exit(1);
}
private bool LoadConfig()
{
bool ret = false;
cfg = new Config();
string MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Connect Timeout=5;Pooling=false;";
try
{
DBhandle db = new DBhandle(MyConString, cfg.gw_id);
ConnPlussCFG conCfg = db.LoadGWConfig();
if (conCfg != null)
{
cfg.ctrlIP = conCfg.IP;//"gps.kelcomradio.com";// conCfg.IP;
/*
IPAddress ctrIP = null;
if (!IPAddress.TryParse("gps.kelcomradio.com", out ctrIP))
{
ctrIP = Dns.GetHostAddresses(ConnectPlus_GW.cfg.ctrlIP)[0];
}
if (ctrIP == null)
{
Utils.ConsWrite(DebugMSG_Type.always, "Location Thread invalid host address for: " + ConnectPlus_GW.cfg.ctrlIP);
}
string temp = ctrIP.ToString();
Console.WriteLine(temp);
*/
//cfg.ctrlIP = "gps.kelcomradio.com";
cfg.arsPort = conCfg.ars_port;
cfg.locPort = conCfg.lrrp_port;
cfg.smsPort = conCfg.sms_port;
cfg.locPort_r = conCfg.r_lrrp_port;
cfg.smsPort_r = conCfg.r_sms_port;
ret = true;
Utils.ConsWrite(DebugMSG_Type.CFG, "++++++++++++++++++++++++++++++++++++++++");
Utils.ConsWrite(DebugMSG_Type.CFG, "++++++++++++++++++++++++++++++++++++++++");
Utils.ConsWrite(DebugMSG_Type.CFG, "Settings:");
Utils.ConsWrite(DebugMSG_Type.CFG, "gw_id:\t" + cfg.gw_id);
Utils.ConsWrite(DebugMSG_Type.CFG, "ctrlIP:\t" + cfg.ctrlIP);
Utils.ConsWrite(DebugMSG_Type.CFG, "arsPort:\t" + cfg.arsPort);
Utils.ConsWrite(DebugMSG_Type.CFG, "locPort:\t" + cfg.locPort);
Utils.ConsWrite(DebugMSG_Type.CFG, "smsPort:\t" + cfg.smsPort);
Utils.ConsWrite(DebugMSG_Type.CFG, "locPort_r:\t" + cfg.locPort_r);
Utils.ConsWrite(DebugMSG_Type.CFG, "smsPort_r:\t" + cfg.smsPort_r);
Utils.ConsWrite(DebugMSG_Type.CFG, "++++++++++++++++++++++++++++++++++++++++");
Utils.ConsWrite(DebugMSG_Type.CFG, "++++++++++++++++++++++++++++++++++++++++");
}
else
{
Utils.ConsWrite(DebugMSG_Type.CFG, "Gateway configuration was not found into the DB.");
}
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
}
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.gw_id);
//Start ARS DB thread
DBThreadobjARS = new Thread(new ThreadStart(DBConnection.HandleConnectionARS));
DBThreadobjARS.IsBackground = true;
DBThreadobjARS.Start();
//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 SMS DB thread
DBThreadobjSMS = new Thread(new ThreadStart(DBConnection.HandleConnectionSMS));
DBThreadobjSMS.IsBackground = true;
DBThreadobjSMS.Start();
//Start Addres insert thread
DBThreadobjAddr = new Thread(new ThreadStart(DBConnection.HandleConnectionAddr));
DBThreadobjAddr.IsBackground = true;
DBThreadobjAddr.Start();
//Start POLL thread
DBThreadobjPOLL = new Thread(new ThreadStart(DBConnection.HandleConnectionPOLL));
DBThreadobjPOLL.IsBackground = true;
DBThreadobjPOLL.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 StartARSService()
{
string MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Connect Timeout=5;Pooling=false;";
WatcherServerThread server = new WatcherServerThread(cfg.ctrlIP, cfg.arsPort, MyConString, cfg.gw_id,cfg.rep_inter_default.ToString());
server.Start();
//server.SubscribeALL(cfg.dialogid);
server.SubscribeALL(1234);
//server.SubscribeALLinDB(1234);
server.dt_lastSUB = DateTime.UtcNow;
}
void StartEmailService()
{
string MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
emailH = new EmailHandler(MyConString, cfg.gw_id);
}
void StartLocationService()
{
// create thread to listen UDP
LocationConnection = new LocationThread();
LocationConnection.port = cfg.locPort_r;
//LocationConnection.parent = this;
LocationThreadobj = new Thread(new ThreadStart(LocationConnection.HandleConnection));
LocationThreadobj.IsBackground = true;
LocationThreadobj.Start();
}
void StartSMSService()
{
string MyConString = "SERVER=" + cfg.SERVER + ";" +
"DATABASE=" + cfg.DATABASE + ";" +
"UID=" + cfg.UID + ";" +
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
// create thread to listen UDP
ReceiveSMSthr = new ReceiveSMSThread((ushort)cfg.smsPort_r, MyConString,cfg.gw_id);
//ARSConnection.parent = this;
ReceiveSMSThreadobj = new Thread(new ThreadStart(ReceiveSMSthr.HandleConnection));
ReceiveSMSThreadobj.IsBackground = true;
ReceiveSMSThreadobj.Start();
// create thread to listen UDP
SendSMSthr = new SendSMSThread(cfg.smsPort);
//ARSConnection.parent = this;
SendSMSThreadobj = new Thread(new ThreadStart(SendSMSthr.HandleConnection));
SendSMSThreadobj.IsBackground = true;
SendSMSThreadobj.Start();
}
void StartComputeAddr()
{
Address adr = new Address();
CompAddrThread = new Thread(new ThreadStart(adr.Compute));
CompAddrThread.IsBackground = true;
CompAddrThread.Start();
}
}
}