Skip to content

Commit

Permalink
Make Estimote Cloud interactions asynchronous with timeouts to avoid …
Browse files Browse the repository at this point in the history
…blocking UI.
  • Loading branch information
MatthewGerber committed Jan 16, 2019
1 parent f283b17 commit d1f232a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
38 changes: 38 additions & 0 deletions Sensus.Shared/Extensions/WebRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Net;
using System.Threading.Tasks;

namespace Sensus.Extensions
{
public static class WebRequestExtensions
{
public static async Task<WebResponse> GetResponseAsync(this WebRequest webRequest, TimeSpan? timeout = null)
{
Task<WebResponse> webResponseTask = webRequest.GetResponseAsync();
Task timeoutTask = Task.Delay((int)(timeout?.TotalMilliseconds ?? int.MaxValue));

Task completedTask = await Task.WhenAny(webResponseTask, timeoutTask);

if (completedTask == timeoutTask)
{
throw new TimeoutException("Timed out.");
}

return await webResponseTask;
}
}
}
9 changes: 5 additions & 4 deletions Sensus.Shared/Probes/Location/EstimoteBeaconProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Newtonsoft.Json.Linq;
using Sensus.Concurrent;
using System.Threading.Tasks;
using Sensus.Extensions;

namespace Sensus.Probes.Location
{
Expand Down Expand Up @@ -180,7 +181,7 @@ protected override async Task InitializeAsync()
}
}

public List<string> GetBeaconTagsFromCloud()
public async Task<List<string>> GetBeaconTagsFromCloudAsync(TimeSpan? timeout = null)
{
List<string> tags = new List<string>();

Expand All @@ -191,7 +192,7 @@ public List<string> GetBeaconTagsFromCloud()
request.Method = "GET";
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(EstimoteCloudAppId + ":" + EstimoteCloudAppToken)));

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (HttpWebResponse response = await request.GetResponseAsync(timeout) as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
Expand Down Expand Up @@ -235,7 +236,7 @@ public List<string> GetBeaconTagsFromCloud()
return tags;
}

public List<EstimoteLocation> GetLocationsFromCloud()
public async Task<List<EstimoteLocation>> GetLocationsFromCloudAsync(TimeSpan? timeout)
{
List<EstimoteLocation> locations = new List<EstimoteLocation>();

Expand All @@ -246,7 +247,7 @@ public List<EstimoteLocation> GetLocationsFromCloud()
request.Method = "GET";
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(EstimoteCloudAppId + ":" + EstimoteCloudAppToken)));

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (HttpWebResponse response = await request.GetResponseAsync(timeout) as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
Expand Down
1 change: 1 addition & 0 deletions Sensus.Shared/Sensus.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Probes\Location\EstimoteIndoorLocationAccuracy.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\JsonDotNetExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\TypeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\WebRequestExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Probes\User\MicrosoftBand\" />
Expand Down
4 changes: 2 additions & 2 deletions Sensus.Shared/UI/EstimoteBeaconProbeBeaconsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public EstimoteBeaconProbeBeaconsPage(EstimoteBeaconProbe probe)
List<string> beaconTags;
try
{
beaconTags = estimoteBeaconProbe.GetBeaconTagsFromCloud();
beaconTags = await estimoteBeaconProbe.GetBeaconTagsFromCloudAsync(TimeSpan.FromSeconds(10));

if (beaconTags.Count == 0)
{
Expand All @@ -69,7 +69,7 @@ public EstimoteBeaconProbeBeaconsPage(EstimoteBeaconProbe probe)
}
catch (Exception ex)
{
await SensusServiceHelper.Get().FlashNotificationAsync("Cannot add beacon: " + ex);
await SensusServiceHelper.Get().FlashNotificationAsync("Failed to add beacon: " + ex.Message);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions Sensus.Shared/UI/ProbePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public ProbePage(Probe probe)
List<EstimoteLocation> locations;
try
{
locations = estimoteBeaconProbe.GetLocationsFromCloud();
locations = await estimoteBeaconProbe.GetLocationsFromCloudAsync(TimeSpan.FromSeconds(10));

if (locations.Count == 0)
{
Expand All @@ -216,7 +216,7 @@ public ProbePage(Probe probe)
}
catch (Exception ex)
{
await SensusServiceHelper.Get().FlashNotificationAsync("Cannot set location: " + ex);
await SensusServiceHelper.Get().FlashNotificationAsync("Failed to set location: " + ex.Message);
return;
}

Expand Down

0 comments on commit d1f232a

Please sign in to comment.