00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00006 namespace GpsModule {
00007 public class RmcStrategy {
00008
00009 public delegate void updateErrorEventHandler(Exception error);
00010 public event updateErrorEventHandler updateErrorEvent;
00011
00012 RmcData _data;
00013 public RmcData Data {
00014 get { return _data; }
00015 }
00016
00017 public RmcStrategy() {
00018 _data = new RmcData();
00019 }
00020
00021 public void parseSentance(string sentence) {
00022 try {
00023 _data.setSentance(sentence);
00024
00025 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00026 string s = sentence.Substring(0, sentence.IndexOf(","));
00027 int hour;
00028 int.TryParse(s.Substring(0, 2), out hour);
00029 int minute;
00030 int.TryParse(s.Substring(2, 2), out minute);
00031 int second;
00032 int.TryParse(s.Substring(4, 2), out second);
00033
00034 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00035 _data.setStatus(sentence.Substring(0, sentence.IndexOf(","))[0]);
00036
00037 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00038 _data.setLatitude(sentence.Substring(0, sentence.IndexOf(",")));
00039
00040 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00041 _data.setNorthOrSouth(sentence.Substring(0, sentence.IndexOf(","))[0]);
00042
00043 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00044 _data.setLongitude(sentence.Substring(0, sentence.IndexOf(",")));
00045
00046 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00047 _data.setEastOrWest(sentence.Substring(0, sentence.IndexOf(","))[0]);
00048
00049 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00050 _data.setSpeedOverGround(double.Parse(sentence.Substring(0, sentence.IndexOf(","))));
00051
00052 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00053 _data.setTrackMadeGood(double.Parse(sentence.Substring(0, sentence.IndexOf(","))));
00054
00055 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00056 s = sentence.Substring(0, sentence.IndexOf(","));
00057 int day;
00058 int.TryParse(s.Substring(0, 2), out day);
00059 int month;
00060 int.TryParse(s.Substring(2, 2), out month);
00061 int year;
00062 int.TryParse(s.Substring(4, 2), out year);
00063 year += 2000;
00064
00065 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00066
00067
00068 sentence = sentence.Substring(sentence.IndexOf(",") + 1, sentence.Length - sentence.IndexOf(",") - 1);
00069 _data.setEastOrWestM(sentence.Substring(0, sentence.IndexOf("*"))[0]);
00070
00071 sentence = sentence.Substring(sentence.IndexOf("*") + 1, sentence.Length - sentence.IndexOf("*") - 1);
00072
00073 _data.setTime(new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc));
00074 } catch (Exception e) {
00075 if (updateErrorEvent != null)
00076 updateErrorEvent.Invoke(e);
00077 }
00078 }
00079 }
00080 }