Exemple #1
0
        //public GPSTrackerEntryDTO GPSCoordinatesOperation(Token token, GPSCoordinatesOperationParam param)
        //{
        //    var securityInfo = SecurityManager.EnsureAuthentication(token);
        //    var service = new GPSTrackerService(Session, securityInfo, Configuration);
        //    return service.GPSCoordinatesOperation(param);
        //}

        public GPSCoordinatesOperationResult GPSCoordinatesOperation(GPSOperationParam param)
        {
            var securityInfo = SecurityManager.EnsureAuthentication(param);
            var service      = new GPSTrackerService(Session, securityInfo, Configuration);

            return(service.GPSCoordinatesOperation(param));
        }
Exemple #2
0
        static async public Task <GPSCoordinatesOperationResult> GPSCoordinatesOperationAsync(Guid gpsTrackerEntryId, GPSCoordinatesOperationType operationType, IEnumerable <GPSPoint> points)
        {
            GPSOperationParam dto = new GPSOperationParam();
            var settings          = new JsonSerializerSettings();

            settings.NullValueHandling     = NullValueHandling.Ignore;
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            var json  = JsonConvert.SerializeObject(points, settings);
            var bytes = UTF8Encoding.UTF8.GetBytes(json);

            dto.CoordinatesStream = bytes.ToZip();

            GPSOperationData param = new GPSOperationData();

            param.GPSTrackerEntryId = gpsTrackerEntryId;
            param.Operation         = GPSCoordinatesOperationType.UpdateCoordinates;

            var test = exceptionHandling((client) =>
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId", "http://MYBASERVICE.TK/", ApplicationState.Current.SessionData.Token.SessionId));
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Params", "http://MYBASERVICE.TK/", param));
                return(Task <GPSCoordinatesOperationResult> .Factory.FromAsync(client.BeginGPSCoordinatesOperation, client.EndGPSCoordinatesOperation, dto, null));
            });

            return(await test);
        }
 public GPSCoordinatesOperationResult GPSCoordinatesOperation(GPSOperationParam param)
 {
     //int y = 0;
     //int yy = 45/y;
     return(exceptionHandling(null, () => InternalService.GPSCoordinatesOperation(param)));
 }
        //http://stackoverflow.com/questions/472906/net-string-to-byte-array-c-sharp
        //public PagedResult<GPSPoint> GetGPSCoordinates(Guid entryId,PartialRetrievingInfo pageInfo)
        //{
        //    Log.WriteWarning("GetGPSCoordinates:Username={0},gpsTrackerEntryId={1}", SecurityInfo.SessionData.Profile.UserName, entryId);

        //    using (var transactionScope = Session.BeginGetTransaction())
        //    {
        //        var res = Session.QueryOver<GPSTrackerEntry>().Fetch(x => x.Coordinates).Eager.Where(x => x.GlobalId == entryId).SingleOrDefault();
        //        if (res.Coordinates == null)
        //        {
        //            return new PagedResult<GPSPoint>(new List<GPSPoint>(),0,0 );
        //        }
        //        char[] chars = new char[res.Coordinates.Content.Length / sizeof(char)];
        //        System.Buffer.BlockCopy(res.Coordinates.Content, 0, chars, 0, res.Coordinates.Content.Length);

        //        var pointsCollection = JsonConvert.DeserializeObject<IList<GPSPoint>>(new string(chars));

        //        transactionScope.Commit();
        //        return pointsCollection.ToPagedResult(pageInfo);
        //    }
        //}

        //public GPSTrackerEntryDTO GPSCoordinatesOperation(GPSCoordinatesOperationParam param)
        //{
        //    Log.WriteWarning("GPSCoordinatesOperation:Username={0},gpsTrackerEntryId={1}", SecurityInfo.SessionData.Profile.UserName, param.GPSTrackerEntryId);

        //    using (var transactionScope = Session.BeginGetTransaction())
        //    {
        //        var res = Session.QueryOver<GPSTrackerEntry>().Fetch(x => x.Coordinates).Eager.SingleOrDefault();

        //        if(param.Operation==GPSCoordinatesOperationType.DeleteCoordinates)
        //        {
        //            var coordinates = res.Coordinates;
        //            res.Coordinates = null;
        //            if(coordinates!=null)
        //            {
        //                Session.Delete(coordinates);
        //            }
        //        }
        //        else
        //        {
        //            string output = JsonConvert.SerializeObject(param.Coordinates);
        //            var bytes = UTF8Encoding.UTF8.GetBytes(output);
        //            if (res.Coordinates == null)
        //            {
        //                res.Coordinates = new GPSCoordinates();
        //            }
        //            res.Coordinates.Content = bytes.ToZip();
        //        }

        //        Session.SaveOrUpdate(res);
        //        transactionScope.Commit();
        //        return res.Map<GPSTrackerEntryDTO>();
        //    }


        //}

        public GPSCoordinatesOperationResult GPSCoordinatesOperation(GPSOperationParam param)
        {
            Log.WriteWarning("GPSCoordinatesOperation:Username={0},gpsTrackerEntryId={1}", SecurityInfo.SessionData.Profile.UserName, param.Params.GPSTrackerEntryId);

            using (var transactionScope = Session.BeginGetTransaction())
            {
                var dbProfile = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId);

                var res = Session.QueryOver <GPSTrackerEntry>().Fetch(x => x.Coordinates).Eager.Where(x => x.GlobalId == param.Params.GPSTrackerEntryId).SingleOrDefault();
                if (res == null)
                {
                    throw new ObjectNotFoundException();
                }
                if (res.TrainingDay.Profile != dbProfile)
                {
                    throw new CrossProfileOperationException();
                }
                if (param.Params.Operation == GPSCoordinatesOperationType.DeleteCoordinates)
                {
                    deletesCoordinates(res);
                }
                else
                {
                    try
                    {
                        MemoryStream zippedStream = new MemoryStream();
                        param.CoordinatesStream.CopyTo(zippedStream);
                        zippedStream.Seek(0, SeekOrigin.Begin);
                        if (zippedStream.Length == 0)
                        {
                            deletesCoordinates(res);
                        }
                        else
                        {
                            int maxPointNumber = 21600;//points after 24h, one point every 4 sec
                            var unzippedData   = zippedStream.FromZip();
                            var json           = UTF8Encoding.UTF8.GetString(unzippedData);
                            var points         = JsonConvert.DeserializeObject <IList <GPSPoint> >(json).OrderBy(x => x.Duration).ToList();
                            if (points.Count == 0)
                            {
                                deletesCoordinates(res);
                            }
                            else if (points.Count > maxPointNumber)
                            {
                                throw new ArgumentOutOfRangeException("You can upload max " + maxPointNumber + " points");
                            }
                            else
                            {
                                if (param.Params.Operation == GPSCoordinatesOperationType.UpdateCoordinatesWithCorrection)
                                {//correct altitude data
                                    GPSTrackerHelper.CorrectGpsData(points);
                                }
                                performGpsPointsCalculations(res, points, dbProfile);

                                if (SecurityInfo.Licence.IsPremium)
                                {//for premium account store coordinates in the db
                                    if (res.Coordinates == null)
                                    {
                                        res.Coordinates = new GPSCoordinates();
                                    }
                                    if (param.Params.Operation == GPSCoordinatesOperationType.UpdateCoordinatesWithCorrection)
                                    {
//now zip corrected data again and save them to the db
                                        var settings = new JsonSerializerSettings();
                                        settings.NullValueHandling     = NullValueHandling.Ignore;
                                        settings.MissingMemberHandling = MissingMemberHandling.Ignore;
                                        json = JsonConvert.SerializeObject(points, settings);
                                        var bytes = UTF8Encoding.UTF8.GetBytes(json);
                                        res.Coordinates.Content = bytes.ToZip();
                                    }
                                    else
                                    {
                                        res.Coordinates.Content = zippedStream.ToArray();
                                    }
                                }
                            }
                        }
                    }
                    catch (ZipException ex)
                    {
                        throw new ConsistencyException(ex.Message, ex);
                    }
                    catch (JsonSerializationException ex)
                    {
                        throw  new ConsistencyException(ex.Message, ex);
                    }
                }

                Session.SaveOrUpdate(res);
                transactionScope.Commit();
                return(new GPSCoordinatesOperationResult()
                {
                    GPSTrackerEntry = res.Map <GPSTrackerEntryDTO>()
                });
            }
        }