Beispiel #1
0
        public GPSModel Parse(string[] values)
        {
            GPSModel retInstance = (GPSModel)modelType.GetConstructor(new Type[0]).Invoke(null);

            foreach (var def in fieldDefinition)
            {
                if (values[def.Index] != null && values[def.Index].Length != 0)
                {
                    try
                    {
                        if (def.TargetType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            var internalType = def.TargetType.GenericTypeArguments[0];
                            def.PropertyTarget.SetValue(retInstance, NMEAFormat.ParseValue(internalType, values[def.Index], def.DependentIndex != null ? values[def.DependentIndex.Value] : null));
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                    catch (Exception)
                    {
                        def.PropertyTarget.SetValue(retInstance, NMEAFormat.ParseValue(def.TargetType, values[def.Index], def.DependentIndex != null ? values[def.DependentIndex.Value] : null));
                    }
                }
            }

            return(retInstance);
        }
Beispiel #2
0
        public GPSModel Parse(string input)
        {
            var parseMatch = parsingRegex.Match(input);

            GPSModel parsedModel = null;

            if (parseMatch.Success)
            {
                string keyword = parseMatch.Groups[2].Value;

                try
                {
                    byte textChecksum  = Checksum(parseMatch.Groups[1].Value);
                    byte validChecksum = byte.Parse(parseMatch.Groups[4].Value, System.Globalization.NumberStyles.HexNumber);

                    if (textChecksum != validChecksum)
                    {
                        throw new InvalidChecksumException(String.Format("Checksum expected {0} while computed {1}", validChecksum, textChecksum));
                    }


                    string[] objectContent = parseMatch.Groups[3].Value.Split(',');

                    parsedModel = this.parserList[keyword].Parse(objectContent);
                }
                catch (KeyNotFoundException)
                {
                    throw new UnknownMessageException(String.Format("Unknown message with type {0}", keyword));
                }
                catch (Exception)
                {
                    // Invalid string
                    parsedModel = null;
                    throw;
                }
            }

            return(parsedModel);
        }