Skip to content

Commit

Permalink
Closes #9, REQUEST_SYNC support, and Closes #14, better logging aroun…
Browse files Browse the repository at this point in the history
…d transactions
  • Loading branch information
i8beef committed Jul 23, 2018
1 parent 2e1a544 commit 2cc342e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/HomeAutio.Mqtt.GoogleHome/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tempkey.rsa
tokens.json
appsettings.Development.json
config/tempkey.rsa
config/tokens.json
config/googleDevices.json
appsettings.*.json
14 changes: 14 additions & 0 deletions src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public GoogleHomeGraphClient(
/// <returns>An awaitable <see cref="Task"/>.</returns>
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
Expand All @@ -84,6 +91,13 @@ public async Task RequestSyncAsync()
/// <returns>An awaitable <see cref="Task"/>.</returns>
public async Task SendUpdatesAsync(IList<Models.State.Device> devices, IDictionary<string, string> 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))
{
Expand Down
32 changes: 17 additions & 15 deletions src/HomeAutio.Mqtt.GoogleHome/MqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,27 @@ protected override Task StopServiceAsync(CancellationToken cancellationToken)
/// <inheritdoc />
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();
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/HomeAutio.Mqtt.GoogleHome/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ public void ConfigureServices(IServiceCollection services)
// Google Home Graph client
services.AddSingleton<GoogleHomeGraphClient>(serviceProvider =>
{
ServiceAccount serviceAccount = null;
var googleHomeServiceAccountFile = Configuration.GetValue<string>("googleHomeGraph:serviceAccountFile");
if (!string.IsNullOrEmpty(googleHomeServiceAccountFile) && File.Exists(googleHomeServiceAccountFile))
{
var googleHomeServiceAccountFileContents = File.ReadAllText(googleHomeServiceAccountFile);
var serviceAccount = JsonConvert.DeserializeObject<ServiceAccount>(googleHomeServiceAccountFileContents);
return new GoogleHomeGraphClient(
serviceProvider.GetRequiredService<ILogger<GoogleHomeGraphClient>>(),
serviceProvider.GetRequiredService<IHttpClientFactory>(),
serviceAccount,
Configuration.GetValue<string>("googleHomeGraph:agentUserId"),
Configuration.GetValue<string>("googleHomeGraph:apiKey"));
serviceAccount = JsonConvert.DeserializeObject<ServiceAccount>(googleHomeServiceAccountFileContents);
}

return null;
return new GoogleHomeGraphClient(
serviceProvider.GetRequiredService<ILogger<GoogleHomeGraphClient>>(),
serviceProvider.GetRequiredService<IHttpClientFactory>(),
serviceAccount,
Configuration.GetValue<string>("googleHomeGraph:agentUserId"),
Configuration.GetValue<string>("googleHomeGraph:apiKey"));
});

// Setup client
Expand All @@ -114,7 +114,7 @@ public void ConfigureServices(IServiceCollection services)
serviceProvider.GetRequiredService<DeviceConfiguration>(),
serviceProvider.GetRequiredService<StateCache>(),
serviceProvider.GetRequiredService<IMessageHub>(),
serviceProvider.GetService<GoogleHomeGraphClient>(),
serviceProvider.GetRequiredService<GoogleHomeGraphClient>(),
brokerSettings);
});

Expand Down

0 comments on commit 2cc342e

Please sign in to comment.