• R/O
  • HTTP
  • SSH
  • HTTPS

yubeshi: Commit

source codes and tests


Commit MetaInfo

Revisionddf5ce3309579bdce39db2fc52a45d79b2fe9ae9 (tree)
Time2012-06-08 21:01:32
Authorkimikage <kimikage_ceo@hotm...>
Commiterkimikage

Log Message

無効な入力に対する例外発生を抑制

Change Summary

Incremental Difference

--- a/Yubeshi/Degree.cs
+++ b/Yubeshi/Degree.cs
@@ -114,7 +114,11 @@ namespace Yubeshi
114114
115115 public static Degree FromNmeaFormat(string angle, string direction)
116116 {
117- decimal raw = Decimal.Parse(angle);
117+ decimal raw;
118+ if (!Decimal.TryParse(angle, out raw))
119+ {
120+ return new Degree(Double.NaN);
121+ }
118122 double deg = (double)(Math.Floor(raw / 100m) + (raw % 100m) / 60m);
119123 if (direction == "S" || direction == "W")
120124 {
--- a/Yubeshi/Nmea/GpGga.cs
+++ b/Yubeshi/Nmea/GpGga.cs
@@ -61,10 +61,16 @@ namespace Yubeshi.Nmea
6161 Degree.FromNmeaFormat(v[3], v[4]));
6262 FixQuality = (FixQualityClass)Int32.Parse(v[5]);
6363 TrackedSatellites = Int32.Parse(v[6]);
64- Dilution = Decimal.Parse(v[7]);
65- MslAltitude = GetLength(v[8], v[9]);
66- GeoidSeparation = GetLength(v[10], v[11]);
64+ HorizontalDop = ParseDouble(v[7]);
65+ MslAltitude = new Height(GetLength(v[8], v[9]),
66+ Height.Base.MeanSeaLevel);
67+ GeoidSeparation = new Height(GetLength(v[10], v[11]),
68+ Height.Base.Wgs84Ellipsoid);
69+ double age = ParseDouble(v[12]);
6770
71+ AgeOfDgpsCorrections = Double.IsNaN(age)?
72+ TimeSpan.MinValue :TimeSpan.FromSeconds(age);
73+ DgpsStation = ParseInt(v[13], -1);
6874 CheckSum = elements.CheckSum;
6975 }
7076
@@ -96,25 +102,31 @@ namespace Yubeshi.Nmea
96102 private set;
97103 }
98104
99- public decimal Dilution
105+ public double HorizontalDop
100106 {
101107 get;
102108 private set;
103109 }
104110
105- public decimal MslAltitude
111+ public Height MslAltitude
106112 {
107113 get;
108114 private set;
109115 }
110116
111- public decimal GeoidSeparation
117+ public Height GeoidSeparation
112118 {
113119 get;
114120 private set;
115121 }
116122
117- public int StationID
123+ public TimeSpan AgeOfDgpsCorrections
124+ {
125+ get;
126+ private set;
127+ }
128+
129+ public int DgpsStation
118130 {
119131 get;
120132 private set;
--- a/Yubeshi/Nmea/Packet.cs
+++ b/Yubeshi/Nmea/Packet.cs
@@ -23,6 +23,13 @@ namespace Yubeshi.Nmea
2323 public int CheckSum;
2424 public int PacketLength;
2525 }
26+
27+ public enum Status
28+ {
29+ Unknown,
30+ Invalid,
31+ Valid,
32+ }
2633 #endregion
2734
2835 #region fields
@@ -83,17 +90,42 @@ namespace Yubeshi.Nmea
8390
8491 protected static Elements GetElements(byte[] sentence, int elementNum)
8592 {
86- string[] values = new string[elementNum];
8793 int elementIndex = 0;
8894 int lastCommaPos = 6;
8995 if (sentence[1] == 'G' && sentence[2] == 'P')
9096 {
9197 lastCommaPos = 7;
9298 }
99+ if (elementNum < 0)
100+ {
101+ elementNum = 0;
102+ for (int i = lastCommaPos; i < sentence.Length; ++i)
103+ {
104+ if (sentence[i] == ',')
105+ {
106+ elementNum++;
107+ continue;
108+ }
109+ if (sentence[i] == '*')
110+ {
111+ elementNum++;
112+ break;
113+ }
114+ }
115+ }
116+ if (elementNum < 1)
117+ {
118+ return null;
119+ }
120+ string[] values = new string[elementNum];
93121 for (int i = lastCommaPos; i < sentence.Length; ++i)
94122 {
95123 if (sentence[i] == ',' || sentence[i] == '*')
96124 {
125+ if (elementIndex >= elementNum)
126+ {
127+ return null;
128+ }
97129 values[elementIndex++] = Encoding.ASCII.GetString(
98130 sentence, lastCommaPos, i - lastCommaPos);
99131 lastCommaPos = i + 1;
@@ -145,6 +177,7 @@ namespace Yubeshi.Nmea
145177 }
146178 return true;
147179 }
180+
148181 protected TimeSpan ParseTime(string time)
149182 {
150183 int h = Int32.Parse(time.Substring(0, 2));
@@ -155,13 +188,13 @@ namespace Yubeshi.Nmea
155188 return new TimeSpan(0, h, m, sec, msec);
156189 }
157190
158- protected static decimal GetLength(string value, string unit)
191+ protected static double GetLength(string value, string unit)
159192 {
160- decimal v;
193+ double v;
161194
162- if (!Decimal.TryParse(value, out v))
195+ if (!Double.TryParse(value, out v))
163196 {
164- return -Decimal.MaxValue;;
197+ return Double.NaN;
165198 }
166199 if (unit == "M")
167200 {
@@ -169,7 +202,46 @@ namespace Yubeshi.Nmea
169202 }
170203 else
171204 {
172- return -Decimal.MaxValue; ;
205+ return Double.NaN;
206+ }
207+ }
208+
209+ protected static int ParseInt(string intString, int defaultValue)
210+ {
211+ int v;
212+ if (!Int32.TryParse(intString, out v))
213+ {
214+ return defaultValue;
215+ }
216+ return v;
217+ }
218+
219+ protected static double ParseDouble(string doubleString)
220+ {
221+ return ParseDouble(doubleString, Double.NaN);
222+ }
223+
224+ protected static double ParseDouble(string doubleString,
225+ double defaultValue)
226+ {
227+ double v;
228+ if (!Double.TryParse(doubleString, out v))
229+ {
230+ return defaultValue;
231+ }
232+ return v;
233+ }
234+
235+ protected static Status ParseStatus(string status)
236+ {
237+ switch (status)
238+ {
239+ case "V":
240+ return Status.Invalid;
241+ case "A":
242+ return Status.Valid;
243+ default:
244+ return Status.Unknown;
173245 }
174246 }
175247 #endregion
Show on old repository browser