private async Task SendValue(string topic, object value, CancellationToken token) { bool log = _logger.IsEnabled(LogLevel.Debug); object sentValue; if (value is string str) { sentValue = str; await _mqttClient.SendValueAsync(topic, str, token); } else if (value == null) { sentValue = "<null>"; await _mqttClient.SendValueAsync(topic, string.Empty, token); } else { sentValue = null; JToken converted = JToken.FromObject(value, CustomJsonSerializer.Serializer); await _mqttClient.SendJsonAsync(topic, converted, token); if (log) { sentValue = converted.ToString(Formatting.None); } } if (log) { _logger.LogDebug("Pushed {Value} to {Topic}", sentValue, topic); } }
async Task IMqttEventReceiver.OnConnect(MqttClientConnectedEventArgs args, CancellationToken token) { // Hook to on connect // Connected: Push "ok" message // Testament (When disconnected): Leave "problem" message if (token.IsCancellationRequested) { return; } await _mqttClient.SendValueAsync(StateTopic, OkMessage, token); }