예제 #1
0
        protected async Task <bool> UpdateFencesAsync(IFenceUpdateRequest updateRequest)
        {
            bool success = false;

            try
            {
                // we've seen cases where the update blocks indefinitely (e.g., due to outdated google play services on the
                // phone). impose a timeout to avoid such blocks.
                Task <Statuses> updateFencesTask = Awareness.FenceApi.UpdateFencesAsync(_awarenessApiClient, updateRequest);
                Task            timeoutTask      = Task.Delay(TimeSpan.FromSeconds(60));
                Task            finishedTask     = await Task.WhenAny(updateFencesTask, timeoutTask);

                if (finishedTask == updateFencesTask)
                {
                    Statuses status = await updateFencesTask;

                    if (status.IsSuccess)
                    {
                        SensusServiceHelper.Get().Logger.Log("Updated Google Awareness API fences.", LoggingLevel.Normal, GetType());
                        success = true;
                    }
                    else if (status.IsCanceled)
                    {
                        SensusServiceHelper.Get().Logger.Log("Google Awareness API fence update canceled.", LoggingLevel.Normal, GetType());
                    }
                    else if (status.IsInterrupted)
                    {
                        SensusServiceHelper.Get().Logger.Log("Google Awareness API fence update interrupted", LoggingLevel.Normal, GetType());
                    }
                    else
                    {
                        string message = "Unrecognized fence update status:  " + status;
                        SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType());
                        SensusException.Report(message);
                        throw new Exception(message);
                    }
                }
                else
                {
                    throw new Exception("Fence update timed out.");
                }
            }
            // catch any errors from calling UpdateFences
            catch (Exception ex)
            {
                // ensure that wait is always set
                SensusServiceHelper.Get().Logger.Log("Exception while updating fences:  " + ex, LoggingLevel.Normal, GetType());
            }

            return(success);
        }
예제 #2
0
 public static async Task <Statuses> UpdateFencesAsync(this IFenceApi api, GoogleApiClient client, IFenceUpdateRequest fenceUpdateRequest)
 {
     return((await api.UpdateFences(client, fenceUpdateRequest)).JavaCast <Statuses> ());
 }
예제 #3
0
        protected bool UpdateFences(IFenceUpdateRequest updateRequest)
        {
            ManualResetEvent updateWait = new ManualResetEvent(false);

            bool success = false;

            try
            {
                // update fences is asynchronous
                Awareness.FenceApi.UpdateFences(_awarenessApiClient, updateRequest).SetResultCallback <Statuses>(status =>
                {
                    try
                    {
                        if (status.IsSuccess)
                        {
                            SensusServiceHelper.Get().Logger.Log("Updated Google Awareness API fences.", LoggingLevel.Normal, GetType());
                            success = true;
                        }
                        else if (status.IsCanceled)
                        {
                            SensusServiceHelper.Get().Logger.Log("Google Awareness API fence update canceled.", LoggingLevel.Normal, GetType());
                        }
                        else if (status.IsInterrupted)
                        {
                            SensusServiceHelper.Get().Logger.Log("Google Awareness API fence update interrupted", LoggingLevel.Normal, GetType());
                        }
                        else
                        {
                            string message = "Unrecognized fence update status:  " + status;
                            SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType());
                            SensusException.Report(message);
                        }
                    }
                    catch (Exception ex)
                    {
                        SensusServiceHelper.Get().Logger.Log("Exception while processing update status:  " + ex, LoggingLevel.Normal, GetType());
                    }
                    finally
                    {
                        // ensure that wait is always set
                        updateWait.Set();
                    }
                });
            }
            // catch any errors from calling UpdateFences
            catch (Exception ex)
            {
                // ensure that wait is always set
                SensusServiceHelper.Get().Logger.Log("Exception while updating fences:  " + ex, LoggingLevel.Normal, GetType());
                updateWait.Set();
            }

            // we've seen cases where the update blocks indefinitely (e.g., due to outdated google play services on the phone). impose
            // a timeout to avoid such blocks.
            if (!updateWait.WaitOne(TimeSpan.FromSeconds(60)))
            {
                SensusServiceHelper.Get().Logger.Log("Timed out while updating fences.", LoggingLevel.Normal, GetType());
            }

            return(success);
        }
 public Task UpdateFencesAsync(IFenceUpdateRequest fenceUpdateRequest)
 {
     return(UpdateFences(fenceUpdateRequest).AsAsync());
 }