Exemple #1
0
        /// <inheritdoc />
        protected override void Mqtt_MqttMsgPublishReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
        {
            var message = e.ApplicationMessage.ConvertPayloadToString();

            _log.LogInformation("MQTT message received for topic " + e.ApplicationMessage.Topic + ": " + message);

            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] = message;

                // 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();
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// ReportStateAndNotification event handler.
 /// </summary>
 /// <param name="reportStateEvent">Report state event to handle.</param>
 private async void HandleGoogleReportState(ReportStateEvent reportStateEvent)
 {
     // Send updated to Google Home Graph
     if (reportStateEvent.Devices.Count() > 0)
     {
         await _googleHomeGraphClient.SendUpdatesAsync(reportStateEvent.Devices, _stateCache);
     }
 }