diff --git a/addon/Easywave2MQTT/CHANGELOG.md b/addon/Easywave2MQTT/CHANGELOG.md
index c17d3c0..9f4ee5e 100644
--- a/addon/Easywave2MQTT/CHANGELOG.md
+++ b/addon/Easywave2MQTT/CHANGELOG.md
@@ -9,3 +9,5 @@
| 0.5 beta | Allow addon to start when no serial ports are found, or when a non-existing port was specified |
| 0.5.2 beta | Fix crash when serialport is throwing IOException for a timeout, instead of a TimeoutException |
| 0.5.3 beta | Fix bug in detection mechanism for button repeats |
+| 0.6 | Automatically reconnect to MQTT server when connection breaks
Persist state of lights in MQTT|
+
diff --git a/addon/Easywave2MQTT/app/Easywave2Mqtt.deps.json b/addon/Easywave2MQTT/app/Easywave2Mqtt.deps.json
index eb2b411..4fd2b83 100644
--- a/addon/Easywave2MQTT/app/Easywave2Mqtt.deps.json
+++ b/addon/Easywave2MQTT/app/Easywave2Mqtt.deps.json
@@ -10,8 +10,8 @@
"dependencies": {
"InMemoryBus": "1.0.0",
"JetBrains.Annotations": "2022.3.1",
- "MQTTnet.Extensions.ManagedClient": "4.1.4.563",
- "Microsoft.Extensions.Hosting": "7.0.0",
+ "MQTTnet": "4.1.4.563",
+ "Microsoft.Extensions.Hosting": "7.0.1",
"Microsoft.Extensions.Hosting.Systemd": "7.0.0",
"Microsoft.Extensions.Hosting.WindowsServices": "7.0.0",
"Serilog.Extensions.Hosting": "5.0.1",
@@ -22,7 +22,14 @@
"Easywave2Mqtt.dll": {}
}
},
- "JetBrains.Annotations/2022.3.1": {},
+ "JetBrains.Annotations/2022.3.1": {
+ "runtime": {
+ "lib/netstandard2.0/JetBrains.Annotations.dll": {
+ "assemblyVersion": "4242.42.42.42",
+ "fileVersion": "2022.3.1.0"
+ }
+ }
+ },
"Microsoft.Extensions.Configuration/7.0.0": {
"dependencies": {
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
@@ -46,14 +53,14 @@
}
}
},
- "Microsoft.Extensions.Configuration.Binder/7.0.0": {
+ "Microsoft.Extensions.Configuration.Binder/7.0.3": {
"dependencies": {
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0"
},
"runtime": {
"lib/net7.0/Microsoft.Extensions.Configuration.Binder.dll": {
"assemblyVersion": "7.0.0.0",
- "fileVersion": "7.0.22.51805"
+ "fileVersion": "7.0.323.6910"
}
}
},
@@ -176,11 +183,11 @@
}
}
},
- "Microsoft.Extensions.Hosting/7.0.0": {
+ "Microsoft.Extensions.Hosting/7.0.1": {
"dependencies": {
"Microsoft.Extensions.Configuration": "7.0.0",
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
- "Microsoft.Extensions.Configuration.Binder": "7.0.0",
+ "Microsoft.Extensions.Configuration.Binder": "7.0.3",
"Microsoft.Extensions.Configuration.CommandLine": "7.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "7.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "7.0.0",
@@ -198,12 +205,13 @@
"Microsoft.Extensions.Logging.Debug": "7.0.0",
"Microsoft.Extensions.Logging.EventLog": "7.0.0",
"Microsoft.Extensions.Logging.EventSource": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0"
+ "Microsoft.Extensions.Options": "7.0.1",
+ "System.Diagnostics.DiagnosticSource": "7.0.1"
},
"runtime": {
"lib/net7.0/Microsoft.Extensions.Hosting.dll": {
"assemblyVersion": "7.0.0.0",
- "fileVersion": "7.0.22.51805"
+ "fileVersion": "7.0.323.6910"
}
}
},
@@ -222,7 +230,7 @@
},
"Microsoft.Extensions.Hosting.Systemd/7.0.0": {
"dependencies": {
- "Microsoft.Extensions.Hosting": "7.0.0"
+ "Microsoft.Extensions.Hosting": "7.0.1"
},
"runtime": {
"lib/net7.0/Microsoft.Extensions.Hosting.Systemd.dll": {
@@ -233,7 +241,7 @@
},
"Microsoft.Extensions.Hosting.WindowsServices/7.0.0": {
"dependencies": {
- "Microsoft.Extensions.Hosting": "7.0.0",
+ "Microsoft.Extensions.Hosting": "7.0.1",
"Microsoft.Extensions.Logging.EventLog": "7.0.0",
"System.ServiceProcess.ServiceController": "7.0.0"
},
@@ -249,7 +257,7 @@
"Microsoft.Extensions.DependencyInjection": "7.0.0",
"Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0",
"Microsoft.Extensions.Logging.Abstractions": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0"
+ "Microsoft.Extensions.Options": "7.0.1"
},
"runtime": {
"lib/net7.0/Microsoft.Extensions.Logging.dll": {
@@ -270,11 +278,11 @@
"dependencies": {
"Microsoft.Extensions.Configuration": "7.0.0",
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
- "Microsoft.Extensions.Configuration.Binder": "7.0.0",
+ "Microsoft.Extensions.Configuration.Binder": "7.0.3",
"Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0",
"Microsoft.Extensions.Logging": "7.0.0",
"Microsoft.Extensions.Logging.Abstractions": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0",
+ "Microsoft.Extensions.Options": "7.0.1",
"Microsoft.Extensions.Options.ConfigurationExtensions": "7.0.0"
},
"runtime": {
@@ -290,7 +298,7 @@
"Microsoft.Extensions.Logging": "7.0.0",
"Microsoft.Extensions.Logging.Abstractions": "7.0.0",
"Microsoft.Extensions.Logging.Configuration": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0",
+ "Microsoft.Extensions.Options": "7.0.1",
"System.Text.Json": "7.0.0"
},
"runtime": {
@@ -318,7 +326,7 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0",
"Microsoft.Extensions.Logging": "7.0.0",
"Microsoft.Extensions.Logging.Abstractions": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0",
+ "Microsoft.Extensions.Options": "7.0.1",
"System.Diagnostics.EventLog": "7.0.0"
},
"runtime": {
@@ -333,7 +341,7 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0",
"Microsoft.Extensions.Logging": "7.0.0",
"Microsoft.Extensions.Logging.Abstractions": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0",
+ "Microsoft.Extensions.Options": "7.0.1",
"Microsoft.Extensions.Primitives": "7.0.0",
"System.Text.Json": "7.0.0"
},
@@ -344,7 +352,7 @@
}
}
},
- "Microsoft.Extensions.Options/7.0.0": {
+ "Microsoft.Extensions.Options/7.0.1": {
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0",
"Microsoft.Extensions.Primitives": "7.0.0"
@@ -352,16 +360,16 @@
"runtime": {
"lib/net7.0/Microsoft.Extensions.Options.dll": {
"assemblyVersion": "7.0.0.0",
- "fileVersion": "7.0.22.51805"
+ "fileVersion": "7.0.323.6910"
}
}
},
"Microsoft.Extensions.Options.ConfigurationExtensions/7.0.0": {
"dependencies": {
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
- "Microsoft.Extensions.Configuration.Binder": "7.0.0",
+ "Microsoft.Extensions.Configuration.Binder": "7.0.3",
"Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0",
- "Microsoft.Extensions.Options": "7.0.0",
+ "Microsoft.Extensions.Options": "7.0.1",
"Microsoft.Extensions.Primitives": "7.0.0"
},
"runtime": {
@@ -387,17 +395,6 @@
}
}
},
- "MQTTnet.Extensions.ManagedClient/4.1.4.563": {
- "dependencies": {
- "MQTTnet": "4.1.4.563"
- },
- "runtime": {
- "lib/net6.0/MQTTnet.Extensions.ManagedClient.dll": {
- "assemblyVersion": "4.1.4.563",
- "fileVersion": "4.1.4.563"
- }
- }
- },
"runtime.linux-arm.runtime.native.System.IO.Ports/7.0.0": {
"runtimeTargets": {
"runtimes/linux-arm/native/libSystem.IO.Ports.Native.so": {
@@ -498,6 +495,7 @@
}
}
},
+ "System.Diagnostics.DiagnosticSource/7.0.1": {},
"System.Diagnostics.EventLog/7.0.0": {
"runtime": {
"lib/net7.0/System.Diagnostics.EventLog.dll": {
@@ -604,12 +602,12 @@
"path": "microsoft.extensions.configuration.abstractions/7.0.0",
"hashPath": "microsoft.extensions.configuration.abstractions.7.0.0.nupkg.sha512"
},
- "Microsoft.Extensions.Configuration.Binder/7.0.0": {
+ "Microsoft.Extensions.Configuration.Binder/7.0.3": {
"type": "package",
"serviceable": true,
- "sha512": "sha512-tgU4u7bZsoS9MKVRiotVMAwHtbREHr5/5zSEV+JPhg46+ox47Au84E3D2IacAaB0bk5ePNaNieTlPrfjbbRJkg==",
- "path": "microsoft.extensions.configuration.binder/7.0.0",
- "hashPath": "microsoft.extensions.configuration.binder.7.0.0.nupkg.sha512"
+ "sha512": "sha512-1eRFwJBrkkncTpvh6mivB8zg4uBVm6+Y6stEJERrVEqZZc8Hvf+N1iIgj2ySYDUQko4J1Gw1rLf1M8bG83F0eA==",
+ "path": "microsoft.extensions.configuration.binder/7.0.3",
+ "hashPath": "microsoft.extensions.configuration.binder.7.0.3.nupkg.sha512"
},
"Microsoft.Extensions.Configuration.CommandLine/7.0.0": {
"type": "package",
@@ -681,12 +679,12 @@
"path": "microsoft.extensions.filesystemglobbing/7.0.0",
"hashPath": "microsoft.extensions.filesystemglobbing.7.0.0.nupkg.sha512"
},
- "Microsoft.Extensions.Hosting/7.0.0": {
+ "Microsoft.Extensions.Hosting/7.0.1": {
"type": "package",
"serviceable": true,
- "sha512": "sha512-4nFc8xCfK26G524ioreZvz/IeIKN/gY1LApoGpaIThKqBdTwauUo4ETCf12lQcoefijqe3Imnfvnk31IezFatg==",
- "path": "microsoft.extensions.hosting/7.0.0",
- "hashPath": "microsoft.extensions.hosting.7.0.0.nupkg.sha512"
+ "sha512": "sha512-aoeMou6XSW84wiqd895OdaGyO9PfH6nohQJ0XBcshRDafbdIU6PQIVl8TpOCssPYq3ciRseP5064hbFyCR9J9w==",
+ "path": "microsoft.extensions.hosting/7.0.1",
+ "hashPath": "microsoft.extensions.hosting.7.0.1.nupkg.sha512"
},
"Microsoft.Extensions.Hosting.Abstractions/7.0.0": {
"type": "package",
@@ -758,12 +756,12 @@
"path": "microsoft.extensions.logging.eventsource/7.0.0",
"hashPath": "microsoft.extensions.logging.eventsource.7.0.0.nupkg.sha512"
},
- "Microsoft.Extensions.Options/7.0.0": {
+ "Microsoft.Extensions.Options/7.0.1": {
"type": "package",
"serviceable": true,
- "sha512": "sha512-lP1yBnTTU42cKpMozuafbvNtQ7QcBjr/CcK3bYOGEMH55Fjt+iecXjT6chR7vbgCMqy3PG3aNQSZgo/EuY/9qQ==",
- "path": "microsoft.extensions.options/7.0.0",
- "hashPath": "microsoft.extensions.options.7.0.0.nupkg.sha512"
+ "sha512": "sha512-pZRDYdN1FpepOIfHU62QoBQ6zdAoTvnjxFfqAzEd9Jhb2dfhA5i6jeTdgGgcgTWFRC7oT0+3XrbQu4LjvgX1Nw==",
+ "path": "microsoft.extensions.options/7.0.1",
+ "hashPath": "microsoft.extensions.options.7.0.1.nupkg.sha512"
},
"Microsoft.Extensions.Options.ConfigurationExtensions/7.0.0": {
"type": "package",
@@ -786,13 +784,6 @@
"path": "mqttnet/4.1.4.563",
"hashPath": "mqttnet.4.1.4.563.nupkg.sha512"
},
- "MQTTnet.Extensions.ManagedClient/4.1.4.563": {
- "type": "package",
- "serviceable": true,
- "sha512": "sha512-QgrSDz12YnVUdbV5Wu1tP9sgTRUJbN4jZUsHC2Bh93tAPf+6vPaeq2WAEPoyJ7MjCadyGSW0n4QFyCjgNG8Odg==",
- "path": "mqttnet.extensions.managedclient/4.1.4.563",
- "hashPath": "mqttnet.extensions.managedclient.4.1.4.563.nupkg.sha512"
- },
"runtime.linux-arm.runtime.native.System.IO.Ports/7.0.0": {
"type": "package",
"serviceable": true,
@@ -863,6 +854,13 @@
"path": "serilog.sinks.console/4.1.0",
"hashPath": "serilog.sinks.console.4.1.0.nupkg.sha512"
},
+ "System.Diagnostics.DiagnosticSource/7.0.1": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-T9SLFxzDp0SreCffRDXSAS5G+lq6E8qP4knHS2IBjwCdx2KEvGnGZsq7gFpselYOda7l6gXsJMD93TQsFj/URA==",
+ "path": "system.diagnostics.diagnosticsource/7.0.1",
+ "hashPath": "system.diagnostics.diagnosticsource.7.0.1.nupkg.sha512"
+ },
"System.Diagnostics.EventLog/7.0.0": {
"type": "package",
"serviceable": true,
diff --git a/addon/Easywave2MQTT/app/Easywave2Mqtt.dll b/addon/Easywave2MQTT/app/Easywave2Mqtt.dll
index a66a806..42434c1 100644
Binary files a/addon/Easywave2MQTT/app/Easywave2Mqtt.dll and b/addon/Easywave2MQTT/app/Easywave2Mqtt.dll differ
diff --git a/addon/Easywave2MQTT/app/Easywave2Mqtt.exe b/addon/Easywave2MQTT/app/Easywave2Mqtt.exe
index 6b21520..6a70a92 100644
Binary files a/addon/Easywave2MQTT/app/Easywave2Mqtt.exe and b/addon/Easywave2MQTT/app/Easywave2Mqtt.exe differ
diff --git a/addon/Easywave2MQTT/app/InMemoryBus.dll b/addon/Easywave2MQTT/app/InMemoryBus.dll
index 18501aa..288055f 100644
Binary files a/addon/Easywave2MQTT/app/InMemoryBus.dll and b/addon/Easywave2MQTT/app/InMemoryBus.dll differ
diff --git a/addon/Easywave2MQTT/app/JetBrains.Annotations.dll b/addon/Easywave2MQTT/app/JetBrains.Annotations.dll
new file mode 100644
index 0000000..0749354
Binary files /dev/null and b/addon/Easywave2MQTT/app/JetBrains.Annotations.dll differ
diff --git a/addon/Easywave2MQTT/app/MQTTnet.Extensions.ManagedClient.dll b/addon/Easywave2MQTT/app/MQTTnet.Extensions.ManagedClient.dll
deleted file mode 100644
index cb0e165..0000000
Binary files a/addon/Easywave2MQTT/app/MQTTnet.Extensions.ManagedClient.dll and /dev/null differ
diff --git a/addon/Easywave2MQTT/app/Microsoft.Extensions.Configuration.Binder.dll b/addon/Easywave2MQTT/app/Microsoft.Extensions.Configuration.Binder.dll
index 587cf85..3976a23 100644
Binary files a/addon/Easywave2MQTT/app/Microsoft.Extensions.Configuration.Binder.dll and b/addon/Easywave2MQTT/app/Microsoft.Extensions.Configuration.Binder.dll differ
diff --git a/addon/Easywave2MQTT/app/Microsoft.Extensions.Hosting.dll b/addon/Easywave2MQTT/app/Microsoft.Extensions.Hosting.dll
index 336e864..6574b85 100644
Binary files a/addon/Easywave2MQTT/app/Microsoft.Extensions.Hosting.dll and b/addon/Easywave2MQTT/app/Microsoft.Extensions.Hosting.dll differ
diff --git a/addon/Easywave2MQTT/app/Microsoft.Extensions.Options.dll b/addon/Easywave2MQTT/app/Microsoft.Extensions.Options.dll
index 1aa2d3f..09a4ad5 100644
Binary files a/addon/Easywave2MQTT/app/Microsoft.Extensions.Options.dll and b/addon/Easywave2MQTT/app/Microsoft.Extensions.Options.dll differ
diff --git a/addon/Easywave2MQTT/app/appsettings.json b/addon/Easywave2MQTT/app/appsettings.json
index f29afb4..1117e80 100644
--- a/addon/Easywave2MQTT/app/appsettings.json
+++ b/addon/Easywave2MQTT/app/appsettings.json
@@ -4,7 +4,7 @@
"SerialPort": "/dev/ttyUSB0",
"EasywaveActionTimeout": "500",
"EasywaveRepeatTimeout": "80",
- "MQTTServer": "192.168.0.12",
+ "MQTTServer": "192.168.0.13",
"MQTTPort": "1883",
"MQTTUser": "mqtt",
"MQTTPassword": "mqtt",
@@ -159,56 +159,6 @@
}
]
},
- {
- "Id": "nachth",
- "Type": "Light",
- "Name": "Nachthal",
- "Area": "Hal",
- "Subscriptions": [
- {
- "Address": "000040",
- "KeyCode": "A",
- "CanSend": "true"
- },
- {
- "Address": "227e60",
- "KeyCode": "C"
- },
- {
- "Address": "229630",
- "KeyCode": "A"
- },
- {
- "Address": "22963e",
- "KeyCode": "A"
- },
- {
- "Address": "229964",
- "KeyCode": "A"
- }
- ]
- },
- {
- "Id": "slk1_1",
- "Type": "Light",
- "Name": "Slaapkamer Ouders",
- "Area": "Slaapkamer Ouders",
- "Subscriptions": [
- {
- "Address": "000041",
- "KeyCode": "A",
- "CanSend": "true"
- },
- {
- "Address": "229589",
- "KeyCode": "A"
- },
- {
- "Address": "22759c",
- "KeyCode": "A"
- }
- ]
- },
{
"Id": "slk1_2",
"Type": "Light",
diff --git a/addon/Easywave2MQTT/config.yaml b/addon/Easywave2MQTT/config.yaml
index 76390cc..eb94470 100644
--- a/addon/Easywave2MQTT/config.yaml
+++ b/addon/Easywave2MQTT/config.yaml
@@ -1,6 +1,6 @@
name: "Easywave2MQTT"
description: "Support Easywave devices in Home Assistant using MQTT"
-version: "0.5.3"
+version: "0.6"
url: "https://github.com/marcselis/Easywave2MQTT"
slug: "easywave2mqtt"
init: false
diff --git a/src/Easywave2Mqtt/Easywave2Mqtt.csproj b/src/Easywave2Mqtt/Easywave2Mqtt.csproj
index e44ccc3..898fb28 100644
--- a/src/Easywave2Mqtt/Easywave2Mqtt.csproj
+++ b/src/Easywave2Mqtt/Easywave2Mqtt.csproj
@@ -12,12 +12,14 @@
- all
+ All
+
+
-
+
-
+
diff --git a/src/Easywave2Mqtt/Mqtt/MessagingService.cs b/src/Easywave2Mqtt/Mqtt/MessagingService.cs
index 8071561..7ddee42 100644
--- a/src/Easywave2Mqtt/Mqtt/MessagingService.cs
+++ b/src/Easywave2Mqtt/Mqtt/MessagingService.cs
@@ -4,7 +4,6 @@
using InMemoryBus;
using MQTTnet;
using MQTTnet.Client;
-using MQTTnet.Extensions.ManagedClient;
using MQTTnet.Packets;
using MQTTnet.Protocol;
@@ -13,17 +12,26 @@ namespace Easywave2Mqtt.Mqtt
public partial class MessagingService : BackgroundService
{
- private static readonly MqttFactory MqttFactory = new();
- // private static readonly MqttClientOptions Options;
+ private readonly IMqttClient _client = new MqttFactory().CreateMqttClient();
+ private readonly MqttClientOptions _clientOptions;
private readonly IBus _bus;
- private readonly IManagedMqttClient _client;
private readonly ILogger _logger;
+ private readonly MqttTopicFilter _outgoing = new MqttTopicFilterBuilder().WithAtLeastOnceQoS().WithTopic("easywave2mqtt/#").Build();
+ private readonly MqttTopicFilter _incoming = new MqttTopicFilterBuilder().WithAtLeastOnceQoS().WithTopic("mqtt2easywave/#").Build();
+ private readonly MqttClientSubscribeOptions _subscribeOptions;
+ private bool _reconnecting = false;
public MessagingService(ILogger logger, IBus bus)
{
_logger = logger;
_bus = bus;
- _client = MqttFactory.CreateManagedMqttClient();
+ _clientOptions = new MqttClientOptionsBuilder()
+ .WithClientId("Easywave2MQTT")
+ .WithTcpServer(Program.Settings!.MQTTServer, Program.Settings!.MQTTPort)
+ .WithCredentials(Program.Settings!.MQTTUser, Program.Settings!.MQTTPassword)
+ .Build();
+ ;
+ _subscribeOptions = new MqttClientSubscribeOptionsBuilder().WithTopicFilter(_outgoing).WithTopicFilter(_incoming).Build();
}
public override void Dispose()
@@ -36,25 +44,66 @@ public override void Dispose()
public override async Task StartAsync(CancellationToken cancellationToken)
{
LogServiceStart();
- MqttClientOptions clientOptions = new MqttClientOptionsBuilder()
- .WithClientId("Easywave2MQTT")
- .WithTcpServer(Program.Settings!.MQTTServer, Program.Settings!.MQTTPort)
- .WithCredentials(Program.Settings!.MQTTUser, Program.Settings!.MQTTPassword)
- .Build();
- ManagedMqttClientOptions managedClientOptions = new ManagedMqttClientOptionsBuilder()
- .WithClientOptions(clientOptions)
- .WithPendingMessagesOverflowStrategy(MQTTnet.Server.MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage)
- .Build();
_client.ApplicationMessageReceivedAsync += MessageHandler;
- await _client.StartAsync(managedClientOptions).ConfigureAwait(false);
+ _client.DisconnectedAsync += DisconnectedHandler;
+ var connectResult = await ConnectAsync().ConfigureAwait(false);
+ if (connectResult.ResultCode != MqttClientConnectResultCode.Success)
+ {
+ LogClientConnectionFailed(connectResult.ResultCode);
+ return;
+ }
+
+ var subscribeResult = await SubscribeAsync().ConfigureAwait(false);
+ foreach (var item in subscribeResult.Items)
+ {
+ if (item.ResultCode != MqttClientSubscribeResultCode.GrantedQoS0)
+ {
+ LogClientSubscriptionFailed(item.TopicFilter.Topic, item.ResultCode);
+ }
+ }
await base.StartAsync(cancellationToken).ConfigureAwait(false);
}
+ private async Task DisconnectedHandler(MqttClientDisconnectedEventArgs arg)
+ {
+ if (_reconnecting)
+ return;
+ _reconnecting = true;
+ LogClientConnectionBroken();
+
+ while (!_client.IsConnected)
+ {
+ try
+ {
+ _=await ConnectAsync().ConfigureAwait(false);
+ _=await SubscribeAsync().ConfigureAwait(false);
+ LogClientReconnected();
+ _reconnecting=false;
+ break;
+ }
+ catch (Exception ex)
+ {
+ LogClientReconnectFailed(ex.Message);
+ await Task.Delay(TimeSpan.FromSeconds(10)).ConfigureAwait(false);
+ }
+ }
+ }
+
+ private Task ConnectAsync()
+ {
+ return _client.ConnectAsync(_clientOptions);
+ }
+
+ private Task SubscribeAsync()
+ {
+ return _client.SubscribeAsync(_subscribeOptions);
+ }
+
public override async Task StopAsync(CancellationToken cancellationToken)
{
LogServiceStop();
_client.ApplicationMessageReceivedAsync -= MessageHandler;
- await _client.StopAsync().ConfigureAwait(false);
+ await _client.DisconnectAsync().ConfigureAwait(false);
await base.StopAsync(cancellationToken).ConfigureAwait(false);
}
@@ -75,9 +124,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
using (_bus.Subscribe(SendSwitchTurnedOn))
using (_bus.Subscribe(SendSwitchTurnedOff))
{
- MqttTopicFilter outgoing = new MqttTopicFilterBuilder().WithAtLeastOnceQoS().WithTopic("easywave2mqtt/#").Build();
- MqttTopicFilter incoming = new MqttTopicFilterBuilder().WithAtLeastOnceQoS().WithTopic("mqtt2easywave/#").Build();
- await _client.SubscribeAsync(new List() { outgoing, incoming }).ConfigureAwait(false);
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(100, stoppingToken).ConfigureAwait(false);
@@ -91,12 +137,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
private Task SendSwitchTurnedOff(EasywaveSwitchTurnedOff sw)
{
- return Send($"easywave2mqtt/{sw.Id}/state", "off");
+ return Send($"easywave2mqtt/{sw.Id}/state", "off", true);
}
private Task SendSwitchTurnedOn(EasywaveSwitchTurnedOn sw)
{
- return Send($"easywave2mqtt/{sw.Id}/state", "on");
+ return Send($"easywave2mqtt/{sw.Id}/state", "on", true);
}
private async Task MessageHandler(MqttApplicationMessageReceivedEventArgs arg)
@@ -184,15 +230,21 @@ private Task SendButtonHold(string id, char btn)
return Send($"easywave2mqtt/{id}/action", $"button_{btn}_hold");
}
- private Task Send(string topic, string payload, bool retain = false)
+ private async Task Send(string topic, string payload, bool retain = false)
{
LogTopicPublish(topic, payload);
- MqttApplicationMessage message = new MqttApplicationMessageBuilder().WithTopic(topic)
- .WithPayload(payload)
- .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
- .WithRetainFlag(retain)
- .Build();
- return _client.EnqueueAsync(message);
+ MqttClientPublishResult publishResult = await _client.PublishAsync(new MqttApplicationMessageBuilder()
+ .WithTopic(topic)
+ .WithPayload(payload)
+ .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
+ .WithRetainFlag(retain)
+ .Build());
+
+ if (publishResult.ReasonCode != MqttClientPublishReasonCode.Success)
+ {
+ LogPublishFailed(publishResult.ReasonCode, publishResult.ReasonString);
+ return;
+ }
}
#region LoggingMethods
@@ -214,6 +266,21 @@ private Task Send(string topic, string payload, bool retain = false)
[LoggerMessage(EventId = 6, Level = LogLevel.Warning, Message = "MQTT Publish failed with result code {ReasonCode} and readon {ReasonString}")]
private partial void LogPublishFailed(MqttClientPublishReasonCode reasonCode, string reasonString);
+ [LoggerMessage(EventId = 7, Level = LogLevel.Warning, Message = "Connection to MQTT broker closed. Attempting to reconnect...")]
+ private partial void LogClientConnectionBroken();
+
+ [LoggerMessage(EventId = 8, Level = LogLevel.Warning, Message = "Successfully reconnected to MQTT broker.")]
+ private partial void LogClientReconnected();
+
+ [LoggerMessage(EventId = 9, Level = LogLevel.Information, Message = "Failed to reconnect to MQTT broker: {Message}")]
+ private partial void LogClientReconnectFailed(string message);
+
+ [LoggerMessage(EventId = 10, Level = LogLevel.Error, Message = "Failed to connect to MQTT broker: {Result}")]
+ private partial void LogClientConnectionFailed(MqttClientConnectResultCode result);
+
+ [LoggerMessage(EventId = 11, Level = LogLevel.Error, Message = "Failed to subscribe to topic {Topic}: {Result}")]
+ private partial void LogClientSubscriptionFailed(string topic, MqttClientSubscribeResultCode result);
+
[LoggerMessage(EventId = 17, Level = LogLevel.Information, Message = "Button {Id}:{ButtonCode} pressed")]
private partial void LogButtonPress(string id, char buttonCode);
diff --git a/src/Easywave2Mqtt/Properties/launchSettings.json b/src/Easywave2Mqtt/Properties/launchSettings.json
index 645db0c..5105f6a 100644
--- a/src/Easywave2Mqtt/Properties/launchSettings.json
+++ b/src/Easywave2Mqtt/Properties/launchSettings.json
@@ -2,12 +2,13 @@
"profiles": {
"Easywave2Mqtt": {
"commandName": "Project",
- "environmentVariables": {
- "DOTNET_ENVIRONMENT": "Development",
- "LOGLEVEL": "Verbose",
- "SERIALPORT": "COM1",
- "MQTTServer": "localhost"
- },
+ "environmentVariables": {
+ "DOTNET_ENVIRONMENT": "Development",
+ "LOGLEVEL": "Verbose",
+ "SERIALPORT": "COM1",
+ "MQTTServer": "192.168.0.13",
+ "MQTTPassword": "ttqm"
+ },
"dotnetRunMessages": true
},
"WSL": {
diff --git a/src/Easywave2Mqtt/appsettings.json b/src/Easywave2Mqtt/appsettings.json
index f29afb4..1117e80 100644
--- a/src/Easywave2Mqtt/appsettings.json
+++ b/src/Easywave2Mqtt/appsettings.json
@@ -4,7 +4,7 @@
"SerialPort": "/dev/ttyUSB0",
"EasywaveActionTimeout": "500",
"EasywaveRepeatTimeout": "80",
- "MQTTServer": "192.168.0.12",
+ "MQTTServer": "192.168.0.13",
"MQTTPort": "1883",
"MQTTUser": "mqtt",
"MQTTPassword": "mqtt",
@@ -159,56 +159,6 @@
}
]
},
- {
- "Id": "nachth",
- "Type": "Light",
- "Name": "Nachthal",
- "Area": "Hal",
- "Subscriptions": [
- {
- "Address": "000040",
- "KeyCode": "A",
- "CanSend": "true"
- },
- {
- "Address": "227e60",
- "KeyCode": "C"
- },
- {
- "Address": "229630",
- "KeyCode": "A"
- },
- {
- "Address": "22963e",
- "KeyCode": "A"
- },
- {
- "Address": "229964",
- "KeyCode": "A"
- }
- ]
- },
- {
- "Id": "slk1_1",
- "Type": "Light",
- "Name": "Slaapkamer Ouders",
- "Area": "Slaapkamer Ouders",
- "Subscriptions": [
- {
- "Address": "000041",
- "KeyCode": "A",
- "CanSend": "true"
- },
- {
- "Address": "229589",
- "KeyCode": "A"
- },
- {
- "Address": "22759c",
- "KeyCode": "A"
- }
- ]
- },
{
"Id": "slk1_2",
"Type": "Light",