Skip to content

Commit

Permalink
Use BlockingCollection for thread safe access #249
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Apr 19, 2021
1 parent 34dcf27 commit 6049017
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 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
6 changes: 5 additions & 1 deletion src/Q42.HueApi/HueClient-Lights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,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 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
3 changes: 2 additions & 1 deletion 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 All @@ -12,7 +13,7 @@ namespace Q42.HueApi.Models.Groups
/// <summary>
/// A PUT or POST returns a list which can contain multiple success and errors
/// </summary>
public class HueResults : List<DefaultHueResult>
public class HueResults : BlockingCollection<DefaultHueResult>
{

public bool HasErrors()
Expand Down

0 comments on commit 6049017

Please sign in to comment.