September 7, 2008

GPS Monitoring Test Provider

Here is a test provider
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Diagnostics;
using System.IO;

namespace GpsTest
{
  class GpsTestProvider : IGpsDataProvider, IDisposable
  {
    Timer timer = new Timer();
    int index = 0;
    string[] sampleData = new string[]
      {
          "$GPRMC,135150.000,A,5106.7058,N,00123.8886,W,4.07,192.74,250808,,,A*74",
          "$GPRMC,135151.000,A,5106.7044,N,00123.8903,W,4.75,210.87,250808,,,A*74",
          "$GPRMC,135152.000,A,5106.7037,N,00123.8916,W,3.52,212.41,250808,,,A*7D",
          "$GPRMC,135153.000,A,5106.7033,N,00123.8927,W,2.88,208.03,250808,,,A*71",
          "$GPRMC,135154.000,A,5106.7030,N,00123.8928,W,2.72,205.69,250808,,,A*7E",
          "$GPRMC,135155.000,A,5106.7024,N,00123.8942,W,2.70,211.83,250808,,,A*75",
          "$GPRMC,135156.000,A,5106.7021,N,00123.8945,W,1.60,188.74,250808,,,A*7D",
          "$GPRMC,135157.000,A,5106.7017,N,00123.8945,W,1.21,168.54,250808,,,A*70",
      };


    public GpsTestProvider()
    {
      const string sampleDataFile = "GPSTestData.txt";
      if (File.Exists(sampleDataFile))
      {
        sampleData = File.ReadAllLines(sampleDataFile);
      }
    }

    #region Dispose Implementation

    // Use C# destructor syntax for finalization code.
    ~GpsTestProvider()
    {
      Debug.Assert(false, "This object was not disposed of");
      Dispose(false);
    }

    private bool m_Disposed = false;

    //Implement IDisposable.
    public void Dispose()
    {
      Dispose(true);
      GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
      if (m_Disposed)
        return;

      if (disposing)
      {
        // TODO: cleanup managed resources in GpsTestProvider
      }
      Close();
      timer.Dispose();

      m_Disposed = true;
    }

    #endregion Dispose Implementation


    #region IGpsDataProvider Members

    public void Close()
    {
      timer.Stop();
      timer.Enabled = false;
    }

    public event GpsDataReceivedEventHandler GpsDataReceived;

    public void Open()
    {
      timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
      timer.Interval = 100;
      timer.Start();
      timer.Enabled = true;
    }

    public bool IsOpen()
    {
      return timer.Enabled;
    }
    #endregion

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
      if (GpsDataReceived != null)
      {
        GpsDataReceived(this, new GpsDataLineEventArgs(sampleData[index]));
        index = (index + 1) % sampleData.Length;
      }
    }

  }
}

No comments: