Skip to content

Commit

Permalink
Merge pull request #71 from zivillian/qos
Browse files Browse the repository at this point in the history
add mqtt qos parameter
  • Loading branch information
zivillian committed Nov 1, 2023
2 parents ef17acf + eb65e58 commit 437e248
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ENV \
ISM7_PASSWORD= \
ISM7_MQTTUSERNAME= \
ISM7_MQTTPASSWORD= \
ISM7_MQTTQOS= \
ISM7_DISABLEJSON=false \
ISM7_RETAIN=false \
ISM7_INTERVAL=60 \
Expand Down
16 changes: 12 additions & 4 deletions src/ism7mqtt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Protocol;

namespace ism7mqtt
{
class Program
{
private static bool _useSeparateTopics = false;
private static bool _retain = false;
private static MqttQualityOfServiceLevel _qos = MqttQualityOfServiceLevel.AtMostOnce;

private static string _discoveryId = null;

Expand All @@ -31,6 +33,7 @@ static async Task Main(string[] args)
string parameter = "parameter.json";
string mqttUsername = GetEnvString("ISM7_MQTTUSERNAME");
string mqttPassword = GetEnvString("ISM7_MQTTPASSWORD");
_qos = (MqttQualityOfServiceLevel)GetEnvInt32("ISM7_MQTTQOS", 0);
_useSeparateTopics = GetEnvBool("ISM7_SEPARATE");
_retain = GetEnvBool("ISM7_RETAIN");
int interval = GetEnvInt32("ISM7_INTERVAL", 60);
Expand All @@ -43,6 +46,7 @@ static async Task Main(string[] args)
{"t|parameter=", $"path to parameter.json - defaults to {parameter}", x => parameter = x},
{"mqttuser=", "MQTT username", x => mqttUsername = x},
{"mqttpass=", "MQTT password", x => mqttPassword = x},
{"mqttqos=", "MQTT QoS", (int x) => _qos = (MqttQualityOfServiceLevel)x},
{"s|separate", "send values to separate mqtt topics - also disables json payload", x=> _useSeparateTopics = x != null},
{"retain", "retain mqtt messages", x=> _retain = x != null},
{"interval=", "push interval in seconds (defaults to 60)", (int x) => interval = x},
Expand Down Expand Up @@ -177,7 +181,8 @@ private static async Task PublishDiscoveryInfo(Ism7Config config, IMqttClient mq
.WithTopic(message.Path)
.WithPayload(data)
.WithContentType("application/json")
.WithRetainFlag();
.WithRetainFlag()
.WithQualityOfServiceLevel(_qos);
var payload = builder
.Build();
await mqttClient.PublishAsync(payload, cancellationToken);
Expand Down Expand Up @@ -231,7 +236,8 @@ private static async Task OnMessage(IMqttClient client, Ism7Config config, bool
var builder = new MqttApplicationMessageBuilder()
.WithTopic(message.Path)
.WithPayload(data)
.WithContentType("application/json");
.WithContentType("application/json")
.WithQualityOfServiceLevel(_qos);
if (_retain)
builder = builder.WithRetainFlag();
var payload = builder
Expand All @@ -249,8 +255,10 @@ private static async Task OnMessage(IMqttClient client, Ism7Config config, bool
foreach (var message in messages)
{
var topic = message.Path;
var builder = new MqttApplicationMessageBuilder().WithTopic(topic)
.WithPayload(message.Content);
var builder = new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(message.Content)
.WithQualityOfServiceLevel(_qos);
if (_retain)
builder = builder.WithRetainFlag();
var payload = builder.Build();
Expand Down

0 comments on commit 437e248

Please sign in to comment.