Ejemplo n.º 1
1
        public static void SortLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                FileInfo info = new FileInfo(logfile);

                if (info.Length <= 1024)
                {
                    try
                    {
                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                 + "SMALL" + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                            Directory.CreateDirectory(destdir);

                        File.Move(logfile, destdir + Path.GetFileName(logfile));
                        File.Move(logfile.Replace(".tlog", ".rlog"), destdir + Path.GetFileName(logfile).Replace(".tlog", ".rlog"));
                    }
                    catch { }
                    continue;
                }

                MAVLinkInterface mine = new MAVLinkInterface();

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

                        byte[] hbpacket = mine.getHeartBeat();

                        if (hbpacket.Length == 0)
                            continue;

                        MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket);

                        mine.logreadmode = false;
                        mine.logplaybackfile.Close();

                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                            + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                            + hbpacket[3] + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                            Directory.CreateDirectory(destdir);

                        File.Move(logfile, destdir + Path.GetFileName(logfile));

                        try
                        {
                            File.Move(logfile.Replace(".tlog", ".rlog"), destdir + Path.GetFileName(logfile).Replace(".tlog", ".rlog"));
                        }
                        catch { }
                    }
                }
                catch { continue; }
            }
        }
Ejemplo n.º 2
0
        public MAVLinkSerialPort(MAVLinkInterface mavint, MAVLink.SERIAL_CONTROL_DEV port)
        {
            this.mavint = mavint;
            this.port = port;

            if (!mavint.BaseStream.IsOpen)
            {
                mavint.BaseStream.Open();
            }

            if (mavint.getHeartBeat().Length == 0)
            {
                throw new Exception("No valid heartbeats read from port");
            }

            if (subscription.Value != null)
                mavint.UnSubscribeToPacketType(subscription);

            subscription = mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.SERIAL_CONTROL, ReceviedPacket, true);

            bgdata = new Thread(mainloop);
            bgdata.Name = "MAVLinkSerialPort";
            bgdata.IsBackground = true;
            bgdata.Start();
        }
Ejemplo n.º 3
0
        public MAVLinkSerialPort(MAVLinkInterface mavint, MAVLink.SERIAL_CONTROL_DEV port)
        {
            this.mavint = mavint;
            this.port   = port;

            if (!mavint.BaseStream.IsOpen)
            {
                throw new Exception(Strings.PleaseConnect);
            }

            if (mavint.getHeartBeat().Length == 0)
            {
                throw new Exception("No valid heartbeats read from port");
            }

            if (subscription.Value != null)
            {
                mavint.UnSubscribeToPacketType(subscription);
            }

            subscription = mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.SERIAL_CONTROL, ReceviedPacket, true);

            bgdata              = new Thread(mainloop);
            bgdata.Name         = "MAVLinkSerialPort";
            bgdata.IsBackground = true;
            bgdata.Start();
        }
Ejemplo n.º 4
0
        private void clientdata(IAsyncResult ar)
        {
            var client = ((UdpClient)ar.AsyncState);

            if (client == null || client.Client == null)
            {
                return;
            }
            try
            {
                var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;

                var udpclient = new UdpSerial(client);

                var mav = new MAVLinkInterface();
                mav.BaseStream = udpclient;

                MainV2.comPort = mav;
                MainV2.Comports.Add(mav);

                //MainV2.instance.doConnect(mav, "preset", port.ToString());
                Log.Warning("", "mav init " + mav.ToString());
                var hb = mav.getHeartBeat();
                Log.Warning("", "getHeartBeat " + hb.ToString());
                mav.setAPType(mav.MAV.sysid, mav.MAV.compid);
                Log.Warning("", "setAPType " + mav.MAV.ToJSON());


                Forms.Device.BeginInvokeOnMainThread(() =>
                {
                });

                Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            while (mav.BaseStream.BytesToRead < 10 || mav.giveComport == true)
                            {
                                Thread.Sleep(20);
                            }

                            var packet = mav.readPacket();

                            mav.MAV.cs.UpdateCurrentSettings(null);
                        }
                        catch (Exception ex)
                        {
                            Log.Warning("", ex.ToString());
                            Thread.Sleep(10);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Log.Warning("", ex.ToString());
            }
        }
Ejemplo n.º 5
0
        public static void doUpload(string file)
        {
            if (!validcred)
            {
                doUserAndPassword();
            }

            string droneshareusername = MainV2.getConfig("droneshareusername");

            string dronesharepassword = MainV2.getConfig("dronesharepassword");

            if (dronesharepassword != "")
            {
                try
                {
                    // fail on bad entry
                    var crypto = new Crypto();
                    dronesharepassword = crypto.DecryptString(dronesharepassword);
                }
                catch { }
            }

            MAVLinkInterface mav = new MAVLinkInterface();

            mav.BaseStream          = new Comms.CommsFile();
            mav.BaseStream.PortName = file;
            mav.BaseStream.Open();
            if (mav.getHeartBeat().Length == 0)
            {
                CustomMessageBox.Show("Invalid log");
                return;
            }
            mav.Close();

            string viewurl = Utilities.DroneApi.droneshare.doUpload(file, droneshareusername, dronesharepassword, mav.MAV.Guid, Utilities.DroneApi.APIConstants.apiKey);

            if (viewurl != "")
            {
                try
                {
                    validcred = true;
                    System.Diagnostics.Process.Start(viewurl);
                }
                catch (Exception ex) { log.Error(ex); CustomMessageBox.Show("Failed to open url " + viewurl); }
            }
        }
Ejemplo n.º 6
0
        static void ATmainloop()
        {
            ATthreadrun = true;

            DowdingPlugin.UpdateOutput += DowdingPlugin_UpdateOutput;

            try
            {
                mavlink            = new MAVLinkInterface();
                mavlink.BaseStream = ATStream;
                mavlink.getHeartBeat();
            }
            catch
            {
                ATthreadrun = false;
                CustomMessageBox.Show("Failed to detect antenna tracker");
            }

            starttime = DateTime.Now;

            while (ATthreadrun)
            {
                try
                {
                    while (mavlink.giveComport)
                    {
                        Thread.Sleep(2);
                    }

                    mavlink.readPacket();
                }
                catch
                {
                    Thread.Sleep(2);
                }
            }

            DowdingPlugin.UpdateOutput -= DowdingPlugin_UpdateOutput;
        }
Ejemplo n.º 7
0
        public MAVLinkSerialPort(MAVLinkInterface mavint, MAVLink.SERIAL_CONTROL_DEV port)
        {
            this.mavint = mavint;
            this.port   = port;

            if (!mavint.BaseStream.IsOpen)
            {
                mavint.BaseStream.Open();
            }

            if (mavint.getHeartBeat().Length == 0)
            {
                throw new Exception("No valid heartbeats read from port");
            }

            if (subscription.Value != null)
            {
                mavint.UnSubscribeToPacketType(subscription);
            }

            subscription = mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.SERIAL_CONTROL, ReceviedPacket, true);
        }
Ejemplo n.º 8
0
        public static void doUpload(string file)
        {
            doUserAndPassword();

            string droneshareusername = MainV2.getConfig("droneshareusername");

            string dronesharepassword = MainV2.getConfig("dronesharepassword");

            if (dronesharepassword != "")
            {
                try
                {
                    // fail on bad entry
                    var crypto = new Crypto();
                    dronesharepassword = crypto.DecryptString(dronesharepassword);
                }
                catch { }
            }

            MAVLinkInterface mav = new MAVLinkInterface();
            mav.BaseStream = new Comms.CommsFile();
            mav.BaseStream.PortName = file;
            mav.getHeartBeat();
            mav.Close();

            string viewurl = Utilities.DroneApi.droneshare.doUpload(file, droneshareusername, dronesharepassword, mav.MAV.Guid , Utilities.DroneApi.APIConstants.apiKey);

            if (viewurl != "")
            {
                try
                {
                    System.Diagnostics.Process.Start(viewurl);
                }
                catch (Exception ex) { log.Error(ex); CustomMessageBox.Show("Failed to open url " + viewurl); }
            }
        }
Ejemplo n.º 9
0
        void processbg(object directory)
        {
            //this.Invoke((MethodInvoker)delegate { objectListView1.Clear(); });            

            string[] files1 = Directory.GetFiles(directory.ToString(), "*.tlog", SearchOption.AllDirectories);
            string[] files2 = Directory.GetFiles(directory.ToString(), "*.bin", SearchOption.AllDirectories);
            string[] files3 = Directory.GetFiles(directory.ToString(), "*.log", SearchOption.AllDirectories);

            List<string> files = new List<string>();

            files.AddRange(files1);
            files.AddRange(files2);
            files.AddRange(files3);

            //objectListView1.VirtualListSize = files.Length;

            List<object> logs = new List<object>();

            foreach (var file in files)
            {
                if (!File.Exists(file + ".jpg"))
                {
                    LogMap.MapLogs(new string[] {file});
                }

                var loginfo = new loginfo();

                loginfo.fullname = file;

                if (File.Exists(file + ".jpg"))
                {
                    loginfo.img = new Bitmap(file + ".jpg");
                }

                if (file.ToLower().EndsWith(".tlog"))
                {
                    using (MAVLinkInterface mine = new MAVLinkInterface())
                    {
                        try
                        {
                            mine.logplaybackfile =
                                new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                        }
                        catch (Exception ex)
                        {
                            log.Debug(ex.ToString());
                            CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                            return;
                        }
                        mine.logreadmode = true;

                        mine.MAV.packets.Initialize(); // clear

                        mine.getHeartBeat();

                        loginfo.Date = mine.lastlogread;
                        loginfo.Aircraft = mine.sysidcurrent;

                        var start = mine.lastlogread;

                        try
                        {
                            mine.logplaybackfile.BaseStream.Seek(-100000, SeekOrigin.End);
                        }
                        catch
                        {
                        }

                        var end = mine.lastlogread;

                        while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                        {
                            mine.readPacket();

                            if (mine.lastlogread > end)
                                end = mine.lastlogread;
                        }

                        loginfo.Duration = (end - start).ToString();
                    }
                }

                objectListView1.AddObject(loginfo);

                logs.Add(loginfo);
            }
            /*
            this.Invoke((MethodInvoker)delegate
            {
                objectListView1.AddObjects(logs);
            });
             */
        }
Ejemplo n.º 10
0
        void processbg(string file)
        {
            if (!File.Exists(file + ".jpg"))
            {
                LogMap.MapLogs(new string[] { file });
            }

            var loginfo = new loginfo();

            loginfo.fullname = file;

            loginfo.Size = new FileInfo(file).Length;

            if (File.Exists(file + ".jpg"))
            {
                loginfo.img = new Bitmap(file + ".jpg");
            }

            if (file.ToLower().EndsWith(".tlog"))
            {
                using (MAVLinkInterface mine = new MAVLinkInterface())
                {
                    try
                    {
                        mine.logplaybackfile =
                            new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                        return;
                    }
                    mine.logreadmode   = true;
                    mine.speechenabled = false;

                    mine.getHeartBeat();

                    loginfo.Date     = mine.lastlogread;
                    loginfo.Aircraft = mine.sysidcurrent;

                    var start = mine.lastlogread;

                    try
                    {
                        if (mine.logplaybackfile.BaseStream.Length > MaxPlaybackLogSize)
                        {
                            mine.logplaybackfile.BaseStream.Seek(-100000, SeekOrigin.End);
                        }
                        else
                        {
                            mine.logplaybackfile.BaseStream.Seek(0, SeekOrigin.Begin);
                        }
                    }
                    catch
                    {
                    }

                    var end = mine.lastlogread;

                    while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                    {
                        mine.readPacket();

                        if (mine.lastlogread > end)
                        {
                            end = mine.lastlogread;
                        }
                    }

                    loginfo.Duration = (end - start).ToString();
                }
            }

            logs.Add(loginfo);
        }
Ejemplo n.º 11
0
        public static void doUpload(string file)
        {
            doUserAndPassword();

            string droneshareusername = MainV2.getConfig("droneshareusername");

            string dronesharepassword = MainV2.getConfig("dronesharepassword");

            if (dronesharepassword != "")
            {
                try
                {
                    // fail on bad entry
                    var crypto = new Crypto();
                    dronesharepassword = crypto.DecryptString(dronesharepassword);
                }
                catch { }
            }

            MAVLinkInterface mav = new MAVLinkInterface();
            mav.BaseStream = new Comms.CommsFile();
            mav.BaseStream.PortName = file;
            mav.getHeartBeat();
            mav.Close();

            string guid = Guid.NewGuid().ToString();

            switch (mav.MAV.cs.firmware)
            {
                case MainV2.Firmwares.ArduCopter2:
                    guid = MainV2.config["copter_guid"].ToString();
                    break;
                case MainV2.Firmwares.ArduPlane:
                    guid = MainV2.config["plane_guid"].ToString();
                    break;
                case MainV2.Firmwares.ArduRover:
                    guid = MainV2.config["rover_guid"].ToString();
                    break;
            }


            string viewurl = Utilities.droneshare.doUpload(file, droneshareusername, dronesharepassword, guid, Utilities.droneshare.APIConstants.apiKey);

            if (viewurl != "")
            {
                try
                {
                    System.Diagnostics.Process.Start(viewurl);
                }
                catch (Exception ex) { CustomMessageBox.Show("Failed to open url "+ viewurl); }
            }
        }
Ejemplo n.º 12
0
        void processbg(string file)
        {
            a++;
            Loading.ShowLoading(a + "/" + files.Count + " " + file, this);

            if (!File.Exists(file + ".jpg"))
            {
                LogMap.MapLogs(new string[] { file });
            }

            var loginfo = new loginfo();

            loginfo.fullname = file;

            try
            {
                // file not found exception even though it passes the exists check above.
                loginfo.Size = new FileInfo(file).Length;
            }
            catch
            {
            }

            if (File.Exists(file + ".jpg"))
            {
                loginfo.imgfile = file + ".jpg";
            }

            if (file.ToLower().EndsWith(".tlog"))
            {
                using (MAVLinkInterface mine = new MAVLinkInterface())
                {
                    try
                    {
                        mine.logplaybackfile =
                            new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                        return;
                    }
                    mine.logreadmode   = true;
                    mine.speechenabled = false;

                    // file is to small
                    if (mine.logplaybackfile.BaseStream.Length < 1024 * 4)
                    {
                        return;
                    }

                    mine.getHeartBeat();

                    loginfo.Date     = mine.lastlogread;
                    loginfo.Aircraft = mine.sysidcurrent;

                    loginfo.Frame = mine.MAV.aptype.ToString();

                    var start = mine.lastlogread;

                    try
                    {
                        mine.logplaybackfile.BaseStream.Seek(0, SeekOrigin.Begin);
                    }
                    catch
                    {
                    }

                    var end = mine.lastlogread;

                    var length = mine.logplaybackfile.BaseStream.Length;

                    var a = 0;

                    // abandon last 100 bytes
                    while (mine.logplaybackfile.BaseStream.Position < (length - 100))
                    {
                        var packet = mine.readPacket();

                        // gcs
                        if (packet.sysid == 255)
                        {
                            continue;
                        }

                        if (packet.msgid == (uint)MAVLink.MAVLINK_MSG_ID.CAMERA_FEEDBACK)
                        {
                            loginfo.CamMSG++;
                        }

                        if (a % 10 == 0)
                        {
                            mine.MAV.cs.UpdateCurrentSettings(null, true, mine);
                        }

                        a++;

                        if (mine.lastlogread > end)
                        {
                            end = mine.lastlogread;
                        }
                    }

                    loginfo.Home = mine.MAV.cs.Location;

                    loginfo.TimeInAir = mine.MAV.cs.timeInAir;

                    loginfo.DistTraveled = mine.MAV.cs.distTraveled;

                    loginfo.Duration = (end - start).ToString();
                }
            }
            else if (file.ToLower().EndsWith(".bin") || file.ToLower().EndsWith(".log"))
            {
                using (DFLogBuffer colbuf = new DFLogBuffer(File.OpenRead(file)))
                {
                    PointLatLngAlt lastpos = null;
                    DateTime       start   = DateTime.MinValue;
                    DateTime       end     = DateTime.MinValue;
                    DateTime       tia     = DateTime.MinValue;
                    // set time in air/home/distancetraveled
                    foreach (var dfItem in colbuf.GetEnumeratorType("GPS"))
                    {
                        if (dfItem["Status"] != null)
                        {
                            var status = int.Parse(dfItem["Status"]);
                            if (status >= 3)
                            {
                                var pos = new PointLatLngAlt(
                                    double.Parse(dfItem["Lat"], CultureInfo.InvariantCulture),
                                    double.Parse(dfItem["Lng"], CultureInfo.InvariantCulture),
                                    double.Parse(dfItem["Alt"], CultureInfo.InvariantCulture));

                                if (lastpos == null)
                                {
                                    lastpos = pos;
                                }

                                if (start == DateTime.MinValue)
                                {
                                    loginfo.Date = dfItem.time;
                                    start        = dfItem.time;
                                }

                                end = dfItem.time;

                                // add distance
                                loginfo.DistTraveled += (float)lastpos.GetDistance(pos);

                                // set home
                                if (loginfo.Home == null)
                                {
                                    loginfo.Home = pos;
                                }

                                if (dfItem.time > tia.AddSeconds(1))
                                {
                                    // ground speed  > 0.2 or  alt > homelat+2
                                    if (double.Parse(dfItem["Spd"], CultureInfo.InvariantCulture) > 0.2 ||
                                        pos.Alt > (loginfo.Home.Alt + 2))
                                    {
                                        loginfo.TimeInAir++;
                                    }
                                    tia = dfItem.time;
                                }
                            }
                        }
                    }

                    loginfo.Duration = (end - start).ToString();

                    loginfo.CamMSG = colbuf.GetEnumeratorType("CAM").Count();

                    loginfo.Aircraft = 0;      //colbuf.dflog.param[""];

                    loginfo.Frame = "Unknown"; //mine.MAV.aptype.ToString();
                }
            }

            lock (logs)
                logs.Add(loginfo);
        }
Ejemplo n.º 13
0
        public static void SortLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                FileInfo info = new FileInfo(logfile);

                if (info.Length == 0)
                {
                    try
                    {
                        File.Delete(logfile);
                    }
                    catch { }
                    continue;
                }

                if (info.Length <= 1024)
                {
                    try
                    {
                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                                         + "SMALL" + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                        {
                            Directory.CreateDirectory(destdir);
                        }

                        File.Move(logfile, destdir + Path.GetFileName(logfile));
                        File.Move(logfile.Replace(".tlog", ".rlog"), destdir + Path.GetFileName(logfile).Replace(".tlog", ".rlog"));
                    }
                    catch { }
                    continue;
                }

                MAVLinkInterface mine = new MAVLinkInterface();

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

                        byte[] hbpacket = mine.getHeartBeat();

                        if (hbpacket.Length == 0)
                        {
                            mine.logreadmode = false;
                            mine.logplaybackfile.Close();

                            if (!Directory.Exists(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD"))
                            {
                                Directory.CreateDirectory(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD");
                            }

                            File.Move(logfile, Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar + Path.GetFileName(logfile));
                            continue;
                        }

                        MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket);

                        mine.logreadmode = false;
                        mine.logplaybackfile.Close();

                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                                         + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                                         + hbpacket[3] + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                        {
                            Directory.CreateDirectory(destdir);
                        }

                        File.Move(logfile, destdir + Path.GetFileName(logfile));

                        try
                        {
                            File.Move(logfile.Replace(".tlog", ".rlog"), destdir + Path.GetFileName(logfile).Replace(".tlog", ".rlog"));
                        }
                        catch { }
                    }
                }
                catch { continue; }
            }
        }
Ejemplo n.º 14
0
        public static void SortLogs(string[] logs, string masterdestdir = "")
        {
            foreach (var logfile in logs)
            {
                if (masterdestdir == "")
                {
                    masterdestdir = Path.GetDirectoryName(logfile);
                }

                issitl = false;

                FileInfo info = new FileInfo(logfile);

                // delete 0 size files
                if (info.Length == 0)
                {
                    try
                    {
                        File.Delete(logfile);
                    }
                    catch
                    {
                    }
                    continue;
                }

                // move small logs - most likerly invalid
                if (info.Length <= 1024)
                {
                    try
                    {
                        string destdir = masterdestdir + Path.DirectorySeparatorChar
                                         + "SMALL" + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                        {
                            Directory.CreateDirectory(destdir);
                        }

                        log.Info("Move log small " + logfile + " to " + destdir + Path.GetFileName(logfile));

                        movefileusingmask(logfile, destdir);
                    }
                    catch
                    {
                    }
                    continue;
                }

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

                            var midpoint = mine.logplaybackfile.BaseStream.Length / 2;

                            mine.logplaybackfile.BaseStream.Seek(midpoint, SeekOrigin.Begin);

                            // used for sitl detection
                            mine.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.SIMSTATE, sitldetection);
                            mine.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.SIM_STATE, sitldetection);

                            MAVLink.MAVLinkMessage hbpacket  = mine.getHeartBeat();
                            MAVLink.MAVLinkMessage hbpacket1 = mine.getHeartBeat();
                            MAVLink.MAVLinkMessage hbpacket2 = mine.getHeartBeat();
                            MAVLink.MAVLinkMessage hbpacket3 = mine.getHeartBeat();

                            if (hbpacket.Length == 0 && hbpacket1.Length == 0 && hbpacket2.Length == 0 && hbpacket3.Length == 0)
                            {
                                mine.logreadmode = false;
                                mine.logplaybackfile.Close();

                                if (!Directory.Exists(masterdestdir + Path.DirectorySeparatorChar + "BAD"))
                                {
                                    Directory.CreateDirectory(masterdestdir + Path.DirectorySeparatorChar +
                                                              "BAD");
                                }

                                log.Info("Move log bad " + logfile + " to " + masterdestdir +
                                         Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar +
                                         Path.GetFileName(logfile));

                                movefileusingmask(logfile,
                                                  masterdestdir + Path.DirectorySeparatorChar + "BAD" +
                                                  Path.DirectorySeparatorChar);
                                continue;
                            }

                            if (hbpacket.Length != 0)
                            {
                                MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket);
                            }

                            if (hbpacket1.Length != 0)
                            {
                                MAVLink.mavlink_heartbeat_t hb1 = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket1);
                            }

                            if (hbpacket2.Length != 0)
                            {
                                MAVLink.mavlink_heartbeat_t hb2 = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket2);
                            }

                            if (hbpacket3.Length != 0)
                            {
                                MAVLink.mavlink_heartbeat_t hb3 = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket3);
                            }

                            // find most appropriate
                            if (mine.MAVlist.Count > 1)
                            {
                                foreach (var mav in mine.MAVlist)
                                {
                                    if (mav.aptype == MAVLink.MAV_TYPE.ANTENNA_TRACKER)
                                    {
                                        continue;
                                    }
                                    if (mav.aptype == MAVLink.MAV_TYPE.GCS)
                                    {
                                        continue;
                                    }

                                    mine.sysidcurrent  = mav.sysid;
                                    mine.compidcurrent = mav.compid;
                                }
                            }

                            mine.logreadmode = false;
                            mine.logplaybackfile.Close();

                            string destdir = masterdestdir + Path.DirectorySeparatorChar
                                             + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                                             + mine.MAV.sysid + Path.DirectorySeparatorChar;

                            if (issitl)
                            {
                                destdir = masterdestdir + Path.DirectorySeparatorChar
                                          + "SITL" + Path.DirectorySeparatorChar
                                          + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                                          + mine.MAV.sysid + Path.DirectorySeparatorChar;
                            }

                            if (!Directory.Exists(destdir))
                            {
                                Directory.CreateDirectory(destdir);
                            }

                            movefileusingmask(logfile, destdir);
                        }
                }
                catch
                {
                    continue;
                }
            }
        }
Ejemplo n.º 15
0
        private void FWUpload(object?a)
        {
            try
            {
                Tuple <int, string> passin = (Tuple <int, string>)a;
                var index     = passin.Item1;
                var portname  = passin.Item2;
                var tryupload = true;

                lock (ports)
                    if (ports.Contains(portname))
                    {
                        tryupload = false;
                    }

                if (tryupload)
                {
                    var      fw = Firmware.ProcessFirmware(_args[0]);
                    Uploader up = null;
                    try
                    {
                        up = new px4uploader.Uploader(new SerialPort(portname, 115200));
                    }
                    catch
                    {
                    }

                    if (up != null)
                    {
                        up.ProgressEvent += completed =>
                        {
                            /* this.BeginInvoke(new Action(() =>
                             * {
                             *   textBox1.AppendText(portname + " " + (int)completed + "\r\n");
                             * }));*/
                        };

                        bool flashit    = false;
                        bool validcomms = false;
                        try
                        {
                            up.identify();
                            validcomms = true;
                            up.currentChecksum(fw);
                            flashit = true;
                        }
                        catch (Exception ex)
                        {
                            AppendLine(ex.Message);
                        }

                        if (flashit)
                        {
                            this.BeginInvoke(new Action(() => { textBox1.AppendText(portname + " Flashing fw\r\n"); }));
                            up.upload(fw);
                        }
                        else
                        {
                            this.BeginInvoke(new Action(() => { textBox1.AppendText(portname + " reboot\r\n"); }));
                            up.__reboot();
                        }

                        lock (ports)
                            ports.Add(portname);
                        up.close();
                        this.BeginInvoke(new Action(() => { textBox1.AppendText(portname + " Done fw\r\n"); }));
                        if (validcomms)
                        {
                            return;
                        }
                    }
                }

                var mav = new MAVLinkInterface();
                mav.BaseStream = new SerialPort((string)portname, 115200);
                var bldone = false;
                mav.OnPacketReceived += (sender, message) =>
                {
                    if (message.msgid == (ulong)MAVLink.MAVLINK_MSG_ID.STATUSTEXT)
                    {
                        if (ASCIIEncoding.ASCII.GetString(message.ToStructure <MAVLink.mavlink_statustext_t>().text).Trim().Contains("Bootloader up-to-date") ||
                            ASCIIEncoding.ASCII.GetString(message.ToStructure <MAVLink.mavlink_statustext_t>().text).Trim().Contains("Flash OK"))
                        {
                            bldone = true;
                        }
                        DisplayText(message);
                    }
                };
                try
                {
                    mav.BaseStream.Open();

                    mav.getHeartBeat();

                    mav.doCommand(MAVLink.MAV_CMD.FLASH_BOOTLOADER, 0, 0, 0, 0, 290876, 0, 0, false);

                    mav.getHeartBeat();
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }

                    mav.Close();


                    this.BeginInvoke(new Action(() =>
                    {
                        textBox1.AppendText(portname + " Done bl " + (bldone ? "OK" : "FAIL") + "\r\n");
                    }));
                }
                catch (Exception ex)
                {
                    AppendLine(ex.Message);
                }
            }
            catch (Exception ex)
            {
                AppendLine(ex.Message);
            }
        }
Ejemplo n.º 16
0
        void processbg(string file)
        {
            if (!File.Exists(file + ".jpg"))
            {
                LogMap.MapLogs(new string[] {file});
            }

            var loginfo = new loginfo();

            loginfo.fullname = file;

            loginfo.Size = new FileInfo(file).Length;

            if (File.Exists(file + ".jpg"))
            {
                loginfo.img = new Bitmap(file + ".jpg");
            }

            if (file.ToLower().EndsWith(".tlog"))
            {
                using (MAVLinkInterface mine = new MAVLinkInterface())
                {
                    try
                    {
                        mine.logplaybackfile =
                            new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                        return;
                    }
                    mine.logreadmode = true;

                    mine.getHeartBeat();

                    loginfo.Date = mine.lastlogread;
                    loginfo.Aircraft = mine.sysidcurrent;

                    var start = mine.lastlogread;

                    try
                    {
                        if (mine.logplaybackfile.BaseStream.Length > MaxPlaybackLogSize)
                        {
                            mine.logplaybackfile.BaseStream.Seek(-100000, SeekOrigin.End);
                        }
                        else
                        {
                            mine.logplaybackfile.BaseStream.Seek(0, SeekOrigin.Begin);
                        }
                    }
                    catch
                    {
                    }

                    var end = mine.lastlogread;

                    while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                    {
                        mine.readPacket();

                        if (mine.lastlogread > end)
                            end = mine.lastlogread;
                    }

                    loginfo.Duration = (end - start).ToString();
                }
            }

            logs.Add(loginfo);
        }
Ejemplo n.º 17
0
        public static void SortLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                FileInfo info = new FileInfo(logfile);

                // delete 0 size files
                if (info.Length == 0)
                {
                    try
                    {
                        File.Delete(logfile);
                    }
                    catch { }
                    continue;
                }

                // move small logs - most likerly invalid
                if (info.Length <= 1024)
                {
                    try
                    {
                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                                         + "SMALL" + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                        {
                            Directory.CreateDirectory(destdir);
                        }

                        log.Info("Move log small " + logfile + " to " + destdir + Path.GetFileName(logfile));

                        movefileusingmask(logfile, destdir);
                    }
                    catch { }
                    continue;
                }

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

                            byte[] hbpacket = mine.getHeartBeat();

                            if (hbpacket.Length == 0)
                            {
                                mine.logreadmode = false;
                                mine.logplaybackfile.Close();

                                if (!Directory.Exists(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD"))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD");
                                }

                                log.Info("Move log bad " + logfile + " to " + Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar + Path.GetFileName(logfile));

                                movefileusingmask(logfile, Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar);
                                continue;
                            }

                            MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket);

                            mine.logreadmode = false;
                            mine.logplaybackfile.Close();

                            string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                                             + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                                             + hbpacket[3] + Path.DirectorySeparatorChar;

                            if (!Directory.Exists(destdir))
                            {
                                Directory.CreateDirectory(destdir);
                            }

                            movefileusingmask(logfile, destdir);
                        }
                }
                catch { continue; }
            }
        }
Ejemplo n.º 18
0
        public static void SortLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                FileInfo info = new FileInfo(logfile);

                // delete 0 size files
                if (info.Length == 0)
                {
                    try
                    {
                        File.Delete(logfile);
                    }
                    catch
                    {
                    }
                    continue;
                }

                // move small logs - most likerly invalid
                if (info.Length <= 1024)
                {
                    try
                    {
                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                                         + "SMALL" + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                            Directory.CreateDirectory(destdir);

                        log.Info("Move log small " + logfile + " to " + destdir + Path.GetFileName(logfile));

                        movefileusingmask(logfile, destdir);
                    }
                    catch
                    {
                    }
                    continue;
                }

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

                        var midpoint = mine.logplaybackfile.BaseStream.Length / 2;

                        mine.logplaybackfile.BaseStream.Seek(midpoint, SeekOrigin.Begin);

                        MAVLink.MAVLinkMessage hbpacket = mine.getHeartBeat();
                        MAVLink.MAVLinkMessage hbpacket1 = mine.getHeartBeat();
                        MAVLink.MAVLinkMessage hbpacket2 = mine.getHeartBeat();
                        MAVLink.MAVLinkMessage hbpacket3 = mine.getHeartBeat();

                        if (hbpacket.Length == 0 && hbpacket1.Length == 0 && hbpacket2.Length == 0 && hbpacket3.Length == 0)
                        {
                            mine.logreadmode = false;
                            mine.logplaybackfile.Close();

                            if (!Directory.Exists(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD"))
                                Directory.CreateDirectory(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar +
                                                          "BAD");

                            log.Info("Move log bad " + logfile + " to " + Path.GetDirectoryName(logfile) +
                                     Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar +
                                     Path.GetFileName(logfile));

                            movefileusingmask(logfile,
                                Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD" +
                                Path.DirectorySeparatorChar);
                            continue;
                        }

                        if (hbpacket.Length != 0)
                        {
                            MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t) mine.DebugPacket(hbpacket);
                        }

                        if (hbpacket1.Length != 0)
                        {
                            MAVLink.mavlink_heartbeat_t hb1 = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket1);
                        }

                        if (hbpacket2.Length != 0)
                        {
                            MAVLink.mavlink_heartbeat_t hb2 = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket2);
                        }

                        if (hbpacket3.Length != 0)
                        {
                            MAVLink.mavlink_heartbeat_t hb3 = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket3);
                        }

                        // find most appropriate
                        if (mine.MAVlist.Count > 1)
                        {
                            foreach (var mav in mine.MAVlist.GetMAVStates())
                            {
                                if (mav.aptype == MAVLink.MAV_TYPE.ANTENNA_TRACKER)
                                    continue;
                                if (mav.aptype == MAVLink.MAV_TYPE.GCS)
                                    continue;

                                mine.sysidcurrent = mav.sysid;
                                mine.compidcurrent = mav.compid;
                            }
                        }

                        mine.logreadmode = false;
                        mine.logplaybackfile.Close();

                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                                         + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                                         + mine.MAV.sysid + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                            Directory.CreateDirectory(destdir);

                        movefileusingmask(logfile, destdir);
                    }
                }
                catch
                {
                    continue;
                }
            }
        }
Ejemplo n.º 19
0
        void processbg(string file)
        {
            Loading.ShowLoading(file, this);

            if (!File.Exists(file + ".jpg"))
            {
                LogMap.MapLogs(new string[] {file});
            }

            var loginfo = new loginfo();

            loginfo.fullname = file;

            loginfo.Size = new FileInfo(file).Length;

            if (File.Exists(file + ".jpg"))
            {
                loginfo.img = new Bitmap(file + ".jpg");
            }

            if (file.ToLower().EndsWith(".tlog"))
            {
                using (MAVLinkInterface mine = new MAVLinkInterface())
                {
                    try
                    {
                        mine.logplaybackfile =
                            new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                        return;
                    }
                    mine.logreadmode = true;
                    mine.speechenabled = false;

                    // file is to small
                    if (mine.logplaybackfile.BaseStream.Length < 1024*4)
                        return;

                    mine.getHeartBeat();

                    loginfo.Date = mine.lastlogread;
                    loginfo.Aircraft = mine.sysidcurrent;

                    loginfo.Frame = mine.MAV.aptype.ToString();

                    var start = mine.lastlogread;

                    try
                    {
                        mine.logplaybackfile.BaseStream.Seek(0, SeekOrigin.Begin);
                    }
                    catch
                    {
                    }

                    var end = mine.lastlogread;

                    var length = mine.logplaybackfile.BaseStream.Length;

                    var a = 0;

                    // abandon last 100 bytes
                    while (mine.logplaybackfile.BaseStream.Position < (length-100))
                    {
                        var packet = mine.readPacket();

                        // gcs
                        if(packet.sysid == 255)
                            continue;

                        if(a % 10 == 0)
                            mine.MAV.cs.UpdateCurrentSettings(null, true, mine);

                        a++;

                        if (mine.lastlogread > end)
                            end = mine.lastlogread;
                    }

                    loginfo.Home = mine.MAV.cs.Location;

                    loginfo.TimeInAir = mine.MAV.cs.timeInAir;

                    loginfo.DistTraveled = mine.MAV.cs.distTraveled;

                    loginfo.Duration = (end - start).ToString();
                }
            }

            logs.Add(loginfo);
        }
Ejemplo n.º 20
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;

            if (args.Length < 2)
            {
                Console.WriteLine("usage: UAVCANFlasher com1 flow-crc.bin");
                Console.WriteLine("this program will flash the firmware to every connected uavcan node. DONT connect devices that its not intended for");
                Console.ReadKey();
                return;
            }

            var portname      = args[0];
            var firmware_name = args[1];

            var port = new MissionPlanner.Comms.SerialPort(args[0], 115200);

            port.Open();

            if (!File.Exists(firmware_name))
            {
                Console.WriteLine("firmware file not found");
                Console.ReadKey();
                return;
            }

            MAVLinkInterface mav = new MAVLinkInterface();

            mav.BaseStream = port;
            mav.getHeartBeat();

            try
            {
                mav.GetParam((byte)mav.sysidcurrent, (byte)mav.compidcurrent, "CAN_SLCAN_TIMOUT");
                mav.GetParam((byte)mav.sysidcurrent, (byte)mav.compidcurrent, "CAN_SLCAN_SERNUM");

                mav.setParam("CAN_SLCAN_TIMOUT", 2, true);

                mav.setParam("CAN_SLCAN_SERNUM", 0, true); // usb
            }
            catch { }

            DroneCAN.DroneCAN can = new DroneCAN.DroneCAN();

            can.SourceNode = 127;

            //can.Update();


            // updater
            var       firmware_namebytes = ASCIIEncoding.ASCII.GetBytes(Path.GetFileName(firmware_name.ToLower()));
            ulong     firmware_crc       = ulong.MaxValue;
            Exception exception          = null;
            Dictionary <int, DateTime> lastseenDateTimes = new Dictionary <int, DateTime>();

            DroneCAN.DroneCAN.MessageRecievedDel updatedelegate = (frame, msg, transferID) =>
            {
                if (frame.IsServiceMsg && frame.SvcDestinationNode != can.SourceNode)
                {
                    return;
                }

                if (frame.SourceNode == 0)
                {
                    return;
                }

                lastseenDateTimes[frame.SourceNode] = DateTime.Now;

                if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_file_BeginFirmwareUpdate_res))
                {
                    var bfures = msg as DroneCAN.DroneCAN.uavcan_protocol_file_BeginFirmwareUpdate_res;
                    if (bfures.error != 0)
                    {
                        exception = new Exception(frame.SourceNode + " Begin Firmware Update returned an error");
                    }
                }
                else if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_res))
                {
                    var gnires = msg as DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_res;
                    Console.WriteLine("GetNodeInfo: seen '{0}' from {1}",
                                      ASCIIEncoding.ASCII.GetString(gnires.name).TrimEnd('\0'), frame.SourceNode);
                    if (firmware_crc != gnires.software_version.image_crc || firmware_crc == ulong.MaxValue)
                    {
                        if (gnires.status.mode != DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_MODE_SOFTWARE_UPDATE)
                        {
                            Console.WriteLine("Update node " + frame.SourceNode);
                            var req_msg =
                                new DroneCAN.DroneCAN.uavcan_protocol_file_BeginFirmwareUpdate_req()
                            {
                                image_file_remote_path = new DroneCAN.DroneCAN.uavcan_protocol_file_Path()
                                {
                                    path = firmware_namebytes
                                },
                                source_node_id = can.SourceNode
                            };
                            req_msg.image_file_remote_path.path_len = (byte)firmware_namebytes.Length;

                            var slcan = can.PackageMessageSLCAN(frame.SourceNode, frame.Priority, transferID, req_msg);
                            can.WriteToStreamSLCAN(slcan);
                        }
                        else
                        {
                            //exception = new Exception("already in update mode");
                            return;
                        }
                    }
                    else
                    {
                        Console.WriteLine(frame.SourceNode + " CRC matchs");
                    }
                }
            };
            can.MessageReceived += updatedelegate;

            // getfile crc
            using (var stream = File.OpenRead(firmware_name))
            {
                string app_descriptor_fmt = "<8cQI";
                var    SHARED_APP_DESCRIPTOR_SIGNATURES = new byte[][] {
                    new byte[] { 0xd7, 0xe4, 0xf7, 0xba, 0xd0, 0x0f, 0x9b, 0xee },
                    new byte[] { 0x40, 0xa2, 0xe4, 0xf1, 0x64, 0x68, 0x91, 0x06 }
                };

                var app_descriptor_len = 8 * 1 + 8 + 4;

                var location = GetPatternPositions(stream, SHARED_APP_DESCRIPTOR_SIGNATURES[0]);
                stream.Seek(0, SeekOrigin.Begin);
                var location2 = GetPatternPositions(stream, SHARED_APP_DESCRIPTOR_SIGNATURES[1]);

                if (location.Count > 0 || location2.Count > 0)
                {
                    var offset = location.Count > 0 ? location[0] : location2[0];
                    stream.Seek(offset, SeekOrigin.Begin);
                    byte[] buf = new byte[app_descriptor_len];
                    stream.Read(buf, 0, app_descriptor_len);
                    firmware_crc = BitConverter.ToUInt64(buf, 8);
                }
            }

            can.NodeAdded += (id, status) =>
            {
                Console.WriteLine(id + " Node status seen");
                // get node info
                DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_req gnireq = new DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_req()
                {
                };

                var slcan = can.PackageMessageSLCAN((byte)id, 30, 0, gnireq);

                can.WriteToStreamSLCAN(slcan);
            };

            DroneCAN.DroneCAN.uavcan_protocol_NodeStatus node;
            can.FileSendComplete += (id, file) =>
            {
                Console.WriteLine(id + " File send complete " + file);

                can.NodeList.TryRemove(id, out node);
                lastseenDateTimes.Remove(id);
            };

            can.StartSLCAN(port.BaseStream);

            can.ServeFile(args[1]);

            can.SetupFileServer();

            can.SetupDynamicNodeAllocator();

            Console.WriteLine("Ready.. " + portname + " -> " + firmware_name);
            run = true;
            while (run)
            {
                Thread.Sleep(100);

                foreach (var lastseenDateTime in lastseenDateTimes.ToArray())
                {
                    if (lastseenDateTime.Value.AddSeconds(3) < DateTime.Now)
                    {
                        can.NodeList.TryRemove(lastseenDateTime.Key, out node);
                        lastseenDateTimes.Remove(lastseenDateTime.Key);
                        Console.WriteLine(lastseenDateTime.Key + " Remove old node - 3 seconds of no data");
                    }
                }

                if (exception != null)
                {
                    Console.WriteLine(exception.ToString());
                    exception = null;
                }
            }
        }
Ejemplo n.º 21
0
        void processbg(object directory)
        {
            //this.Invoke((MethodInvoker)delegate { objectListView1.Clear(); });

            string[] files1 = Directory.GetFiles(directory.ToString(), "*.tlog", SearchOption.AllDirectories);
            string[] files2 = Directory.GetFiles(directory.ToString(), "*.bin", SearchOption.AllDirectories);
            string[] files3 = Directory.GetFiles(directory.ToString(), "*.log", SearchOption.AllDirectories);

            List <string> files = new List <string>();

            files.AddRange(files1);
            files.AddRange(files2);
            files.AddRange(files3);

            //objectListView1.VirtualListSize = files.Length;

            List <object> logs = new List <object>();

            foreach (var file in files)
            {
                if (!File.Exists(file + ".jpg"))
                {
                    LogMap.MapLogs(new string[] { file });
                }

                var loginfo = new loginfo();

                loginfo.fullname = file;

                if (File.Exists(file + ".jpg"))
                {
                    loginfo.img = new Bitmap(file + ".jpg");
                }

                if (file.ToLower().EndsWith(".tlog"))
                {
                    using (MAVLinkInterface mine = new MAVLinkInterface())
                    {
                        try
                        {
                            mine.logplaybackfile =
                                new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                        }
                        catch (Exception ex)
                        {
                            log.Debug(ex.ToString());
                            CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                            return;
                        }
                        mine.logreadmode = true;

                        mine.MAV.packets.Initialize(); // clear

                        mine.getHeartBeat();

                        loginfo.Date     = mine.lastlogread;
                        loginfo.Aircraft = mine.sysidcurrent;

                        var start = mine.lastlogread;

                        try
                        {
                            mine.logplaybackfile.BaseStream.Seek(-100000, SeekOrigin.End);
                        }
                        catch
                        {
                        }

                        var end = mine.lastlogread;

                        while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                        {
                            mine.readPacket();

                            if (mine.lastlogread > end)
                            {
                                end = mine.lastlogread;
                            }
                        }

                        loginfo.Duration = (end - start).ToString();
                    }
                }

                objectListView1.AddObject(loginfo);

                logs.Add(loginfo);
            }

            /*
             * this.Invoke((MethodInvoker)delegate
             * {
             *  objectListView1.AddObjects(logs);
             * });
             */
        }
Ejemplo n.º 22
0
        public static void SortLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                FileInfo info = new FileInfo(logfile);

                // delete 0 size files
                if (info.Length == 0)
                {
                    try
                    {
                        File.Delete(logfile);
                    }
                    catch { }
                    continue;
                }

                // move small logs - most likerly invalid
                if (info.Length <= 1024)
                {
                    try
                    {
                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                 + "SMALL" + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                            Directory.CreateDirectory(destdir);

                        log.Info("Move log small " + logfile + " to " + destdir + Path.GetFileName(logfile));

                        movefileusingmask(logfile, destdir);
                    }
                    catch { }
                    continue;
                }

                MAVLinkInterface mine = new MAVLinkInterface();

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

                        byte[] hbpacket = mine.getHeartBeat();

                        if (hbpacket.Length == 0)
                        {
                            mine.logreadmode = false;
                            mine.logplaybackfile.Close();

                            if (!Directory.Exists(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD"))
                                Directory.CreateDirectory(Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD");

                            log.Info("Move log bad " + logfile + " to " + Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar + Path.GetFileName(logfile));

                            movefileusingmask(logfile, Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar + "BAD" + Path.DirectorySeparatorChar);
                            continue;
                        }

                        MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)mine.DebugPacket(hbpacket);

                        mine.logreadmode = false;
                        mine.logplaybackfile.Close();

                        string destdir = Path.GetDirectoryName(logfile) + Path.DirectorySeparatorChar
                            + mine.MAV.aptype.ToString() + Path.DirectorySeparatorChar
                            + hbpacket[3] + Path.DirectorySeparatorChar;

                        if (!Directory.Exists(destdir))
                            Directory.CreateDirectory(destdir);

                        movefileusingmask(logfile, destdir);
                    }
                }
                catch { continue; }
            }
        }
Ejemplo n.º 23
0
        void processbg(string file)
        {
            Loading.ShowLoading(file, this);

            if (!File.Exists(file + ".jpg"))
            {
                LogMap.MapLogs(new string[] { file });
            }

            var loginfo = new loginfo();

            loginfo.fullname = file;

            loginfo.Size = new FileInfo(file).Length;

            if (File.Exists(file + ".jpg"))
            {
                loginfo.img = new Bitmap(file + ".jpg");
            }

            if (file.ToLower().EndsWith(".tlog"))
            {
                using (MAVLinkInterface mine = new MAVLinkInterface())
                {
                    try
                    {
                        mine.logplaybackfile =
                            new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read));
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        CustomMessageBox.Show("Log Can not be opened. Are you still connected?");
                        return;
                    }
                    mine.logreadmode   = true;
                    mine.speechenabled = false;

                    // file is to small
                    if (mine.logplaybackfile.BaseStream.Length < 1024 * 4)
                    {
                        return;
                    }

                    mine.getHeartBeat();

                    loginfo.Date     = mine.lastlogread;
                    loginfo.Aircraft = mine.sysidcurrent;

                    loginfo.Frame = mine.MAV.aptype.ToString();

                    var start = mine.lastlogread;

                    try
                    {
                        mine.logplaybackfile.BaseStream.Seek(0, SeekOrigin.Begin);
                    }
                    catch
                    {
                    }

                    var end = mine.lastlogread;

                    var length = mine.logplaybackfile.BaseStream.Length;

                    var a = 0;

                    // abandon last 100 bytes
                    while (mine.logplaybackfile.BaseStream.Position < (length - 100))
                    {
                        var packet = mine.readPacket();

                        // gcs
                        if (packet.sysid == 255)
                        {
                            continue;
                        }

                        if (a % 10 == 0)
                        {
                            mine.MAV.cs.UpdateCurrentSettings(null, true, mine);
                        }

                        a++;

                        if (mine.lastlogread > end)
                        {
                            end = mine.lastlogread;
                        }
                    }

                    loginfo.Home = mine.MAV.cs.Location;

                    loginfo.TimeInAir = mine.MAV.cs.timeInAir;

                    loginfo.DistTraveled = mine.MAV.cs.distTraveled;

                    loginfo.Duration = (end - start).ToString();
                }
            }

            logs.Add(loginfo);
        }
Ejemplo n.º 24
0
        private void FWUpload(object a)
        {
            Tuple <int, string> passin = (Tuple <int, string>)a;
            var index    = passin.Item1;
            var portname = passin.Item2;

            Console.WriteLine("Thread start for " + portname);
            try
            {
                if (!portstatus.ContainsKey(portname))
                {
                    portstatus[portname] = "";
                }

                {
                    Uploader up = null;
                    try
                    {
                        up = new px4uploader.Uploader(new SerialPort(portname, 115200));
                    }
                    catch
                    {
                    }

                    if (up != null)
                    {
                        up.ProgressEvent += completed =>
                        {
                            /* this.BeginInvoke(new Action(() =>
                             * {
                             *   textBox1.AppendText(portname + " " + (int)completed + "\r\n");
                             * }));*/
                        };

                        bool     flashit    = false;
                        bool     validcomms = false;
                        Firmware fw         = null;
                        try
                        {
                            up.identify();
                            validcomms            = true;
                            portstatus[portname] += "BL ";
                            UpdateTextBox();
                            fw = Firmware.ProcessFirmware(_args[0]);
                            up.currentChecksum(fw);

                            fw.board_id = up.board_type;

                            flashit = true;
                            portstatus[portname] += "CS ";
                            UpdateTextBox();
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message.Contains("Same Firmware"))
                            {
                                portstatus[portname] += "BLUPD OK ";
                            }
                            else
                            {
                                portstatus[portname] += "BLEX ";
                            }
                            UpdateTextBox();
                        }

                        if (flashit)
                        {
                            DateTime ts = DateTime.Now;
                            up.ProgressEvent += completed =>
                            {
                                if (ts.Second != DateTime.Now.Second)
                                {
                                    Console.WriteLine("{0}: {1}", portname, completed);
                                    ts = DateTime.Now;
                                }
                            };
                            up.upload(fw);
                            portstatus[portname] += "BLUPD OK ";
                            UpdateTextBox();
                        }
                        else
                        {
                            up.__reboot();
                            UpdateTextBox();
                        }

                        up.close();

                        if (validcomms)
                        {
                            return;
                        }
                    }
                }

                var ports = getPortByVPidInt(txt_vid.Text, txt_pid.Text, txt_int.Text);
                if (!ports.Contains(portname))
                {
                    portstatus[portname] += "NOTML OK ";
                    UpdateTextBox();
                    return;
                }

                var mav = new MAVLinkInterface();
                mav.BaseStream = new SerialPort((string)portname, 115200);
                var bldone = false;
                mav.OnPacketReceived += (sender, message) =>
                {
                    if (message.msgid == (ulong)MAVLink.MAVLINK_MSG_ID.STATUSTEXT)
                    {
                        if (ASCIIEncoding.ASCII.GetString(message.ToStructure <MAVLink.mavlink_statustext_t>().text).Trim().Contains("Bootloader up-to-date") ||
                            ASCIIEncoding.ASCII.GetString(message.ToStructure <MAVLink.mavlink_statustext_t>().text).Trim().Contains("Flash OK"))
                        {
                            bldone = true;

                            portstatus[portname] += ASCIIEncoding.ASCII
                                                    .GetString(message.ToStructure <MAVLink.mavlink_statustext_t>().text).Trim()
                                                    .TrimEnd(new char[] { '\0', '\r', '\n' });
                            UpdateTextBox();
                        }
                        //DisplayText(message);
                    }
                };
                try
                {
                    mav.BaseStream.Open();

                    mav.getHeartBeat();

                    mav.doCommand(MAVLink.MAV_CMD.FLASH_BOOTLOADER, 0, 0, 0, 0, 290876, 0, 0, false);

                    mav.getHeartBeat();
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }
                    if (!bldone)
                    {
                        mav.getHeartBeat();
                    }

                    mav.Close();


                    if (bldone)
                    {
                        portstatus[portname] += "MLBLUPD OK";
                    }

                    UpdateTextBox();
                }
                catch (Exception ex)
                {
                    portstatus[portname] += "MLEX ";

                    UpdateTextBox();
                }
            }
            catch (Exception ex)
            {
                portstatus[portname] += "EX!! ";

                UpdateTextBox();
            }

            UpdateTextBox();
        }