200 lines
6.9 KiB
C#
200 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using SafeNetLib;
|
|
using System.Threading;
|
|
|
|
namespace PAL_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());
|
|
|
|
}
|
|
}
|
|
|
|
private void StopDB()
|
|
{
|
|
DB.StopDB();
|
|
}
|
|
|
|
int count = 0;
|
|
|
|
//for GPS DB thread
|
|
int gps_count = 0;
|
|
public void HandleConnectionGPS()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "GPS DB thread...");
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
if (gps_count > 5000)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "DB maintenance restart!");
|
|
StopDB();
|
|
Thread.Sleep(5000);
|
|
gps_count = 0;
|
|
}
|
|
gps_count++;
|
|
while (!DB.DBconnected)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to DB!!!");
|
|
StartDB();
|
|
Thread.Sleep(5000);
|
|
}
|
|
htCell_t cell = SN_Queues.DBQueueLocation.GetItem(100);
|
|
|
|
if (cell != null)
|
|
{
|
|
DateTime before = DateTime.Now;
|
|
bool ret = DB.Insert_messages(cell);
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Trying to insert messasge in DB..... if (cell != null)");
|
|
if (ret)
|
|
{
|
|
SN_Queues.initAddressQueue.PostItem(cell);
|
|
|
|
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);
|
|
}
|
|
else
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Could not insert address in DB");
|
|
}
|
|
Console.WriteLine("-----------------------------------");
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always,"Insert message error "+ 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)
|
|
{
|
|
while (!DB.DBconnected)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "HandleConnectionAux thread waiting for DB connection!!");
|
|
Thread.Sleep(1000);
|
|
}
|
|
if (countUnit > 15 * 60)//every 15 minutes
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Loading new units");
|
|
Program.unitsLoaded = DB.LoadUnitInfo(gatewayID);
|
|
countUnit = 0;
|
|
}
|
|
|
|
GWstatus gw_msg = SN_Queues.gwstatusQueue.GetItem(10);
|
|
while (gw_msg != null)
|
|
{
|
|
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 inserting address Thread
|
|
public void HandleConnectionAddr()
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread ...");
|
|
|
|
//get address list
|
|
while (!DB.DBconnected)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread waiting for DB connection!!");
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
Program.addressLoaded = DB.LoadAddressList(gatewayID);
|
|
|
|
while (!Program.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 (!Program.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 from queue...");
|
|
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)
|
|
}
|
|
}
|
|
}
|