From 6049017db7fa08ed9547bd6d1f144d346c9e6363 Mon Sep 17 00:00:00 2001 From: michielpost Date: Mon, 19 Apr 2021 13:21:20 +0200 Subject: [PATCH] Use BlockingCollection for thread safe access #249 --- src/Q42.HueApi/HueClient-Groups.cs | 4 ++-- src/Q42.HueApi/HueClient-Lights.cs | 6 +++++- src/Q42.HueApi/HueClient-Rules.cs | 4 ++-- src/Q42.HueApi/HueClient-Scenes.cs | 4 ++-- src/Q42.HueApi/Models/Groups/DefaultHueResult.cs | 3 ++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Q42.HueApi/HueClient-Groups.cs b/src/Q42.HueApi/HueClient-Groups.cs index f06f80eb..aefdf60e 100644 --- a/src/Q42.HueApi/HueClient-Groups.cs +++ b/src/Q42.HueApi/HueClient-Groups.cs @@ -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()) diff --git a/src/Q42.HueApi/HueClient-Lights.cs b/src/Q42.HueApi/HueClient-Lights.cs index cf714627..c734e4ab 100644 --- a/src/Q42.HueApi/HueClient-Lights.cs +++ b/src/Q42.HueApi/HueClient-Lights.cs @@ -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) { diff --git a/src/Q42.HueApi/HueClient-Rules.cs b/src/Q42.HueApi/HueClient-Rules.cs index 19b8abe0..d4668f7d 100644 --- a/src/Q42.HueApi/HueClient-Rules.cs +++ b/src/Q42.HueApi/HueClient-Rules.cs @@ -145,9 +145,9 @@ public Task 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()) diff --git a/src/Q42.HueApi/HueClient-Scenes.cs b/src/Q42.HueApi/HueClient-Scenes.cs index dae26373..6682bd15 100644 --- a/src/Q42.HueApi/HueClient-Scenes.cs +++ b/src/Q42.HueApi/HueClient-Scenes.cs @@ -93,9 +93,9 @@ public async Task> 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()) diff --git a/src/Q42.HueApi/Models/Groups/DefaultHueResult.cs b/src/Q42.HueApi/Models/Groups/DefaultHueResult.cs index 4525ebd7..e61bc709 100644 --- a/src/Q42.HueApi/Models/Groups/DefaultHueResult.cs +++ b/src/Q42.HueApi/Models/Groups/DefaultHueResult.cs @@ -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; @@ -12,7 +13,7 @@ namespace Q42.HueApi.Models.Groups /// /// A PUT or POST returns a list which can contain multiple success and errors /// - public class HueResults : List + public class HueResults : BlockingCollection { public bool HasErrors()