-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix wording - reported by @nathanr * #15 - Retain Discovery Messages (#42) * Fixes #15 - Retion discovery messages. Improved documentation. * Added ability to only run a single discovery job. (Also, fixes an error if anyone ever made a discovery interval less then 0.) * removed unused line. * Implements #43 - Adds Status topic & Last will message. * Fixes #40 - Adds random segment to clientid. Also, fixes keep-alive messages.
- Loading branch information
1 parent
eb661aa
commit 55fe188
Showing
8 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using HiveMQtt.Client; | ||
using System.Diagnostics; | ||
using System.Timers; | ||
using Timer = System.Timers.Timer; | ||
|
||
namespace rPDU2MQTT.Classes; | ||
|
||
public class MqttEventHandler | ||
{ | ||
private readonly HiveMQClient client; | ||
private readonly Timer _timer; | ||
public MqttEventHandler(HiveMQClient client) | ||
{ | ||
this.client = client; | ||
client.AfterConnect += Client_AfterConnect; | ||
client.OnDisconnectSent += Client_OnDisconnectSent; | ||
client.OnDisconnectReceived += Client_OnDisconnectReceived; | ||
|
||
_timer = new Timer(TimeSpan.FromSeconds(10)); | ||
_timer.Elapsed += HealthTimer; | ||
_timer.AutoReset = true; // Ensures the event fires repeatedly at the interval | ||
_timer.Enabled = true; // Starts the timer | ||
} | ||
|
||
private void Client_OnDisconnectReceived(object? sender, HiveMQtt.Client.Events.OnDisconnectReceivedEventArgs e) | ||
{ | ||
Log.Error("Received disconnect from broker."); | ||
} | ||
|
||
private void Client_OnDisconnectSent(object? sender, HiveMQtt.Client.Events.OnDisconnectSentEventArgs e) | ||
{ | ||
Log.Information("Sending disconnect to broker"); | ||
} | ||
|
||
private void HealthTimer(object? sender, ElapsedEventArgs e) | ||
{ | ||
Log.Verbose("Timer.Tick()"); | ||
sendStatusOnline(); | ||
|
||
} | ||
|
||
private void Client_AfterConnect(object? sender, HiveMQtt.Client.Events.AfterConnectEventArgs e) | ||
{ | ||
Log.Information("MQTT Client Connected"); | ||
sendStatusOnline(); | ||
} | ||
|
||
|
||
private void sendStatusOnline() | ||
{ | ||
TaskManager.AddTask(() => client.PublishAsync(client.Options!.LastWillAndTestament!.Topic, "online", HiveMQtt.MQTT5.Types.QualityOfService.AtLeastOnceDelivery)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace rPDU2MQTT.Classes; | ||
|
||
/// <summary> | ||
/// This class, exists to watch tasks until completion. Non-async code, which needs to execute a task, just passes the task off to this class. | ||
/// </summary> | ||
public static class TaskManager | ||
{ | ||
private static readonly ConcurrentBag<Task> taskBag = new ConcurrentBag<Task>(); | ||
|
||
public static void AddTask(Action task) | ||
{ | ||
Task newTask = Task.Run(task); | ||
taskBag.Add(newTask); | ||
} | ||
|
||
public static void AddTask(Task task) | ||
{ | ||
taskBag.Add(task); | ||
} | ||
|
||
public static void WaitForAllTasks() | ||
{ | ||
foreach (var task in taskBag) | ||
{ | ||
task.Wait(); | ||
} | ||
|
||
taskBag.Clear(); // ConcurrentBag doesn't have a clear method, but assigning a new one is equivalent to clearing. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters