using System; using System.Collections.Generic; using System.Linq; using System.Text; using SafeNetLib; using System.Threading; namespace LMdirect_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, Program.cfg.locPort); } 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 public void HandleConnectionGPS() { Utils.ConsWrite(DebugMSG_Type.DB, "GPS DB thread..."); while (true) { try { while (!DB.DBconnected) { Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to DB!!!"); StartDB(); Thread.Sleep(1000); } htCell_t cell = SN_Queues.DBQueueLocation.GetItem(100); if (cell != null) { DateTime before = DateTime.Now; bool ret = DB.Insert_messages(cell); 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 + ">"); LOGS.LOG(DateTime.Now.ToString() + " : 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); LOGS.LOG(DateTime.Now.ToString() + " : 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) { count++; if (count % 60 == 0) { Int64 upTime = Utils.DateTo70Format(DateTime.Now) - Utils.DateTo70Format(Program.startTime); Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; GWstatus gws = new GWstatus(); gws.gw_id = Program.cfg.gatewayID; gws.status = 1; gws.message = String.Format("LMU GW: Uptime:{0};Version:{1};", upTime, v.ToString()); SN_Queues.gwstatusQueue.PostItem(gws); } while (!DB.DBconnected) { Thread.Sleep(1000); } if (countUnit > 5 * 60)//every 5 minutes { 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.message); 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!!"); LOGS.LOG(DateTime.Now.ToString() + " : 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!!"); //LOGS.LOG(DateTime.Now.ToString() + " : 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); LOGS.LOG(DateTime.Now.ToString() + " : 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); LOGS.LOG(DateTime.Now.ToString() + " : 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) } } }