Exemplo n.º 1
0
        public void threadStartUSB()
        {
            int       iIndex    = Int32.Parse(Thread.CurrentThread.Name);
            GPSSource gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex];

            while (m_GpsTracker.m_fCloseThreads == false)
            {
                lock ("threadStartUSB")
                {
                    //Use GpsBabel
                    string sBabelCommandLine = "-T;-i;" + gpsSource.sUSBDevice + ";-f;usb:;-o;kml;-F;" + GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".kml;";
                    if (File.Exists(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".kml"))
                    {
                        File.Delete(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".kml");
                    }
                    m_GpsBabel.Execute(sBabelCommandLine);

                    if (File.Exists(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".kml"))
                    {
                        FileInfo fInfo = new FileInfo(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".kml");
                        if (fInfo.Length > 0)
                        {
                            sBabelCommandLine  = "-w;-i;kml;-f;" + GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".kml;";
                            sBabelCommandLine += "-o;nmea;-F;" + GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".nmea;";
                            if (File.Exists(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".nmea"))
                            {
                                File.Delete(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".nmea");
                            }
                            m_GpsBabel.Execute(sBabelCommandLine);

                            if (File.Exists(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".nmea"))
                            {
                                fInfo = new FileInfo(GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".nmea");

                                if (fInfo.Length > 0)
                                {
                                    gpsSource.sFileNameSession = GpsTrackerPlugin.m_sPluginDirectory + "\\USB" + Convert.ToString(iIndex) + ".nmea";
                                    gpsSource.iFilePlaySpeed   = 0;
                                    gpsSource.iReload          = 1;
                                    gpsSource.bBabelNMEA       = true;
                                    gpsSource.datePosition     = fInfo.LastWriteTimeUtc;
                                    threadFile();
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(500);
            }
            gpsSource.eventThreadSync.Set();
        }
Exemplo n.º 2
0
        //
        //NMEA Parsing
        //
        //Parse the data in cNMEAMessage looking for supported messages,
        //if found get lat and lon and return true (locked).
        public bool ParseGPSMessage(char [] cNMEAMessage, int iMsgLength, bool fCheck, int iIndex)
        {
            bool fLocked = false;
            SupportedMessages supportedMsg = SupportedMessages.msgNotSupported;
            GPSSource         gpsSource    = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex];

            string [] sMsgField;
            String    sGpsLat     = "";
            String    sGpsLatMin  = "";
            String    sGpsLon     = "";
            String    sGpsLonMin  = "";
            String    sNS         = "";
            String    sEW         = "";
            String    sGpsSpeed   = "";
            String    sGpsAlt     = "";
            String    sGpsAltUnit = "";
            String    sGpsHeading = "";
            String    sGpsDate    = "";
            //String sCallSign="";
            int iIndex1;

            gpsSource.GpsPos.m_sName    = "";
            gpsSource.GpsPos.m_sComment = "";

//			#if !DEBUG
            try
//			#endif
            {
                //cNMEAMessage[iMsgLength]=(char)0; //ensure null terminated;
                char [] trimChars = { ' ', '\t', '\r', '\n' };
                String  sGPRMC    = new String(cNMEAMessage, 0, iMsgLength);
                sGPRMC = sGPRMC.TrimStart(trimChars);
                sGPRMC = sGPRMC.TrimEnd(trimChars);

                //System.Diagnostics.Debug.Write(sGPRMC);
                //System.Diagnostics.Debug.Write(Environment.NewLine);

                #region Check for supported message
                //look for the GPRMC message
                if (iMsgLength > 11)
                {
                    if (sGPRMC.StartsWith("$GPRMC") == true)
                    {
                        supportedMsg = SupportedMessages.msgGPRMC;
                    }
                    else
                    if (sGPRMC.StartsWith("$GPGGA") == true)
                    {
                        supportedMsg = SupportedMessages.msgGPGGA;
                    }
                }
                #endregion

                if (supportedMsg != SupportedMessages.msgNotSupported)
                {
                    if (fCheck)                     //they just want to know if this is a Supported message
                    {
                        fLocked = true;
                    }
                    else
                    {
                        switch (supportedMsg)
                        {
                            //if we have the correct message, parse data and get Lat and Lon.
                            //TODO: rewrite this in a more efficient way.
                            #region GPRMC
                        case SupportedMessages.msgGPRMC:
                            sMsgField = m_CSVParser.GetCSVLine(sGPRMC);
                            //Proceed with Lat and Lon only if we have an Active lock into the
                            //sattelites.
                            if (sMsgField[2].ToUpper() == "A" || gpsSource.bBabelNMEA == true)
                            {
                                sGpsLat    = sMsgField[3];
                                iIndex1    = sGpsLat.IndexOf('.', 0);
                                sGpsLatMin = sGpsLat.Substring(iIndex1 - 2);
                                sGpsLat    = sGpsLat.Substring(0, iIndex1 - 2);
                                sNS        = sMsgField[4];

                                sGpsLon    = sMsgField[5];
                                iIndex1    = sGpsLon.IndexOf('.', 0);
                                sGpsLonMin = sGpsLon.Substring(iIndex1 - 2);
                                sGpsLon    = sGpsLon.Substring(0, iIndex1 - 2);
                                sEW        = sMsgField[6];

                                sGpsSpeed   = sMsgField[7];
                                sGpsHeading = sMsgField[8];

                                sGpsDate = sMsgField[9];

                                sGpsAlt = "NA";

                                GetNMEATime(iIndex, sGPRMC);

                                fLocked = true;
                            }
                            break;

                            #endregion
                            #region GPGGA
                        case SupportedMessages.msgGPGGA:
                            sMsgField = m_CSVParser.GetCSVLine(sGPRMC);
                            //Proceed with Lat and Lon only if we have an Active lock into the
                            //sattelites.
                            if (sMsgField[6] == "1" || sMsgField[6] == "2" || gpsSource.bBabelNMEA == true)
                            {
                                sGpsLat    = sMsgField[2];
                                iIndex1    = sGpsLat.IndexOf('.', 0);
                                sGpsLatMin = sGpsLat.Substring(iIndex1 - 2);
                                sGpsLat    = sGpsLat.Substring(0, iIndex1 - 2);
                                sNS        = sMsgField[3];

                                sGpsLon    = sMsgField[4];
                                iIndex1    = sGpsLon.IndexOf('.', 0);
                                sGpsLonMin = sGpsLon.Substring(iIndex1 - 2);
                                sGpsLon    = sGpsLon.Substring(0, iIndex1 - 2);
                                sEW        = sMsgField[5];


                                sGpsAlt     = sMsgField[9];
                                sGpsAltUnit = sMsgField[10];

                                sGpsDate    = "NA";
                                sGpsSpeed   = "NA";
                                sGpsHeading = "NA";

                                GetNMEATime(iIndex, sGPRMC);

                                fLocked = true;
                            }
                            break;
                            #endregion
                        }

                        if (fLocked)
                        {
                            #region Convert to WorldWind format
                            switch (supportedMsg)
                            {
                                #region GPRMC, GPGGA
                            case SupportedMessages.msgGPRMC:
                            case SupportedMessages.msgGPGGA:
                                sGpsLatMin = sGpsLatMin.Replace('.', Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                                gpsSource.GpsPos.m_fLat = System.Convert.ToDouble(sGpsLatMin) / (float)60;
                                gpsSource.GpsPos.m_fLat = System.Convert.ToDouble(sGpsLat) + gpsSource.GpsPos.m_fLat;
                                if (sNS.ToUpper() == "S")
                                {
                                    gpsSource.GpsPos.m_fLat *= (float)-1;
                                }

                                sGpsLonMin = sGpsLonMin.Replace('.', Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                                gpsSource.GpsPos.m_fLon = System.Convert.ToDouble(sGpsLonMin) / (float)60;
                                gpsSource.GpsPos.m_fLon = System.Convert.ToDouble(sGpsLon) + gpsSource.GpsPos.m_fLon;
                                if (sEW.ToUpper() == "W")
                                {
                                    gpsSource.GpsPos.m_fLon *= (float)-1;
                                }

                                if (sGpsSpeed != "NA")
                                {
                                    sGpsSpeed = sGpsSpeed.Replace('.', Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                                    gpsSource.GpsPos.m_fSpeed     = System.Convert.ToSingle(sGpsSpeed);
                                    gpsSource.GpsPos.m_sSpeedUnit = "knots";
                                    gpsSource.GpsPos.m_iSpeedUnit = (int)SpeedUnits.unitKnots;
                                }

                                if (sGpsAlt != "NA")
                                {
                                    sGpsAlt = sGpsAlt.Replace('.', Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                                    gpsSource.GpsPos.m_fAlt          = System.Convert.ToSingle(sGpsAlt);
                                    gpsSource.GpsPos.m_sAltUnit      = sGpsAltUnit;
                                    gpsSource.GpsPos.m_iAltitudeUnit = (int)AltitudeUnits.unitMeters;
                                }

                                if (sGpsHeading != "NA")
                                {
                                    sGpsHeading = sGpsHeading.Replace('.', Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                                    gpsSource.GpsPos.m_fHeading = System.Convert.ToSingle(sGpsHeading);
                                }

                                if (sGpsDate != "NA")
                                {
                                    gpsSource.GpsPos.m_iDay   = Convert.ToInt32(sGpsDate.Substring(0, 2));
                                    gpsSource.GpsPos.m_iMonth = Convert.ToInt32(sGpsDate.Substring(2, 2));
                                    gpsSource.GpsPos.m_iYear  = Convert.ToInt32(sGpsDate.Substring(4, 2));
                                }

                                //if we have a valid time in datePosition then use it instead of the one
                                //in the nmea message. This is mainly used with GpsBabel USB nmea files
                                if (gpsSource.datePosition.Year != 1771 && gpsSource.datePosition.Month != 6 &&
                                    gpsSource.datePosition.Month != 9)
                                {
                                    gpsSource.GpsPos.m_iDay   = gpsSource.datePosition.Day;
                                    gpsSource.GpsPos.m_iMonth = gpsSource.datePosition.Month;
                                    gpsSource.GpsPos.m_iYear  = gpsSource.datePosition.Year;
                                    gpsSource.GpsPos.m_iHour  = gpsSource.datePosition.Hour;
                                    gpsSource.GpsPos.m_iMin   = gpsSource.datePosition.Minute;
                                    gpsSource.GpsPos.m_iSec   = gpsSource.datePosition.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;
                                break;
                                #endregion
                            }
                            #endregion
                        }
                    }
                }
            }
//			#if !DEBUG
            catch (Exception)
            {
                fLocked = false;
            }
//			#endif


            return(fLocked);
        }
Exemplo n.º 3
0
        //
        //return the UTC time from the NMEA message
        public DateTime GetNMEATime(int iGPSIndex, string sNMEA)
        {
            string []         sMsgField;
            string            sGPSTime;
            string            sHour;
            string            sMinute;
            string            sSecond;
            SupportedMessages supportedMsg = SupportedMessages.msgNotSupported;
            GPSSource         gpsSource    = null;

            if (iGPSIndex >= 0)
            {
                gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[iGPSIndex];
            }

            DateTime dtGPSTime = new DateTime(1771, 6, 9);           //This year signal that we did not get the time

                        #if !DEBUG
            try
                        #endif
            {
                #region Check for supported message
                //look for the GPRMC message
                if (sNMEA.StartsWith("$GPRMC") == true)
                {
                    supportedMsg = SupportedMessages.msgGPRMC;
                }
                else
                if (sNMEA.StartsWith("$GPGGA") == true)
                {
                    supportedMsg = SupportedMessages.msgGPGGA;
                }
                else
                if (sNMEA.StartsWith("$PIXSE,TIME__") == true)
                {
                    //supportedMsg=SupportedMessages.msgTIME__;
                    supportedMsg = SupportedMessages.msgNotSupported;
                }
                #endregion

                if (iGPSIndex >= 0)
                {
                    gpsSource.GpsPos.m_iHour = -1;
                    gpsSource.GpsPos.m_iMin  = -1;
                    gpsSource.GpsPos.m_iSec  = -1;
                }

                switch (supportedMsg)
                {
                    #region GPRMC, GPGGA
                case SupportedMessages.msgGPRMC:
                case SupportedMessages.msgGPGGA:
                    sMsgField = m_CSVParser.GetCSVLine(sNMEA);
                    sGPSTime  = sMsgField[1];
                    sHour     = sGPSTime.Substring(0, 2);
                    sMinute   = sGPSTime.Substring(2, 2);
                    sSecond   = sGPSTime.Substring(4, 2);
                    dtGPSTime = new DateTime(2001, 1, 1, Convert.ToInt32(sHour), Convert.ToInt32(sMinute), Convert.ToInt32(sSecond));
                    if (iGPSIndex >= 0)
                    {
                        gpsSource.GpsPos.m_iHour = Convert.ToInt32(sHour);
                        gpsSource.GpsPos.m_iMin  = Convert.ToInt32(sMinute);
                        gpsSource.GpsPos.m_iSec  = Convert.ToSingle(sSecond);
                    }
                    break;
                    #endregion
                }
            }
                        #if !DEBUG
            catch (Exception)
            {
                dtGPSTime = new DateTime(1771, 6, 9);               //This year signal that we did not get the time
            }
                        #endif
            return(dtGPSTime);
        }
Exemplo n.º 4
0
        public void UdpReceiveData(IAsyncResult iar)
        {
            try
            {
                String sGPS;

                // Create temporary remote end Point
                IPEndPoint sender       = new IPEndPoint(IPAddress.Any, 0);
                EndPoint   tempRemoteEP = (EndPoint)sender;

                // Get the Socket
                TCPSockets tcpSockets = (TCPSockets)iar.AsyncState;
                Socket     remote     = tcpSockets.socketUDP;

                // Call EndReceiveFrom to get the received Data
                int nBytesRec = remote.EndReceiveFrom(iar, ref tempRemoteEP);

                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);
                }

                GPSSource gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[tcpSockets.iDeviceIndex];
                EndPoint  endPoint  = new IPEndPoint(IPAddress.Any, Convert.ToInt32(gpsSource.iUDPPort));
                remote.BeginReceiveFrom(tcpSockets.byTcpBuffer, 0, 1024, SocketFlags.None, ref endPoint, new AsyncCallback(UdpReceiveData), tcpSockets);
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 5
0
        public void threadFile()
        {
            int       iIndex    = Int32.Parse(Thread.CurrentThread.Name);
            GPSSource gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex];

            gpsSource.bPlay = true;
            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;
            int      iLineNumber   = 0;

            try
            {
                if (File.Exists(gpsSource.sFileNameSession))
                {
                    while (m_GpsTracker.m_fCloseThreads == false)
                    {
                        bRestartTrack = true;
                        iLineNumber   = 0;
                        string[] sLines = File.ReadAllLines(gpsSource.sFileNameSession);
                        if (sLines.Length > 0)
                        {
                            if (m_GpsTracker.m_fPlayback == true)
                            {
                                fsGpsFile = File.OpenText(gpsSource.sFileNameSession);
                                m_GpsTracker.LoadSettings(fsGpsFile, false);
                                fsGpsFile.Close();
                                fsGpsFile = null;

                                do
                                {
                                    sIndex = sLines[iLineNumber++];
                                    if (sIndex == "--------------------------------------------------" || iLineNumber == sLines.Length)
                                    {
                                        break;
                                    }
                                } while (m_GpsTracker.m_fCloseThreads == false);

                                iIndexFile = iIndex + 1;
                                do
                                {
                                    sIndex = sLines[iLineNumber++];
                                    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   = sLines[iLineNumber++];
                            }

                            //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);
                                }
                            }

                            int iLineCount = sLines.Length;
                            while (sIndex != null && sGpsLine != null && m_GpsTracker.m_fCloseThreads == false && iIndexFile == iIndex && iLineNumber < iLineCount)
                            {
                                if (gpsSource.bPlay)
                                {
                                    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, iLineCount, iLineNumber) == true)
                                        {
                                            if (bRestartTrack)
                                            {
                                                bRestartTrack = false;
                                            }

                                            if (gpsSource.bUpdateLineNumber)
                                            {
                                                iLineNumber = gpsSource.iLineNumber;
                                                gpsSource.bUpdateLineNumber = false;
                                                bRestartTrack = true;
                                            }

                                            //Get current NMEA msg time
                                            dtCurrent = dtNext;
                                            while (m_GpsTracker.m_fCloseThreads == false)
                                            {
                                                //Get next line from file
                                                if (m_GpsTracker.m_fPlayback == true)
                                                {
                                                    if (iLineNumber < iLineCount)
                                                    {
                                                        sIndex = sLines[iLineNumber++];
                                                    }
                                                    else
                                                    {
                                                        break;
                                                    }
                                                    //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);
                                                    if (iLineNumber < iLineCount)
                                                    {
                                                        sGpsLine = sLines[iLineNumber++];
                                                    }
                                                    else
                                                    {
                                                        break;
                                                    }
                                                }

                                                //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;
                                            if (iLineNumber < iLineCount)
                                            {
                                                sGpsLine = sLines[iLineNumber++];
                                            }
                                            else
                                            {
                                                break;
                                            }

                                            //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 == "")
                                        {
                                            if (iLineNumber < iLineCount)
                                            {
                                                sGpsLine = sLines[iLineNumber++];
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }

                                        Thread.Sleep(1);
                                        //break;
                                    }
                                }
                                else
                                {
                                    Thread.Sleep(500);
                                }
                            } //while

                            if (gpsSource.iReload == 0 && m_GpsTracker.m_fCloseThreads == false)
                            {
                                Thread.Sleep(3000);
                            }
                            else
                            {
                                break;
                            }
                        } //if (sLines.Length > 0)
                        else
                        {
                            break;
                        }
                    }
                }                 //exists
            }
            catch (Exception)
            {
            }

            if (fsGpsFile != null)
            {
                fsGpsFile.Close();
                fsGpsFile = null;
            }

            if (m_GpsTracker.m_fPlayback == true)
            {
                gpsSource.eventThreadSync.Set();
            }
        }
Exemplo n.º 6
0
        //
        // File functions
        //

        public void PreprocessFile(int iIndex, String sFileName, string sRealName, bool bCheckPre, bool bForePreprocessing)
        {
            GPSSource gpsSource = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex];

                        #if !DEBUG
            try
                        #endif
            {
                //check if preprocess exists
                if ((File.Exists(sFileName + ".TrackAtOnce") || IsTrackAtOnce(sFileName)) &&
                    (bForePreprocessing == false || bCheckPre))
                {
                    string sTAOFileName;
                    if (IsTrackAtOnce(sFileName))
                    {
                        sTAOFileName = sFileName;
                    }
                    else
                    {
                        sTAOFileName = sFileName + ".TrackAtOnce";
                    }

                    gpsSource.GpsPos.m_gpsTrack = null;
                    BinaryReader binReader = new BinaryReader(File.Open(sTAOFileName, FileMode.Open));
                    // If the file is not empty,
                    // read it
                    if (binReader.PeekChar() != -1)
                    {
                        gpsSource.GpsPos.m_gpsTrack = new GPSTrack();

                        string  sSign        = "GPSTracker.TrackAtOnce";
                        char [] sSignature   = binReader.ReadChars(sSign.Length);
                        string  sSignCompare = new string(sSignature);
                        if (sSignCompare == sSign)
                        {
                            gpsSource.GpsPos.m_gpsTrack.m_uPointCount = binReader.ReadUInt32();
                            gpsSource.GpsPos.m_gpsTrack.SetSize(gpsSource.GpsPos.m_gpsTrack.m_uPointCount);

                            byte[] byData = new byte[gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8];
                            byData = binReader.ReadBytes((int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);
                            System.Buffer.BlockCopy(byData, 0, gpsSource.GpsPos.m_gpsTrack.m_fLat, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);

                            byData = binReader.ReadBytes((int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);
                            System.Buffer.BlockCopy(byData, 0, gpsSource.GpsPos.m_gpsTrack.m_fLon, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);

                            byData = binReader.ReadBytes((int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 4);
                            System.Buffer.BlockCopy(byData, 0, gpsSource.GpsPos.m_gpsTrack.m_fAlt, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 4);
                        }
                    }
                    binReader.Close();
                }
                else
                {
                    uint         uProgress;
                    uint         uMaxProgress;
                    StreamReader fsGpsFile = null;
                    String       sGpsLine;
                    byte []      byNMEA;
                    char []      cNMEA    = null;
                    char []      cNMEAMsg = new char[512];
                    System.Text.ASCIIEncoding asciiEncoder = new System.Text.ASCIIEncoding();
                    if (File.Exists(sFileName) && !IsTrackAtOnce(sFileName))
                    {
                        FileInfo fInfo = new FileInfo(sFileName);

                        uProgress    = 0;
                        uMaxProgress = (uint)fInfo.Length;
                        m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Processing " + sRealName + ": " + Convert.ToString(uProgress) + " of " + Convert.ToString(uMaxProgress) + " bytes.");

                        fsGpsFile = File.OpenText(sFileName);
                        gpsSource.GpsPos.m_gpsTrack = new GPSTrack();
                        gpsSource.GpsPos.m_gpsTrack.SetSize(500); //set an initial array size for the track
                        uint uResizeArraySize = 500;
                        while (true)
                        {
                            sGpsLine = fsGpsFile.ReadLine();
                            if (sGpsLine == null)
                            {
                                break;
                            }
                            uProgress += (uint)sGpsLine.Length;
                            m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Processing " + sRealName + ": " + Convert.ToString(uProgress) + " of " + Convert.ToString(uMaxProgress) + " bytes.");

                            byNMEA = asciiEncoder.GetBytes(sGpsLine);
                            cNMEA  = asciiEncoder.GetChars(byNMEA);
                            cNMEA.CopyTo(cNMEAMsg, 0);
                            if (m_GpsTracker.m_NMEA.ParseGPSMessage(cNMEAMsg, cNMEA.Length, false, iIndex) == true)
                            {
                                gpsSource.GpsPos.m_gpsTrack.AddPoint(gpsSource.GpsPos.m_fLat, gpsSource.GpsPos.m_fLon, gpsSource.GpsPos.m_fAlt);
                            }

                            //Keep resizing the track array as necessary
                            if (gpsSource.GpsPos.m_gpsTrack.m_uPointCount >= uResizeArraySize)
                            {
                                uResizeArraySize += 100;
                                gpsSource.GpsPos.m_gpsTrack.Resize(uResizeArraySize);
                            }
                        }
                        uProgress = (uint)fInfo.Length;
                        m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Processing " + sRealName + ": " + Convert.ToString(uProgress) + " of " + Convert.ToString(uMaxProgress) + " bytes.");
                        fsGpsFile.Close();

                        //Resize track array to minimum size;
                        gpsSource.GpsPos.m_gpsTrack.Resize(gpsSource.GpsPos.m_gpsTrack.m_uPointCount);

                        m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Creating " + sRealName + ".TrackAtOnce");

                        using (BinaryWriter binWriter = new BinaryWriter(File.Open(sFileName + ".TrackAtOnce", FileMode.Create)))
                        {
                            string  sSign      = "GPSTracker.TrackAtOnce";
                            char [] sSignature = new char[sSign.Length];
                            sSignature = sSign.ToCharArray();
                            binWriter.Write(sSignature);

                            binWriter.Write(gpsSource.GpsPos.m_gpsTrack.m_uPointCount);

                            byte[] byData = new byte[gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8];
                            System.Buffer.BlockCopy(gpsSource.GpsPos.m_gpsTrack.m_fLat, 0, byData, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);
                            binWriter.Write(byData, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);

                            System.Buffer.BlockCopy(gpsSource.GpsPos.m_gpsTrack.m_fLon, 0, byData, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);
                            binWriter.Write(byData, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 8);

                            System.Buffer.BlockCopy(gpsSource.GpsPos.m_gpsTrack.m_fAlt, 0, byData, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 4);
                            binWriter.Write(byData, 0, (int)gpsSource.GpsPos.m_gpsTrack.m_uPointCount * 4);

                            binWriter.Close();
                        }
                    }
                }
            }
                        #if !DEBUG
            catch (Exception)
            {
                m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("");
                m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Active");
            }
                        #endif
            m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("");
            m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Active");
        }
Exemplo n.º 7
0
        public void threadStartFile()
        {
            int       iIndex     = Int32.Parse(Thread.CurrentThread.Name);
            GPSSource gpsSource  = (GPSSource)m_GpsTracker.m_gpsSourceList[iIndex];
            bool      bShowError = true;

            while (m_GpsTracker.m_fCloseThreads == false)
            {
                gpsSource.GpsPos.m_gpsTrack = null;

                try
                {
                    string sFileName = "";
                    lock ("threadStartFile")
                    {
                        m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Downloading " + gpsSource.sFileName);

                        WebClient myWebClient = new WebClient();
                        sFileName = GpsTrackerPlugin.m_sPluginDirectory + "\\DownloadedFile" + Convert.ToString(iIndex) + ".gpsTracker";
                        if (File.Exists(sFileName))
                        {
                            File.Delete(sFileName);
                        }
                        string sWebReload = "";
                        if (!File.Exists(gpsSource.sFileName))
                        {
                            sWebReload = "?r=" + System.Guid.NewGuid().ToString();
                        }

                        myWebClient.DownloadFile(gpsSource.sFileName + sWebReload, sFileName);
                    }

                    if (File.Exists(sFileName))
                    {
                        gpsSource.bBabelNMEA = false;
                        lock ("threadStartFile")
                        {
                            //Convert files to a known format
                            switch (gpsSource.saGpsBabelFormat[0])
                            {
                            case "nasabaloon":
                                if (m_GpsTracker.m_NMEA.CheckConvertNasaBaloon(sFileName, gpsSource.sFileName))
                                {
                                    sFileName = sFileName + ".NMEAText";
                                }
                                break;

                            case "nmea":
                            case "GpsTracker":
                            case "Unknown":
                                //Nothing to convert
                                break;

                            default:
                                //Use GpsBabel
                                string sBabelCommandLine;
                                if (gpsSource.bWaypoints)
                                {
                                    sBabelCommandLine = "-w;";
                                }
                                else
                                {
                                    sBabelCommandLine = "-t;-r;";
                                }
                                sBabelCommandLine += "-i;" + gpsSource.saGpsBabelFormat[0] + ";";
                                sBabelCommandLine += "-f;" + sFileName + ";";
                                if (gpsSource.bWaypoints)
                                {
                                    sBabelCommandLine += "-o;csv;-F;" + sFileName + ".WaypointsCSV;";
                                    if (File.Exists(sFileName + ".WaypointsCSV"))
                                    {
                                        File.Delete(sFileName + ".WaypointsCSV");
                                    }
                                }
                                else
                                {
                                    sBabelCommandLine += "-o;nmea;-F;" + sFileName + ".NMEAText;";
                                    if (File.Exists(sFileName + ".NMEAText"))
                                    {
                                        File.Delete(sFileName + ".NMEAText");
                                    }
                                }
                                m_GpsBabel.Execute(sBabelCommandLine);

                                if (gpsSource.bWaypoints && File.Exists(sFileName + ".WaypointsCSV"))
                                {
                                    FileInfo fInfo = new FileInfo(sFileName + ".WaypointsCSV");
                                    if (fInfo.Length > 0)
                                    {
                                        sFileName += ".WaypointsCSV";
                                    }
                                }
                                else
                                if (gpsSource.bWaypoints == false && File.Exists(sFileName + ".NMEAText"))
                                {
                                    FileInfo fInfo = new FileInfo(sFileName + ".NMEAText");
                                    if (fInfo.Length > 0)
                                    {
                                        sFileName           += ".NMEAText";
                                        gpsSource.bBabelNMEA = true;
                                    }
                                }
                                break;
                            }
                        }

                        if (gpsSource.bTrackAtOnce != true && !m_GpsTracker.m_File.IsTrackAtOnce(sFileName))
                        {
                            if (!gpsSource.bWaypoints)
                            {
                                m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("");
                                m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Active");

                                gpsSource.sFileNameSession = sFileName;
                                gpsSource.iFilePlaySpeed   = gpsSource.iPlaySpeed;
                                threadFile();
                            }
                            else
                            {
                                m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("");
                                m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Active");

                                ArrayList    listWaypoints = new ArrayList();
                                StreamReader sReader       = null;
                                try
                                {
                                    sReader = File.OpenText(sFileName);
                                    string sLine;
                                    do
                                    {
                                        sLine = sReader.ReadLine();
                                        if (sLine != null)
                                        {
                                            listWaypoints.Add(sLine);
                                        }
                                    } while(sLine != null);
                                }
                                catch (Exception)
                                {
                                }
                                if (sReader != null)
                                {
                                    sReader.Close();
                                }

                                if (listWaypoints.Count > 0)
                                {
                                    char [] cSep   = { ',' };
                                    bool    bTrack = gpsSource.bTrack;
                                    for (int i = 0; i < listWaypoints.Count; i++)
                                    {
                                        string    sFormat = (string)listWaypoints[i];
                                        string [] sWaypointData;
                                        sWaypointData = sFormat.Split(cSep);
                                        if (sWaypointData.Length == 3)
                                        {
                                            try
                                            {
                                                m_GpsTracker.AddPOI(sWaypointData[2], Convert.ToDouble(sWaypointData[0]), Convert.ToDouble(sWaypointData[1]), false, gpsSource);
                                                if (gpsSource.bTrack)
                                                {
                                                    gpsSource.bTrack = false;
                                                }
                                            }
                                            catch (Exception)
                                            {
                                                Thread.Sleep(200);
                                            }
                                        }
                                    }
                                    gpsSource.bTrack = bTrack;
                                }
                            }
                        }
                        else
                        {
                            if (gpsSource.iReload > 0 && File.Exists(sFileName + ".TrackAtOnce"))
                            {
                                File.Delete(sFileName + ".TrackAtOnce");
                            }

                            lock ("threadStartFile")
                            {
                                //track at once
                                m_GpsTracker.m_File.PreprocessFile(iIndex, sFileName, gpsSource.sFileName, false, gpsSource.bForcePreprocessing);
                            }

                            GPSRenderInformation renderInfo = new GPSRenderInformation();
                            renderInfo.bPOI                   = false;
                            renderInfo.iIndex                 = iIndex;
                            renderInfo.sDescription           = gpsSource.sDescription;
                            renderInfo.fFix                   = false;
                            renderInfo.bShowInfo              = m_GpsTracker.m_bInfoText;
                            renderInfo.bTrackLine             = false;
                            renderInfo.gpsTrack               = gpsSource.GpsPos.m_gpsTrack;
                            renderInfo.bRestartTrack          = false;
                            renderInfo.iDay                   = gpsSource.GpsPos.m_iDay;
                            renderInfo.iMonth                 = gpsSource.GpsPos.m_iMonth;
                            renderInfo.iYear                  = gpsSource.GpsPos.m_iYear;
                            renderInfo.colorTrack             = gpsSource.colorTrack;
                            renderInfo.sIcon                  = gpsSource.sIconPath;
                            renderInfo.fTrackOnTop            = gpsSource.bTrackOnTop;
                            renderInfo.bShowPosition          = gpsSource.bShowPosition;
                            renderInfo.bShowName              = gpsSource.bShowName;
                            renderInfo.bShowComment           = gpsSource.bShowComment;
                            renderInfo.bShowSpeed             = gpsSource.bShowSpeed;
                            renderInfo.bShowHeading           = gpsSource.bShowHeading;
                            renderInfo.bShowTimeDate          = gpsSource.bShowTimeDate;
                            renderInfo.bShowTrackDistance     = gpsSource.bShowTrackDistance;
                            renderInfo.bShowReferenceAngles   = gpsSource.bShowReferenceAngles;
                            renderInfo.bShowReferenceDistance = gpsSource.bShowReferenceDistance;
                            renderInfo.iInformationFontSize   = gpsSource.iInformationFontSize;
                            renderInfo.colorInformation       = gpsSource.colorInformation;
                            renderInfo.iDistanceUnit          = gpsSource.iDistanceUnit;
                            renderInfo.iPositionUnit          = gpsSource.iPositionUnit;
//                            while (m_GpsTracker.m_fCloseThreads == false)
//                            {
                            m_GpsTracker.m_gpsTrackerPlugin.pluginShowOverlay(renderInfo);
                            if (gpsSource.bTrack == true)
                            {
                                m_GpsTracker.m_gpsTrackerPlugin.pluginWorldWindowGotoLatLonHeading(gpsSource.GpsPos.m_gpsTrack.m_fLat[0], gpsSource.GpsPos.m_gpsTrack.m_fLon[0], -1F, gpsSource.iStartAltitud);
                            }
                            if (gpsSource.iStartAltitud > 0)
                            {
                                gpsSource.iStartAltitud = 0;
                            }

//                                Thread.Sleep(3000);
//                            }
                        }
                    }

                    if (gpsSource.iReload == 0 || m_GpsTracker.m_fCloseThreads == true)
                    {
                        break;
                    }
                    else
                    {
                        for (int i = 0; i < gpsSource.iReload && m_GpsTracker.m_fCloseThreads == false; i++)
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
                catch (Exception)
                {
                    if (bShowError)
                    {
                        lock ("threadStartFile")
                        {
                            bShowError = false;
                            m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Unable to Download " + gpsSource.sFileName + ". Retrying in the background.");
                            for (int i = 0; i < 5 && m_GpsTracker.m_fCloseThreads == false; i++)
                            {
                                Thread.Sleep(1000);
                            }
                            m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("");
                            m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Active");
                        }
                        for (int i = 0; i < 60 && m_GpsTracker.m_fCloseThreads == false; i++)
                        {
                            Thread.Sleep(1000);
                        }
                    }
                    else
                    {
                        m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("");
                        m_GpsTracker.m_gpsTrackerPlugin.pluginShowFixInfo("GPSTracker: Active");
                        for (int i = 0; i < 60 && m_GpsTracker.m_fCloseThreads == false; i++)
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
            }

            gpsSource.eventThreadSync.Set();
        }