diff --git a/src/Q42.HueApi.Streaming/Q42.HueApi.Streaming.csproj b/src/Q42.HueApi.Streaming/Q42.HueApi.Streaming.csproj index 644e1994..3517c68d 100644 --- a/src/Q42.HueApi.Streaming/Q42.HueApi.Streaming.csproj +++ b/src/Q42.HueApi.Streaming/Q42.HueApi.Streaming.csproj @@ -14,7 +14,7 @@ GitHub philips hue lights entertainment Q42.HueApi.Entertainment - 3.15.0 + 3.18.0-beta1 true True snupkg @@ -22,7 +22,7 @@ - + diff --git a/src/Q42.HueApi.Tests/Q42.HueApi.Tests.csproj b/src/Q42.HueApi.Tests/Q42.HueApi.Tests.csproj index ff611323..133873a4 100644 --- a/src/Q42.HueApi.Tests/Q42.HueApi.Tests.csproj +++ b/src/Q42.HueApi.Tests/Q42.HueApi.Tests.csproj @@ -47,8 +47,8 @@ - - ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll diff --git a/src/Q42.HueApi.Tests/packages.config b/src/Q42.HueApi.Tests/packages.config index 14f2ca5d..d7b227b2 100644 --- a/src/Q42.HueApi.Tests/packages.config +++ b/src/Q42.HueApi.Tests/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/src/Q42.HueApi.UniversalWindows.Sample/Q42.HueApi.UniversalWindows.Sample.csproj b/src/Q42.HueApi.UniversalWindows.Sample/Q42.HueApi.UniversalWindows.Sample.csproj index 9eba705b..44aaae6f 100644 --- a/src/Q42.HueApi.UniversalWindows.Sample/Q42.HueApi.UniversalWindows.Sample.csproj +++ b/src/Q42.HueApi.UniversalWindows.Sample/Q42.HueApi.UniversalWindows.Sample.csproj @@ -174,7 +174,7 @@ 2.0.3 - 12.0.3 + 13.0.1 1.5.2.42 diff --git a/src/Q42.HueApi/HttpBridgeLocator.cs b/src/Q42.HueApi/HttpBridgeLocator.cs index 9372a5a9..4394fdd7 100644 --- a/src/Q42.HueApi/HttpBridgeLocator.cs +++ b/src/Q42.HueApi/HttpBridgeLocator.cs @@ -28,11 +28,15 @@ public override async Task> LocateBridgesAsync(Cancel { string content = await response.Content.ReadAsStringAsync(); - NuPnPResponse[] responseModel = JsonConvert.DeserializeObject(content); - - var locatedBridges = responseModel.Select(x => new LocatedBridge() { BridgeId = x.Id, IpAddress = x.InternalIpAddress }).ToList(); - locatedBridges.ForEach(OnBridgeFound); - return locatedBridges; + NuPnPResponse[]? responseModel = JsonConvert.DeserializeObject(content); + if (responseModel != null) + { + var locatedBridges = responseModel.Select(x => new LocatedBridge() { BridgeId = x.Id, IpAddress = x.InternalIpAddress }).ToList(); + locatedBridges.ForEach(OnBridgeFound); + return locatedBridges; + } + else + return Enumerable.Empty(); } else { diff --git a/src/Q42.HueApi/HueClient-Config.cs b/src/Q42.HueApi/HueClient-Config.cs index 270da0b9..bf1ca948 100644 --- a/src/Q42.HueApi/HueClient-Config.cs +++ b/src/Q42.HueApi/HueClient-Config.cs @@ -58,7 +58,7 @@ public async Task DeleteWhiteListEntryAsync(string entry) BridgeConfig? config = await GetConfigAsync().ConfigureAwait(false); if (config == null) return null; - + return config.WhiteList.Select(l => l.Value).ToList(); } @@ -80,30 +80,33 @@ public async Task DeleteWhiteListEntryAsync(string entry) return new Bridge(jsonResult); } - - + + /// /// Get bridge config /// /// BridgeConfig object public async Task GetConfigAsync() { - //Not needed to check if initialized, can be used without API key + //Not needed to check if initialized, can be used without API key - HttpClient client = await GetHttpClient().ConfigureAwait(false); - string stringResult = await client.GetStringAsync(new Uri(String.Format("{0}config", ApiBase))).ConfigureAwait(false); - JToken token = JToken.Parse(stringResult); - BridgeConfig? config = null; - if (token.Type == JTokenType.Object) - { - var jsonResult = (JObject)token; - config = JsonConvert.DeserializeObject(jsonResult.ToString()); + HttpClient client = await GetHttpClient().ConfigureAwait(false); + string stringResult = await client.GetStringAsync(new Uri(String.Format("{0}config", ApiBase))).ConfigureAwait(false); + JToken token = JToken.Parse(stringResult); + BridgeConfig? config = null; + if (token.Type == JTokenType.Object) + { + var jsonResult = (JObject)token; + config = JsonConvert.DeserializeObject(jsonResult.ToString()); - //Fix whitelist IDs - foreach (var whitelist in config.WhiteList) - whitelist.Value.Id = whitelist.Key; + if (config != null) + { + //Fix whitelist IDs + foreach (var whitelist in config.WhiteList) + whitelist.Value.Id = whitelist.Key; } - return config; + } + return config; } /// diff --git a/src/Q42.HueApi/HueClient-Groups.cs b/src/Q42.HueApi/HueClient-Groups.cs index 72d22662..f06f80eb 100644 --- a/src/Q42.HueApi/HueClient-Groups.cs +++ b/src/Q42.HueApi/HueClient-Groups.cs @@ -142,10 +142,13 @@ public async Task> GetGroupsAsync() foreach (var prop in jsonResult.Properties()) { - Group newGroup = JsonConvert.DeserializeObject(prop.Value.ToString()); - newGroup.Id = prop.Name; + Group? newGroup = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (newGroup != null) + { + newGroup.Id = prop.Name; - results.Add(newGroup); + results.Add(newGroup); + } } } diff --git a/src/Q42.HueApi/HueClient-Lights.cs b/src/Q42.HueApi/HueClient-Lights.cs index c6f5e247..cf714627 100644 --- a/src/Q42.HueApi/HueClient-Lights.cs +++ b/src/Q42.HueApi/HueClient-Lights.cs @@ -135,9 +135,12 @@ public async Task> GetLightsAsync() foreach (var prop in jsonResult.Properties()) { - Light newLight = JsonConvert.DeserializeObject(prop.Value.ToString()); - newLight.Id = prop.Name; - results.Add(newLight); + Light? newLight = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (newLight != null) + { + newLight.Id = prop.Name; + results.Add(newLight); + } } } return results; @@ -268,10 +271,13 @@ public async Task> GetNewLightsAsync() { if (prop.Name != "lastscan") { - Light newLight = JsonConvert.DeserializeObject(prop.Value.ToString()); - newLight.Id = prop.Name; + Light? newLight = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (newLight != null) + { + newLight.Id = prop.Name; - results.Add(newLight); + results.Add(newLight); + } } } diff --git a/src/Q42.HueApi/HueClient-ResourceLinks.cs b/src/Q42.HueApi/HueClient-ResourceLinks.cs index 99aba092..f0706fd6 100644 --- a/src/Q42.HueApi/HueClient-ResourceLinks.cs +++ b/src/Q42.HueApi/HueClient-ResourceLinks.cs @@ -60,10 +60,13 @@ public async Task> GetResourceLinksAsync() foreach (var prop in jsonResult.Properties()) { - ResourceLink newResourceLink = JsonConvert.DeserializeObject(prop.Value.ToString()); - newResourceLink.Id = prop.Name; + ResourceLink? newResourceLink = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (newResourceLink != null) + { + newResourceLink.Id = prop.Name; - results.Add(newResourceLink); + results.Add(newResourceLink); + } } } @@ -111,9 +114,9 @@ public async Task> GetResourceLinksAsync() var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); - DefaultHueResult[] resourceLinkResult = JsonConvert.DeserializeObject(jsonResult); + DefaultHueResult[]? resourceLinkResult = JsonConvert.DeserializeObject(jsonResult); - if (resourceLinkResult.Length > 0 && resourceLinkResult[0].Success != null && !string.IsNullOrEmpty(resourceLinkResult[0].Success.Id)) + if (resourceLinkResult != null && resourceLinkResult.Length > 0 && resourceLinkResult[0].Success != null && !string.IsNullOrEmpty(resourceLinkResult[0].Success.Id)) { return resourceLinkResult[0].Success.Id; } diff --git a/src/Q42.HueApi/HueClient-Rules.cs b/src/Q42.HueApi/HueClient-Rules.cs index 7a2c5abc..19b8abe0 100644 --- a/src/Q42.HueApi/HueClient-Rules.cs +++ b/src/Q42.HueApi/HueClient-Rules.cs @@ -47,10 +47,13 @@ public async Task> GetRulesAsync() foreach (var prop in jsonResult.Properties()) { - Rule rule = JsonConvert.DeserializeObject(prop.Value.ToString()); - rule.Id = prop.Name; + Rule? rule = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (rule != null) + { + rule.Id = prop.Name; - results.Add(rule); + results.Add(rule); + } } } diff --git a/src/Q42.HueApi/HueClient-Scenes.cs b/src/Q42.HueApi/HueClient-Scenes.cs index 0bf67d3b..dae26373 100644 --- a/src/Q42.HueApi/HueClient-Scenes.cs +++ b/src/Q42.HueApi/HueClient-Scenes.cs @@ -42,10 +42,13 @@ public async Task> GetScenesAsync() foreach (var prop in jsonResult.Properties()) { - Scene scene = JsonConvert.DeserializeObject(prop.Value.ToString()); - scene.Id = prop.Name; + Scene? scene = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (scene != null) + { + scene.Id = prop.Name; - results.Add(scene); + results.Add(scene); + } } } diff --git a/src/Q42.HueApi/HueClient-Schedules.cs b/src/Q42.HueApi/HueClient-Schedules.cs index 8fc22715..160b10d7 100644 --- a/src/Q42.HueApi/HueClient-Schedules.cs +++ b/src/Q42.HueApi/HueClient-Schedules.cs @@ -36,13 +36,15 @@ public async Task> GetSchedulesAsync() //Each property is a light var jsonResult = (JObject)token; - foreach (var prop in jsonResult.Properties()) - { - Schedule newSchedule = JsonConvert.DeserializeObject(prop.Value.ToString()); - newSchedule.Id = prop.Name; - - results.Add(newSchedule); - } + foreach (var prop in jsonResult.Properties()) + { + Schedule? newSchedule = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (newSchedule != null) + { + newSchedule.Id = prop.Name; + results.Add(newSchedule); + } + } } @@ -101,9 +103,9 @@ public async Task> GetSchedulesAsync() var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); - DefaultHueResult[] scheduleResult = JsonConvert.DeserializeObject(jsonResult); + DefaultHueResult[]? scheduleResult = JsonConvert.DeserializeObject(jsonResult); - if (scheduleResult.Length > 0 && scheduleResult[0].Success != null && !string.IsNullOrEmpty(scheduleResult[0].Success.Id)) + if (scheduleResult != null && scheduleResult.Length > 0 && scheduleResult[0].Success != null && !string.IsNullOrEmpty(scheduleResult[0].Success.Id)) { return scheduleResult[0].Success.Id; } diff --git a/src/Q42.HueApi/HueClient-Sensors.cs b/src/Q42.HueApi/HueClient-Sensors.cs index 5362a9e2..13c2d9af 100644 --- a/src/Q42.HueApi/HueClient-Sensors.cs +++ b/src/Q42.HueApi/HueClient-Sensors.cs @@ -45,10 +45,13 @@ public async Task> GetSensorsAsync() foreach (var prop in jsonResult.Properties()) { - Sensor scene = JsonConvert.DeserializeObject(prop.Value.ToString()); - scene.Id = prop.Name; + Sensor? scene = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (scene != null) + { + scene.Id = prop.Name; - results.Add(scene); + results.Add(scene); + } } } @@ -76,9 +79,9 @@ public async Task> GetSensorsAsync() var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); - DefaultHueResult[] sensorResult = JsonConvert.DeserializeObject(jsonResult); + DefaultHueResult[]? sensorResult = JsonConvert.DeserializeObject(jsonResult); - if (sensorResult.Length > 0 && sensorResult[0].Success != null && !string.IsNullOrEmpty(sensorResult[0].Success.Id)) + if (sensorResult != null && sensorResult.Length > 0 && sensorResult[0].Success != null && !string.IsNullOrEmpty(sensorResult[0].Success.Id)) { var id = sensorResult[0].Success.Id; sensor.Id = id; @@ -133,10 +136,13 @@ public async Task> GetNewSensorsAsync() { if (prop.Name != "lastscan") { - Sensor newSensor = JsonConvert.DeserializeObject(prop.Value.ToString()); - newSensor.Id = prop.Name; + Sensor? newSensor = JsonConvert.DeserializeObject(prop.Value.ToString()); + if (newSensor != null) + { + newSensor.Id = prop.Name; - results.Add(newSensor); + results.Add(newSensor); + } } } diff --git a/src/Q42.HueApi/HueClient.cs b/src/Q42.HueApi/HueClient.cs index 6f88df27..6b98aa23 100644 --- a/src/Q42.HueApi/HueClient.cs +++ b/src/Q42.HueApi/HueClient.cs @@ -90,7 +90,7 @@ private void CheckInitialized() { try { - T objResult = JsonConvert.DeserializeObject(json); + T? objResult = JsonConvert.DeserializeObject(json); return objResult; @@ -118,7 +118,9 @@ protected static HueResults DeserializeDefaultHueResult(string json) try { - result = JsonConvert.DeserializeObject(json); + var jsonResult = JsonConvert.DeserializeObject(json); + if (jsonResult != null) + return jsonResult; } catch (JsonSerializationException) { @@ -139,7 +141,9 @@ private static IReadOnlyCollection DeserializeDefaultHueResult(string json try { - result = JsonConvert.DeserializeObject>(json); + var jsonResult = JsonConvert.DeserializeObject>(json); + if (jsonResult != null) + return jsonResult; } catch (JsonSerializationException) { diff --git a/src/Q42.HueApi/Models/Schedule/GenericScheduleCommand.cs b/src/Q42.HueApi/Models/Schedule/GenericScheduleCommand.cs index 1ec50dc8..2a2ff99c 100644 --- a/src/Q42.HueApi/Models/Schedule/GenericScheduleCommand.cs +++ b/src/Q42.HueApi/Models/Schedule/GenericScheduleCommand.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Q42.HueApi.Interfaces; using System; @@ -37,17 +37,17 @@ public bool IsSceneCommand() public SceneCommand AsSceneCommand() { - return JsonConvert.DeserializeObject(this.JsonString); + return JsonConvert.DeserializeObject(this.JsonString)!; } public LightCommand AsLightCommand() { - return JsonConvert.DeserializeObject(this.JsonString); + return JsonConvert.DeserializeObject(this.JsonString)!; } public SensorState AsSensorCommand() { - return JsonConvert.DeserializeObject(this.JsonString); + return JsonConvert.DeserializeObject(this.JsonString)!; } } } diff --git a/src/Q42.HueApi/Q42.HueApi.csproj b/src/Q42.HueApi/Q42.HueApi.csproj index b8748b63..900d56de 100644 --- a/src/Q42.HueApi/Q42.HueApi.csproj +++ b/src/Q42.HueApi/Q42.HueApi.csproj @@ -5,7 +5,7 @@ 8.0 enable CS8600;CS8601;CS8602;CS8603;CS8625;CS8613 - 3.17.0 + 3.18.0-beta1 Q42 Michiel Post, Q42 Open source library for interaction with the Philips Hue Bridge. Allows you to control your lights from C#. Supports .Net Standard 2.0 and .net45 @@ -20,7 +20,7 @@ - + diff --git a/src/Q42.HueApi/RemoteAuthenticationClient.cs b/src/Q42.HueApi/RemoteAuthenticationClient.cs index f97f9cbf..ff391f7d 100644 --- a/src/Q42.HueApi/RemoteAuthenticationClient.cs +++ b/src/Q42.HueApi/RemoteAuthenticationClient.cs @@ -17,7 +17,7 @@ public class RemoteAuthenticationClient : IRemoteAuthenticationClient private readonly string _clientSecret; private readonly string _appId; - private AccessTokenResponse _lastAuthorizationResponse; + private AccessTokenResponse? _lastAuthorizationResponse; private HttpClient _httpClient; /// @@ -207,16 +207,19 @@ private static string CalculateHash(string clientId, string clientSecret, string /// public async Task GetValidToken() { - if (_lastAuthorizationResponse.AccessTokenExpireTime() > DateTimeOffset.UtcNow.AddMinutes(-5)) + if (_lastAuthorizationResponse != null) { - return _lastAuthorizationResponse.Access_token; - } + if (_lastAuthorizationResponse.AccessTokenExpireTime() > DateTimeOffset.UtcNow.AddMinutes(-5)) + { + return _lastAuthorizationResponse.Access_token; + } - if (_lastAuthorizationResponse.RefreshTokenExpireTime() < DateTimeOffset.UtcNow) - { - var newToken = await this.RefreshToken(_lastAuthorizationResponse.Refresh_token).ConfigureAwait(false); + if (_lastAuthorizationResponse.RefreshTokenExpireTime() < DateTimeOffset.UtcNow) + { + var newToken = await this.RefreshToken(_lastAuthorizationResponse.Refresh_token).ConfigureAwait(false); - return newToken?.Access_token; + return newToken?.Access_token; + } } throw new HueException("Unable to get access token. Access token and Refresh token expired.");