From 2cc342eff7c39113689d2fa78ebe7173d35dac7d Mon Sep 17 00:00:00 2001 From: Michael Hallock Date: Sun, 22 Jul 2018 20:39:37 -0400 Subject: [PATCH] Closes #9, REQUEST_SYNC support, and Closes #14, better logging around transactions --- src/HomeAutio.Mqtt.GoogleHome/.gitignore | 7 ++-- .../GoogleHomeGraphClient.cs | 14 ++++++++ src/HomeAutio.Mqtt.GoogleHome/MqttService.cs | 32 ++++++++++--------- src/HomeAutio.Mqtt.GoogleHome/Startup.cs | 18 +++++------ 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/HomeAutio.Mqtt.GoogleHome/.gitignore b/src/HomeAutio.Mqtt.GoogleHome/.gitignore index 4ac2506..ba14cd0 100644 --- a/src/HomeAutio.Mqtt.GoogleHome/.gitignore +++ b/src/HomeAutio.Mqtt.GoogleHome/.gitignore @@ -1,3 +1,4 @@ -tempkey.rsa -tokens.json -appsettings.Development.json +config/tempkey.rsa +config/tokens.json +config/googleDevices.json +appsettings.*.json diff --git a/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs b/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs index 4d164fe..21969af 100644 --- a/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs +++ b/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs @@ -58,6 +58,13 @@ public GoogleHomeGraphClient( /// An awaitable . public async Task RequestSyncAsync() { + // If no api key has been provided, don't attempt to call + if (string.IsNullOrEmpty(_googleHomeGraphApiKey)) + { + _log.LogWarning("REQUEST_SYNC triggered but Google Home Graph API was blank"); + return; + } + var request = new Request { AgentUserId = _agentUserId @@ -84,6 +91,13 @@ public async Task RequestSyncAsync() /// An awaitable . public async Task SendUpdatesAsync(IList devices, IDictionary stateCache) { + // If no service account has been provided, don't attempt to call + if (_serviceAccount == null) + { + _log.LogWarning("WillReportState triggered but Google Home Graph serviceAccountFile setting was blank, or the file didn't exist"); + return; + } + // Ensure access token is available if (_accessToken == null || _accessToken.ExpiresAt <= DateTime.Now.AddMinutes(-1)) { diff --git a/src/HomeAutio.Mqtt.GoogleHome/MqttService.cs b/src/HomeAutio.Mqtt.GoogleHome/MqttService.cs index 66338d1..84f1359 100644 --- a/src/HomeAutio.Mqtt.GoogleHome/MqttService.cs +++ b/src/HomeAutio.Mqtt.GoogleHome/MqttService.cs @@ -86,25 +86,27 @@ protected override Task StopServiceAsync(CancellationToken cancellationToken) /// protected override void Mqtt_MqttMsgPublishReceived(object sender, MqttApplicationMessageReceivedEventArgs e) { - if (_stateCache.ContainsKey(e.ApplicationMessage.Topic)) + if (e.ApplicationMessage.Topic == TopicRoot + "REQUEST_SYNC") + { + // Handle REQUEST_SYNC + _googleHomeGraphClient.RequestSyncAsync() + .GetAwaiter().GetResult(); + } + else if (_stateCache.ContainsKey(e.ApplicationMessage.Topic)) { _stateCache[e.ApplicationMessage.Topic] = e.ApplicationMessage.ConvertPayloadToString(); - // Handle reportState - if (_googleHomeGraphClient != null) + // Identify devices that handle reportState + var devices = _deviceConfig.Values + .Where(x => x.WillReportState) + .Where(x => x.Traits.Any(trait => trait.State.Values.Any(state => state.Topic == e.ApplicationMessage.Topic))) + .ToList(); + + // Send updated to Google Home Graph + if (devices.Count() > 0) { - // Identify devices that handle reportState - var devices = _deviceConfig.Values - .Where(x => x.WillReportState) - .Where(x => x.Traits.Any(trait => trait.State.Values.Any(state => state.Topic == e.ApplicationMessage.Topic))) - .ToList(); - - // Send updated to Google Home Graph - if (devices.Count() > 0) - { - _googleHomeGraphClient.SendUpdatesAsync(devices, _stateCache) - .GetAwaiter().GetResult(); - } + _googleHomeGraphClient.SendUpdatesAsync(devices, _stateCache) + .GetAwaiter().GetResult(); } } } diff --git a/src/HomeAutio.Mqtt.GoogleHome/Startup.cs b/src/HomeAutio.Mqtt.GoogleHome/Startup.cs index fb1f4a3..5023a3b 100644 --- a/src/HomeAutio.Mqtt.GoogleHome/Startup.cs +++ b/src/HomeAutio.Mqtt.GoogleHome/Startup.cs @@ -82,20 +82,20 @@ public void ConfigureServices(IServiceCollection services) // Google Home Graph client services.AddSingleton(serviceProvider => { + ServiceAccount serviceAccount = null; var googleHomeServiceAccountFile = Configuration.GetValue("googleHomeGraph:serviceAccountFile"); if (!string.IsNullOrEmpty(googleHomeServiceAccountFile) && File.Exists(googleHomeServiceAccountFile)) { var googleHomeServiceAccountFileContents = File.ReadAllText(googleHomeServiceAccountFile); - var serviceAccount = JsonConvert.DeserializeObject(googleHomeServiceAccountFileContents); - return new GoogleHomeGraphClient( - serviceProvider.GetRequiredService>(), - serviceProvider.GetRequiredService(), - serviceAccount, - Configuration.GetValue("googleHomeGraph:agentUserId"), - Configuration.GetValue("googleHomeGraph:apiKey")); + serviceAccount = JsonConvert.DeserializeObject(googleHomeServiceAccountFileContents); } - return null; + return new GoogleHomeGraphClient( + serviceProvider.GetRequiredService>(), + serviceProvider.GetRequiredService(), + serviceAccount, + Configuration.GetValue("googleHomeGraph:agentUserId"), + Configuration.GetValue("googleHomeGraph:apiKey")); }); // Setup client @@ -114,7 +114,7 @@ public void ConfigureServices(IServiceCollection services) serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService(), - serviceProvider.GetService(), + serviceProvider.GetRequiredService(), brokerSettings); });