コード例 #1
0
ファイル: TestHub.cs プロジェクト: RokiFoki/DroneWebApp
 public PositionData(Data p)
 {
     X = p.Longitude; // *100000 * 1.1
     Y = p.Latitude; // *100000 *1.1
     Z = p.Altitude;
     Ping = p.pingInMiliseconds / p.pingNumberOfData;
     Ping_loss = p.pingLossPercentige / p.pingNumberOfData;
     Ftp_upload = p.ftpUpload / p.ftpUploadNumberOfData;
     Ftp_download = p.ftpDownload / p.ftpDownloadNumberOfData;
 }
コード例 #2
0
        /// <summary>
        /// This method saves data to database and also changes(fills) the catche
        /// </summary>
        /// <param name="data">data in pairs</param>
        /// 
        /*
         {;testID=0;longitude=45,8031718;latitude=2,0;altitude=3,0;accuracy=99,1337;ftpDownload=8893,714037563783}
         {;pingTime=14,796;pingLossPercentige=0,0;longitude=1,0;latitude=2,0;altitude=3,0;accuracy=99,1337}
         */
        private void saveToDatabase(ICollection<string[]> data)
        {
            Data dataFromAndoid = new Data();
            int testID = -1;

            double ftp_download = -1;
            int ftp_download_counter = 0;
            double ftp_upload = -1;
            int ftp_upload_counter = 0;

            double ping_time = -1;
            double ping_loss_perc = 0;
            int ping_counter = 0;

            //inicjalizacija "data" -> napraviti metodu tvornicu ili obican konstruktor koji prima string?
            foreach (string[] d in data)
            {
                if (d[0].Equals("longitude"))
                {
                    dataFromAndoid.Longitude = Convert.ToInt32(float.Parse(d[1], CultureInfo.InvariantCulture.NumberFormat) * 100000);
                }
                else if (d[0].Equals("latitude"))
                {
                    dataFromAndoid.Latitude = Convert.ToInt32(float.Parse(d[1], CultureInfo.InvariantCulture.NumberFormat) * 100000);
                }
                else if (d[0].Equals("altitude"))
                {
                    dataFromAndoid.Altitude = Convert.ToInt32(float.Parse(d[1], CultureInfo.InvariantCulture.NumberFormat) * 100000);
                }
                else if (d[0].Equals("testID"))
                {
                    testID = Convert.ToInt32(d[1]);
                }
                else if (d[0].Equals("ftpDownload"))
                {
                    ftp_download = 0;
                    foreach (var str in d[1].Split('a'))
                    {
                        ftp_download += float.Parse(str, CultureInfo.InvariantCulture.NumberFormat);
                        ftp_download_counter++;
                    }
                }
                else if (d[0].Equals("ftpUpload"))
                {
                    ftp_upload = 0;
                    foreach (var str in d[1].Split('a'))
                    {
                        ftp_upload += float.Parse(str, CultureInfo.InvariantCulture.NumberFormat);
                        ftp_upload_counter++;
                    }

                }
                else if (d[0].Equals("pingTime"))
                {
                    ping_time = float.Parse(d[1], CultureInfo.InvariantCulture.NumberFormat);
                    ping_counter = 1;
                }
                else if (d[0].Equals("pingLossPercentige"))
                {
                    ping_loss_perc = float.Parse(d[1], CultureInfo.InvariantCulture.NumberFormat);
                    ping_counter = 1;
                }
            }

            Test test = db.Test.Where(o => o.TestID == testID).FirstOrDefault();
            if (test == null)
            {
                throw new Exception("Test not found.");
            }

            // podesavanje koordinata
            dataFromAndoid.Longitude = dataFromAndoid.Longitude - dataFromAndoid.Longitude % precision;
            dataFromAndoid.Latitude = dataFromAndoid.Latitude - dataFromAndoid.Latitude % precision;
            dataFromAndoid.Altitude = dataFromAndoid.Altitude - dataFromAndoid.Altitude % precision;

            // postoji li podatak već u bazi podataka?
            var dataFromDB = db.Data.Where(dat =>
                dat.Test.TestID == testID &&
                    dat.Longitude == dataFromAndoid.Longitude &&
                    dat.Latitude == dataFromAndoid.Latitude &&
                    dat.Altitude == dataFromAndoid.Altitude
            ).FirstOrDefault();

            //ako ne postoji
            if (dataFromDB == null)
            {
                if (ftp_download_counter > 0)
                {
                    dataFromAndoid.ftpDownload = ftp_download;
                    dataFromAndoid.ftpDownloadNumberOfData = 1;
                }

                if (ftp_upload_counter > 0)
                {
                    dataFromAndoid.ftpUpload = ftp_upload;
                    dataFromAndoid.ftpUploadNumberOfData = 1;
                }

                if (ping_counter > 0)
                {
                    dataFromAndoid.pingInMiliseconds = ping_time;
                    dataFromAndoid.pingLossPercentige = ping_loss_perc;
                    dataFromAndoid.pingNumberOfData = ping_counter;
                }
                if (ftp_download_counter + ftp_upload_counter + ping_counter > 0)
                {
                    dataFromAndoid.Test = test;
                    // ubaci
                    db.Data.Add(dataFromAndoid);

                    Broadcaster.Instance.updateWebGL(new PositionData(dataFromAndoid));
                }
            }
            else
            {
                if (ping_counter > 0)
                {
                    /* ping */
                    dataFromDB.pingInMiliseconds =
                        (dataFromDB.pingInMiliseconds * dataFromDB.pingNumberOfData + ping_time) /
                        (dataFromDB.pingNumberOfData + ping_counter);

                    dataFromDB.pingLossPercentige =
                        (dataFromDB.pingLossPercentige * dataFromDB.pingNumberOfData + ping_loss_perc) /
                        (dataFromDB.pingNumberOfData + ping_counter);

                    dataFromDB.pingNumberOfData += ping_counter;
                }

                if (ftp_download_counter > 0)
                {
                    /* ftp Download */
                    dataFromDB.ftpDownload =
                        (dataFromDB.ftpDownload * dataFromDB.ftpDownloadNumberOfData + ftp_download) /
                        (dataFromDB.ftpDownloadNumberOfData + ftp_download_counter);

                    dataFromDB.ftpDownloadNumberOfData += ftp_download_counter;
                    //throw new Exception(dataFromDB.ftpDownloadNumberOfData.ToString());
                }

                if (ftp_upload_counter > 0)
                {
                    /* ftp Upload */
                    dataFromDB.ftpUpload =
                        (dataFromDB.ftpUpload * dataFromDB.ftpUploadNumberOfData + ftp_upload) /
                        (dataFromDB.ftpUploadNumberOfData + ftp_upload_counter);

                    dataFromDB.ftpUploadNumberOfData += ftp_upload_counter;
                }

                db.Entry(dataFromDB).State = EntityState.Modified;
                db.SaveChanges();

            }

            if (ping_time != -1)
            {
                PingData pd = new PingData()
                {
                    Altitude = dataFromAndoid.Altitude,
                    Latitude = dataFromAndoid.Latitude,
                    Longitude = dataFromAndoid.Longitude,
                    PingInMiliseconds = ping_time,
                    PingPercentageLoss = ping_loss_perc,
                    Test = test,
                    Time = DateTime.Now
                };
                db.PingData.Add(pd);

                Broadcaster.Instance.updateSpecificTest("Ping", testID, new Dot(pd.Time, (int)pd.PingInMiliseconds));
                Broadcaster.Instance.updateSpecificTest("Ping Percentage Loss", testID, new Dot(pd.Time, (int)pd.PingPercentageLoss));

            }

            if (ftp_download != -1)
            {
                FTPDownloadData fd = new FTPDownloadData()
                {
                    Altitude = dataFromAndoid.Altitude,
                    Latitude = dataFromAndoid.Latitude,
                    Longitude = dataFromAndoid.Longitude,
                    Speed = ftp_download,
                    Test = test,
                    Time = DateTime.Now
                };
                db.FTPDownloadData.Add(fd);

                Broadcaster.Instance.updateSpecificTest("FTP Download", testID, new Dot(fd.Time, (int)fd.Speed));

            }

            if (ftp_upload != -1)
            {
                FTPUploadData fd = new FTPUploadData()
                {
                    Altitude = dataFromAndoid.Altitude,
                    Latitude = dataFromAndoid.Latitude,
                    Longitude = dataFromAndoid.Longitude,
                    Speed = ftp_upload,
                    Test = test,
                    Time = DateTime.Now
                };
                db.FTPUploadData.Add(fd);

                Broadcaster.Instance.updateSpecificTest("FTP Upload", testID, new Dot(fd.Time, (int)fd.Speed));
            }

            db.SaveChanges();
        }