341 lines
12 KiB
C#
341 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using MySql.Data.MySqlClient;
|
|
using SafeNetLib;
|
|
using System.Threading;
|
|
|
|
|
|
namespace Hytera_SOC
|
|
{
|
|
class DBconnThread
|
|
{
|
|
private static string MyConString;
|
|
private string gatewayID;
|
|
|
|
private DBhandle DB;
|
|
|
|
public DBconnThread(string p_dbConStr, string p_gatewayID)
|
|
{
|
|
MyConString = p_dbConStr;
|
|
gatewayID = p_gatewayID;
|
|
|
|
DB = new DBhandle(MyConString, gatewayID);
|
|
|
|
}
|
|
|
|
private void StartDB()
|
|
{
|
|
try
|
|
{
|
|
DB.StartDB();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
|
|
}
|
|
}
|
|
|
|
public void StopDB()
|
|
{
|
|
DB.StopDB();
|
|
}
|
|
|
|
int count = 0;
|
|
//for ARS DB thread
|
|
public void HandleConnectionARS()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "ARS DB thread...");
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
while(!DB.DBconnected)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.always, "Reconneting to DB!!!");
|
|
//StartDB();
|
|
|
|
//wait for valid DB connection!!!
|
|
Utils.ConsWrite(DebugMSG_Type.always, "ARS DB thread waiting for valid DB connection!!!");
|
|
Thread.Sleep(5000);
|
|
}
|
|
|
|
//add ARS in DB
|
|
ArsMSG ars_msg = SN_Queues.arsMsgQueue.GetItem(10);
|
|
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;
|
|
lock (SN_Queues.ht_SUInfo.SyncRoot)
|
|
{
|
|
if (SN_Queues.ht_SUInfo != null)
|
|
{
|
|
if (SN_Queues.ht_SUInfo.ContainsKey(ars_msg.imei))
|
|
suDBid = ((SUinfo)SN_Queues.ht_SUInfo[ars_msg.imei]).DBid;
|
|
}
|
|
}
|
|
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 != "")
|
|
{
|
|
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 (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
DB.DBconnected = false;
|
|
}
|
|
} // end while (true)
|
|
}
|
|
|
|
//for GPS DB thread
|
|
public void HandleConnectionGPS()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "GPS DB thread...");
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
while (!DB.DBconnected)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "!!!!!!!!!!!!!!!!! GPD DB thread -> Reconneting to DB !!!!!!!");
|
|
StartDB();
|
|
Thread.Sleep(5000);
|
|
}
|
|
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);
|
|
Hytera_GW.lastEntry = DateTime.Now;
|
|
if (ret)
|
|
{
|
|
DateTime after = DateTime.Now;
|
|
TimeSpan diff = after.Subtract(before);
|
|
count++;
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "GPS SUID: " + cell.suid + " LAT<" + cell.lat + " > LNG<" + cell.lng + ">");
|
|
//Utils.ConsWrite(DebugMSG_Type.DB, "Added count:" + count + " in queue:" + SN_Queues.DBQueueLocation.Count + " Time for insert(msecs):" + diff.Ticks / TimeSpan.TicksPerMillisecond + "." + diff.Ticks % TimeSpan.TicksPerMillisecond);
|
|
}
|
|
//Console.WriteLine("-----------------------------------");
|
|
}
|
|
|
|
}
|
|
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 (true)
|
|
{
|
|
if (countUnit > 5 * 60)//every 5 minutes
|
|
{
|
|
Hytera_GW.unitsLoaded = DB.LoadUnitInfo(gatewayID);
|
|
countUnit = 0;
|
|
}
|
|
|
|
GWstatus gw_msg = SN_Queues.gwstatusQueue.GetItem(10);
|
|
while (gw_msg != null)
|
|
{
|
|
|
|
while (!DB.DBconnected)
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
DB.Insert_GW_status(gw_msg.gw_id, gw_msg.status);
|
|
gw_msg = SN_Queues.gwstatusQueue.GetItem(10);
|
|
}
|
|
|
|
countUnit++;
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
} // end while (true)
|
|
}
|
|
|
|
//for SMS DB thread
|
|
public void HandleConnectionSMS()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "SMS DB thread ...");
|
|
|
|
while (true)
|
|
{
|
|
//check for new SMS in DB only every 30s
|
|
Thread.Sleep(30 * 1000);
|
|
|
|
Utils.ConsWrite(DebugMSG_Type.DB, DateTime.Now.ToString("HH:mm:ss") + "->Check for SMS in DB (DB OK!!!)");
|
|
DB.checkSMSinDB();
|
|
|
|
} // end while (true)
|
|
}
|
|
|
|
//for SMS Conf DB thread
|
|
public void HandleConnectionSMS_conf()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "SMS_conf DB thread ...");
|
|
|
|
while (true)
|
|
{
|
|
//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);
|
|
}
|
|
Thread.Sleep(5 * 1000);
|
|
} // end while (true)
|
|
}
|
|
|
|
//for inserting address Thread
|
|
public void HandleConnectionAddr()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread ...");
|
|
|
|
//get address list
|
|
while(!DB.DBconnected)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.always, "Reconneting to DB!!!");
|
|
//StartDB();
|
|
|
|
//wait for valid DB connection!!!
|
|
Utils.ConsWrite(DebugMSG_Type.always, "address DB thread waiting for valid DB connection!!!");
|
|
Thread.Sleep(5000);
|
|
|
|
}
|
|
|
|
Hytera_GW.addressLoaded = DB.LoadAddressList(gatewayID);
|
|
|
|
while (!Hytera_GW.addressLoaded)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread waiting for address list!!");
|
|
Thread.Sleep(1000);
|
|
}
|
|
Utils.ConsWrite(DebugMSG_Type.DB," SN_Queues.ht_addressList.Count" + SN_Queues.ht_addressList.Count);
|
|
|
|
while (true)
|
|
{
|
|
while (!Hytera_GW.addressLoaded)
|
|
Thread.Sleep(100);
|
|
//add addr to tb if needed
|
|
|
|
while(!DB.DBconnected)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "address DB thread waiting for valid DB connection!!!");
|
|
Thread.Sleep(5000);
|
|
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
} // end while (true)
|
|
}
|
|
|
|
//for poll Thread
|
|
public void HandleConnectionPOLL()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "POLL DB thread ...");
|
|
|
|
while (true)
|
|
{
|
|
//check for new POLL requests in DB only every 3s
|
|
Thread.Sleep(3000);
|
|
Utils.ConsWrite(DebugMSG_Type.DEV, "Checking for poll at:" + DateTime.Now);
|
|
//get new POLL requests
|
|
DB.GetPollRequests();
|
|
|
|
//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.UpdatePollGPS(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);
|
|
}
|
|
}
|
|
|
|
} // end while (true)
|
|
}
|
|
}
|
|
}
|