487 lines
17 KiB
C#
487 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using SafeMobileLib;
|
|
using System.Threading;
|
|
using SafeMobileLib.MessageDecoders;
|
|
|
|
|
|
namespace MotoRepeater
|
|
{
|
|
public class DBconnThread
|
|
{
|
|
private string gatewayID;
|
|
private bool working = false;
|
|
|
|
public DBhandle DB;
|
|
|
|
|
|
public DBconnThread(string Server, string Database, string User, string Password, string p_gatewayID)
|
|
{
|
|
gatewayID = p_gatewayID;
|
|
working = true;
|
|
DB = new DBhandle(Server, Database, User, Password);
|
|
DBhandle.gatewayID = p_gatewayID;
|
|
|
|
}
|
|
|
|
public void StartDB()
|
|
{
|
|
try
|
|
{
|
|
working = true;
|
|
DB.StartDB();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine("StartDB Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
}
|
|
|
|
public void StopDB()
|
|
{
|
|
try
|
|
{
|
|
working = false;
|
|
DB.StopDB();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine("StopDB Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
}
|
|
|
|
int count = 0;
|
|
//for ARS DB thread
|
|
public void HandleConnectionARS()
|
|
{
|
|
Utils.WriteLine("ARS DB thread...");
|
|
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
try
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
//add ARS in DB
|
|
ArsMSG ars_msg = SN_Queues.arsMsgQueue.GetItem(100);
|
|
while (ars_msg != null)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.Debug, "Got ARS updating DB suid:" + ars_msg.imei + "msg:" + ars_msg.msg);
|
|
//insertARSinDB(ars_msg.suid, ars_msg.msg);
|
|
//insert ars in ars message table
|
|
|
|
int suDBid = 0;
|
|
suDBid = GetDbID4RadioID(ars_msg.imei);
|
|
|
|
if (suDBid == 0)
|
|
{
|
|
Utils.WriteLine("Unit with IMEI:" + ars_msg.imei + " not found in DB!!!!");
|
|
LOGS.LOG("Unit with IMEI:" + ars_msg.imei + " not found in DB!!!!");
|
|
}
|
|
else
|
|
{
|
|
if (ars_msg.imei != "0" && ars_msg.imei != "")
|
|
{
|
|
MotoRepeater_GW.lastEntry = DateTime.Now;
|
|
DB.Insert_ARSmsg(suDBid, ars_msg.msg);
|
|
}
|
|
else
|
|
{
|
|
Utils.WriteLine("%%%%% Got ARS invalid suid:" + ars_msg.suid + "msg:" + ars_msg.msg);
|
|
}
|
|
//update ARS last pos.
|
|
//Update_ARSlp(dbID);
|
|
}
|
|
|
|
ars_msg = SN_Queues.arsMsgQueue.GetItem(10);
|
|
}
|
|
|
|
|
|
}
|
|
catch (ThreadAbortException)
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine("Handle ARS Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
} // end while (true)
|
|
}
|
|
|
|
|
|
public DateTime startInsertGPS = DateTime.Now;
|
|
public DateTime endInsertGPS = DateTime.Now;
|
|
//for GPS DB thread
|
|
public void HandleConnectionGPS()
|
|
{
|
|
Utils.WriteLine("GPS DB thread...");
|
|
|
|
// first time the connection is made outside this loop
|
|
while (!DB.DBconnected)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
int count = 0;
|
|
int gpsCount = 0;
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
try
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
if (++count % 10 == 0)
|
|
{
|
|
StopDB();
|
|
Thread.Sleep(500);
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to DB!!!");
|
|
Utils.WriteLine("Database is not connected!!! Restarting it...", ConsoleColor.Red);
|
|
StartDB();
|
|
count = 0;
|
|
}
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
htCell_t cell = SN_Queues.DBQueueLocation.GetItem(100);
|
|
|
|
if(++gpsCount % 100 == 0)
|
|
{
|
|
Utils.WriteLine("GPS Queue contains " + SN_Queues.DBQueueLocation.Count + " positions", ConsoleColor.Green);
|
|
gpsCount = 0;
|
|
}
|
|
|
|
if (cell != null)
|
|
{
|
|
SN_Queues.initAddressQueue.PostItem(cell);
|
|
|
|
startInsertGPS = DateTime.Now;
|
|
bool ret = DB.Insert_messages(cell);
|
|
MotoRepeater_GW.lastEntry = DateTime.Now;
|
|
if (ret)
|
|
{
|
|
endInsertGPS = DateTime.Now;
|
|
TimeSpan diff = endInsertGPS.Subtract(endInsertGPS);
|
|
count++;
|
|
//Utils.ConsWrite(DebugMSG_Type.always, "GPS SUID: " + cell.suid + " LAT<" + cell.lat + " > LNG<" + cell.lng + ">");
|
|
//Utils.ConsWrite(DebugMSG_Type.always, "Added count:" + count + " in queue:" + SN_Queues.DBQueueLocation.Count + " Time for insert(msecs):" + diff.Ticks / TimeSpan.TicksPerMillisecond + "." + diff.Ticks % TimeSpan.TicksPerMillisecond);
|
|
}
|
|
//Utils.ConsWrite(DebugMSG_Type.always, "-----------------------------------");
|
|
}
|
|
|
|
}
|
|
catch (ThreadAbortException tae)
|
|
{
|
|
Utils.WriteLine("Handle GPS ThreadAbortException Exception: " + tae.ToString(), ConsoleColor.Red);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine("Handle GPS Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
DB.DBconnected = false;
|
|
}
|
|
} // end while (true)
|
|
|
|
Utils.WriteLine("Handle GPS Thread is getting killed", ConsoleColor.Yellow);
|
|
}
|
|
|
|
//for Aux DB thread (units, gateway status)
|
|
int countUnit = 30 * 61;
|
|
public void HandleConnectionAux()
|
|
{
|
|
Utils.WriteLine("Aux DB thread ...");
|
|
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
try
|
|
{
|
|
if (countUnit > 2 * 60)//every 5 minutes
|
|
{
|
|
DB.LoadUnitInfo(gatewayID);
|
|
countUnit = 0;
|
|
|
|
// check last GPS inserted
|
|
if ((DateTime.Now - SN_Queues.DBQueueLocation.LastReadTime) > new TimeSpan(0, 1, 0))
|
|
{
|
|
Utils.WriteLine($"Last GPS check was @{SN_Queues.DBQueueLocation.LastReadTime}", ConsoleColor.Yellow);
|
|
OnGPSInsertedBlocked?.Invoke();
|
|
}
|
|
}
|
|
|
|
GWstatus gw_msg = SN_Queues.gwstatusQueue.GetItem(200);
|
|
while (gw_msg != null)
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
bool result = false;
|
|
|
|
if (gw_msg.message != null && gw_msg.message.Length > 0)
|
|
result = DB.Insert_GW_status(gw_msg.gw_id, gw_msg.status, gw_msg.message);
|
|
else
|
|
result = DB.Insert_GW_status(gw_msg.gw_id, gw_msg.status);
|
|
|
|
|
|
// check last inserted GPS to prevent blocked insert
|
|
if(endInsertGPS < startInsertGPS && Math.Abs(endInsertGPS.Subtract(endInsertGPS).Seconds) > 5)
|
|
{
|
|
Utils.WriteLine($"GPS INSERT HAD BLOCKED!!! Last inserted GPS was @ {endInsertGPS.ToString()}", ConsoleColor.Magenta);
|
|
OnGPSInsertedBlocked?.Invoke();
|
|
}
|
|
|
|
/*
|
|
if (result)
|
|
MotoTRBO_GW.lastEntry = DateTime.Now;
|
|
*/
|
|
gw_msg = SN_Queues.gwstatusQueue.GetItem(200);
|
|
}
|
|
|
|
countUnit++;
|
|
}
|
|
catch (ThreadAbortException)
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine("Handle ARS Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
} // end while (true)
|
|
}
|
|
|
|
//for SMS DB thread
|
|
public void HandleConnectionSMS()
|
|
{
|
|
Utils.WriteLine("SMS DB thread ...");
|
|
|
|
int count = 0;
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
if (++count % 15 == 0)
|
|
{
|
|
LOGS.LOG("@SMS@ " + DateTime.Now.ToString("HH:mm:ss") + "->Check for SMS in DB (DB OK!!!)");
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, DateTime.Now.ToString("HH:mm:ss") + "->Check for SMS in DB (DB OK!!!)");
|
|
DB.checkSMSinDB();
|
|
|
|
count = 0;
|
|
}
|
|
|
|
Thread.Sleep(500);
|
|
} // end while (true)
|
|
}
|
|
|
|
//for SMS Conf DB thread
|
|
public void HandleConnectionSMS_conf()
|
|
{
|
|
Utils.WriteLine("SMS_conf DB thread ...");
|
|
|
|
int count = 0;
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
if (++count % 11 == 0)
|
|
{
|
|
//check SMS confirmation
|
|
SMSmsg msg = SN_Queues.confSMSQueue.GetItem(100);
|
|
while (msg != null)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Confirming SMS in DB. DBmsgID:" + msg.DBmsg_id + " msg:" + msg.msg);
|
|
DB.confirmSMS(msg);
|
|
msg = SN_Queues.confSMSQueue.GetItem(100);
|
|
}
|
|
|
|
count = 0;
|
|
}
|
|
Thread.Sleep(500);
|
|
} // end while (true)
|
|
}
|
|
|
|
//for inserting address Thread
|
|
public void HandleConnectionAddr()
|
|
{
|
|
Utils.WriteLine("Address DB thread ...");
|
|
|
|
//get address list
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread waiting for DB connection!!");
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
//MotoTRBO_GW.addressLoaded = DB.LoadAddressList(gatewayID);
|
|
|
|
|
|
while (!MotoRepeater_GW.addressLoaded)
|
|
{
|
|
Utils.WriteLine("Address DB thread waiting for address list!!");
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
while (!MotoRepeater_GW.addressLoaded && MotoRepeater_GW.isRunning)
|
|
Thread.Sleep(100);
|
|
//add addr to tb if needed
|
|
|
|
htCell_t cell = SN_Queues.initAddressQueue.GetItem(100);
|
|
if (cell != null)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Address thread item fetched...");
|
|
string addr_hash = Utils.Compute4digitALG(cell.d_lat, cell.d_lng);
|
|
Address addr = new Address(cell);
|
|
|
|
lock (SN_Queues.ht_addressList.SyncRoot)
|
|
{
|
|
|
|
if (!SN_Queues.ht_addressList.ContainsKey(addr_hash))
|
|
{
|
|
//insert value in DB
|
|
DB.Insert_AddressList(cell);
|
|
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "addr_hash:" + addr_hash + " addr.addr_hash:" + addr.addr_hash + " SN_Queues.ht_addressList.Count" + SN_Queues.ht_addressList.Count);
|
|
//add to comp addr queue
|
|
/*
|
|
if (SN_Queues.computeAddressQueue.Count < 100)
|
|
{
|
|
SN_Queues.computeAddressQueue.PostItem(addr);
|
|
}
|
|
*/
|
|
//add in our own ht
|
|
SN_Queues.ht_addressList.Add(addr.addr_hash, addr);
|
|
}
|
|
else
|
|
{
|
|
//no need to add to DB .. addr already in
|
|
}
|
|
}
|
|
}
|
|
Thread.Sleep(50);
|
|
|
|
} // end while (true)
|
|
}
|
|
|
|
//for poll Thread
|
|
public void HandleConnectionPOLL()
|
|
{
|
|
Utils.WriteLine("POLL DB thread ...");
|
|
|
|
int count = 0;
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
if (++count % 10 == 0)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Checking for poll at:" + DateTime.Now);
|
|
//get new POLL requests
|
|
DB.GetPollRequests4Conventional();
|
|
|
|
//check Poll sent
|
|
POLLmsg p_msg = SN_Queues.sentPOLLQueue.GetItem(100);
|
|
if (p_msg != null)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DEV, "MotoTRBOGW.sentPOLLQueue.GetItem(100);");
|
|
DB.UpdatePollSentTime(p_msg.DBid, p_msg.sent);
|
|
}
|
|
|
|
//update GPS in DB from poll response
|
|
POLLmsg gps_msg = SN_Queues.recvPOLLQueue.GetItem(100);
|
|
if (gps_msg != null)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Got poll location updating DB [" + gps_msg.DBid + "] suid:" + gps_msg.suid);
|
|
DB.UpdatePollGPSConventional(gps_msg);
|
|
|
|
//remove poll from hashtable ... poll completed sucsessfully
|
|
lock (SN_Queues.ht_POLL_List.SyncRoot)
|
|
{
|
|
SN_Queues.ht_POLL_List.Remove(gps_msg.DBid);
|
|
}
|
|
}
|
|
|
|
count = 0;
|
|
}
|
|
|
|
Thread.Sleep(500);
|
|
|
|
//TODO check if polls have expired -> were sent more than 2 hours ago
|
|
|
|
} // end while (true)
|
|
}
|
|
|
|
|
|
|
|
//for Tallysman events Thread
|
|
public void HandleTallysmanEvents()
|
|
{
|
|
Utils.WriteLine("Tallysman Events DB thread ...");
|
|
|
|
int count = 0;
|
|
while (MotoRepeater_GW.isRunning)
|
|
{
|
|
while (!DB.DBconnected && MotoRepeater_GW.isRunning)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
if (++count % 10 == 0)
|
|
{
|
|
|
|
//update GPS in DB from poll response
|
|
TallysmanEventArgs tallysmanMsg = DataBaseSN.tallysmanEventsQueue.GetItem(100);
|
|
if (tallysmanMsg != null)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Got poll location updating DB [" + gps_msg.DBid + "] suid:" + gps_msg.suid);
|
|
DB.InsertTallysmanEvent(tallysmanMsg.GetTallysmanMessage());
|
|
}
|
|
|
|
count = 0;
|
|
}
|
|
|
|
Thread.Sleep(500);
|
|
|
|
//TODO check if polls have expired -> were sent more than 2 hours ago
|
|
|
|
} // end while (true)
|
|
}
|
|
|
|
|
|
public static int GetDbID4RadioID(string radioID)
|
|
{
|
|
int result = 0;
|
|
lock (SN_Queues.ht_SUInfo.SyncRoot)
|
|
{
|
|
if (SN_Queues.ht_SUInfo != null)
|
|
{
|
|
if (SN_Queues.ht_SUInfo.ContainsKey(radioID))
|
|
result = ((SUinfo)SN_Queues.ht_SUInfo[radioID]).DBid;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
public delegate void GPSInsertedBlockedDel();
|
|
public event GPSInsertedBlockedDel OnGPSInsertedBlocked;
|
|
}
|
|
}
|