• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

net-daemon / netdaemon / 6594659689

19 Oct 2023 05:58PM UTC coverage: 80.941% (-0.3%) from 81.222%
6594659689

push

github

web-flow
Fix alot of warnings (#956)

811 of 1148 branches covered (0.0%)

Branch coverage included in aggregate %.

28 of 28 new or added lines in 15 files covered. (100.0%)

2939 of 3485 relevant lines covered (84.33%)

50.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.21
/src/Extensions/NetDaemon.Extensions.MqttEntityManager/MessageSender.cs
1
#region
2

3
using Microsoft.Extensions.Logging;
4
using MQTTnet;
5
using MQTTnet.Extensions.ManagedClient;
6
using MQTTnet.Protocol;
7
using NetDaemon.Extensions.MqttEntityManager.Exceptions;
8

9
#endregion
10

11
namespace NetDaemon.Extensions.MqttEntityManager;
12

13
/// <summary>
14
///     Manage connections and message publishing to MQTT
15
/// </summary>
16
internal class MessageSender : IMessageSender
17
{
18
    private readonly ILogger<MessageSender> _logger;
19
    private readonly IAssuredMqttConnection _assuredMqttConnection;
20

21
    /// <summary>
22
    ///     Manage connections and message publishing to MQTT
23
    /// </summary>
24
    /// <param name="logger"></param>
25
    /// <param name="assuredMqttConnection"></param>
26
    public MessageSender(ILogger<MessageSender> logger, IAssuredMqttConnection assuredMqttConnection)
25✔
27
    {
28
        _logger = logger;
25✔
29
        _assuredMqttConnection = assuredMqttConnection;
25✔
30
    }
25✔
31

32
    /// <summary>
33
    ///     Publish a message to the given topic
34
    /// </summary>
35
    /// <param name="topic"></param>
36
    /// <param name="payload">Json structure of payload</param>
37
    /// <param name="retain"></param>
38
    /// <param name="qos"></param>
39
    public async Task SendMessageAsync(string topic, string payload, bool retain, MqttQualityOfServiceLevel qos)
40
    {
41
        var mqttClient = await _assuredMqttConnection.GetClientAsync();
25✔
42

43
        await PublishMessage(mqttClient, topic, payload, retain, qos);
25✔
44
    }
25✔
45

46
    private async Task PublishMessage(IManagedMqttClient mqttClient, string topic, string payload, bool retain,
47
        MqttQualityOfServiceLevel qos)
48
    {
49
        var message = new MqttApplicationMessageBuilder().WithTopic(topic)
25✔
50
            .WithPayload(payload)
25✔
51
            .WithRetainFlag(retain)
25✔
52
            .WithQualityOfServiceLevel(qos)
25✔
53
            .Build();
25✔
54

55
        _logger.LogTrace("MQTT sending to {Topic}: {Message}", message.Topic, message.ConvertPayloadToString());
25✔
56

57
        try
58
        {
59
            await mqttClient.EnqueueAsync(message).ConfigureAwait(false);
25✔
60
        }
25✔
61
        catch (Exception e)
×
62
        {
63
            _logger.LogError(e, "Failed to publish MQTT message to to {Topic}: {Message}", message.Topic, message.ConvertPayloadToString());
×
64
            throw new MqttPublishException(e.Message, e);
×
65
        }
66
    }
25✔
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc