mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 09:13:02 +08:00
Add new PowerShell project
This commit is contained in:
@@ -33,6 +33,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Extensions.TopicTem
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.Server", "Source\MQTTnet.Server\MQTTnet.Server.csproj", "{C876EFFD-C5AD-4E26-BD9F-4C48252C02BC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.PowerShell", "Source\MQTTnet.PowerShell\MQTTnet.PowerShell.csproj", "{37B129C5-0231-4EB5-AF13-77959068911C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -79,6 +81,10 @@ Global
|
||||
{C876EFFD-C5AD-4E26-BD9F-4C48252C02BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C876EFFD-C5AD-4E26-BD9F-4C48252C02BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C876EFFD-C5AD-4E26-BD9F-4C48252C02BC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{37B129C5-0231-4EB5-AF13-77959068911C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{37B129C5-0231-4EB5-AF13-77959068911C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{37B129C5-0231-4EB5-AF13-77959068911C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{37B129C5-0231-4EB5-AF13-77959068911C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommunications.Connect, "MqttSession")]
|
||||
[OutputType(typeof(MqttClientConnectResult))]
|
||||
public class ConnectMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter]
|
||||
public SwitchParameter CleanSession { get; set; } = true;
|
||||
|
||||
[Parameter]
|
||||
public string? ClientId { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required new string Host { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? Password { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int Port { get; set; } = 1883;
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? Username { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public SwitchParameter UseTls { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var clientOptionsBuilder = new MqttClientOptionsBuilder();
|
||||
clientOptionsBuilder.WithTcpServer(Host, Port);
|
||||
|
||||
if (Username != null)
|
||||
{
|
||||
clientOptionsBuilder.WithCredentials(Username, Password);
|
||||
}
|
||||
|
||||
var response = Session.Client.ConnectAsync(clientOptionsBuilder.Build()).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommunications.Disconnect, "MqttSession")]
|
||||
public class DisconnectMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter]
|
||||
public MqttClientDisconnectOptionsReason Reason { get; set; } = MqttClientDisconnectOptionsReason.NormalDisconnection;
|
||||
|
||||
[Parameter]
|
||||
public string? ReasonString { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public uint SessionExpiryInterval { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (Session.Client.IsConnected)
|
||||
{
|
||||
var options = new MqttClientDisconnectOptionsBuilder().WithSessionExpiryInterval(SessionExpiryInterval).WithReason(Reason).WithReasonString(ReasonString).Build();
|
||||
|
||||
Session.Client.DisconnectAsync(options).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
WriteObject("Disconnected.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommon.New, "MqttSession")]
|
||||
[OutputType(typeof(MqttSession))]
|
||||
public class NewMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
WriteObject(new MqttSession());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Management.Automation;
|
||||
using System.Text;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsData.Publish, "MqttMessage")]
|
||||
public class PublishMqttMessageCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true)]
|
||||
public string? Payload { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int QoS { get; set; } = 0;
|
||||
|
||||
[Parameter]
|
||||
public SwitchParameter Retain { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required string Topic { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
// if (Session == null || !Session.IsConnected)
|
||||
// throw new InvalidOperationException("Session not connected.");
|
||||
|
||||
var msg = new MqttApplicationMessageBuilder().WithTopic(Topic)
|
||||
.WithPayload(Encoding.UTF8.GetBytes(Payload ?? string.Empty))
|
||||
.WithQualityOfServiceLevel((MqttQualityOfServiceLevel)QoS)
|
||||
.WithRetainFlag(Retain)
|
||||
.Build();
|
||||
|
||||
var response = Session.Client.PublishAsync(msg).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Management.Automation;
|
||||
using System.Management.Automation.Runspaces;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsLifecycle.Register, "MqttMessageHandler")]
|
||||
public class RegisterMqttMessageHandlerCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public ScriptBlock? Action { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
EventHandler<MqttMessage> handler = (s, e) =>
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
//InvokeCommand.InvokeScript(Action, false, PipelineResultTypes.Output, null, new object[] { e.Topic, e.Payload });
|
||||
};
|
||||
|
||||
Session.MessageReceived += handler;
|
||||
WriteObject($"Handler registered for MQTT messages on session.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommon.Remove, "MqttSession")]
|
||||
public class RemoveMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
Session.Dispose();
|
||||
WriteObject("Session removed.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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("Subscribe", "MqttTopic")]
|
||||
[OutputType(typeof(MqttClientSubscribeResult))]
|
||||
public class SubscribeMqttTopicCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter]
|
||||
public int QoS { get; set; } = 0;
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required string Topic { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var topicFilter = new MqttTopicFilterBuilder().WithTopic(Topic).Build();
|
||||
|
||||
var response = Session.Client.SubscribeAsync(topicFilter).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet("Unsubscribe", "MqttTopic")]
|
||||
[OutputType(typeof(MqttClientUnsubscribeResult))]
|
||||
public class UnsubscribeMqttTopicCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required string Topic { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var response = Session.Client.UnsubscribeAsync(Topic).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>MQTTnet.PowerShell</AssemblyName>
|
||||
<LangVersion>default</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1">
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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 MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.PowerShell;
|
||||
|
||||
public class MqttMessage
|
||||
{
|
||||
public required string? ContentType { get; init; }
|
||||
public required string Payload { get; init; }
|
||||
public int QoS { get; init; }
|
||||
public required byte[] RawPayload { get; init; }
|
||||
public string? ResponseTopic { get; init; }
|
||||
public bool Retain { get; init; }
|
||||
public required string Topic { get; init; }
|
||||
public required List<MqttUserProperty>? UserProperties { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// 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.Buffers;
|
||||
using System.Text;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.PowerShell;
|
||||
|
||||
public sealed class MqttSession : IDisposable
|
||||
{
|
||||
public MqttSession()
|
||||
{
|
||||
var factory = new MqttClientFactory();
|
||||
Client = factory.CreateMqttClient();
|
||||
|
||||
Client.ApplicationMessageReceivedAsync += e =>
|
||||
{
|
||||
MessageReceived?.Invoke(this, new MqttMessage
|
||||
{
|
||||
Topic = e.ApplicationMessage.Topic,
|
||||
Payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload),
|
||||
RawPayload = e.ApplicationMessage.Payload.ToArray(),
|
||||
UserProperties = e.ApplicationMessage.UserProperties,
|
||||
QoS = (int)e.ApplicationMessage.QualityOfServiceLevel,
|
||||
ContentType = e.ApplicationMessage.ContentType,
|
||||
Retain = e.ApplicationMessage.Retain,
|
||||
ResponseTopic = e.ApplicationMessage.ResponseTopic
|
||||
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
// Client.ConnectedAsync += e =>
|
||||
// {
|
||||
// //Connected?.Invoke(this, e);
|
||||
// return Task.CompletedTask;
|
||||
// };
|
||||
//
|
||||
// Client.DisconnectedAsync += e =>
|
||||
// {
|
||||
// //Disconnected?.Invoke(this, e);
|
||||
// return Task.CompletedTask;
|
||||
// };
|
||||
}
|
||||
|
||||
// public event EventHandler<MqttClientConnectedEventArgs> Connected;
|
||||
// public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
||||
public event EventHandler<MqttMessage>? MessageReceived;
|
||||
|
||||
public IMqttClient Client { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Client != null)
|
||||
{
|
||||
if (Client.IsConnected)
|
||||
{
|
||||
Client.DisconnectAsync().Wait();
|
||||
}
|
||||
|
||||
Client.Dispose();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
Import-Module ./bin/Debug/net8.0/MQTTnet.PowerShell.dll
|
||||
|
||||
$session = New-MqttSession
|
||||
|
||||
Connect-MqttSession -Session $session -Host "192.168.1.16"
|
||||
|
||||
Publish-MqttMessage -Session $session -Topic "Test" -Payload "Hello from PowerShell"
|
||||
|
||||
Subscribe-MqttTopic -Session $session -Topic "test/demo"
|
||||
|
||||
Receive-MqttMessage -Session $session -TimeoutSeconds 200
|
||||
|
||||
Disconnect-MqttSession -Session $session
|
||||
Remove-MqttSession -Session $session
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#if DEBUG
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
@@ -56,7 +57,7 @@ public sealed class Load_Tests : BaseTestClass
|
||||
|
||||
for (var j = 0; j < 1000; j++)
|
||||
{
|
||||
publishPacket.Topic = j.ToString();
|
||||
publishPacket.Topic = j.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
await client.SendAsync(publishPacket, CancellationToken.None);
|
||||
}
|
||||
@@ -143,7 +144,7 @@ public sealed class Load_Tests : BaseTestClass
|
||||
|
||||
for (var j = 0; j < 1000; j++)
|
||||
{
|
||||
var message = applicationMessageBuilder.WithTopic(j.ToString()).Build();
|
||||
var message = applicationMessageBuilder.WithTopic(j.ToString(CultureInfo.InvariantCulture)).Build();
|
||||
|
||||
await client.PublishAsync(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user