mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 02:53:06 +08:00
Implement enhanced PowerShell cmdlets with async handler, WebSocket, TLS, and more features
Co-authored-by: chkr1011 <6939810+chkr1011@users.noreply.github.com>
This commit is contained in:
co-authored by
chkr1011
parent
71140d12c7
commit
419d7622d8
@@ -12,13 +12,15 @@ public class ConnectMqttSessionCmdlet : PSCmdlet
|
||||
[Parameter]
|
||||
public string? ClientId { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required new string Host { get; set; }
|
||||
[Parameter(Mandatory = true, ParameterSetName = "Tcp")]
|
||||
[Parameter(Mandatory = true, ParameterSetName = "TcpWithTls")]
|
||||
public new string? Host { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? Password { get; set; }
|
||||
|
||||
[Parameter]
|
||||
[Parameter(ParameterSetName = "Tcp")]
|
||||
[Parameter(ParameterSetName = "TcpWithTls")]
|
||||
public int Port { get; set; } = 1883;
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
@@ -27,19 +29,90 @@ public class ConnectMqttSessionCmdlet : PSCmdlet
|
||||
[Parameter]
|
||||
public string? Username { get; set; }
|
||||
|
||||
[Parameter]
|
||||
[Parameter(ParameterSetName = "TcpWithTls")]
|
||||
public SwitchParameter UseTls { get; set; }
|
||||
|
||||
[Parameter(ParameterSetName = "TcpWithTls")]
|
||||
public SwitchParameter AllowUntrustedCertificates { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true, ParameterSetName = "WebSocket")]
|
||||
public string? WebSocketUri { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int KeepAlivePeriod { get; set; } = 15;
|
||||
|
||||
[Parameter]
|
||||
public int Timeout { get; set; } = 10;
|
||||
|
||||
[Parameter]
|
||||
public string? WillTopic { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? WillPayload { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int WillQoS { get; set; } = 0;
|
||||
|
||||
[Parameter]
|
||||
public SwitchParameter WillRetain { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var clientOptionsBuilder = new MqttClientOptionsBuilder();
|
||||
clientOptionsBuilder.WithTcpServer(Host, Port);
|
||||
|
||||
// Configure transport
|
||||
if (ParameterSetName == "WebSocket")
|
||||
{
|
||||
clientOptionsBuilder.WithWebSocketServer(o => o.WithUri(WebSocketUri!));
|
||||
}
|
||||
else
|
||||
{
|
||||
clientOptionsBuilder.WithTcpServer(Host!, Port);
|
||||
|
||||
if (UseTls)
|
||||
{
|
||||
clientOptionsBuilder.WithTlsOptions(o =>
|
||||
{
|
||||
if (AllowUntrustedCertificates)
|
||||
{
|
||||
o.WithCertificateValidationHandler(_ => true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Configure authentication
|
||||
if (Username != null)
|
||||
{
|
||||
clientOptionsBuilder.WithCredentials(Username, Password);
|
||||
}
|
||||
|
||||
// Configure session
|
||||
clientOptionsBuilder.WithCleanSession(CleanSession);
|
||||
|
||||
if (!string.IsNullOrEmpty(ClientId))
|
||||
{
|
||||
clientOptionsBuilder.WithClientId(ClientId);
|
||||
}
|
||||
|
||||
// Configure keep alive
|
||||
clientOptionsBuilder.WithKeepAlivePeriod(TimeSpan.FromSeconds(KeepAlivePeriod));
|
||||
clientOptionsBuilder.WithTimeout(TimeSpan.FromSeconds(Timeout));
|
||||
|
||||
// Configure Will message
|
||||
if (!string.IsNullOrEmpty(WillTopic))
|
||||
{
|
||||
clientOptionsBuilder.WithWillTopic(WillTopic);
|
||||
|
||||
if (!string.IsNullOrEmpty(WillPayload))
|
||||
{
|
||||
clientOptionsBuilder.WithWillPayload(System.Text.Encoding.UTF8.GetBytes(WillPayload));
|
||||
}
|
||||
|
||||
clientOptionsBuilder.WithWillQualityOfServiceLevel((Protocol.MqttQualityOfServiceLevel)WillQoS);
|
||||
clientOptionsBuilder.WithWillRetain(WillRetain);
|
||||
}
|
||||
|
||||
var response = Session.GetClient().ConnectAsync(clientOptionsBuilder.Build()).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
|
||||
Reference in New Issue
Block a user