mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
Add new PowerShell project
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommunications.Receive, "MqttMessage")]
|
||||
[OutputType(typeof(MqttMessage))]
|
||||
public class ReceiveMqttMessageCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
[ValidateRange(0, int.MaxValue)]
|
||||
public int TimeoutSeconds { get; set; } = 0;
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var tcs = new TaskCompletionSource<MqttMessage>();
|
||||
EventHandler<MqttMessage>? handler = null;
|
||||
CancellationTokenSource? cts = null;
|
||||
|
||||
try
|
||||
{
|
||||
handler = (s, e) =>
|
||||
{
|
||||
if (!tcs.Task.IsCompleted)
|
||||
{
|
||||
tcs.TrySetResult(e);
|
||||
}
|
||||
};
|
||||
|
||||
Session.MessageReceived += handler;
|
||||
|
||||
// Optionaler Timeout
|
||||
if (TimeoutSeconds > 0)
|
||||
{
|
||||
cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(TimeoutSeconds));
|
||||
cts.Token.Register(() =>
|
||||
{
|
||||
if (!tcs.Task.IsCompleted)
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
WriteVerbose("Waiting for next MQTT message...");
|
||||
|
||||
try
|
||||
{
|
||||
var message = tcs.Task.GetAwaiter().GetResult();
|
||||
WriteObject(message);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
WriteWarning($"Timeout after {TimeoutSeconds} seconds — no message received.");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (handler != null)
|
||||
{
|
||||
Session.MessageReceived -= handler;
|
||||
}
|
||||
|
||||
cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user