SafeNet/MotoTRBO_SOC/DBconnThread.cs

456 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using SafeNetLib;
using System.Threading;
namespace MotoTRBO_SOC
{
public class DBconnThread
{
private static string MyConString;
private string gatewayID;
private bool working = false;
public DBhandle DB;
public DBconnThread(string p_dbConStr, string p_gatewayID)
{
MyConString = p_dbConStr;
gatewayID = p_gatewayID;
working = true;
DB = new DBhandle(MyConString, gatewayID);
}
public void StartDB()
{
try
{
working = true;
DB.StartDB();
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
}
}
public void StopDB()
{
try
{
working = false;
DB.StopDB();
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
}
}
int count = 0;
//for ARS DB thread
public void HandleConnectionARS()
{
Utils.ConsWrite(DebugMSG_Type.DB, "ARS DB thread...");
while (MotoTRBO_GW.running)
{
try
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
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.ConsWrite(DebugMSG_Type.Debug, "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 != "")
{
MotoTRBO_GW.lastEntry = DateTime.Now;
DB.Insert_ARSmsg(suDBid, ars_msg.msg);
}
else
{
Utils.ConsWrite(DebugMSG_Type.Debug, "%%%%% 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.ConsWrite(DebugMSG_Type.always, ex.ToString());
}
} // end while (true)
}
//for GPS DB thread
public void HandleConnectionGPS()
{
Utils.ConsWrite(DebugMSG_Type.DB, "GPS DB thread...");
// first time the connection is made outside this loop
while (!DB.DBconnected)
{
Thread.Sleep(500);
}
int count = 0;
while (MotoTRBO_GW.running)
{
try
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
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 (cell != null)
{
SN_Queues.initAddressQueue.PostItem(cell);
DateTime before = DateTime.Now;
bool ret = DB.Insert_messages(cell);
MotoTRBO_GW.lastEntry = DateTime.Now;
if (ret)
{
DateTime after = DateTime.Now;
TimeSpan diff = after.Subtract(before);
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)
{
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
DB.DBconnected = false;
}
} // end while (true)
}
//for Aux DB thread (units, gateway status)
int countUnit = 30*61;
public void HandleConnectionAux()
{
Utils.ConsWrite(DebugMSG_Type.DB, "Aux DB thread ...");
while (MotoTRBO_GW.running)
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
Thread.Sleep(500);
}
try
{
if (countUnit > 2 * 60)//every 5 minutes
{
MotoTRBO_GW.unitsLoaded = DB.LoadUnitInfo(gatewayID);
countUnit = 0;
}
GWstatus gw_msg = SN_Queues.gwstatusQueue.GetItem(10);
while (gw_msg != null)
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
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);
/*
if (result)
MotoTRBO_GW.lastEntry = DateTime.Now;
*/
gw_msg = SN_Queues.gwstatusQueue.GetItem(10);
}
countUnit++;
Thread.Sleep(300);
}
catch (ThreadAbortException)
{
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
}
} // end while (true)
}
//for Tallysman events Thread
public void HandleTallysmanEvents()
{
Utils.ConsWrite(DebugMSG_Type.always, "Tallysman Events DB thread ...");
int count = 0;
while (MotoTRBO_GW.running)
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
Thread.Sleep(500);
}
if (++count % 10 == 0)
{
//update GPS in DB from poll response
MotoTRBO_SOC.TallysmanReceiveThread.TallysmanEventArgs tallysmanMsg = MotoTRBO_GW.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)
}
//for SMS DB thread
public void HandleConnectionSMS()
{
Utils.ConsWrite(DebugMSG_Type.DB, "SMS DB thread ...");
int count = 0;
while (MotoTRBO_GW.running)
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
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.ConsWrite(DebugMSG_Type.DB, "SMS_conf DB thread ...");
int count = 0;
while (MotoTRBO_GW.running)
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
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.ConsWrite(DebugMSG_Type.DB, "Address DB thread ...");
//get address list
while (!DB.DBconnected && MotoTRBO_GW.running)
{
//Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread waiting for DB connection!!");
Thread.Sleep(500);
}
MotoTRBO_GW.addressLoaded = DB.LoadAddressList(gatewayID);
while (!MotoTRBO_GW.addressLoaded)
{
Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread waiting for address list!!");
Thread.Sleep(500);
}
while (MotoTRBO_GW.running)
{
while (!MotoTRBO_GW.addressLoaded)
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.ConsWrite(DebugMSG_Type.DB, "POLL DB thread ...");
int count = 0;
while (MotoTRBO_GW.running)
{
while (!DB.DBconnected && MotoTRBO_GW.running)
{
Thread.Sleep(500);
}
if (++count % 18 == 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)
}
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;
}
}
}