Exemple #1
0
        public async Task <bool> SyncAsync(string routeServerHash, bool loadOnlyPreviewImg = false)
        {
            var localRoute = _routeManager.GetViewRouteById(_routeId);

            //новый маршрут
            if (string.IsNullOrEmpty(routeServerHash))
            {
                bool updateResult = await updateRoute(routeServerHash, new RouteRoot()
                {
                    Route = new SharedModelsWS.Route()
                    {
                        Version = -1
                    }
                }, localRoute);

                _log.AddStringEvent($"route {_routeId}, create result:{updateResult}");
            }

            //независимо от того, новый маршрут или нет, проверяем, что изменилось - придется пройтись по всему маршруту и узнать
            RouteRoot routeRoot = await _routesApi.GetRouteRoot(_routeId);

            bool AuthRequired = (_routesApi.GetLastHttpStatusCode() == HttpStatusCode.Forbidden || _routesApi.GetLastHttpStatusCode() == HttpStatusCode.Unauthorized);

            if ((!AuthRequired) && (routeRoot != null))
            {
                bool updateResult = await updateRoute(routeServerHash, routeRoot, localRoute);

                if (!updateResult)
                {
                    return(false);
                }

                if (!localRoute.IsDeleted)
                {
                    updateResult = await updatePoints(routeRoot);

                    if (!updateResult)
                    {
                        return(false);
                    }

                    List <ViewRoutePointMediaObject> mediaForDownload = new List <ViewRoutePointMediaObject>();
                    List <ViewRoutePointMediaObject> mediaForUpload   = new List <ViewRoutePointMediaObject>();
                    (updateResult, mediaForUpload, mediaForDownload) = await updateMedias(routeRoot);

                    if (!updateResult)
                    {
                        return(false);
                    }

                    if (mediaForDownload.Count > 0)
                    {
                        await downloadMedias(mediaForDownload, loadOnlyPreviewImg);
                    }
                    if (mediaForUpload.Count > 0)
                    {
                        await uploadMedias(mediaForUpload);
                    }
                    TrackRouteRequest trackRequest = new TrackRouteRequest(_authToken);
                    var trackResponse = await trackRequest.GetTrackPlacesAsync(_routeId);

                    if ((trackResponse != null) && (trackResponse.Places.Any()))
                    {
                        _trackFileManager.RemoveAllTracksFromRoute(_routeId);
                        var viewTrackPlaces = trackRequest.GetViewTrackPlaces(trackResponse.Places);
                        _trackFileManager.SaveTrack(_routeId, viewTrackPlaces);
                    }
                }
            }
            else
            {
                return(false);
            }

            var serverHash = await _routesApi.UpdateHash(_routeId);

            if (_routesApi.LastHttpStatusCode == HttpStatusCode.OK)
            {
                var updatedLocalRoute = _routeManager.GetViewRouteById(_routeId);
                updatedLocalRoute.ServerSynced = true;
                updatedLocalRoute.ObjVerHash   = HashManager.Generate(getVersionsForRoute(updatedLocalRoute).ToString());
                if (updatedLocalRoute.ObjVerHash.Equals(serverHash))
                {
                    _log.AddStringEvent($"set route {_routeId}, versions {serverHash}");
                    updatedLocalRoute.Save();
                }
                else
                {
                    _log.AddStringEvent($"failed set route {_routeId}, versions {serverHash}");
                    HandleError.Process("SyncRoute", "ErrorUpdateHash", new Exception("Client and Server hash different"), false, $"server:[{serverHash}], client:[{updatedLocalRoute.ObjVerHash}]");
                }

                if (updatedLocalRoute.IsDeleted)
                {
                    _log.AddStringEvent($"set delete route {_routeId}");
                    deleteRouteContain(updatedLocalRoute);
                }
            }
            else
            {
                HandleError.Process("SyncRoute", "ErrorUpdateHash", new Exception("Http error:" + _routesApi.LastHttpStatusCode.ToString()), false);
            }

            return(true);
        }
Exemple #2
0
 protected void refreshHashForRoute(ViewRoute viewRoute, StringBuilder versions)
 {
     viewRoute.ObjVerHash = HashManager.Generate(versions.ToString());
     viewRoute.Save();
 }