Пример #1
0
        public MavlinkProtocolModel Parse(string fileName, Stream strm)
        {
            var protocol = new MavlinkProtocolModel
            {
                FileName = fileName,
            };
            XmlReader rdr = new XmlTextReader(strm);

            rdr.Read();
            while (rdr.Read())
            {
                switch (rdr.Name)
                {
                case "include":
                    protocol.Include.Add(rdr.ReadElementContentAsString());
                    break;

                case "version":
                    protocol.Version = rdr.ReadElementContentAsInt();
                    break;

                case "dialect":
                    protocol.Dialect = rdr.ReadElementContentAsInt();
                    break;

                case "enums":
                    ParseEnumsSection(rdr, protocol.Enums);
                    break;

                case "messages":
                    ParseMessagesSection(rdr, protocol.Messages);
                    break;
                }
            }
            return(protocol);
        }
Пример #2
0
        public string Generate(string template, MavlinkProtocolModel model)
        {
            var args = Hash.FromAnonymousObject(
                new
            {
                Tool        = typeof(Program).Assembly.GetTitle(),
                ToolVersion = typeof(Program).Assembly.GetInformationalVersion(),
                Dialect     = model.Dialect,
                Version     = model.Version,
                Namespace   = NameConverter(Path.GetFileNameWithoutExtension(model.FileName)),
                Enums       = model.Enums.Select(en =>
                                                 new
                {
                    Value         = en.Value,
                    Desc          = en.Desc,
                    Name          = en.Name,
                    CamelCaseName = NameConverter(en.Name),
                    Entries       = en.Entries.Select(
                        enEntry => new
                    {
                        Name          = enEntry.Name,
                        CamelCaseName = NameConverter(enEntry.Name),
                        Desc          = enEntry.Desc,
                        Value         = enEntry.Value,
                        Params        = enEntry.Params.Select(p => new
                        {
                            p.Index,
                            p.Desc,
                        })
                    })
                }),
                Messages = model.Messages.Select(msg =>
                                                 new
                {
                    Name                 = msg.Name,
                    CamelCaseName        = NameConverter(msg.Name),
                    Desc                 = msg.Desc,
                    Id                   = msg.Id,
                    CrcExtra             = msg.CrcExtra,
                    PayloadByteSize      = msg.GetAllFields().Sum(_ => _.FieldByteSize),
                    ExtendedFieldsLength = msg.ExtendedFields.Sum(_ => _.FieldByteSize),
                    Fields               = msg.GetAllFields().Select(field =>
                                                                     new
                    {
                        Name                       = field.Name,
                        CamelCaseName              = NameConverter(field.Name),
                        Units                      = field.Units,
                        IsExtended                 = field.IsExtended,
                        FieldTypeByteSize          = field.FieldTypeByteSize,
                        ArrayLength                = field.ArrayLength,
                        Desc                       = field.Desc,
                        IsArray                    = field.IsArray,
                        IsTheLargestArrayInMessage = field.IsTheLargestArrayInMessage,
                        IsEnum                     = field.Enum != null,
                        Type                       = ConvertTypeName(field.Type),
                        EnumCamelCaseName          = NameConverter(field.Enum),
                    })
                })
            });

            var result = Template.Parse(template);

            return(result.Render(args));
        }