Skip to content

Commit

Permalink
Merge pull request #250 from Q42/feature/blocking-collection
Browse files Browse the repository at this point in the history
Use BlockingCollection for thread safe access #249
  • Loading branch information
michielpost authored Apr 19, 2021
2 parents 34dcf27 + ad7379b commit 3c6a6cc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Q42.HueApi/HueClient-Groups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public partial class HueClient : IHueClient_Groups

HueResults groupResult = DeserializeDefaultHueResult(jsonResult);

if (groupResult.Count > 0 && groupResult[0].Success != null && !string.IsNullOrEmpty(groupResult[0].Success.Id))
if (groupResult.Count > 0 && groupResult.First().Success != null && !string.IsNullOrEmpty(groupResult.First().Success.Id))
{
return groupResult[0].Success.Id.Replace("/groups/", string.Empty);
return groupResult.First().Success.Id.Replace("/groups/", string.Empty);
}

if (groupResult.HasErrors())
Expand Down
13 changes: 10 additions & 3 deletions src/Q42.HueApi/HueClient-Lights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Dynamic;
using Q42.HueApi.Models;
using Q42.HueApi.Interfaces;
using System.Collections.Concurrent;

namespace Q42.HueApi
{
Expand Down Expand Up @@ -183,7 +184,7 @@ public async Task<HueResults> SendCommandRawAsync(string command, IEnumerable<st
}
else
{
HueResults results = new HueResults();
BlockingCollection<DefaultHueResult> results = new BlockingCollection<DefaultHueResult>();
HttpClient client = await GetHttpClient().ConfigureAwait(false);

await lightList.ForEachAsync(_parallelRequests, async (lightId) =>
Expand All @@ -193,7 +194,11 @@ await lightList.ForEachAsync(_parallelRequests, async (lightId) =>
var result = await client.PutAsync(new Uri(ApiBase + $"lights/{lightId}/state"), new JsonContent(command)).ConfigureAwait(false);

string jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
results.AddRange(DeserializeDefaultHueResult(jsonResult));
var hueResults = DeserializeDefaultHueResult(jsonResult);
foreach(var hueResult in hueResults)
{
results.Add(hueResult);
}
}
catch(Exception ex)
{
Expand All @@ -209,7 +214,9 @@ await lightList.ForEachAsync(_parallelRequests, async (lightId) =>

}).ConfigureAwait(false);

return results;
HueResults hueResults = new HueResults();
hueResults.AddRange(results);
return hueResults;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Q42.HueApi/HueClient-Rules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ public Task<string> CreateRule(Rule rule)

HueResults rulesResult = DeserializeDefaultHueResult(jsonResult);

if (rulesResult.Count > 0 && rulesResult[0].Success != null && !string.IsNullOrEmpty(rulesResult[0].Success.Id))
if (rulesResult.Count > 0 && rulesResult.First().Success != null && !string.IsNullOrEmpty(rulesResult.First().Success.Id))
{
return rulesResult[0].Success.Id;
return rulesResult.First().Success.Id;
}

if (rulesResult.HasErrors())
Expand Down
4 changes: 2 additions & 2 deletions src/Q42.HueApi/HueClient-Scenes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public async Task<IReadOnlyCollection<Scene>> GetScenesAsync()

HueResults sceneResult = DeserializeDefaultHueResult(jsonResult);

if (sceneResult.Count > 0 && sceneResult[0].Success != null && !string.IsNullOrEmpty(sceneResult[0].Success.Id))
if (sceneResult.Count > 0 && sceneResult.First().Success != null && !string.IsNullOrEmpty(sceneResult.First().Success.Id))
{
return sceneResult[0].Success.Id;
return sceneResult.First().Success.Id;
}

if (sceneResult.HasErrors())
Expand Down
1 change: 1 addition & 0 deletions src/Q42.HueApi/Models/Groups/DefaultHueResult.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down

0 comments on commit 3c6a6cc

Please sign in to comment.