using System; using System.Collections.Generic; using System.Text; using MySql.Data.MySqlClient; using SafeNetLib; using System.Threading; namespace ConnectPlus_SOC { class DBconnThread { private static string MyConString; private string gatewayID; public 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 ARS DB thread public void HandleConnectionARS() { Utils.ConsWrite(DebugMSG_Type.DB, "ARS DB thread..."); while (ConnectPlus_GW.isRunning) { try { while (!DB.DBconnected && ConnectPlus_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; 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 != "") { ConnectPlus_GW.lastEntry = DateTime.Now; DateTime before = DateTime.Now; DB.Insert_ARSmsg(suDBid, ars_msg.msg); DateTime after = DateTime.Now; TimeSpan diff = after.Subtract(before); string deltaTime = diff.Ticks / TimeSpan.TicksPerMillisecond + "." + diff.Ticks % TimeSpan.TicksPerMillisecond; Utils.ConsWrite(DebugMSG_Type.DB, "Insert_ARSmsg DB time:" + deltaTime); } 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 (ConnectPlus_GW.isRunning) { try { while (!DB.DBconnected && ConnectPlus_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 (cell != null) { // check to see if the position time is in far future if (DateTime.Now.ToUniversalTime().AddHours(3).CompareTo(cell.location_time.ToUniversalTime()) < 0) { Console.ForegroundColor = ConsoleColor.Red; Utils.ConsWrite(DebugMSG_Type.always, "FUTURE POSITION"); Console.ForegroundColor = ConsoleColor.Gray; continue; } //test id this unit is assigned in DB int unitID = Utils.GetDBidFromSUID(cell.suid); if (unitID == -1) { Utils.ConsWrite(DebugMSG_Type.GPS, "Unit <" + cell.suid + "> not assign to this GW!!!"); //LOGS.LOG("Unit <" + cell.suid + "> not assign to this GW!!!"); continue; } //LOGS.LOG("&GPS& " + "HandleConnectionGPS(); Message received from queue cell.suid:" + cell.suid); SN_Queues.initAddressQueue.PostItem(cell); DateTime before = DateTime.Now; bool ret = DB.Insert_messages(cell); ConnectPlus_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 + ">"); string deltaTime = diff.Ticks / TimeSpan.TicksPerMillisecond + "." + diff.Ticks % TimeSpan.TicksPerMillisecond; //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("&GPS& " + "HandleConnectionGPS(); message added to DB + cell.suid:" + cell.suid+ " Time for insert(msecs):" + deltaTime); } else { //LOGS.LOG("&GPS& " + "HandleConnectionGPS(); FAILED to add message to DB + cell.suid:" + cell.suid); Utils.ConsWrite(DebugMSG_Type.DB, "&GPS& " + "HandleConnectionGPS(); FAILED to add message to DB + cell.suid:" + cell.suid); SN_Queues.DBQueueLocation.PostItem(cell); Thread.Sleep(200); } //Console.WriteLine("-----------------------------------"); } } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always,"HandleConnectionGPS->"+ 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 ..."); int count = 1; while (ConnectPlus_GW.isRunning) { if (countUnit % 60 == 0 )//every one minute { //add gateway status!! GWstatus gws = new GWstatus(); gws.gw_id = ConnectPlus_GW.cfg.gw_id; gws.status = ConnectPlus_SOC.WatcherServerThread.connDown ? 0 : 1; if ((DateTime.Now - ConnectPlus_SOC.WatcherServerThread.lastPingTime).TotalSeconds >= 45) { gws.message = "NO PING [" + String.Format("{0:HH:mm:ss}", ConnectPlus_SOC.WatcherServerThread.lastPingTime) + "]"; SN_Queues.gwstatusQueue.PostItem(gws); } //else // gws.message = "LAST PING [" + String.Format("{0:HH:mm:ss}", ConnectPlus_SOC.WatcherServerThread.lastPingTime) + "]"; } try { while (!DB.DBconnected && ConnectPlus_GW.isRunning) { Thread.Sleep(500); } if (countUnit > 5 * 60)//every 5 minutes { //Utils.ConsWrite(DebugMSG_Type.always, "Checking for new units..."); ConnectPlus_GW.unitsLoaded = DB.LoadUnitInfo(gatewayID); countUnit = 0; } } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, "[HandleConnectionAux] Thread Error: " + ex.ToString()); } try { GWstatus gw_msg = SN_Queues.gwstatusQueue.GetItem(10); while (gw_msg != null) { while (!DB.DBconnected && ConnectPlus_GW.isRunning) { Thread.Sleep(500); } // decide if the gateway status message contains an actual message or not if(gw_msg.message != null) DB.Insert_GW_status(gw_msg.gw_id, gw_msg.status, gw_msg.message); else DB.Insert_GW_status(gw_msg.gw_id, gw_msg.status); // get next message gw_msg = SN_Queues.gwstatusQueue.GetItem(10); } countUnit++; Thread.Sleep(300); } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, "Gateway Status Thread Error: " + ex.ToString()); } } // end while (true) } //for SMS DB thread public void HandleConnectionSMS() { Utils.ConsWrite(DebugMSG_Type.DB, "SMS DB thread ..."); int count = 0; while (ConnectPlus_GW.isRunning) { while (!DB.DBconnected && ConnectPlus_GW.isRunning) { Thread.Sleep(500); } if (++count % 10 == 0) { //check SMS confirmation SMSmsg msg = SN_Queues.confSMSQueue.GetItem(100); while (msg != null) { DB.confirmSMS(msg); msg = SN_Queues.confSMSQueue.GetItem(100); } Utils.ConsWrite(DebugMSG_Type.DB, DateTime.Now.ToString("HH:mm:ss") + "->Check for SMS in DB (DB OK!!!)"); DB.checkSMSinDB(); count = 0; } Thread.Sleep(300); } // 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 (ConnectPlus_GW.isRunning) { while (!DB.DBconnected && ConnectPlus_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.ConsWrite(DebugMSG_Type.DB, "Address DB thread ..."); while (ConnectPlus_GW.isRunning) { //get address list while (!DB.DBconnected && ConnectPlus_GW.isRunning) { //Utils.ConsWrite(DebugMSG_Type.DB, "Address DB thread waiting for DB connection!!"); Thread.Sleep(500); } htCell_t cell = SN_Queues.initAddressQueue.GetItem(200); 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); //insert value in DB DateTime before = DateTime.Now; bool ret = DB.Insert_AddressList(cell); DateTime after = DateTime.Now; TimeSpan diff = after.Subtract(before); string deltaTime = diff.Ticks / TimeSpan.TicksPerMillisecond + "." + diff.Ticks % TimeSpan.TicksPerMillisecond; //Utils.ConsWrite(DebugMSG_Type.DB, "Insert_AddressList DB time:" + deltaTime); //Utils.ConsWrite(DebugMSG_Type.DB, "addr_hash:" + addr_hash + " addr.addr_hash:" + addr.addr_hash); } } // end while (true) } //for poll Thread public void HandleConnectionPOLL() { Utils.ConsWrite(DebugMSG_Type.DB, "POLL DB thread ..."); int count = 0; while (ConnectPlus_GW.isRunning) { while (!DB.DBconnected && ConnectPlus_GW.isRunning) { Thread.Sleep(500); } if (++count % 10 == 0) { //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.DEV, "Got poll location updating DB 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); } } } Thread.Sleep(500); } // end while (true) } } }