mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Expose more properties
This commit is contained in:
@@ -22,7 +22,7 @@ public class ConnectMqttSessionCmdlet : PSCmdlet
|
||||
public int Port { get; set; } = 1883;
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? Username { get; set; }
|
||||
@@ -40,7 +40,7 @@ public class ConnectMqttSessionCmdlet : PSCmdlet
|
||||
clientOptionsBuilder.WithCredentials(Username, Password);
|
||||
}
|
||||
|
||||
var response = Session.Client.ConnectAsync(clientOptionsBuilder.Build()).GetAwaiter().GetResult();
|
||||
var response = Session.GetClient().ConnectAsync(clientOptionsBuilder.Build()).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
|
||||
@@ -12,18 +12,18 @@ public class DisconnectMqttSessionCmdlet : PSCmdlet
|
||||
public string? ReasonString { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public uint SessionExpiryInterval { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (Session.Client.IsConnected)
|
||||
if (Session.GetClient().IsConnected)
|
||||
{
|
||||
var options = new MqttClientDisconnectOptionsBuilder().WithSessionExpiryInterval(SessionExpiryInterval).WithReason(Reason).WithReasonString(ReasonString).Build();
|
||||
|
||||
Session.Client.DisconnectAsync(options).GetAwaiter().GetResult();
|
||||
Session.GetClient().DisconnectAsync(options).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
WriteObject("Disconnected.");
|
||||
|
||||
@@ -3,11 +3,11 @@ using System.Management.Automation;
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommon.New, "MqttSession")]
|
||||
[OutputType(typeof(MqttSession))]
|
||||
[OutputType(typeof(PsMqttSession))]
|
||||
public class NewMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
WriteObject(new MqttSession());
|
||||
WriteObject(new PsMqttSession());
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,19 @@ public class PublishMqttMessageCmdlet : PSCmdlet
|
||||
public SwitchParameter Retain { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required string Topic { get; set; }
|
||||
|
||||
public string? ContentType { get; set; }
|
||||
|
||||
public string? ResponseTopic { get; set; }
|
||||
|
||||
public ushort TopicAlias { get; set; }
|
||||
|
||||
public uint MessageExpiryInterval { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
// if (Session == null || !Session.IsConnected)
|
||||
@@ -31,9 +39,13 @@ public class PublishMqttMessageCmdlet : PSCmdlet
|
||||
.WithPayload(Encoding.UTF8.GetBytes(Payload ?? string.Empty))
|
||||
.WithQualityOfServiceLevel((MqttQualityOfServiceLevel)QoS)
|
||||
.WithRetainFlag(Retain)
|
||||
.WithContentType(ContentType)
|
||||
.WithResponseTopic(ResponseTopic)
|
||||
.WithTopicAlias(TopicAlias)
|
||||
.WithMessageExpiryInterval(MessageExpiryInterval)
|
||||
.Build();
|
||||
|
||||
var response = Session.Client.PublishAsync(msg).GetAwaiter().GetResult();
|
||||
var response = Session.GetClient().PublishAsync(msg).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ using System.Management.Automation;
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommunications.Receive, "MqttMessage")]
|
||||
[OutputType(typeof(MqttMessage))]
|
||||
[OutputType(typeof(PsMqttMessage))]
|
||||
public class ReceiveMqttMessageCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
[ValidateRange(0, int.MaxValue)]
|
||||
@@ -19,8 +19,8 @@ public class ReceiveMqttMessageCmdlet : PSCmdlet
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var tcs = new TaskCompletionSource<MqttMessage>();
|
||||
EventHandler<MqttMessage>? handler = null;
|
||||
var tcs = new TaskCompletionSource<PsMqttMessage>();
|
||||
EventHandler<PsMqttMessage>? handler = null;
|
||||
CancellationTokenSource? cts = null;
|
||||
|
||||
try
|
||||
|
||||
@@ -7,14 +7,14 @@ namespace MQTTnet.PowerShell.Cmdlets;
|
||||
public class RegisterMqttMessageHandlerCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public ScriptBlock? Action { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
EventHandler<MqttMessage> handler = (s, e) =>
|
||||
EventHandler<PsMqttMessage> handler = (s, e) =>
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
//InvokeCommand.InvokeScript(Action, false, PipelineResultTypes.Output, null, new object[] { e.Topic, e.Payload });
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace MQTTnet.PowerShell.Cmdlets;
|
||||
public class RemoveMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Management.Automation;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
@@ -10,20 +11,44 @@ namespace MQTTnet.PowerShell.Cmdlets;
|
||||
[OutputType(typeof(MqttClientSubscribeResult))]
|
||||
public class SubscribeMqttTopicCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter]
|
||||
public bool NoLocal { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int QoS { get; set; } = 0;
|
||||
|
||||
[Parameter]
|
||||
public bool RetainAsPublished { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public MqttRetainHandling RetainHandling { get; set; } = MqttRetainHandling.SendAtSubscribe;
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public uint SubscriptionIdentifier { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required string Topic { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var topicFilter = new MqttTopicFilterBuilder().WithTopic(Topic).Build();
|
||||
var topicFilter = new MqttTopicFilterBuilder().WithTopic(Topic)
|
||||
.WithQualityOfServiceLevel((MqttQualityOfServiceLevel)QoS)
|
||||
.WithNoLocal(NoLocal)
|
||||
.WithRetainAsPublished(RetainAsPublished)
|
||||
.WithRetainHandling(RetainHandling)
|
||||
.Build();
|
||||
|
||||
var response = Session.Client.SubscribeAsync(topicFilter).GetAwaiter().GetResult();
|
||||
var options = new MqttClientSubscribeOptionsBuilder().WithTopicFilter(topicFilter);
|
||||
|
||||
if (SubscriptionIdentifier > 0)
|
||||
{
|
||||
options.WithSubscriptionIdentifier(SubscriptionIdentifier);
|
||||
}
|
||||
|
||||
var response = Session.GetClient().SubscribeAsync(options.Build()).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,16 @@ namespace MQTTnet.PowerShell.Cmdlets;
|
||||
public class UnsubscribeMqttTopicCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required string Topic { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var response = Session.Client.UnsubscribeAsync(Topic).GetAwaiter().GetResult();
|
||||
var options = new MqttClientUnsubscribeOptionsBuilder().WithTopicFilter(Topic).Build();
|
||||
|
||||
var response = Session.GetClient().UnsubscribeAsync(options).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
// 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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-1
@@ -2,11 +2,22 @@
|
||||
// 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;
|
||||
using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.PowerShell;
|
||||
|
||||
public class MqttMessage
|
||||
public class PsMqttPublishResult
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class PsMqttConnectResult
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class PsMqttMessage
|
||||
{
|
||||
public required string? ContentType { get; init; }
|
||||
public required string Payload { get; init; }
|
||||
@@ -0,0 +1,50 @@
|
||||
// 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;
|
||||
|
||||
namespace MQTTnet.PowerShell;
|
||||
|
||||
public sealed class PsMqttSession : IDisposable
|
||||
{
|
||||
readonly IMqttClient _client;
|
||||
|
||||
public PsMqttSession()
|
||||
{
|
||||
var factory = new MqttClientFactory();
|
||||
_client = factory.CreateMqttClient();
|
||||
|
||||
_client.ApplicationMessageReceivedAsync += e =>
|
||||
{
|
||||
MessageReceived?.Invoke(
|
||||
this,
|
||||
new PsMqttMessage
|
||||
{
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
public event EventHandler<PsMqttMessage>? MessageReceived;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
}
|
||||
|
||||
public IMqttClient GetClient()
|
||||
{
|
||||
return _client;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user