MissionPlanner.MAVLinkInterface.GetLog C# (CSharp) Method

GetLog() public method

public GetLog ( ushort no ) : FileStream
no ushort
return System.IO.FileStream
        public FileStream GetLog(ushort no)
        {
            FileStream ms = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite);
            Hashtable set = new Hashtable();

            giveComport = true;
            MAVLinkMessage buffer;

            if (Progress != null)
            {
                Progress((int) 0, "");
            }

            uint totallength = 0;
            uint ofs = 0;
            uint bps = 0;
            DateTime bpstimer = DateTime.Now;

            mavlink_log_request_data_t req = new mavlink_log_request_data_t();

            req.target_component = MAV.compid;
            req.target_system = MAV.sysid;
            req.id = no;
            req.ofs = ofs;
            // entire log
            req.count = 0xFFFFFFFF;

            // request point
            generatePacket((byte) MAVLINK_MSG_ID.LOG_REQUEST_DATA, req);

            DateTime start = DateTime.Now;
            int retrys = 3;

            while (true)
            {
                if (!(start.AddMilliseconds(3000) > DateTime.Now))
                {
                    if (retrys > 0)
                    {
                        log.Info("GetLog Retry " + retrys + " - giv com " + giveComport);
                        generatePacket((byte) MAVLINK_MSG_ID.LOG_REQUEST_DATA, req);
                        start = DateTime.Now;
                        retrys--;
                        continue;
                    }
                    giveComport = false;
                    throw new TimeoutException("Timeout on read - GetLog");
                }

                buffer = readPacket();
                if (buffer.Length > 5)
                {
                    if (buffer.msgid == (byte) MAVLINK_MSG_ID.LOG_DATA)
                    {
                        var data = buffer.ToStructure<mavlink_log_data_t>();

                        if (data.id != no)
                            continue;

                        // reset retrys
                        retrys = 3;
                        start = DateTime.Now;

                        bps += data.count;

                        // record what we have received
                        set[(data.ofs/90).ToString()] = 1;

                        ms.Seek((long) data.ofs, SeekOrigin.Begin);
                        ms.Write(data.data, 0, data.count);

                        // update new start point
                        req.ofs = data.ofs + data.count;

                        if (bpstimer.Second != DateTime.Now.Second)
                        {
                            if (Progress != null)
                            {
                                Progress((int) req.ofs, "");
                            }

                            //Console.WriteLine("log dl bps: " + bps.ToString());
                            bpstimer = DateTime.Now;
                            bps = 0;
                        }

                        // if data is less than max packet size or 0 > exit
                        if (data.count < 90 || data.count == 0)
                        {
                            totallength = data.ofs + data.count;
                            log.Info("start fillin len " + totallength + " count " + set.Count + " datalen " +
                                     data.count);
                            break;
                        }
                    }
                }
            }

            log.Info("set count " + set.Count);
            log.Info("count total " + ((totallength)/90 + 1));
            log.Info("totallength " + totallength);
            log.Info("current length " + ms.Length);

            while (true)
            {
                if (totallength == ms.Length && set.Count >= ((totallength) / 90 + 1))
                {
                    giveComport = false;
                    return ms;
                }

                if (!(start.AddMilliseconds(200) > DateTime.Now))
                {
                    for (int a = 0; a < ((totallength)/90 + 1); a++)
                    {
                        if (!set.ContainsKey(a.ToString()))
                        {
                            // request large chunk if they are back to back
                            uint bytereq = 90;
                            int b = a + 1;
                            while (!set.ContainsKey(b.ToString()))
                            {
                                bytereq += 90;
                                b++;
                            }

                            req.ofs = (uint) (a*90);
                            req.count = bytereq;
                            log.Info("req missing " + req.ofs + " " + req.count);
                            generatePacket((byte) MAVLINK_MSG_ID.LOG_REQUEST_DATA, req);
                            start = DateTime.Now;
                            break;
                        }
                    }
                }

                buffer = readPacket();
                if (buffer.Length > 5)
                {
                    if (buffer.msgid == (byte) MAVLINK_MSG_ID.LOG_DATA)
                    {
                        var data = buffer.ToStructure<mavlink_log_data_t>();

                        if (data.id != no)
                            continue;

                        // reset retrys
                        retrys = 3;
                        start = DateTime.Now;

                        bps += data.count;

                        // record what we have received
                        set[(data.ofs/90).ToString()] = 1;

                        ms.Seek((long)data.ofs, SeekOrigin.Begin);
                        ms.Write(data.data, 0, data.count);

                        // update new start point
                        req.ofs = data.ofs + data.count;

                        if (bpstimer.Second != DateTime.Now.Second)
                        {
                            if (Progress != null)
                            {
                                Progress((int) req.ofs, "");
                            }

                            //Console.WriteLine("log dl bps: " + bps.ToString());
                            bpstimer = DateTime.Now;
                            bps = 0;
                        }

                        // check if we have next set and invalidate to request next packets
                        if (set.ContainsKey(((data.ofs/90) + 1).ToString()))
                        {
                            start = DateTime.MinValue;
                        }

                        // if data is less than max packet size or 0 > exit
                        if (data.count < 90 || data.count == 0)
                        {
                            continue;
                        }
                    }
                }
            }
        }