Skip to content

Commit e566d4d

Browse files
committed
Addresses #122, alert to MQTT topic for SYNC and QUERY requests
1 parent 82aeb3e commit e566d4d

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

src/HomeAutio.Mqtt.GoogleHome/IntentHandlers/ExecuteIntentHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public Models.Response.ExecutionResponsePayload Handle(Models.Request.ExecuteInt
131131
{
132132
foreach (var execution in command.Execution)
133133
{
134-
// Convert command to a event to publish now that its passed all verifications
134+
// Convert command to an event to publish now that its passed all verifications
135135
var commandEvent = new DeviceCommandExecutionEvent { DeviceId = deviceId, Execution = execution };
136136
_messageHub.Publish(commandEvent);
137137

src/HomeAutio.Mqtt.GoogleHome/IntentHandlers/QueryIntentHandler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public Models.Response.QueryResponsePayload Handle(Models.Request.QueryIntent in
6161
.Select(group => group.First())
6262
.Select(device => device.Id);
6363

64+
// Convert to an event to publish
65+
var commandEvent = new QueryIntentReceivedEvent { Devices = intent.Payload.Devices, Time = DateTimeOffset.Now };
66+
_messageHub.Publish(commandEvent);
67+
6468
var devices = _deviceRepository.GetAll()
6569
.Where(device => !device.Disabled)
6670
.Where(device => distinctRequestDeviceIds.Contains(device.Id))

src/HomeAutio.Mqtt.GoogleHome/IntentHandlers/SyncIntentHandler.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Linq;
3+
using Easy.MessageHub;
4+
using HomeAutio.Mqtt.GoogleHome.Models.Events;
35
using Microsoft.Extensions.Configuration;
46
using Microsoft.Extensions.Logging;
57

@@ -12,21 +14,25 @@ public class SyncIntentHandler
1214
{
1315
private readonly ILogger<SyncIntentHandler> _log;
1416

17+
private readonly IMessageHub _messageHub;
1518
private readonly IConfiguration _config;
1619
private readonly IGoogleDeviceRepository _deviceRepository;
1720

1821
/// <summary>
1922
/// Initializes a new instance of the <see cref="SyncIntentHandler"/> class.
2023
/// </summary>
2124
/// <param name="logger">Logging instance.</param>
25+
/// <param name="messageHub">Message nhub.</param>
2226
/// <param name="configuration">Configuration.</param>
2327
/// <param name="deviceRepository">Device repository.</param>
2428
public SyncIntentHandler(
2529
ILogger<SyncIntentHandler> logger,
30+
IMessageHub messageHub,
2631
IConfiguration configuration,
2732
IGoogleDeviceRepository deviceRepository)
2833
{
2934
_log = logger ?? throw new ArgumentException(nameof(logger));
35+
_messageHub = messageHub ?? throw new ArgumentException(nameof(messageHub));
3036
_config = configuration ?? throw new ArgumentException(nameof(configuration));
3137
_deviceRepository = deviceRepository ?? throw new ArgumentException(nameof(deviceRepository));
3238
}
@@ -40,6 +46,10 @@ public Models.Response.SyncResponsePayload Handle(Models.Request.SyncIntent inte
4046
{
4147
_log.LogInformation("Received SYNC intent");
4248

49+
// Convert to an event to publish
50+
var commandEvent = new SyncIntentReceivedEvent { Time = DateTimeOffset.Now };
51+
_messageHub.Publish(commandEvent);
52+
4353
var syncResponsePayload = new Models.Response.SyncResponsePayload
4454
{
4555
AgentUserId = _config.GetValue<string>("googleHomeGraph:agentUserId"),
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace HomeAutio.Mqtt.GoogleHome.Models.Events
5+
{
6+
/// <summary>
7+
/// Query intent received event.
8+
/// </summary>
9+
public class QueryIntentReceivedEvent
10+
{
11+
/// <summary>
12+
/// Devices.
13+
/// </summary>
14+
public IList<Models.Request.Device> Devices { get; set; }
15+
16+
/// <summary>
17+
/// Time of event.
18+
/// </summary>
19+
public DateTimeOffset Time { get; set; }
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace HomeAutio.Mqtt.GoogleHome.Models.Events
4+
{
5+
/// <summary>
6+
/// Sync intent received event.
7+
/// </summary>
8+
public class SyncIntentReceivedEvent
9+
{
10+
/// <summary>
11+
/// Time of event.
12+
/// </summary>
13+
public DateTimeOffset Time { get; set; }
14+
}
15+
}

src/HomeAutio.Mqtt.GoogleHome/Services/MqttService.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ public MqttService(
6969
protected override Task StartServiceAsync(CancellationToken cancellationToken)
7070
{
7171
// Subscribe to event aggregator
72-
_messageHubSubscriptions.Add(_messageHub.Subscribe<DeviceCommandExecutionEvent>((e) => HandleGoogleHomeCommand(e)));
7372
_messageHubSubscriptions.Add(_messageHub.Subscribe<ConfigSubscriptionChangeEvent>((e) => HandleConfigSubscriptionChange(e)));
73+
_messageHubSubscriptions.Add(_messageHub.Subscribe<DeviceCommandExecutionEvent>((e) => HandleGoogleHomeCommand(e)));
74+
_messageHubSubscriptions.Add(_messageHub.Subscribe<SyncIntentReceivedEvent>((e) => HandleGoogleHomeSyncIntent(e)));
75+
_messageHubSubscriptions.Add(_messageHub.Subscribe<QueryIntentReceivedEvent>((e) => HandleGoogleHomeQueryIntent(e)));
7476

7577
return Task.CompletedTask;
7678
}
@@ -174,7 +176,7 @@ await MqttClient.SubscribeAsync(
174176
}
175177

176178
/// <summary>
177-
/// Hanlder for Google Home commands.
179+
/// Handler for Google Home commands.
178180
/// </summary>
179181
/// <param name="deviceCommandExecutionEvent">The device command to handle.</param>
180182
private async void HandleGoogleHomeCommand(DeviceCommandExecutionEvent deviceCommandExecutionEvent)
@@ -262,6 +264,40 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
262264
}
263265
}
264266

267+
/// <summary>
268+
/// Handler for Google Home SYNC intent.
269+
/// </summary>
270+
/// <param name="syncIntentReceivedEvent">The SYNC intent to handle.</param>
271+
private async void HandleGoogleHomeSyncIntent(SyncIntentReceivedEvent syncIntentReceivedEvent)
272+
{
273+
var delegateTopic = $"{TopicRoot}/sync/lastRequest";
274+
var delegatePayload = syncIntentReceivedEvent.Time.ToString();
275+
276+
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
277+
.WithTopic(delegateTopic)
278+
.WithPayload(delegatePayload)
279+
.WithAtLeastOnceQoS()
280+
.Build())
281+
.ConfigureAwait(false);
282+
}
283+
284+
/// <summary>
285+
/// Handler for Google Home QUERY intent.
286+
/// </summary>
287+
/// <param name="queryIntentReceivedEvent">The QUERY intent to handle.</param>
288+
private async void HandleGoogleHomeQueryIntent(QueryIntentReceivedEvent queryIntentReceivedEvent)
289+
{
290+
var delegateTopic = $"{TopicRoot}/query/lastRequest";
291+
var delegatePayload = JsonConvert.SerializeObject(queryIntentReceivedEvent);
292+
293+
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
294+
.WithTopic(delegateTopic)
295+
.WithPayload(delegatePayload)
296+
.WithAtLeastOnceQoS()
297+
.Build())
298+
.ConfigureAwait(false);
299+
}
300+
265301
#endregion
266302

267303
#region IDisposable Support

0 commit comments

Comments
 (0)