OSDN Git Service

無効な入力に対する例外発生を抑制
authorkimikage <kimikage_ceo@hotmail.com>
Fri, 8 Jun 2012 11:57:03 +0000 (20:57 +0900)
committerkimikage <kimikage_ceo@hotmail.com>
Fri, 8 Jun 2012 12:01:32 +0000 (21:01 +0900)
Yubeshi/Degree.cs
Yubeshi/Nmea/GpGga.cs
Yubeshi/Nmea/Packet.cs

index 893ff08..2dcfdce 100755 (executable)
@@ -114,7 +114,11 @@ namespace Yubeshi
 \r
         public static Degree FromNmeaFormat(string angle, string direction)\r
         {\r
-            decimal raw = Decimal.Parse(angle);\r
+            decimal raw;\r
+            if (!Decimal.TryParse(angle, out raw))\r
+            {\r
+                return new Degree(Double.NaN);\r
+            }\r
             double deg = (double)(Math.Floor(raw / 100m) + (raw % 100m) / 60m);\r
             if (direction == "S" || direction == "W")\r
             {\r
index 9be7955..7754ddb 100755 (executable)
@@ -61,10 +61,16 @@ namespace Yubeshi.Nmea
                                     Degree.FromNmeaFormat(v[3], v[4]));\r
             FixQuality = (FixQualityClass)Int32.Parse(v[5]);\r
             TrackedSatellites = Int32.Parse(v[6]);\r
-            Dilution = Decimal.Parse(v[7]);\r
-            MslAltitude = GetLength(v[8], v[9]);\r
-            GeoidSeparation = GetLength(v[10], v[11]);\r
+            HorizontalDop = ParseDouble(v[7]);\r
+            MslAltitude = new Height(GetLength(v[8], v[9]), \r
+                                                    Height.Base.MeanSeaLevel);\r
+            GeoidSeparation = new Height(GetLength(v[10], v[11]),\r
+                                                Height.Base.Wgs84Ellipsoid);\r
+            double age = ParseDouble(v[12]);\r
             \r
+            AgeOfDgpsCorrections = Double.IsNaN(age)?\r
+                                TimeSpan.MinValue :TimeSpan.FromSeconds(age);\r
+            DgpsStation = ParseInt(v[13], -1);\r
             CheckSum = elements.CheckSum;\r
         }\r
 \r
@@ -96,25 +102,31 @@ namespace Yubeshi.Nmea
             private set;\r
         }\r
 \r
-        public decimal Dilution\r
+        public double HorizontalDop\r
         {\r
             get;\r
             private set;\r
         }\r
 \r
-        public decimal MslAltitude\r
+        public Height MslAltitude\r
         {\r
             get;\r
             private set;\r
         }\r
 \r
-        public decimal GeoidSeparation\r
+        public Height GeoidSeparation\r
         {\r
             get;\r
             private set;\r
         }\r
 \r
-        public int StationID\r
+        public TimeSpan AgeOfDgpsCorrections\r
+        {\r
+            get;\r
+            private set;\r
+        }\r
+\r
+        public int DgpsStation\r
         {\r
             get;\r
             private set;\r
index 72d49d4..819d89c 100755 (executable)
@@ -23,6 +23,13 @@ namespace Yubeshi.Nmea
             public int CheckSum;\r
             public int PacketLength;\r
         }\r
+\r
+        public enum Status\r
+        {\r
+            Unknown,\r
+            Invalid,\r
+            Valid,\r
+        }\r
         #endregion\r
 \r
         #region fields\r
@@ -83,17 +90,42 @@ namespace Yubeshi.Nmea
 \r
         protected static Elements GetElements(byte[] sentence, int elementNum)\r
         {\r
-            string[] values = new string[elementNum];\r
             int elementIndex = 0;\r
             int lastCommaPos = 6;\r
             if (sentence[1] == 'G' && sentence[2] == 'P')\r
             {\r
                 lastCommaPos = 7;\r
             }\r
+            if (elementNum < 0)\r
+            {\r
+                elementNum = 0;\r
+                for (int i = lastCommaPos; i < sentence.Length; ++i)\r
+                {\r
+                    if (sentence[i] == ',')\r
+                    {\r
+                        elementNum++;\r
+                        continue;\r
+                    }\r
+                    if (sentence[i] == '*')\r
+                    {\r
+                        elementNum++;\r
+                        break;\r
+                    }\r
+                }\r
+            }\r
+            if (elementNum < 1)\r
+            {\r
+                return null;\r
+            }\r
+            string[] values = new string[elementNum];\r
             for (int i = lastCommaPos; i < sentence.Length; ++i)\r
             {\r
                 if (sentence[i] == ',' || sentence[i] == '*')\r
                 {\r
+                    if (elementIndex >= elementNum)\r
+                    {\r
+                        return null;\r
+                    }\r
                     values[elementIndex++] = Encoding.ASCII.GetString(\r
                                     sentence, lastCommaPos, i - lastCommaPos);\r
                     lastCommaPos = i + 1;\r
@@ -145,6 +177,7 @@ namespace Yubeshi.Nmea
             }\r
             return true;\r
         }\r
+\r
         protected TimeSpan ParseTime(string time)\r
         {\r
             int h = Int32.Parse(time.Substring(0, 2));\r
@@ -155,13 +188,13 @@ namespace Yubeshi.Nmea
             return new TimeSpan(0, h, m, sec, msec);\r
         }\r
 \r
-        protected static decimal GetLength(string value, string unit)\r
+        protected static double GetLength(string value, string unit)\r
         { \r
-            decimal v;\r
+            double v;\r
             \r
-            if (!Decimal.TryParse(value, out v))\r
+            if (!Double.TryParse(value, out v))\r
             {\r
-                return -Decimal.MaxValue;;\r
+                return Double.NaN;\r
             }\r
             if (unit == "M")\r
             {\r
@@ -169,7 +202,46 @@ namespace Yubeshi.Nmea
             }\r
             else\r
             {\r
-                return -Decimal.MaxValue; ;\r
+                return Double.NaN;\r
+            }\r
+        }\r
+\r
+        protected static int ParseInt(string intString, int defaultValue)\r
+        {\r
+            int v;\r
+            if (!Int32.TryParse(intString, out v))\r
+            {\r
+                return defaultValue;\r
+            }\r
+            return v;\r
+        }\r
+\r
+        protected static double ParseDouble(string doubleString)\r
+        {\r
+            return ParseDouble(doubleString, Double.NaN);\r
+        }\r
+        \r
+        protected static double ParseDouble(string doubleString,\r
+                                                        double defaultValue)\r
+        {\r
+            double v;\r
+            if (!Double.TryParse(doubleString, out v))\r
+            {\r
+                return defaultValue;\r
+            }\r
+            return v;\r
+        }\r
+\r
+        protected static Status ParseStatus(string status)\r
+        {\r
+            switch (status)\r
+            {\r
+                case "V":\r
+                    return Status.Invalid;\r
+                case "A":\r
+                    return Status.Valid;\r
+                default:\r
+                    return Status.Unknown;\r
             }\r
         }\r
         #endregion\r