private async void SyncEditsButton_Click(object sender, RoutedEventArgs e)
        {
            var gdb = await Geodatabase.OpenAsync(this.LocalDataPathTextBlock.Text);

            // create a new GeodatabaseSyncTask with the uri of the feature server to pull from
            var serverUrl           = this.featureLayerServiceUrl.Substring(0, this.featureLayerServiceUrl.LastIndexOf('/'));
            var uri                 = new Uri(serverUrl);
            var geodatabaseSyncTask = new GeodatabaseSyncTask(uri);

            var geodatabaseSyncParams = new SyncGeodatabaseParameters();

            geodatabaseSyncParams.SyncDirection = SyncDirection.Bidirectional;

            var checkStatusInterval = new TimeSpan(0, 0, 3);

            // Create a System.Progress<T> object to report status as the task executes
            var progress = new Progress <GeodatabaseStatusInfo>();

            progress.ProgressChanged += (s, info) =>
            {
                this.SyncStatusTextBlock.Text = "Sync edits: " + info.Status;
            };

            this.SyncStatusTextBlock.Text   = "Starting sync ...";
            this.SyncProgressBar.Visibility = System.Windows.Visibility.Visible;
            this.SyncStatusPanel.Visibility = System.Windows.Visibility.Visible;
            var result = await geodatabaseSyncTask.SyncGeodatabaseAsync(geodatabaseSyncParams, gdb, syncCompleteCallback, uploadCompleteCallback, checkStatusInterval, progress, new CancellationToken());
        }
Example #2
0
        // Sychronizes local data with the online service
        private async void SyncButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (LocalBirdsLayer == null)
                {
                    throw new ApplicationException("Could not find local geodatabase.");
                }

                IsBusy = true;
                ReportStatus("Synchronizing Local and Online data...");

                var tcs = new TaskCompletionSource <GeodatabaseStatusInfo>();
                Action <GeodatabaseStatusInfo, Exception> completionAction = (info, ex) =>
                {
                    if (ex != null)
                    {
                        tcs.SetException(ex);
                    }
                    tcs.SetResult(info);
                };

                var syncProgress = new Progress <GeodatabaseStatusInfo>();
                syncProgress.ProgressChanged += (sndr, sts) => { SecondaryStatus = sts.Status.ToString(); };

                var syncTask = new GeodatabaseSyncTask(new Uri(BASE_URL));
                var gdbTable = _localBirdsLayer.FeatureTable as GeodatabaseFeatureTable;
                await syncTask.SyncGeodatabaseAsync(gdbTable.Geodatabase,
                                                    completionAction,
                                                    null,
                                                    TimeSpan.FromSeconds(3),
                                                    syncProgress,
                                                    CancellationToken.None);

                await tcs.Task;

                ReportStatus("Refreshing map view...");
                await RefreshDataView();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
        // Sychronizes local data with the online service
        private async void SyncButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (LocalBirdsLayer == null)
                    throw new ApplicationException("Could not find local geodatabase.");

                IsBusy = true;
                ReportStatus("Synchronizing Local and Online data...");

                var tcs = new TaskCompletionSource<GeodatabaseStatusInfo>();
                Action<GeodatabaseStatusInfo, Exception> completionAction = (info, ex) =>
                {
                    if (ex != null)
                        tcs.SetException(ex);
                    tcs.SetResult(info);
                };

                var syncProgress = new Progress<GeodatabaseStatusInfo>();
                syncProgress.ProgressChanged += (sndr, sts) => { SecondaryStatus = sts.Status.ToString(); };

                var syncTask = new GeodatabaseSyncTask(new Uri(BASE_URL));
                var gdbTable = _localBirdsLayer.FeatureTable as GeodatabaseFeatureTable;
                await syncTask.SyncGeodatabaseAsync(gdbTable.Geodatabase,
                    completionAction,
                    null,
                    TimeSpan.FromSeconds(3),
                    syncProgress,
                    CancellationToken.None);

                await tcs.Task;

                ReportStatus("Refreshing map view...");
                await RefreshDataView();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
Example #4
0
        public async Task<GeodatabaseStatusInfo> SyncGeodatabase(string url, Geodatabase gdb, bool unregister, bool syncing)
        {
            Console.WriteLine("Starting Sync " + DateTime.Now + " --> " + syncing);
            var syncTask = new GeodatabaseSyncTask(new Uri(url));


            var layerIdsAndName = new Dictionary<int, string>();

            FeatureServiceInfo serviceInfo = await syncTask.GetServiceInfoAsync();
            foreach (LayerServiceInfo layerServiceInfo in serviceInfo.Layers)
            {
                if (!layerIdsAndName.ContainsKey(layerServiceInfo.ID))
                {
                    layerIdsAndName.Add(layerServiceInfo.ID, layerServiceInfo.Name);
                }
            }


            


            var tcs = new TaskCompletionSource<GeodatabaseStatusInfo>();
            Action<GeodatabaseStatusInfo, Exception> completionAction = (info, ex) =>
            {
                if (ex != null)
                {
                    tcs.SetException(ex);
                    return;
                }
                tcs.SetResult(info);
            };

            var syncProgress = new Progress<GeodatabaseStatusInfo>();
            syncProgress.ProgressChanged += (sndr, sts) => Console.WriteLine("Status:" + sts.Status.ToString() + "," +" --> " + syncing + "," + sts.SubmissionTime + "," + sts.ResultUri);


            IDictionary<int, LayerSyncInfo> layerSync = null;

            layerSync = new Dictionary<int, LayerSyncInfo>();
            foreach (var id_name in layerIdsAndName)
            {
                    var layerQuery = new LayerSyncInfo { SyncDirection = SyncDirection.Bidirectional };
                    layerSync.Add(id_name.Key, layerQuery);
                
            }
            Action<UploadResult> uploadAction = result => { Console.Write(""); };

            //should we remove sync direction since it should be defined in layers?
            var syncParams = new SyncGeodatabaseParameters(gdb) { RollbackOnFailure = false, UnregisterGeodatabase = unregister, LayerSyncInfos = layerSync, SyncDirection = SyncDirection.Bidirectional };
            Console.WriteLine("Calling sync " + DateTime.Now);
            await syncTask.SyncGeodatabaseAsync(syncParams, gdb,
                completionAction,
                uploadAction,
                TimeSpan.FromSeconds(3),
                syncProgress,
                CancellationToken.None);
            
            

            return await tcs.Task;

            

        }
		// function to submit a sync task 
		// -the url for the feature service and the path to the local geodatabase are passed in
		public async Task SyncronizeEditsAsync(FeatureLayerInfo featureLayerInfo, Uri uri)
		{
			// create sync parameters
			var taskParameters = new SyncGeodatabaseParameters
			{
				RollbackOnFailure = true,
				SyncDirection = SyncDirection.Bidirectional
			};

			// cancel if an earlier call was made and hasn't completed
			if (_syncCancellationTokenSource != null)
			{
				_syncCancellationTokenSource.Cancel();
			}

			// create a new cancellation token
			_syncCancellationTokenSource = new CancellationTokenSource();
			var cancelToken = _syncCancellationTokenSource.Token;


			// create a sync task with the url of the feature service to sync
			var syncTask = new GeodatabaseSyncTask(uri);

			// create a new Progress object to report updates to the sync status
			var progress = new Progress<GeodatabaseStatusInfo>();
			progress.ProgressChanged += (sender, s) => { _model.SetMessageInfo("Progress: " + s.Status.ToString()); };

			// call SyncGeodatabaseAsync and pass in: sync params, local geodatabase, completion callback, update interval, progress, and cancellation token

			var geodatabaseFeatureTable = featureLayerInfo.FeatureLayer.FeatureTable as GeodatabaseFeatureTable;

			var gdbResult = await syncTask.SyncGeodatabaseAsync(
				taskParameters,
				geodatabaseFeatureTable.Geodatabase,
				(p, q) =>
				{
					// reset the cancellation token source
					_syncCancellationTokenSource = null;

					// if unsuccessful, report the exception and return
					if (q != null)
					{
						_model.SetMessageInfo("An exception occured: " + q.Message);
						return;
					}

					// if successful, notify the user
					_model.SetMessageInfo("--- Sync completed ---");

					//// optionally, do something with the result
					//var resultUri = p.ResultUri;
					// ...

					UpdateToc(featureLayerInfo);
					_model.SetSyncState(CheckSyncState(false));
				},
				null,
				new TimeSpan(0, 0, 10),
				progress,
				cancelToken);
		}
		private async Task SyncOfflineMap(OfflineMapItem offlineMapItem)
		{
			var gdbFiles = Directory.GetFiles(offlineMapItem.OfflineMapPath,
				string.Format("*{0}", OfflineMapItem.GeodatabaseFilenameExtension));
			foreach (var gdbFile in gdbFiles)
			{
				var gdbPath = gdbFile.Replace("/", @"\");
				var geodatabase = await Geodatabase.OpenAsync(gdbPath);
				if (!geodatabase.FeatureTables.Any(table => table.HasEdits))
				{
					// nothing to sync..
					continue;
				}

				_model.SetMessageInfo("Sync geodatabase...");
				var serviceUri = geodatabase.GetServiceUri();
				var geodatabaseSyncTask = new GeodatabaseSyncTask(serviceUri);
				var statusCallback =
					new Progress<GeodatabaseStatusInfo>(
						statusInfo => _model.SetMessageInfo(string.Format("Current Status: {0}", statusInfo.Status)));

				var tcs = new TaskCompletionSource<GeodatabaseStatusInfo>();

				try
				{
					// At this point, we are submitting a job. Therefore, the await returns pretty fast.
					await geodatabaseSyncTask.SyncGeodatabaseAsync(
						new SyncGeodatabaseParameters
						{
							RollbackOnFailure = true,
							SyncDirection = SyncDirection.Upload,
							UnregisterGeodatabase = false
						},
						geodatabase,
						(geodatabaseStatusInfo, exc) =>
						{
							if (geodatabaseStatusInfo != null)
							{
								tcs.SetResult(geodatabaseStatusInfo);
							}
							if (exc != null)
							{
								tcs.SetException(exc);
							}
						},
						uploadResult => { _model.SetMessageInfo(string.Format("UploadResult: {0}", uploadResult.Success)); },
						TimeSpan.FromSeconds(5),
						statusCallback,
						CancellationToken.None);

					// This is the call we are really waiting for..
					await tcs.Task;
					_model.SetMessageInfo(string.Format("Finished Status: {0}", tcs.Task.Result.Status));
				}
				catch (Exception exc)
				{
					_model.SetMessageInfo(string.Format("Executing SyncGeodatabaseAsync failed: {0}", exc.Message));
				}
				_model.SetMessageInfo("...done syncing geodatabase");
			}
		}
Example #7
0
        private async void Sync_Click(object sender, RoutedEventArgs e)
        {
            var tcs = new TaskCompletionSource <GeodatabaseStatusInfo>();
            Action <GeodatabaseStatusInfo, Exception> completionAction = (info, ex) =>
            {
                if (ex != null)
                {
                    tcs.SetException(ex);
                }
                tcs.SetResult(info);
            };

            Geodatabase gdb      = null;
            var         syncTask = new GeodatabaseSyncTask(new Uri(uristring));

            try
            {
                gdb = await Esri.ArcGISRuntime.Data.Geodatabase.OpenAsync(@"C:\Temp\10_2_7Demo\WildlifeLocal.geodatabase");

                var table = gdb.FeatureTables.FirstOrDefault();

                var lyr = new FeatureLayer
                {
                    ID           = table.Name,
                    DisplayName  = table.Name,
                    FeatureTable = table
                };

                MyMapView.Map.Layers.Add(lyr);

                Console.WriteLine(table.Name);
            }
            catch (Exception et)
            {
                Console.WriteLine(et.Message);
            }

            var taskParameters = new SyncGeodatabaseParameters()
            {
                RollbackOnFailure = false,
                SyncDirection     = Esri.ArcGISRuntime.Tasks.Offline.SyncDirection.Bidirectional
            };

            // cancel if an earlier call was made and hasn't completed
            if (_syncCancellationTokenSource != null)
            {
                _syncCancellationTokenSource.Cancel();
            }

            // create a new cancellation token
            _syncCancellationTokenSource = new CancellationTokenSource();
            var cancelToken = _syncCancellationTokenSource.Token;

            // create a new Progress object to report updates to the sync status
            var progress = new Progress <GeodatabaseStatusInfo>();

            progress.ProgressChanged += (sender2, s) =>
            {
                Console.WriteLine(s.Status);
            };

            try
            {
                // call SyncGeodatabaseAsync and pass in: sync params, local geodatabase, completion callback, update interval, progress, and cancellation token
                var gdbResult = await syncTask.SyncGeodatabaseAsync(
                    taskParameters,
                    gdb,
                    this.syncCompleteCallback,
                    null,
                    new TimeSpan(0, 0, 2),
                    progress,
                    cancelToken);
            }
            catch (ArcGISWebException a)
            {
                Console.WriteLine(a.Code);
                Console.WriteLine(a.StackTrace);
            }
        }