using System; using System.Collections.Generic; using System.Text; using SafeNetLib; using System.Threading; using SN_Server; using SN_Server.ServerMSGS; namespace MotoRepeater_SOC { class QueueManagerThread { private bool working = false; private ServerConnector connector; public QueueManagerThread(ServerConnector connector) { working = true; this.connector = connector; } public void Start() { try { working = true; } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, ex.ToString()); } } public void Stop() { try { working = false; } 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 (working) { try { while (!connector._connected) { //Utils.ConsWrite(DebugMSG_Type.always, "Reconneting to DB!!!"); //StartDB(); //wait for valid DB connection!!! Utils.ConsWrite(DebugMSG_Type.always, "ARS MAnager thread waiting for valid Server 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 Server suid:" + ars_msg.imei + "msg:" + ars_msg.msg); int suDBid = MotoRepeater_GW.unitList.GetDBid(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 != "") { connector.InsertARS(suDBid.ToString(), ars_msg.msg); //DB.Insert_ARSmsg(suDBid, ars_msg.msg); } else { Utils.ConsWrite(DebugMSG_Type.Debug, "%%%%% Got ARS invalid suid:" + ars_msg.suid + "msg:" + ars_msg.msg); } } ars_msg = SN_Queues.arsMsgQueue.GetItem(10); } } catch (ThreadAbortException) { } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, ex.ToString()); } } // end while (true) } //for GPS Server thread public void HandleConnectionGPS() { Utils.ConsWrite(DebugMSG_Type.DB, "GPS DB thread..."); while (working) { try { while (!connector._connected) { Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to Server!!!"); 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); int suDBid = MotoRepeater_GW.unitList.GetDBid(cell.suid); if (suDBid != 0) { GPS gps = converCell2GPS(cell, suDBid); if (gps != null) { bool ret = connector.InsertGPS(gps); 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, "Sent to server 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.Debug, "Unit with IMEI:" + cell.suid + " not found in DB!!!!"); LOGS.LOG("Unit with IMEI:" + cell.suid + " not found in DB!!!!"); } Console.WriteLine("-----------------------------------"); } } catch (ThreadAbortException) { } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, ex.ToString()); } } // end while (true) } //for SMS Server thread public void HandleConnectionSMS() { Utils.ConsWrite(DebugMSG_Type.DB, "SMS DB thread ..."); while (working) { while (!connector._connected) { Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to Server!!!"); Thread.Sleep(5000); } //check for new SMS in DB only every 30s Thread.Sleep(30 * 1000); //check SMS recv SMS recv_msg = SM_SMS.recvSMSQueue.GetItem(100); while (recv_msg != null) { Utils.ConsWrite(DebugMSG_Type.DB, "We got new messages.Sending them to server. RadioID:" + recv_msg.RadioID + " msg:" + recv_msg.Msg); connector.InsertSMS(recv_msg); recv_msg = SM_SMS.recvSMSQueue.GetItem(100); } //check SMS confirmation SMS msg = SM_SMS.confSMSQueue.GetItem(100); while (msg != null) { Utils.ConsWrite(DebugMSG_Type.DB, "Confirming SMS to server. DBmsgID:" + msg.DBid + " msg:" + msg.Msg); connector.InsertSMSack(msg); msg = SM_SMS.confSMSQueue.GetItem(100); } } // end while (true) } //for poll Thread public void HandleConnectionPOLL() { Utils.ConsWrite(DebugMSG_Type.DB, "POLL Server thread ..."); while (working) { while (!connector._connected) { Utils.ConsWrite(DebugMSG_Type.DB, "Reconneting to Server!!!"); Thread.Sleep(5000); } //check for new POLL requests in DB every 3s Thread.Sleep(3000); Utils.ConsWrite(DebugMSG_Type.DB, "Checking for poll at:" + DateTime.Now); //check Poll read SN_Server.ServerMSGS.SM_POLLmsg p_msg1 = SM_GPS.readPOLLQueue.GetItem(100); if (p_msg1 != null) { //Utils.ConsWrite(DebugMSG_Type.DEV, "MotoTRBOGW.sentPOLLQueue.GetItem(100);"); connector.InsertPOLL(1, p_msg1); } //check Poll sent SN_Server.ServerMSGS.SM_POLLmsg p_msg2 = SM_GPS.sentPOLLQueue.GetItem(100); if (p_msg2 != null) { //Utils.ConsWrite(DebugMSG_Type.DEV, "MotoTRBOGW.sentPOLLQueue.GetItem(100);"); connector.InsertPOLL(2, p_msg2); } //update GPS in DB from poll response SN_Server.ServerMSGS.SM_POLLmsg p_msg3 = SM_GPS.recvPOLLQueue.GetItem(100); if (p_msg3 != null) { Utils.ConsWrite(DebugMSG_Type.DEV, "Got poll location updating DB suid:" + p_msg3.suid); connector.InsertPOLL(3, p_msg3); //remove poll from hashtable ... poll completed sucsessfully lock (SN_Queues.ht_POLL_List.SyncRoot) { SN_Queues.ht_POLL_List.Remove(p_msg3.DBid); } } } // end while (true) } public static GPS converCell2GPS(htCell_t cell, int dbid) { try { GPS gps = new GPS(); gps.s_suid = cell.suid; gps.i_dbid = dbid; gps.s_lat = cell.lat; gps.s_lng = cell.lng; gps.d_lat = cell.d_lat; gps.d_lng = cell.d_lng; gps.dt_time = cell.location_time; if (!Int32.TryParse(cell.spd, out gps.i_spd)) gps.i_spd = 0; TimeSpan span = (cell.location_time - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); gps.i_time = (int)span.TotalSeconds; return gps; } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, ex.ToString()); } return null; } private SMS convertSms(SMSmsg old_msg, int sudbid) { try { SMS new_msg = new SMS(); new_msg.DBid = (int)old_msg.DBmsg_id; new_msg.Msg = old_msg.msg; new_msg.RadioID = old_msg.suid; new_msg.SUDBid = sudbid; return new_msg; } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, ex.ToString()); } return null; } } }