// //This is now the Track button handler private void StartStop_Click(object sender, System.EventArgs e) { bool fRet; GPSSource gpsSource; //if not initialized then Initialize if (m_fInitialized==false) { bool bContinue=true; for (int i=0; i<m_gpsSourceList.Count; i++) { gpsSource = (GPSSource)m_gpsSourceList[i]; if (gpsSource.bSetup==false) { bContinue=false; break; } } if (bContinue==false) { if (MessageBox.Show("One or more sources have not been configured.\nContinue anyway?", "GPSTracker", MessageBoxButtons.YesNo,MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) bContinue=true; } if (bContinue) { labelTrackCode.Text=""; fRet=true; m_gpsTrackerPlugin.gpsOverlay.RemoveAll(); m_gpsTrackerPlugin.gpsOverlay.IsOn = true; //If we have COM devices then init the COM ports fRet=true; if (bContinue) { m_fInitialized=true; m_fPlayback=false; progressBarSetup.Maximum=m_gpsSourceList.Count+1; SaveSettings(null); m_iPlaybackSpeed=1; int i; for (i=0; i<m_gpsSourceList.Count; i++) { gpsSource = (GPSSource)m_gpsSourceList[i]; if (gpsSource.bSetup) { if (gpsSource.sType=="File" && gpsSource.bSession==true) { if(File.Exists(gpsSource.sFileName)) { m_iPlaybackSpeed = gpsSource.iPlaySpeed; m_sPlaybackFile = gpsSource.sFileName; m_srReader = File.OpenText(gpsSource.sFileName); if (LoadSettings(m_srReader,false)) { m_srReader.Close(); m_srReader = File.OpenText(m_sPlaybackFile); LoadSettings(m_srReader,true); m_fPlayback=true; m_bRecordSession=false; break; } m_srReader.Close(); m_srReader=null; } } } } if (m_bRecordSession==false && m_fPlayback==false) LoadSettings(null,true); if (m_bRecordSession && m_fPlayback==false) { m_swRecorder = File.CreateText(GpsTrackerPlugin.m_sPluginDirectory + "\\GpsTracker.recording"); SaveSettings(m_swRecorder); } for (i=0; i<m_gpsSourceList.Count && m_fPlayback==false; i++) { gpsSource = (GPSSource)m_gpsSourceList[i]; if (gpsSource.sType=="COM" && gpsSource.bSetup) { // Instantiate base class event handlers. gpsSource.GpsCOM = new SerialPort(this, i, (uint)gpsSource.iCOMPort, (uint)gpsSource.iBaudRate, Convert.ToByte(gpsSource.iByteSize), Convert.ToByte(gpsSource.iParity), Convert.ToByte(gpsSource.iStopBits), (uint)gpsSource.iFlowControl); fRet = gpsSource.GpsCOM.Open(); if (!fRet) { if (MessageBox.Show("Unable to initialize COM" + Convert.ToString(gpsSource.iCOMPort) + ":\nPlease check your port settings.\n\nContinue anyway?", "GPSTracker", MessageBoxButtons.YesNo,MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) bContinue=true; else { CloseCOMs(); bContinue=false; break; } } } } if (bContinue) { Start(); //Start threads if (checkBoxMessagesMonitor.Checked) { m_MessageMonitor = new MessageMonitor(); m_MessageMonitor.Show(); } else m_MessageMonitor = null; //Hide the window //this.Visible=false; //Hide the window by moving it out of the screen //By doing this instead of hideing the window then OnVisibleChanged //does not get call with this.visible=false (this would deinitialize everything, //see comment in OnVisibleChanged). this.Left = -1000; //m_gpsTrackerPlugin.pluginAddOverlay(); m_gpsTrackerPlugin.pluginWorldWindowFocus(); progressBarSetup.Visible = false; labelSettingup.Visible = false; } else { m_fInitialized = false; Stop(); } } } } else { labelTrackCode.ForeColor = System.Drawing.Color.Black; labelTrackCode.Text="Tracking enabled..."; } }
//Close down threads, ports, cleanup public bool Stop() { bool fRet=true; //Remove temporary waypoints int i=0; do { for (i = 0; i < m_gpsSourceList.Count; i++) { GPSSource gpsSource = (GPSSource)m_gpsSourceList[i]; if (gpsSource.bSave==false) { m_gpsSourceList.Remove(gpsSource); break; } } } while(i<m_gpsSourceList.Count); try { if (m_MessageMonitor!=null) { m_MessageMonitor.Close(); m_MessageMonitor=null; } } catch (Exception) { m_MessageMonitor=null; } //Restore vertical exaggeration setting. if (checkBoxVExaggeration.Checked==true) World.Settings.VerticalExaggeration=m_fVerticalExaggeration; m_fCloseThreads=true; Thread.Sleep(500); if (m_timerLocked!=null) { m_timerLocked.Dispose(); m_timerLocked=null; } CloseCOMs(); if (m_hPOIThread!=null) { m_eventPOIThreadSync.WaitOne(5000, false); m_hPOIThread.Interrupt(); m_hPOIThread.Abort(); m_hPOIThread.Join(1000); m_hPOIThread=null; } for (i=0; i<m_gpsSourceList.Count; i++) { GPSSource gpsSource = (GPSSource)m_gpsSourceList[i]; if (gpsSource.tcpSockets.socket != null) { gpsSource.tcpSockets.socket.Close(); gpsSource.tcpSockets.socket = null; gpsSource.tcpSockets.sStream = ""; } if (gpsSource.tcpSockets.socketUDP != null) { gpsSource.tcpSockets.socketUDP.Close(); gpsSource.tcpSockets.socketUDP = null; gpsSource.tcpSockets.sStream = ""; } if (gpsSource.fileThread != null) { gpsSource.eventThreadSync.WaitOne(5000,false); gpsSource.fileThread.Interrupt(); gpsSource.fileThread.Abort(); gpsSource.fileThread.Join(1000); gpsSource.fileThread = null; } if (gpsSource.aprsThread != null) { gpsSource.eventThreadSync.WaitOne(5000, false); gpsSource.aprsThread.Interrupt(); gpsSource.aprsThread.Abort(); gpsSource.aprsThread.Join(1000); gpsSource.aprsThread = null; } if (gpsSource.usbThread != null) { gpsSource.eventThreadSync.WaitOne(5000, false); gpsSource.usbThread.Interrupt(); gpsSource.usbThread.Abort(); gpsSource.usbThread.Join(1000); gpsSource.usbThread = null; } if (gpsSource.swExport != null) { gpsSource.swExport.Close(); gpsSource.swExport = null; } } if (m_swRecorder!=null) { m_swRecorder.Close(); m_swRecorder=null; #if !DEBUG try #endif { SaveFileDialog dlgSaveFile = new SaveFileDialog(); dlgSaveFile.Title = "Select file to save recorded Session" ; dlgSaveFile.Filter = "GPSTrackerSession (*.GPSTrackerSession)|*.GPSTrackerSession" ; dlgSaveFile.FilterIndex = 0 ; dlgSaveFile.RestoreDirectory = true ; if(dlgSaveFile.ShowDialog() == DialogResult.OK) File.Copy(GpsTrackerPlugin.m_sPluginDirectory + "\\GpsTracker.recording", dlgSaveFile.FileName, true); File.Delete(GpsTrackerPlugin.m_sPluginDirectory + "\\GpsTracker.recording"); } #if !DEBUG catch(Exception) { } #endif } if (m_srReader!=null) { m_srReader.Close(); m_srReader=null; } m_gpsTrackerPlugin.pluginRemoveAllOverlay(); m_gpsTrackerPlugin.gpsOverlay.IsOn = false; SaveSettings(null); m_fInitialized = false; return fRet; }
//Show the GPS icon in he World View public bool ShowGPSIcon(char [] cNMEAMessage, int iMsgLength, bool fCheck, int iIndex, bool bRestartTrack, bool bParse) { bool bRet=false; lock(LockShowIcon) { GPSSource gpsSource = (GPSSource)m_gpsSourceList[iIndex]; #if !DEBUG try #endif { if (bParse) bRet=m_NMEA.ParseGPSMessage(cNMEAMessage,iMsgLength,fCheck,iIndex); else bRet=true; if (bRet==true && m_fInitialized==true) { if(m_swRecorder!=null) { String sMsg = new String(cNMEAMessage); char [] cAny = new char[3]; cAny[0]='\r'; cAny[1]='\n'; cAny[2]='\0'; int iEnd=sMsg.IndexOfAny(cAny); if (iEnd>0) sMsg=sMsg.Substring(0,iEnd); sMsg = Convert.ToString(iIndex) + "," + sMsg; m_swRecorder.WriteLine(sMsg); } string sNMEAMsg = ""; if (gpsSource.bNMEAExport) sNMEAMsg = m_NMEA.ConvertToGPGGA(gpsSource.GpsPos); //Export to a raw NMEA file is selected if (gpsSource.bNMEAExport && sNMEAMsg != "") { if (gpsSource.swExport == null) { try { string sPath = textBoxNMEAExportPath.Text; if (sPath == "") sPath = GpsTrackerPlugin.m_sPluginDirectory + "\\NMEAExport\\" + gpsSource.sDescription + ".txt"; else { if (sPath.EndsWith("\\")) sPath = sPath + gpsSource.sDescription + ".txt"; else sPath = sPath + "\\" + gpsSource.sDescription + ".txt"; } gpsSource.swExport = File.CreateText(sPath); } catch (Exception) { gpsSource.swExport = null; } } try { if (gpsSource.swExport != null) { gpsSource.swExport.WriteLine(sNMEAMsg); gpsSource.swExport.Flush(); } } catch (Exception) { } } m_gpsTrackerPlugin.pluginLocked(true); //Show the user selected icon for the gps device on the WorldView string sPortInfo=gpsSource.sType; switch (sPortInfo) { case "UDP": sPortInfo += ": Port " + Convert.ToString(gpsSource.iUDPPort); break; case "COM": sPortInfo += ": Port " + Convert.ToString(gpsSource.iCOMPort); break; case "USB": sPortInfo += ": USB Device " + gpsSource.sUSBDevice; break; case "TCP": sPortInfo += ": IP:Port " + gpsSource.sTCPAddress + ":" + Convert.ToString(gpsSource.iTCPPort); break; case "File": //Do not display port info for an IXSEA file. string sFileName=gpsSource.sFileName; int iIndexName = sFileName.LastIndexOf('\\'); if (iIndexName>=0 && iIndexName<sFileName.Length-1) sFileName=sFileName.Substring(iIndexName+1); sPortInfo += ": " + sFileName; break; } if (m_fInitialized==true) { GPSRenderInformation renderInfo = new GPSRenderInformation(); renderInfo.fTrack=gpsSource.bTrack; renderInfo.bPOI=false; renderInfo.iIndex=iIndex; if (gpsSource.GpsPos.m_sName != "") renderInfo.sDescription = gpsSource.GpsPos.m_sName; else renderInfo.sDescription=gpsSource.sDescription; renderInfo.sComment = gpsSource.GpsPos.m_sComment; renderInfo.iAPRSIconTable = gpsSource.GpsPos.m_iAPRSIconTable; renderInfo.iAPRSIconCode = gpsSource.GpsPos.m_iAPRSIconCode; renderInfo.fFix=true; renderInfo.fLat = gpsSource.GpsPos.m_fLat; renderInfo.fLon = gpsSource.GpsPos.m_fLon; renderInfo.fAlt = gpsSource.GpsPos.m_fAlt; renderInfo.fRoll = gpsSource.GpsPos.m_fRoll; renderInfo.fPitch = gpsSource.GpsPos.m_fPitch; renderInfo.fDepth = gpsSource.GpsPos.m_fDepth; renderInfo.sAltUnit = gpsSource.GpsPos.m_sAltUnit; renderInfo.sSpeedUnit = gpsSource.GpsPos.m_sSpeedUnit; renderInfo.fSpeed = gpsSource.GpsPos.m_fSpeed; renderInfo.fESpeed = gpsSource.GpsPos.m_fESpeed; renderInfo.fNSpeed = gpsSource.GpsPos.m_fNSpeed; renderInfo.fVSpeed = gpsSource.GpsPos.m_fVSpeed; renderInfo.fHeading = gpsSource.GpsPos.m_fHeading; if (gpsSource.GpsPos.m_iAPRSIconCode >= 0 && gpsSource.GpsPos.m_iAPRSIconTable >= 0) { string sPath=""; if (gpsSource.GpsPos.m_iAPRSIconTable == Convert.ToInt32('/')) sPath = GpsTrackerPlugin.m_sPluginDirectory + "\\aprs\\primary"; if (gpsSource.GpsPos.m_iAPRSIconTable == Convert.ToInt32('\\')) sPath = GpsTrackerPlugin.m_sPluginDirectory + "\\aprs\\secondary"; if (sPath!="") { string sFileName = String.Format("{0:000}", gpsSource.GpsPos.m_iAPRSIconCode) + ".png"; if (File.Exists(sPath + "\\" + sFileName)) sPath=sPath + "\\" + sFileName; else sPath=gpsSource.sIconPath; } else sPath=gpsSource.sIconPath; renderInfo.sIcon=sPath; } else renderInfo.sIcon=gpsSource.sIconPath; renderInfo.iStartAltitud=gpsSource.iStartAltitud; renderInfo.sPortInfo=sPortInfo; renderInfo.iHour = gpsSource.GpsPos.m_iHour; renderInfo.iMin = gpsSource.GpsPos.m_iMin; renderInfo.fSec = gpsSource.GpsPos.m_iSec; renderInfo.bShowInfo=m_bInfoText; renderInfo.bTrackLine=m_bTrackLine; renderInfo.gpsTrack = gpsSource.GpsPos.m_gpsTrack; renderInfo.bRestartTrack=bRestartTrack; renderInfo.iDay = gpsSource.GpsPos.m_iDay; renderInfo.iMonth = gpsSource.GpsPos.m_iMonth; renderInfo.iYear = gpsSource.GpsPos.m_iYear; renderInfo.colorTrack=gpsSource.colorTrack; m_gpsTrackerPlugin.pluginShowOverlay(renderInfo); //GeoFence Check lock ("GeoFenceAccess") { for (int iFence = 0; iFence < m_gpsSourceList.Count; iFence++) { GPSSource gpsSourceFence = (GPSSource)m_gpsSourceList[iFence]; if (gpsSourceFence.sType == "GeoFence" && (gpsSourceFence.GeoFence.sSource == gpsSource.sDescription || gpsSourceFence.GeoFence.sSource == "All Gps Sources")) { bool bSourceIn = false; int i = 0; int j = 0; for (i = 0; i < gpsSourceFence.GeoFence.arrayLat.Count; i++) { j++; if (j == gpsSourceFence.GeoFence.arrayLat.Count) j = 0; if ((float)gpsSourceFence.GeoFence.arrayLat[i] < gpsSource.GpsPos.m_fLat && (float)gpsSourceFence.GeoFence.arrayLat[j] >= gpsSource.GpsPos.m_fLat || (float)gpsSourceFence.GeoFence.arrayLat[j] < gpsSource.GpsPos.m_fLat && (float)gpsSourceFence.GeoFence.arrayLat[i] >= gpsSource.GpsPos.m_fLat) { if ((float)gpsSourceFence.GeoFence.arrayLon[i] + (gpsSource.GpsPos.m_fLat - (float)gpsSourceFence.GeoFence.arrayLat[i]) / ((float)gpsSourceFence.GeoFence.arrayLat[j] - (float)gpsSourceFence.GeoFence.arrayLat[i]) * ((float)gpsSourceFence.GeoFence.arrayLon[j] - (float)gpsSourceFence.GeoFence.arrayLon[i]) < gpsSource.GpsPos.m_fLon) bSourceIn = !bSourceIn; } } for (i = 0; i < gpsSourceFence.GeoFence.SourcesIn.Count; i++) { string sSourceIn = (string)gpsSourceFence.GeoFence.SourcesIn[i]; if (sSourceIn == gpsSource.sDescription && bSourceIn == false) { gpsSourceFence.GeoFence.SourcesIn.RemoveAt(i); gpsSourceFence.GeoFence.SourcesOut.Add(gpsSource.sDescription); // Signal current source is going OUT SignalGeoFence(false, gpsSourceFence.GeoFence, gpsSource); break; } } for (i = 0; i < gpsSourceFence.GeoFence.SourcesOut.Count; i++) { string sSourceOut = (string)gpsSourceFence.GeoFence.SourcesOut[i]; if (sSourceOut == gpsSource.sDescription && bSourceIn == true) { gpsSourceFence.GeoFence.SourcesOut.RemoveAt(i); gpsSourceFence.GeoFence.SourcesIn.Add(gpsSource.sDescription); // Signal current source is going IN SignalGeoFence(true, gpsSourceFence.GeoFence, gpsSource); break; } } if (gpsSourceFence.GeoFence.SourcesIn.Count == 0 && bSourceIn == true) { gpsSourceFence.GeoFence.SourcesIn.Add(gpsSource.sDescription); // Signal current source is going IN SignalGeoFence(true, gpsSourceFence.GeoFence, gpsSource); } else if (gpsSourceFence.GeoFence.SourcesOut.Count == 0 && bSourceIn == false) { gpsSourceFence.GeoFence.SourcesOut.Add(gpsSource.sDescription); // Signal current source is going OUT SignalGeoFence(false, gpsSourceFence.GeoFence, gpsSource); } } } } if (gpsSource.iStartAltitud>0) { Thread.Sleep(3000); gpsSource.iStartAltitud=0; } } m_iLocked++; } //Display Message in Message Monitor try { if (m_MessageMonitor!=null && bRet) m_MessageMonitor.AddMessage(cNMEAMessage, iMsgLength); } catch (Exception) { m_MessageMonitor=null; } } #if !DEBUG catch (Exception) { } #endif } return bRet; }
// //Constructor for the GpsTracker class // //public GpsTracker(WorldWindow.WorldWindow worldWindow) public GpsTracker(GpsTrackerPlugin gpsPlugin) { m_gpsTrackerPlugin = gpsPlugin; // Required for Windows Form Designer support InitializeComponent(); m_iSourceNameCount=0; SetupTree(); Bitmap image = new Bitmap(GpsTrackerPlugin.m_sPluginDirectory + "\\satellite.png"); pictureBoxLogo.Image = image; pictureBoxHelpLogo.Image= image; m_gpsSourceList.Clear(); m_NMEA = new GpsTrackerNMEA(this); m_UDPTCP = new GpsTrackerUDPTCP(this); m_File = new GpsTrackerFile(this); m_APRS = new GpsTrackerAPRS(this); m_fVerticalExaggeration=World.Settings.VerticalExaggeration; GetGpsBabelFormats(); m_timerLocked=null; m_hPOIThread=null; m_iLocked=0; m_iPrevLocked=0; progressBarAutoDetect.Value=0; progressBarAutoDetect.Width=0; //using visible to hide the progress bar does not seem to work very well //StartStop.Enabled=false; StartStop.Enabled = true; LoadSettings(null,true); //load user settings m_swRecorder=null; m_srReader=null; m_fPlayback=false; textBoxVersionInfo.Text="Your Version: " + m_gpsTrackerPlugin.m_sVersion; m_MessageMonitor = null; SetDefaultSettings(true); }