MissionPlanner.Log.DFLog.GetDFItemFromLine C# (CSharp) Метод

GetDFItemFromLine() публичный Метод

public GetDFItemFromLine ( string line, int lineno ) : DFItem
line string
lineno int
Результат DFItem
        public DFItem GetDFItemFromLine(string line, int lineno)
        {
            //line = line.Replace(",", ",");
            //line = line.Replace(":", ":");

            string[] items = line.Trim().Split(new char[] {',', ':'}, StringSplitOptions.RemoveEmptyEntries);

            if (line.StartsWith("FMT"))
            {
                FMTLine(line);
            }
            else if (line.StartsWith("GPS"))
            {
                if (line.StartsWith("GPS") && gpsstarttime == DateTime.MinValue)
                {
                    var time = GetTimeGPS(line);

                    if (time != DateTime.MinValue)
                    {
                        gpsstarttime = time;

                        lasttime = gpsstarttime;

                        int indextimems = FindMessageOffset(items[0], "T");

                        if (indextimems != -1)
                        {
                            try
                            {
                                msoffset = int.Parse(items[indextimems]);
                            }
                            catch
                            {
                                gpsstarttime = DateTime.MinValue;
                            }
                        }

                        int indextimeus = FindMessageOffset(items[0], "TimeUS");

                        if (indextimeus != -1)
                        {
                            try
                            {
                                msoffset = long.Parse(items[indextimeus])/1000;
                            }
                            catch
                            {
                                gpsstarttime = DateTime.MinValue;
                            }
                        }
                    }
                }
            }
            else if (line.StartsWith("ERR"))
            {
                Array.Resize(ref items, items.Length + 2);
                try
                {
                    int index = FindMessageOffset("ERR", "Subsys");
                    if (index == -1)
                    {
                        throw new ArgumentNullException();
                    }

                    int index2 = FindMessageOffset("ERR", "ECode");
                    if (index2 == -1)
                    {
                        throw new ArgumentNullException();
                    }

                    items[items.Length - 2] = "" + (DFLog.error_subsystem) int.Parse(items[index]);
                }
                catch
                {
                }
            }
            else if (line.StartsWith("EV"))
            {
                Array.Resize(ref items, items.Length + 1);
                try
                {
                    int index = FindMessageOffset("EV", "Id");
                    if (index == -1)
                    {
                        throw new ArgumentNullException();
                    }

                    items[items.Length - 1] = "" + (DFLog.events) int.Parse(items[index]);
                }
                catch
                {
                }
            }
            else if (line.StartsWith("MAG"))
            {
            }

            DFItem item = new DFItem();
            try
            {
                item.lineno = lineno;

                if (items.Length > 0)
                {
                    item.msgtype = items[0];
                    item.items = items;
                    bool timeus = false;

                    if (logformat.ContainsKey(item.msgtype))
                    {
                        int indextimems = FindMessageOffset(item.msgtype, "TimeMS");

                        if (item.msgtype.StartsWith("GPS"))
                        {
                            indextimems = FindMessageOffset(item.msgtype, "T");
                        }

                        if (indextimems == -1)
                        {
                            indextimems = FindMessageOffset(item.msgtype, "TimeUS");
                            timeus = true;
                        }

                        if (indextimems != -1)
                        {
                            long ntime = 0;

                            if (long.TryParse(items[indextimems], out ntime))
                            {
                                if (timeus)
                                    ntime /= 1000;

                                item.timems = (int) ntime;

                                if (gpsstarttime != DateTime.MinValue)
                                {
                                    item.time = gpsstarttime.AddMilliseconds(item.timems - msoffset);
                                    lasttime = item.time;
                                }
                            }
                            else
                            {
                                item.time = lasttime;
                            }
                        }
                        else
                        {
                            item.time = lasttime;
                        }
                    }
                }
            }
            catch
            {
            }

            return item;
        }

Usage Example

Пример #1
0
        public static void MapLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                if (File.Exists(logfile + ".jpg"))
                {
                    continue;
                }

                double minx = 99999;
                double maxx = -99999;
                double miny = 99999;
                double maxy = -99999;

                List <PointLatLngAlt> locs = new List <PointLatLngAlt>();
                try
                {
                    if (logfile.ToLower().EndsWith(".tlog"))
                    {
                        MAVLinkInterface mine = new MAVLinkInterface();


                        using (mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.Read)))
                        {
                            mine.logreadmode = true;

                            CurrentState cs = new CurrentState();

                            while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                            {
                                byte[] packet = mine.readPacket();

                                if (packet.Length < 5)
                                {
                                    continue;
                                }

                                try
                                {
                                    if (MainV2.speechEngine != null)
                                    {
                                        MainV2.speechEngine.SpeakAsyncCancelAll();
                                    }
                                }
                                catch { }

                                if (packet[5] == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT)
                                {
                                    var loc = packet.ByteArrayToStructure <MAVLink.mavlink_global_position_int_t>(6);

                                    if (loc.lat == 0 || loc.lon == 0)
                                    {
                                        continue;
                                    }

                                    locs.Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f));

                                    minx = Math.Min(minx, loc.lon / 10000000.0f);
                                    maxx = Math.Max(maxx, loc.lon / 10000000.0f);
                                    miny = Math.Min(miny, loc.lat / 10000000.0f);
                                    maxy = Math.Max(maxy, loc.lat / 10000000.0f);
                                }
                            }
                        }
                    }
                    else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log"))
                    {
                        bool bin = logfile.ToLower().EndsWith(".bin");

                        using (var st = File.OpenRead(logfile))
                        {
                            using (StreamReader sr = new StreamReader(st))
                            {
                                while (sr.BaseStream.Position < sr.BaseStream.Length)
                                {
                                    string line = "";

                                    if (bin)
                                    {
                                        line = BinaryLog.ReadMessage(sr.BaseStream);
                                    }
                                    else
                                    {
                                        line = sr.ReadLine();
                                    }

                                    if (line.StartsWith("FMT"))
                                    {
                                        DFLog.FMTLine(line);
                                    }
                                    else if (line.StartsWith("GPS"))
                                    {
                                        var item = DFLog.GetDFItemFromLine(line, 0);

                                        var lat = double.Parse(item.items[DFLog.FindMessageOffset(item.msgtype, "Lat")]);
                                        var lon = double.Parse(item.items[DFLog.FindMessageOffset(item.msgtype, "Lng")]);

                                        if (lat == 0 || lon == 0)
                                        {
                                            continue;
                                        }

                                        locs.Add(new PointLatLngAlt(lat, lon));

                                        minx = Math.Min(minx, lon);
                                        maxx = Math.Max(maxx, lon);
                                        miny = Math.Min(miny, lat);
                                        maxy = Math.Max(maxy, lat);
                                    }
                                }
                            }
                        }
                    }


                    if (locs.Count > 10)
                    {
                        // add a bit of buffer
                        var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001);
                        var map  = GetMap(area);

                        var grap = Graphics.FromImage(map);

                        PointF lastpoint = new PointF();

                        foreach (var loc in locs)
                        {
                            PointF newpoint = GetPixel(area, loc, map.Size);

                            if (!lastpoint.IsEmpty)
                            {
                                grap.DrawLine(Pens.Red, lastpoint, newpoint);
                            }

                            lastpoint = newpoint;
                        }

                        map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                        map.Dispose();

                        map = null;
                    }
                    else
                    {
                        DoTextMap(logfile + ".jpg", "No gps data");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("Mavlink 0.9"))
                    {
                        DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9");
                    }

                    continue;
                }
            }
        }
All Usage Examples Of MissionPlanner.Log.DFLog::GetDFItemFromLine