优化Mqtt推送

This commit is contained in:
iioter 2022-08-23 16:29:29 +08:00
parent 90eac18785
commit 81b3d814b9
10 changed files with 368 additions and 382 deletions

View File

@ -30,7 +30,7 @@ namespace IoTGateway.ViewModel.Config.SystemConfigVMs
{ {
base.DoEdit(updateAllFields); base.DoEdit(updateAllFields);
var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient; var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient;
myMqttClient.ConnectAsync(); myMqttClient.StartManagedClientAsync().Wait();
} }
public override void DoDelete() public override void DoDelete()

View File

@ -9,7 +9,6 @@ using System.ComponentModel.DataAnnotations;
using IoTGateway.Model; using IoTGateway.Model;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using MQTTnet.Server; using MQTTnet.Server;
using MQTTnet.Server.Status;
using MQTTnet.Formatter; using MQTTnet.Formatter;
namespace IoTGateway.ViewModel.MqttClient.MqttServerVMs namespace IoTGateway.ViewModel.MqttClient.MqttServerVMs
@ -45,12 +44,12 @@ namespace IoTGateway.ViewModel.MqttClient.MqttServerVMs
} }
public override void DoSearch() public override void DoSearch()
{ {
var mqttServer = Wtm.ServiceProvider.GetService(typeof(IMqttServer)) as IMqttServer; var mqttServer = Wtm.ServiceProvider.GetService(typeof(MqttServer)) as MqttServer;
foreach (var client in mqttServer.GetClientStatusAsync().Result) foreach (var client in mqttServer.GetClientsAsync().Result)
{ {
MqttClient_View mqttClient_ = new MqttClient_View MqttClient_View mqttClient_ = new MqttClient_View
{ {
ClientId = client.ClientId, ClientId = client.Id,
BytesReceived = client.BytesReceived, BytesReceived = client.BytesReceived,
BytesSent = client.BytesSent, BytesSent = client.BytesSent,
MqttProtocolVersion = client.ProtocolVersion, MqttProtocolVersion = client.ProtocolVersion,

View File

@ -19,12 +19,11 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="MQTTnet" Version="3.1.2" /> <PackageReference Include="MQTTnet" Version="4.1.0.247" />
<PackageReference Include="MQTTnet.AspNetCore" Version="3.1.2" /> <PackageReference Include="MQTTnet.AspNetCore" Version="4.1.0.247" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.1.2" /> <PackageReference Include="MQTTnet.Extensions.Rpc" Version="4.1.0.247" />
<PackageReference Include="MQTTnet.Extensions.Rpc" Version="3.1.2" /> <PackageReference Include="NLog" Version="5.0.2" />
<PackageReference Include="NLog" Version="5.0.1" /> <PackageReference Include="NLog.Web.AspNetCore" Version="5.1.1" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.0" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" /> <PackageReference Include="System.IO.Ports" Version="6.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -6,7 +6,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet.AspNetCore.Extensions; using MQTTnet.AspNetCore;
using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core;
using NLog; using NLog;
using NLog.Web; using NLog.Web;

View File

@ -12,7 +12,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using MQTTnet.AspNetCore; using MQTTnet.AspNetCore;
using MQTTnet.AspNetCore.Extensions;
using Plugin; using Plugin;
using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions; using WalkingTec.Mvvm.Core.Extensions;
@ -66,11 +65,8 @@ namespace IoTGateway
options.ReloadUserFunc = ReloadUser; options.ReloadUserFunc = ReloadUser;
}); });
//MQTTServer //MqttServer
services.AddHostedMqttServer(mqttServer => services.AddHostedMqttServer(mqttServer => mqttServer.WithoutDefaultEndpoint())
{
mqttServer.WithoutDefaultEndpoint();
})
.AddMqttConnectionHandler() .AddMqttConnectionHandler()
.AddConnections(); .AddConnections();
@ -78,7 +74,7 @@ namespace IoTGateway
services.AddHostedService<IoTBackgroundService>(); services.AddHostedService<IoTBackgroundService>();
services.AddSingleton<DeviceService>(); services.AddSingleton<DeviceService>();
services.AddSingleton<DriverService>(); services.AddSingleton<DriverService>();
services.AddSingleton<UAService>(); //services.AddSingleton<UAService>();
services.AddSingleton<MyMqttClient>(); services.AddSingleton<MyMqttClient>();
services.AddSingleton<ModbusSlaveService>(); services.AddSingleton<ModbusSlaveService>();
@ -125,10 +121,15 @@ namespace IoTGateway
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
//MqttServerWebSocket //MqttServer
endpoints.MapConnectionHandler<MqttConnectionHandler>("/mqtt", options => app.UseEndpoints(endpoints =>
{ {
options.WebSockets.SubProtocolSelector = MqttSubProtocolSelector.SelectSubProtocol; endpoints.MapMqtt("/mqtt");
});
app.UseMqttServer(server =>
{
// Todo: Do something with the server
}); });
endpoints.MapControllerRoute( endpoints.MapControllerRoute(

Binary file not shown.

View File

@ -19,18 +19,18 @@ namespace Plugin
public List<DeviceThread> DeviceThreads = new List<DeviceThread>(); public List<DeviceThread> DeviceThreads = new List<DeviceThread>();
private readonly MyMqttClient _myMqttClient; private readonly MyMqttClient _myMqttClient;
private readonly UAService _uAService; private readonly UAService _uAService;
private readonly IMqttServer _mqttServer; private readonly MqttServer _mqttServer;
private readonly string _connnectSetting = IoTBackgroundService.connnectSetting; private readonly string _connnectSetting = IoTBackgroundService.connnectSetting;
private readonly DBTypeEnum _dbType = IoTBackgroundService.DbType; private readonly DBTypeEnum _dbType = IoTBackgroundService.DbType;
//UAService? uAService,
public DeviceService(IConfiguration configRoot, DriverService drvierManager, MyMqttClient myMqttClient, public DeviceService(IConfiguration configRoot, DriverService drvierManager, MyMqttClient myMqttClient,
UAService uAService, IMqttServer mqttServer, ILogger<DeviceService> logger) MqttServer mqttServer, ILogger<DeviceService> logger)
{ {
if (mqttServer == null) throw new ArgumentNullException(nameof(mqttServer));
_logger = logger; _logger = logger;
DrvierManager = drvierManager; DrvierManager = drvierManager;
_myMqttClient = myMqttClient; _myMqttClient = myMqttClient;
_uAService = uAService; //_uAService = uAService;
_mqttServer = mqttServer ?? throw new ArgumentNullException(nameof(mqttServer)); _mqttServer = mqttServer ?? throw new ArgumentNullException(nameof(mqttServer));
try try
{ {

View File

@ -1,11 +1,13 @@
using PluginInterface; using PluginInterface;
using System.Reflection; using System.Reflection;
using System.Text;
using IoTGateway.DataAccess; using IoTGateway.DataAccess;
using IoTGateway.Model; using IoTGateway.Model;
using DynamicExpresso; using DynamicExpresso;
using MQTTnet.Server; using MQTTnet.Server;
using Newtonsoft.Json; using Newtonsoft.Json;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet;
namespace Plugin namespace Plugin
{ {
@ -26,7 +28,7 @@ namespace Plugin
private bool _lastConnected; private bool _lastConnected;
public DeviceThread(Device device, IDriver driver, string projectId, MyMqttClient myMqttClient, public DeviceThread(Device device, IDriver driver, string projectId, MyMqttClient myMqttClient,
IMqttServer mqttServer, ILogger logger) MqttServer mqttServer, ILogger logger)
{ {
_myMqttClient = myMqttClient; _myMqttClient = myMqttClient;
_myMqttClient.OnExcRpc += MyMqttClient_OnExcRpc; _myMqttClient.OnExcRpc += MyMqttClient_OnExcRpc;
@ -119,13 +121,24 @@ namespace Plugin
ret.CookedValue?.ToString()) ret.CookedValue?.ToString())
{ {
//这是设备变量列表要用的 //这是设备变量列表要用的
mqttServer.PublishAsync( var msg = new InjectedMqttApplicationMessage(
new MqttApplicationMessage()
{
Topic =
$"internal/v1/gateway/telemetry/{Device.DeviceName}/{item.Name}", $"internal/v1/gateway/telemetry/{Device.DeviceName}/{item.Name}",
JsonConvert.SerializeObject(ret)); Payload = Encoding.UTF8.GetBytes(
JsonConvert.SerializeObject(ret))
});
mqttServer.InjectApplicationMessage(msg);
//这是在线组态要用的 //这是在线组态要用的
mqttServer.PublishAsync( msg = new InjectedMqttApplicationMessage(
new MqttApplicationMessage()
{
Topic =
$"v1/gateway/telemetry/{Device.DeviceName}/{item.Name}", $"v1/gateway/telemetry/{Device.DeviceName}/{item.Name}",
JsonConvert.SerializeObject(ret.CookedValue)); Payload = Encoding.UTF8.GetBytes(
JsonConvert.SerializeObject(ret.CookedValue))
});
} }
DeviceValues[item.ID] = ret; DeviceValues[item.ID] = ret;
@ -224,7 +237,7 @@ namespace Plugin
if (!writeResponse.IsSuccess) if (!writeResponse.IsSuccess)
{ {
rpcResponse.Description = writeResponse.Description; rpcResponse.Description = writeResponse.Description;
break; continue;
} }
} }
else else

View File

@ -1,13 +1,10 @@
using System.Text; using IoTGateway.DataAccess;
using IoTGateway.DataAccess;
using IoTGateway.Model; using IoTGateway.Model;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet; using MQTTnet;
using MQTTnet.Client; using MQTTnet.Client;
using MQTTnet.Client.Connecting; using MQTTnet.Extensions.ManagedClient;
using MQTTnet.Client.Disconnecting; using MQTTnet.Packets;
using MQTTnet.Client.Options;
using MQTTnet.Client.Receiving;
using MQTTnet.Protocol; using MQTTnet.Protocol;
using Newtonsoft.Json; using Newtonsoft.Json;
using PluginInterface; using PluginInterface;
@ -15,7 +12,6 @@ using PluginInterface.HuaWeiRoma;
using PluginInterface.IotDB; using PluginInterface.IotDB;
using PluginInterface.IoTSharp; using PluginInterface.IoTSharp;
using PluginInterface.ThingsBoard; using PluginInterface.ThingsBoard;
using Quickstarts.ReferenceServer;
namespace Plugin namespace Plugin
{ {
@ -24,113 +20,74 @@ namespace Plugin
private readonly ILogger<MyMqttClient> _logger; private readonly ILogger<MyMqttClient> _logger;
//private readonly ReferenceNodeManager? _uaNodeManager; //private readonly ReferenceNodeManager? _uaNodeManager;
private SystemConfig? _systemConfig; private SystemConfig _systemConfig;
private IMqttClientOptions _clientOptions; private ManagedMqttClientOptions _options;
public bool IsConnected => (Client.IsConnected); public bool IsConnected => (Client.IsConnected);
private IMqttClient Client { get; set; } private IManagedMqttClient? Client { get; set; }
public event EventHandler<RpcRequest> OnExcRpc; public event EventHandler<RpcRequest> OnExcRpc;
public event EventHandler<ISAttributeResponse> OnReceiveAttributes; public event EventHandler<ISAttributeResponse> OnReceiveAttributes;
private readonly string _tbRpcTopic = "v1/gateway/rpc";
public MyMqttClient(UAService uaService, ILogger<MyMqttClient> logger) //UAService uaService,
public MyMqttClient(ILogger<MyMqttClient> logger)
{ {
_logger = logger; _logger = logger;
//_uaNodeManager = uaService.server.m_server.nodeManagers[0] as ReferenceNodeManager; //_uaNodeManager = uaService.server.m_server.nodeManagers[0] as ReferenceNodeManager;
ConnectAsync();
StartManagedClientAsync().Wait();
} }
public async Task ConnectAsync() public async Task StartManagedClientAsync()
{ {
try try
{ {
await using var dc = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DbType); if (Client != null)
_systemConfig = dc.Set<SystemConfig>().FirstOrDefault();
if (_systemConfig == null)
{ {
_systemConfig = new SystemConfig() Client.Dispose();
{
ID = Guid.NewGuid(),
GatewayName = "iotgateway",
ClientId = Guid.NewGuid().ToString(),
MqttIp = "localhost",
MqttPort = 1888,
MqttUName = "user",
MqttUPwd = "pwd",
IoTPlatformType = IoTPlatformType.IoTSharp
};
dc.Set<SystemConfig>().Add(_systemConfig);
await dc.SaveChangesAsync();
} }
Client = new MqttFactory().CreateManagedMqttClient();
await using var dc = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DbType);
_systemConfig = dc.Set<SystemConfig>().First();
var factory = new MqttFactory(); #region ClientOptions
Client = (MqttClient)factory.CreateMqttClient(); // Setup and start a managed MQTT client.
_clientOptions = new MqttClientOptionsBuilder() _options = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithMaxPendingMessages(10000)
.WithClientOptions(new MqttClientOptionsBuilder()
.WithClientId(_systemConfig.ClientId ?? Guid.NewGuid().ToString()) .WithClientId(_systemConfig.ClientId ?? Guid.NewGuid().ToString())
.WithTcpServer(_systemConfig.MqttIp, _systemConfig.MqttPort) .WithTcpServer(_systemConfig.MqttIp, _systemConfig.MqttPort)
.WithCredentials(_systemConfig.MqttUName, _systemConfig.MqttUPwd) .WithCredentials(_systemConfig.MqttUName, _systemConfig.MqttUPwd)
.WithCommunicationTimeout(TimeSpan.FromSeconds(30)) .WithTimeout(TimeSpan.FromSeconds(30))
.WithKeepAlivePeriod(TimeSpan.FromSeconds(20)) .WithKeepAlivePeriod(TimeSpan.FromSeconds(20))
.Build())
.Build(); .Build();
#endregion
#region Topics
Client.ApplicationMessageReceivedHandler = List<MqttTopicFilter> subTopics = new();
new MqttApplicationMessageReceivedHandlerDelegate(Client_ApplicationMessageReceived);
Client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(_ => OnConnected());
Client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(_ => OnDisconnectedAsync());
try
{
await Client.ConnectAsync(_clientOptions);
}
catch (Exception ex)
{
_logger.LogError("MQTT CONNECTING FAILED", ex);
}
_logger.LogInformation("MQTT WAITING FOR APPLICATION MESSAGES");
}
catch (Exception ex)
{
_logger.LogError("MQTT CONNECTING FAILED", ex);
}
}
private async Task OnDisconnectedAsync()
{
try
{
await Client.ConnectAsync(_clientOptions);
}
catch (Exception ex)
{
_logger.LogError("MQTT CONNECTING FAILED", ex);
}
}
private readonly string _tbRpcTopic = "v1/gateway/rpc";
private void OnConnected()
{
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
//{"device": "Device A", "data": {"id": $request_id, "method": "toggle_gpio", "params": {"pin":1}}} //{"device": "Device A", "data": {"id": $request_id, "method": "toggle_gpio", "params": {"pin":1}}}
Client.SubscribeAsync(_tbRpcTopic, MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic(_tbRpcTopic).WithExactlyOnceQoS().Build());
//Message: {"id": $request_id, "device": "Device A", "value": "value1"} //Message: {"id": $request_id, "device": "Device A", "value": "value1"}
Client.SubscribeAsync("v1/gateway/attributes/response", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("v1/gateway/attributes/response").WithExactlyOnceQoS().Build());
//Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}} //Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}}
Client.SubscribeAsync("v1/gateway/attributes", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("v1/gateway/attributese").WithExactlyOnceQoS().Build());
break; break;
case IoTPlatformType.IoTSharp: case IoTPlatformType.IoTSharp:
Client.SubscribeAsync("devices/+/rpc/request/+/+", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("devices/+/rpc/request/+/+").WithExactlyOnceQoS().Build());
Client.SubscribeAsync("devices/+/attributes/update", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("devices/+/attributes/update").WithExactlyOnceQoS().Build());
//Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}} //Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}}
Client.SubscribeAsync("devices/+/attributes/response/+", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("devices/+/attributes/response/+").WithExactlyOnceQoS().Build());
break; break;
case IoTPlatformType.ThingsCloud: case IoTPlatformType.ThingsCloud:
Client.SubscribeAsync("gateway/attributes/response", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("gateway/attributes/response").WithExactlyOnceQoS().Build());
Client.SubscribeAsync("gateway/attributes/get/response", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("gateway/attributes/get/response").WithExactlyOnceQoS().Build());
Client.SubscribeAsync("gateway/attributes/push", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("gateway/attributes/push").WithExactlyOnceQoS().Build());
Client.SubscribeAsync("gateway/event/response", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("gateway/event/response").WithExactlyOnceQoS().Build());
Client.SubscribeAsync("gateway/command/send", MqttQualityOfServiceLevel.ExactlyOnce); subTopics.Add(new MqttTopicFilterBuilder().WithTopic("gateway/command/send").WithExactlyOnceQoS().Build());
break; break;
case IoTPlatformType.AliCloudIoT: case IoTPlatformType.AliCloudIoT:
break; break;
@ -142,11 +99,34 @@ namespace Plugin
break; break;
} }
_logger.LogInformation($"MQTT CONNECTED WITH SERVER "); #endregion
Client.ConnectedAsync += Client_ConnectedAsync;
Client.DisconnectedAsync += Client_DisconnectedAsync;
Client.ApplicationMessageReceivedAsync += Client_ApplicationMessageReceivedAsync;
await Client.SubscribeAsync(subTopics);
await Client.StartAsync(_options);
_logger.LogInformation("MQTT WAITING FOR APPLICATION MESSAGES");
}
catch (Exception ex)
{
_logger.LogError($"StartManagedClientAsync FAILED, {ex}");
}
} }
private Task Client_ConnectedAsync(MqttClientConnectedEventArgs arg)
{
_logger.LogInformation($"MQTT CONNECTED WITH SERVER ");
return Task.CompletedTask;
}
private Task Client_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs e) private Task Client_DisconnectedAsync(MqttClientDisconnectedEventArgs arg)
{
_logger.LogError($"MQTT CONNECTING FAILED, {arg.ReasonString}");
return Task.CompletedTask;
}
private Task Client_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e)
{ {
_logger.LogDebug( _logger.LogDebug(
$"ApplicationMessageReceived Topic {e.ApplicationMessage.Topic} QualityOfServiceLevel:{e.ApplicationMessage.QualityOfServiceLevel} Retain:{e.ApplicationMessage.Retain} "); $"ApplicationMessageReceived Topic {e.ApplicationMessage.Topic} QualityOfServiceLevel:{e.ApplicationMessage.QualityOfServiceLevel} Retain:{e.ApplicationMessage.Retain} ");
@ -269,7 +249,7 @@ namespace Plugin
private async Task ResponseTbRpcAsync(TBRpcResponse tBRpcResponse) private async Task ResponseTbRpcAsync(TBRpcResponse tBRpcResponse)
{ {
await Client.PublishAsync(new MqttApplicationMessageBuilder() await Client.EnqueueAsync(new MqttApplicationMessageBuilder()
.WithTopic(_tbRpcTopic) .WithTopic(_tbRpcTopic)
.WithPayload(JsonConvert.SerializeObject(tBRpcResponse)) .WithPayload(JsonConvert.SerializeObject(tBRpcResponse))
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
@ -278,7 +258,7 @@ namespace Plugin
private async Task ResponseTcRpcAsync(TCRpcRequest tCRpcResponse) private async Task ResponseTcRpcAsync(TCRpcRequest tCRpcResponse)
{ {
var topic = $"command/reply/{tCRpcResponse.RequestData.RequestId}"; var topic = $"command/reply/{tCRpcResponse.RequestData.RequestId}";
await Client.PublishAsync(new MqttApplicationMessageBuilder() await Client.EnqueueAsync(new MqttApplicationMessageBuilder()
.WithTopic(topic) .WithTopic(topic)
.WithPayload(JsonConvert.SerializeObject(tCRpcResponse)) .WithPayload(JsonConvert.SerializeObject(tCRpcResponse))
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
@ -288,7 +268,7 @@ namespace Plugin
{ {
//var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}"; //var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}";
var topic = $"devices/{rpcResult.DeviceId}/rpc/response/{rpcResult.Method}/{rpcResult.ResponseId}"; var topic = $"devices/{rpcResult.DeviceId}/rpc/response/{rpcResult.Method}/{rpcResult.ResponseId}";
await Client.PublishAsync(new MqttApplicationMessageBuilder() await Client.EnqueueAsync(new MqttApplicationMessageBuilder()
.WithTopic(topic) .WithTopic(topic)
.WithPayload(JsonConvert.SerializeObject(rpcResult)) .WithPayload(JsonConvert.SerializeObject(rpcResult))
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
@ -327,7 +307,7 @@ namespace Plugin
try try
{ {
if (Client.IsConnected) if (Client.IsConnected)
return Client.PublishAsync(new MqttApplicationMessageBuilder() return Client.EnqueueAsync(new MqttApplicationMessageBuilder()
.WithTopic($"devices/{deviceName}/attributes").WithPayload(JsonConvert.SerializeObject(obj)) .WithTopic($"devices/{deviceName}/attributes").WithPayload(JsonConvert.SerializeObject(obj))
.Build()); .Build());
} }
@ -341,14 +321,14 @@ namespace Plugin
public async Task UploadIsTelemetryDataAsync(string deviceName, object obj) public async Task UploadIsTelemetryDataAsync(string deviceName, object obj)
{ {
await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"devices/{deviceName}/telemetry") await Client.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic($"devices/{deviceName}/telemetry")
.WithPayload(JsonConvert.SerializeObject(obj)).Build()); .WithPayload(JsonConvert.SerializeObject(obj)).Build());
} }
public async Task UploadTcTelemetryDataAsync(string deviceName, object obj) public async Task UploadTcTelemetryDataAsync(string deviceName, object obj)
{ {
var toSend = new Dictionary<string, object> { { deviceName, obj } }; var toSend = new Dictionary<string, object> { { deviceName, obj } };
await Client.PublishAsync("gateway/attributes", JsonConvert.SerializeObject(toSend)); await Client.EnqueueAsync("gateway/attributes", JsonConvert.SerializeObject(toSend));
} }
public async Task UploadHwTelemetryDataAsync(Device device, object obj) public async Task UploadHwTelemetryDataAsync(Device device, object obj)
@ -374,7 +354,7 @@ namespace Plugin
Devices = hwTelemetry Devices = hwTelemetry
}; };
await Client.PublishAsync($"/v1/devices/{_systemConfig?.GatewayName}/datas", await Client.EnqueueAsync($"/v1/devices/{_systemConfig.GatewayName}/datas",
JsonConvert.SerializeObject(hwTelemetrys)); JsonConvert.SerializeObject(hwTelemetrys));
} }
@ -382,7 +362,6 @@ namespace Plugin
{ {
try try
{ {
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
@ -431,7 +410,6 @@ namespace Plugin
try try
{ {
string id = Guid.NewGuid().ToString(); string id = Guid.NewGuid().ToString();
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
@ -443,7 +421,7 @@ namespace Plugin
{ "client", true }, { "client", true },
{ "key", args[0] } { "key", args[0] }
}; };
await Client.PublishAsync("v1/gateway/attributes/request", await Client.EnqueueAsync("v1/gateway/attributes/request",
JsonConvert.SerializeObject(tbRequestData), MqttQualityOfServiceLevel.ExactlyOnce); JsonConvert.SerializeObject(tbRequestData), MqttQualityOfServiceLevel.ExactlyOnce);
break; break;
case IoTPlatformType.IoTSharp: case IoTPlatformType.IoTSharp:
@ -452,7 +430,7 @@ namespace Plugin
keys.Add(anySide ? "anySide" : "server", string.Join(",", args)); keys.Add(anySide ? "anySide" : "server", string.Join(",", args));
await Client.SubscribeAsync($"devices/{deviceName}/attributes/response/{id}", await Client.SubscribeAsync($"devices/{deviceName}/attributes/response/{id}",
MqttQualityOfServiceLevel.ExactlyOnce); MqttQualityOfServiceLevel.ExactlyOnce);
await Client.PublishAsync(topic, JsonConvert.SerializeObject(keys), await Client.EnqueueAsync(topic, JsonConvert.SerializeObject(keys),
MqttQualityOfServiceLevel.ExactlyOnce); MqttQualityOfServiceLevel.ExactlyOnce);
break; break;
case IoTPlatformType.AliCloudIoT: case IoTPlatformType.AliCloudIoT:
@ -526,11 +504,10 @@ namespace Plugin
{ {
if (CanPubTelemetry(device, sendModel)) if (CanPubTelemetry(device, sendModel))
{ {
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
await Client.PublishAsync("v1/gateway/telemetry", await Client.EnqueueAsync("v1/gateway/telemetry",
JsonConvert.SerializeObject(sendModel)); JsonConvert.SerializeObject(sendModel));
break; break;
case IoTPlatformType.IoTSharp: case IoTPlatformType.IoTSharp:
@ -563,7 +540,7 @@ namespace Plugin
measurements = payload.Values?.Keys.ToList(), measurements = payload.Values?.Keys.ToList(),
values = payload.Values?.Values.ToList() values = payload.Values?.Values.ToList()
}; };
await Client.PublishAsync(device.DeviceName, JsonConvert.SerializeObject(tsData)); await Client.EnqueueAsync(device.DeviceName, JsonConvert.SerializeObject(tsData));
} }
break; break;
@ -599,7 +576,7 @@ namespace Plugin
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError($"PublishTelemetryAsync Error", ex); _logger.LogError($"PublishTelemetryAsync Error:{ex}");
} }
} }
@ -607,12 +584,11 @@ namespace Plugin
{ {
try try
{ {
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
case IoTPlatformType.IoTSharp: case IoTPlatformType.IoTSharp:
await Client.PublishAsync("v1/gateway/connect", await Client.EnqueueAsync("v1/gateway/connect",
JsonConvert.SerializeObject(new Dictionary<string, string> JsonConvert.SerializeObject(new Dictionary<string, string>
{ { "device", device.DeviceName } })); { { "device", device.DeviceName } }));
break; break;
@ -625,7 +601,7 @@ namespace Plugin
case IoTPlatformType.OneNET: case IoTPlatformType.OneNET:
break; break;
case IoTPlatformType.ThingsCloud: case IoTPlatformType.ThingsCloud:
await Client.PublishAsync("gateway/connect", await Client.EnqueueAsync("gateway/connect",
JsonConvert.SerializeObject(new Dictionary<string, string> JsonConvert.SerializeObject(new Dictionary<string, string>
{ { "device", device.DeviceName } })); { { "device", device.DeviceName } }));
break; break;
@ -644,7 +620,7 @@ namespace Plugin
} }
} }
}; };
await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update", await Client.EnqueueAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update",
JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce, JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce,
retain: false); retain: false);
break; break;
@ -660,12 +636,11 @@ namespace Plugin
{ {
try try
{ {
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
case IoTPlatformType.IoTSharp: case IoTPlatformType.IoTSharp:
await Client.PublishAsync("v1/gateway/disconnect", await Client.EnqueueAsync("v1/gateway/disconnect",
JsonConvert.SerializeObject(new Dictionary<string, string> JsonConvert.SerializeObject(new Dictionary<string, string>
{ { "device", device.DeviceName } })); { { "device", device.DeviceName } }));
break; break;
@ -678,7 +653,7 @@ namespace Plugin
case IoTPlatformType.OneNET: case IoTPlatformType.OneNET:
break; break;
case IoTPlatformType.ThingsCloud: case IoTPlatformType.ThingsCloud:
await Client.PublishAsync("gateway/disconnect", await Client.EnqueueAsync("gateway/disconnect",
JsonConvert.SerializeObject(new Dictionary<string, string> JsonConvert.SerializeObject(new Dictionary<string, string>
{ { "device", device.DeviceName } })); { { "device", device.DeviceName } }));
break; break;
@ -697,7 +672,7 @@ namespace Plugin
} }
} }
}; };
await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update", await Client.EnqueueAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update",
JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce, JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce,
retain: false); retain: false);
break; break;
@ -714,7 +689,6 @@ namespace Plugin
{ {
try try
{ {
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
@ -735,7 +709,7 @@ namespace Plugin
} }
); );
await Client.PublishAsync(topic, await Client.EnqueueAsync(topic,
JsonConvert.SerializeObject(addDeviceDto)); JsonConvert.SerializeObject(addDeviceDto));
break; break;
} }
@ -751,7 +725,6 @@ namespace Plugin
{ {
try try
{ {
if (_systemConfig != null)
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
@ -775,7 +748,7 @@ namespace Plugin
} }
}; };
await Client.PublishAsync(topic, await Client.EnqueueAsync(topic,
JsonConvert.SerializeObject(deleteDeviceDto)); JsonConvert.SerializeObject(deleteDeviceDto));
} }
break; break;

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@ -9,8 +9,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DynamicExpresso.Core" Version="2.13.0" /> <PackageReference Include="DynamicExpresso.Core" Version="2.13.0" />
<PackageReference Include="Mono.Options" Version="6.12.0.148" /> <PackageReference Include="Mono.Options" Version="6.12.0.148" />
<PackageReference Include="MQTTnet" Version="3.1.2" /> <PackageReference Include="MQTTnet" Version="4.1.0.247" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.4.369.30" /> <PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.4.370.12" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.1.0.247" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>