Exemple #1
0
    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);
        }
    }