public void TcpOnRecievedData(IAsyncResult ar) { String sGPS; // Get The connection socket from the callback TCPSockets tcpSockets = (TCPSockets)ar.AsyncState; try { // Get The data , if any int nBytesRec = tcpSockets.socket.EndReceive(ar); if (nBytesRec > 0) { sGPS = System.Text.Encoding.ASCII.GetString(tcpSockets.byTcpBuffer, 0, nBytesRec); try { if (m_GpsTracker.m_MessageMonitor != null) { m_GpsTracker.m_MessageMonitor.AddMessageTCPUDPRaw(sGPS); } } catch (Exception) { m_GpsTracker.m_MessageMonitor = null; } tcpSockets.sStream += sGPS; int iIndex = -1; char [] cEOL = { '\n', '\r' }; string sData = ""; do { iIndex = tcpSockets.sStream.IndexOfAny(cEOL); if (iIndex >= 0) { sData = tcpSockets.sStream.Substring(0, iIndex); sData = sData.Trim(cEOL); tcpSockets.sStream = tcpSockets.sStream.Remove(0, iIndex + 1); if (sData != "") { m_GpsTracker.ShowGPSIcon(sData.ToCharArray(), sData.Length, false, tcpSockets.iDeviceIndex, false, true, 0, 0); } } }while(iIndex >= 0); } AsyncCallback recieveData = new AsyncCallback(TcpOnRecievedData); // Begin reading data asyncronously tcpSockets.socket.BeginReceive(tcpSockets.byTcpBuffer, 0, tcpSockets.byTcpBuffer.Length, SocketFlags.None, recieveData, tcpSockets); } catch (Exception) { } }
public void threadFile() { int iIndex = Int32.Parse(Thread.CurrentThread.Name); GPSSource gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex]; int iIndexFile; int iIndexNext; StreamReader fsGpsFile = null; System.Text.ASCIIEncoding asciiEncoder = new System.Text.ASCIIEncoding(); String sGpsLine = null; String sIndex; byte [] byNMEA; char [] cNMEA = null; char [] cNMEAMsg = new char[512]; DateTime dtCurrent; DateTime dtNext = new DateTime(0); int iEnd = 0; bool bRestartTrack = false; try { if (File.Exists(gpsSource.sFileNameSession)) { while (m_GpsTracker.m_fCloseThreads == false) { bRestartTrack = true; fsGpsFile = File.OpenText(gpsSource.sFileNameSession); { if (m_GpsTracker.m_fPlayback == true) { m_GpsTracker.LoadSettings(fsGpsFile, false); iIndexFile = iIndex + 1; do { sIndex = fsGpsFile.ReadLine(); if (sIndex != null) { iEnd = sIndex.IndexOf(","); } if (sIndex != null && iEnd > 0) { iIndexFile = Convert.ToInt32(sIndex.Substring(0, iEnd)); if (iIndexFile == iIndex && sIndex.Length > iEnd + 1) { sGpsLine = sIndex.Substring(iEnd + 1); break; } } else { break; } } while(m_GpsTracker.m_fCloseThreads == false); } else { sIndex = "dummyindex"; iIndexFile = iIndex; sGpsLine = fsGpsFile.ReadLine(); } if (sGpsLine != null) { byNMEA = asciiEncoder.GetBytes(sGpsLine); cNMEA = asciiEncoder.GetChars(byNMEA); cNMEA.CopyTo(cNMEAMsg, 0); if (m_GpsTracker.m_iPlaybackSpeed > 0) { dtNext = m_GpsTracker.m_NMEA.GetNMEATime(-1, sGpsLine); } } while (sIndex != null && sGpsLine != null && m_GpsTracker.m_fCloseThreads == false && iIndexFile == iIndex) { if (sGpsLine != "" && sIndex != "") { try { if (m_GpsTracker.m_MessageMonitor != null) { m_GpsTracker.m_MessageMonitor.AddMessageFileRaw(sGpsLine); } } catch (Exception) { m_GpsTracker.m_MessageMonitor = null; } if (m_GpsTracker.ShowGPSIcon(cNMEAMsg, cNMEA.Length, false, iIndex, bRestartTrack, true) == true) { if (bRestartTrack) { bRestartTrack = false; } //Get current NMEA msg time dtCurrent = dtNext; while (m_GpsTracker.m_fCloseThreads == false) { //Get next line from file if (m_GpsTracker.m_fPlayback == true) { sIndex = fsGpsFile.ReadLine(); if (sIndex != null) { iEnd = sIndex.IndexOf(","); if (iEnd > 0 && sIndex.Length > iEnd + 1) { sGpsLine = sIndex.Substring(iEnd + 1); sIndex = sIndex.Substring(0, iEnd); } else { sIndex = ""; } } } else { sIndex = Convert.ToString(iIndex); sGpsLine = fsGpsFile.ReadLine(); } if (sGpsLine == null || sIndex == null) { break; } else if (sGpsLine != "" && sIndex != "") { iIndexNext = Convert.ToInt32(sIndex); byNMEA = asciiEncoder.GetBytes(sGpsLine); cNMEA = asciiEncoder.GetChars(byNMEA); cNMEA.CopyTo(cNMEAMsg, 0); //Check is a valid NMEA message if (iIndexNext == iIndex && m_GpsTracker.m_NMEA.ParseGPSMessage(cNMEAMsg, cNMEA.Length, true, iIndex) == true) { if (m_GpsTracker.m_iPlaybackSpeed > 0 && gpsSource.iFilePlaySpeed > 0) { //Get time difference from current msg to next message... dtNext = m_GpsTracker.m_NMEA.GetNMEATime(-1, sGpsLine); //Check valid times if (dtCurrent.Year != 1771 && dtNext.Year != 1771) { if (dtNext < dtCurrent) { dtNext = dtNext.AddDays(1); } TimeSpan tsDelay = dtNext.Subtract(dtCurrent); double dDelay = tsDelay.TotalMilliseconds; //apply selected speed double dSpeed = Convert.ToDouble(gpsSource.iFilePlaySpeed); dDelay /= dSpeed; dDelay /= m_GpsTracker.m_iPlaybackSpeed; Thread.Sleep(Convert.ToInt32(dDelay)); break; } else { Thread.Sleep(1); break; } } else { Thread.Sleep(1); break; } } else { Thread.Sleep(1); } } else { Thread.Sleep(1); //break; } } //while } //show else { sIndex = "dummyindex"; iIndexFile = iIndex; sGpsLine = fsGpsFile.ReadLine(); if (sGpsLine != null) { byNMEA = asciiEncoder.GetBytes(sGpsLine); cNMEA = asciiEncoder.GetChars(byNMEA); cNMEA.CopyTo(cNMEAMsg, 0); if (m_GpsTracker.m_iPlaybackSpeed > 0) { dtNext = m_GpsTracker.m_NMEA.GetNMEATime(-1, sGpsLine); } } Thread.Sleep(1); } } // if gpsline else { if (sGpsLine == "") { sGpsLine = fsGpsFile.ReadLine(); } Thread.Sleep(1); //break; } } //while if (fsGpsFile != null) { fsGpsFile.Close(); fsGpsFile = null; } if (gpsSource.iReload == 0 && m_GpsTracker.m_fCloseThreads == false) { Thread.Sleep(3000); } else { break; } } //using } } //exists } catch (Exception) { } if (fsGpsFile != null) { fsGpsFile.Close(); fsGpsFile = null; } }
// //sCallSign is the call sign get data from //use * as wild card (only at the end of the call sign): //eg: sCallSign=N9UMJ-15 //eg: sCallSign=N9UMJ* //eg: sCallSign=* - get data from all available call signs public void threadAPRSIS() { int iIndex = Int32.Parse(Thread.CurrentThread.Name); GPSSource gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex]; string sCallSign = gpsSource.sCallSign; int iRefresh = gpsSource.iRefreshRate; while (sCallSign != null && sCallSign.Length > 0 && m_GpsTracker.m_fCloseThreads == false) { try { HttpWebRequest request = null; request = (HttpWebRequest)WebRequest.Create(gpsSource.sAPRSServerURL.Trim() + sCallSign); HttpWebResponse response = null; // execute the request response = (HttpWebResponse)request.GetResponse(); // we will read data via the response stream Stream resStream = response.GetResponseStream(); resStream.ReadTimeout = 60000; int iRead; byte[] byChar = new byte[1]; StringBuilder sbMsg = new StringBuilder(""); do { iRead = resStream.ReadByte(); if (iRead >= 0) { byChar[0] = Convert.ToByte(iRead); if (byChar[0] != '\r' && byChar[0] != '\n') { sbMsg.Append(Encoding.ASCII.GetString(byChar)); } else { string sMsg = sbMsg.ToString(); try { if (m_GpsTracker.m_MessageMonitor != null) { m_GpsTracker.m_MessageMonitor.AddMessageUnfilteredAPRS(sMsg); } } catch (Exception) { m_GpsTracker.m_MessageMonitor = null; } char [] cMessage = sMsg.ToCharArray(); if (!m_GpsTracker.ShowGPSIcon(cMessage, sMsg.Length, false, iIndex, false, true)) { if (sMsg.StartsWith("\"packet_id\"") == false) { CSVReader csvReader = new CSVReader(); string [] sMsgField = csvReader.GetCSVLine(sMsg); if (sMsgField != null && sMsgField.Length == 14) { //Display icon gpsSource.GpsPos.m_sName = sMsgField[1]; if (sMsgField[2] != "") { gpsSource.GpsPos.m_fLat = Convert.ToSingle(sMsgField[2]); } if (sMsgField[3] != "") { gpsSource.GpsPos.m_fLon = Convert.ToSingle(sMsgField[3]); } if (sMsgField[4] != "") { gpsSource.GpsPos.m_fHeading = Convert.ToSingle(sMsgField[4]); } if (sMsgField[5] != "") { gpsSource.GpsPos.m_fSpeed = Convert.ToSingle(sMsgField[5]); } if (sMsgField[6] != "") { gpsSource.GpsPos.m_fAlt = Convert.ToSingle(sMsgField[6]); } if (sMsgField[7] != "") { gpsSource.GpsPos.m_iAPRSIconTable = Convert.ToInt32(Convert.ToChar(sMsgField[7])); } if (sMsgField[8] != "") { gpsSource.GpsPos.m_iAPRSIconCode = Convert.ToInt32(Convert.ToChar(sMsgField[8])); } gpsSource.GpsPos.m_sComment = sMsgField[9]; if (sMsgField[13] != "") { DateTime dateAPRS = new DateTime(0); dateAPRS = DateTime.Parse(sMsgField[13]); gpsSource.GpsPos.m_iYear = dateAPRS.Year; gpsSource.GpsPos.m_iMonth = dateAPRS.Month; gpsSource.GpsPos.m_iDay = dateAPRS.Day; gpsSource.GpsPos.m_iHour = dateAPRS.Hour; gpsSource.GpsPos.m_iMin = dateAPRS.Minute; gpsSource.GpsPos.m_iSec = Convert.ToSingle(dateAPRS.Second); } gpsSource.GpsPos.m_fRoll = -1000F; gpsSource.GpsPos.m_fPitch = -1000F; gpsSource.GpsPos.m_fESpeed = -1000000; gpsSource.GpsPos.m_fNSpeed = -1000000; gpsSource.GpsPos.m_fVSpeed = -1000000; gpsSource.GpsPos.m_fDepth = -1000000; gpsSource.GpsPos.m_sAltUnit = "m"; gpsSource.GpsPos.m_sSpeedUnit = "km\\h"; sMsg = "APRS:" + sMsg; cMessage = sMsg.ToCharArray(); m_GpsTracker.ShowGPSIcon(cMessage, sMsg.Length, false, iIndex, false, false); } } } sbMsg = new StringBuilder(""); } } } while (iRead >= 0 && m_GpsTracker.m_fCloseThreads == false); } catch (Exception) { } for (int iDelay = 0; iDelay < (iRefresh * 2); iDelay++) { if (m_GpsTracker.m_fCloseThreads == true) { break; } Thread.Sleep(500); } } gpsSource.eventThreadSync.Set(); }