Ejemplo n.º 1
0
        ////[HttpPost]
        //public JsonResult CalculateGPSTrackLength(List<TrkptModel> trkpts)
        //{
        //    if (!LoginHelper.IsAuthenticated())
        //        return Json(new { success = false, errorCode = 403 });
        //    else if (!LoginHelper.IsCurrentUserAdmin() && !LoginHelper.IsCurrentUserMember())
        //        return Json(new { success = false, errorCode = 403 });

        //    var trkptList = DogTrainingHelper.ConvertTrkptModelListToTrkptList(trkpts);
        //    var length = DogTrainingHelper.CalculateGPSTrackLength(new Trkseg() { Trkpt = trkptList });
        //    return Json(new { success = true, result = length });
        //}
        ///?
        //public JsonResult CalculateDelayTime(List<TrkptModel> personTrkpts, List<TrkptModel> dogTrkpts)
        //{
        //    if (!LoginHelper.IsAuthenticated())
        //        return Json(new { success = false, errorCode = 403 });
        //    else if (!LoginHelper.IsCurrentUserAdmin() && !LoginHelper.IsCurrentUserMember())
        //        return Json(new { success = false, errorCode = 403 });

        //    var dogTrkptList = DogTrainingHelper.ConvertTrkptModelListToTrkptList(dogTrkpts);
        //    var personTrkptList = DogTrainingHelper.ConvertTrkptModelListToTrkptList(personTrkpts);

        //    var delay = DogTrainingHelper.CalculateDelayTime(new Trkseg() { Trkpt = dogTrkptList }, new Trkseg() { Trkpt = personTrkptList});
        //    return Json(new { success = true, result = delay });
        //}

        public JsonResult CalculateDuration(List <TrkptModel> trkpts)
        {
            if (!LoginHelper.IsAuthenticated())
            {
                return(Json(new { success = false, errorCode = 403 }));
            }
            else if (!LoginHelper.IsCurrentUserAdmin() && !LoginHelper.IsCurrentUserMember())
            {
                return(Json(new { success = false, errorCode = 403 }));
            }

            var trkptList = DogTrainingHelper.ConvertTrkptModelListToTrkptList(trkpts);
            var duration  = DogTrainingHelper.CalculateDuration(new Trkseg()
            {
                Trkpt = trkptList
            });

            return(Json(new { success = true, result = duration.ToString() }));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Training(int dogId, int trainingId)
        {
            if (!LoginHelper.IsAuthenticated())
            {
                return(RedirectToAction("Login", "Account", new { returnUrl = this.Request.Url.AbsoluteUri }));
            }
            else if (!LoginHelper.IsCurrentUserAdmin() && !LoginHelper.IsCurrentUserMember())
            {
                return(RedirectToAction("Error", "Home", new { error = "Nie masz wystarczających uprawnień by przeglądać tę sekcję" }));
            }
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", LoginHelper.GetToken());
            var blobTrackLinkBase = @"https://kgtstorage.blob.core.windows.net/tracks/";
            HttpResponseMessage responseMessage =
                await client.GetAsync($"dogtrainings/training?trainingId={trainingId}&dogId={dogId}");

            if (responseMessage.IsSuccessStatusCode)
            {
                var responseData         = responseMessage.Content.ReadAsStringAsync().Result;
                var dogTraining          = JsonConvert.DeserializeObject <DogTrainingModel>(responseData);
                var dogTrainingViewModel = new DogTrainingViewModel()
                {
                    Comments         = dogTraining.Comments,
                    Dog              = dogTraining.Dog,
                    DogId            = dogTraining.DogId,
                    DogTrackFilename = dogTraining.DogTrackBlobUrl
                                       .Remove(dogTraining.DogTrackBlobUrl.IndexOf(blobTrackLinkBase), blobTrackLinkBase.Length),
                    LostPerson = dogTraining.LostPerson,
                    LostPersonTrackFilename = dogTraining.LostPersonTrackBlobUrl
                                              .Remove(dogTraining.LostPersonTrackBlobUrl.IndexOf(blobTrackLinkBase), blobTrackLinkBase.Length),
                    Notes      = dogTraining.Notes,
                    Training   = dogTraining.Training,
                    TrainingId = dogTraining.TrainingId,
                    Weather    = dogTraining.Weather,
                    GroundType = dogTraining.GroundType,
                    AdditionalPictureBlobUrl = dogTraining.AdditionalPictureBlobUrl
                };
                if (!String.IsNullOrEmpty(dogTraining.DogTrackBlobUrl))
                {
                    var webRequestDogTrack = WebRequest.Create(dogTraining.DogTrackBlobUrl);
                    try
                    {
                        using (var response = webRequestDogTrack.GetResponse())
                            using (var content = response.GetResponseStream())
                                using (var reader = new StreamReader(content))
                                {
                                    XDocument gpxDoc     = XDocument.Load(reader);
                                    var       serializer = new XmlSerializer(typeof(Trkseg));
                                    var       trkseg     = (Trkseg)serializer.Deserialize(gpxDoc.Root.CreateReader());
                                    if (trkseg != null && trkseg.Trkpt.Any())
                                    {
                                        var t = trkseg.Trkpt;
                                        dogTrainingViewModel.DogTrackPoints = t;
                                        //dogTrainingViewModel.DogTrackLength = DogTrainingHelper.CalculateGPSTrackLength(trkseg);
                                        dogTrainingViewModel.Duration       = DogTrainingHelper.CalculateDuration(trkseg);
                                        dogTrainingViewModel.TimeOfDogStart = DogTrainingHelper.CalculateGPSTrackStartTime(trkseg);
                                    }
                                }
                    }
                    catch (Exception e)
                    {
                        ViewBag.Message = e.Message;
                        return(View("Error"));
                    }
                }
                if (!String.IsNullOrEmpty(dogTraining.LostPersonTrackBlobUrl))
                {
                    var webRequestLostPersonTrack = WebRequest.Create(dogTraining.LostPersonTrackBlobUrl);
                    try
                    {
                        using (var response = webRequestLostPersonTrack.GetResponse())
                            using (var content = response.GetResponseStream())
                                using (var reader = new StreamReader(content))
                                {
                                    XDocument gpxDoc     = XDocument.Load(reader);
                                    var       serializer = new XmlSerializer(typeof(Trkseg));
                                    var       trkseg     = (Trkseg)serializer.Deserialize(gpxDoc.Root.CreateReader());

                                    if (trkseg != null && trkseg.Trkpt.Any())
                                    {
                                        var t = trkseg.Trkpt;
                                        dogTrainingViewModel.LostPersonTrackPoints = t;
                                        //dogTrainingViewModel.LostPersonTrackLength = DogTrainingHelper.CalculateGPSTrackLength(trkseg);
                                        dogTrainingViewModel.TimeOfLostPersonStart = DogTrainingHelper.CalculateGPSTrackStartTime(trkseg);
                                        dogTrainingViewModel.DelayTime             = dogTrainingViewModel.TimeOfDogStart - dogTrainingViewModel.TimeOfLostPersonStart;
                                    }
                                }
                    }
                    catch (Exception e)
                    {
                        ViewBag.Message = e.Message;
                        return(View("Error"));
                    }
                }
                return(View(dogTrainingViewModel));
            }
            else
            {
                ViewBag.Message = "code: " + responseMessage.StatusCode;
                return(View("Error"));
            }
        }