Exemple #1
0
        /// <summary>
        /// Get the position reported by the GPS receiver that is no older than
        /// the maxAge passed in
        /// </summary>
        /// <param name="maxAge">Max age of the gps position data that you want back.
        /// If there is no data within the required age, null is returned.
        /// if maxAge == TimeSpan.Zero, then the age of the data is ignored</param>
        /// <returns>GpsPosition class with all the position details</returns>
        public GpsPosition GetPosition(TimeSpan maxAge)
        {
            GpsPosition gpsPosition = null;

            if (Opened)
            {
                // allocate the necessary memory on the native side.  We have a class (GpsPosition) that
                // has the same memory layout as its native counterpart
                IntPtr ptr = Utils.LocalAlloc(Marshal.SizeOf(typeof(GpsPosition)));

                // fill in the required fields
                gpsPosition           = new GpsPosition();
                gpsPosition.dwVersion = 1;
                gpsPosition.dwSize    = Marshal.SizeOf(typeof(GpsPosition));

                // Marshal our data to the native pointer we allocated.
                Marshal.StructureToPtr(gpsPosition, ptr, false);

                // call native method passing in our native buffer
                int result = GPSGetPosition(gpsHandle, ptr, 500000, 0);
                if (result == 0)
                {
                    // native call succeeded, marshal native data to our managed data
                    gpsPosition = (GpsPosition)Marshal.PtrToStructure(ptr, typeof(GpsPosition));

                    if (maxAge != TimeSpan.Zero)
                    {
                        // check to see if the data is recent enough.
                        if (!gpsPosition.Time.HasValue || DateTime.UtcNow - maxAge > gpsPosition.Time)
                        {
                            gpsPosition = null;
                        }
                    }
                }

                // free our native memory
                Utils.LocalFree(ptr);
            }

            return(gpsPosition);
        }
Exemple #2
0
        /// <summary>
        /// Get the position reported by the GPS receiver that is no older than
        /// the maxAge passed in
        /// </summary>
        /// <param name="maxAge">Max age of the gps position data that you want back. 
        /// If there is no data within the required age, null is returned.  
        /// if maxAge == TimeSpan.Zero, then the age of the data is ignored</param>
        /// <returns>GpsPosition class with all the position details</returns>
        public GpsPosition GetPosition(TimeSpan maxAge)
        {
            GpsPosition gpsPosition = null;
             if(Opened)
             {
            // allocate the necessary memory on the native side.  We have a class (GpsPosition) that
            // has the same memory layout as its native counterpart
            IntPtr ptr = Utils.LocalAlloc(Marshal.SizeOf(typeof(GpsPosition)));

            // fill in the required fields
            gpsPosition = new GpsPosition();
            gpsPosition.dwVersion = 1;
            gpsPosition.dwSize = Marshal.SizeOf(typeof(GpsPosition));

            // Marshal our data to the native pointer we allocated.
            Marshal.StructureToPtr(gpsPosition, ptr, false);

            // call native method passing in our native buffer
            int result = GPSGetPosition(gpsHandle, ptr, 500000, 0);
            if(result == 0)
            {
               // native call succeeded, marshal native data to our managed data
               gpsPosition = (GpsPosition) Marshal.PtrToStructure(ptr, typeof(GpsPosition));

               if(maxAge != TimeSpan.Zero)
               {
                  // check to see if the data is recent enough.
                  if(!gpsPosition.Time.HasValue || DateTime.UtcNow - maxAge > gpsPosition.Time)
                  {
                     gpsPosition = null;
                  }
               }
            }

            // free our native memory
            Utils.LocalFree(ptr);
             }

             return gpsPosition;
        }
Exemple #3
0
        void gps_LocationChanged(object sender, GpsPosition position)
        {
            try
             {
            if(position != null)
            {
               count++;

               //Debug.WriteLine("LocationChanged: " + DateTime.Now.ToLongTimeString() + " -> " + count);

               if(position.Time.HasValue && position.Latitude.HasValue && position.Longitude.HasValue)
               {
                  //Debug.WriteLine("Location: " + position.Latitude.Value + "|" + position.Longitude.Value);

                  // first time
                  if(!TimeUTC.HasValue)
                  {
                     TimeUTC = position.Time;
                     Lat = position.Latitude;
                     Lng = position.Longitude;
                  }

                  if(TimeUTC.HasValue && position.Time - TimeUTC.Value >= delay)
                  {
                     Delta = gps.GetDistance(position.Latitude.Value, position.Longitude.Value, Lat.Value, Lng.Value);
                     Total += Delta;
                     Lat = position.Latitude;
                     Lng = position.Longitude;
                     TimeUTC = position.Time;

                     AddToLogCurrentInfo(position);
                  }
               }
               else
               {
                  Lat = position.Latitude;
                  Lng = position.Longitude;
                  TimeUTC = position.Time;
               }

               // do not update if user is idling
               if(IsVisible)
               {
                  lock(Satellites)
                  {
                     Satellites.Clear();
                     Satellites.AddRange(position.GetSatellitesInView());
                     Satellites.TrimExcess();
                  }

                  Invoke(updateDataHandler, position);
               }
            }
             }
             catch(Exception ex)
             {
            Debug.WriteLine("gps_LocationChanged: " + ex);
             }
        }
Exemple #4
0
        public bool AddToLogCurrentInfo(GpsPosition data)
        {
            if(string.IsNullOrEmpty(LogDb))
             {
            return false;
             }

             bool ret = true;
             try
             {
            {
               if(cmd.Transaction == null)
               {
                  cmd.Transaction = cn.BeginTransaction(IsolationLevel.Serializable);
                  Debug.WriteLine("BeginTransaction: " + DateTime.Now.ToLongTimeString());
               }

               cmd.Parameters["@p1"].Value = data.Time.Value;
               cmd.Parameters["@p2"].Value = countReal++;
               cmd.Parameters["@p3"].Value = Delta;
               cmd.Parameters["@p4"].Value = data.Speed;
               cmd.Parameters["@p5"].Value = data.SeaLevelAltitude;
               cmd.Parameters["@p6"].Value = data.EllipsoidAltitude;
               cmd.Parameters["@p7"].Value = (short?) data.SatellitesInViewCount;
               cmd.Parameters["@p8"].Value = (short?) data.SatelliteCount;
               cmd.Parameters["@p9"].Value = data.Latitude.Value;
               cmd.Parameters["@p10"].Value = data.Longitude.Value;
               cmd.Parameters["@p11"].Value = data.PositionDilutionOfPrecision;
               cmd.Parameters["@p12"].Value = data.HorizontalDilutionOfPrecision;
               cmd.Parameters["@p13"].Value = data.VerticalDilutionOfPrecision;
               cmd.Parameters["@p14"].Value = (byte) data.FixQuality;
               cmd.Parameters["@p15"].Value = (byte) data.FixType;
               cmd.Parameters["@p16"].Value = (byte) data.FixSelection;

               cmd.ExecuteNonQuery();
            }

            if(DateTime.Now - LastFlush >= FlushDelay)
            {
               TryCommitData();
            }
             }
             catch(Exception ex)
             {
            Debug.WriteLine("AddToLog: " + ex.ToString());
            ret = false;
             }

             return ret;
        }
Exemple #5
0
 public LocationChangedEventArgs(GpsPosition position)
 {
     this.position = position;
 }
 public LocationChangedEventArgs(GpsPosition position)
 {
     this.position = position;
 }