/// <summary> /// Handler for Google Home QUERY intent. /// </summary> /// <param name="queryIntentReceivedEvent">The QUERY intent to handle.</param> private async void HandleGoogleHomeQueryIntent(QueryIntentReceivedEvent queryIntentReceivedEvent) { var delegateTopic = $"{TopicRoot}/query/lastRequest"; var delegatePayload = JsonConvert.SerializeObject(queryIntentReceivedEvent); await MqttClient.PublishAsync(new MqttApplicationMessageBuilder() .WithTopic(delegateTopic) .WithPayload(delegatePayload) .WithAtLeastOnceQoS() .Build()) .ConfigureAwait(false); }
/// <summary> /// Handles a <see cref="Models.Request.QueryIntent"/>. /// </summary> /// <param name="intent">Intent to process.</param> /// <returns>A <see cref="Models.Response.QueryResponsePayload"/>.</returns> public Models.Response.QueryResponsePayload Handle(Models.Request.QueryIntent intent) { _log.LogInformation("Received QUERY intent for devices: " + string.Join(", ", intent.Payload.Devices.Select(x => x.Id))); // Payload for disabled a or missing device var offlineDeviceState = new Dictionary <string, object> { { "errorCode", "deviceNotFound" }, { "online", false }, { "status", "ERROR" } }; // Get distinct devices var distinctRequestDeviceIds = intent.Payload.Devices .GroupBy(device => device.Id) .Select(group => group.First()) .Select(device => device.Id); // Convert to an event to publish var commandEvent = new QueryIntentReceivedEvent { Devices = intent.Payload.Devices, Time = DateTimeOffset.Now }; _messageHub.Publish(commandEvent); var devices = _deviceRepository.GetAll() .Where(device => !device.Disabled) .Where(device => distinctRequestDeviceIds.Contains(device.Id)) .ToList(); // Build reponse payload var queryResponsePayload = new Models.Response.QueryResponsePayload { Devices = distinctRequestDeviceIds .ToDictionary( queryDeviceId => queryDeviceId, queryDeviceId => { var device = devices.FirstOrDefault(x => x.Id == queryDeviceId); var results = device != null ? device.GetGoogleState(_stateCache) : offlineDeviceState; // Add explicit status if not specified by state mappings if (!results.ContainsKey("status")) { results.Add("status", "SUCCESS"); } return(results); }) }; // Trigger reportState before returning var reportStateDevices = devices.Where(device => device.WillReportState).ToList(); var shouldTriggerReportState = false; if (reportStateDevices.Any() && shouldTriggerReportState) { _messageHub.Publish(new ReportStateEvent { Devices = reportStateDevices }); } return(queryResponsePayload); }