2023-05-23 23:54:28 +08:00
|
|
|
|
#region copyright
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
|
|
|
|
|
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
|
|
|
|
|
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
2023-07-16 17:48:22 +08:00
|
|
|
|
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
2023-05-23 23:54:28 +08:00
|
|
|
|
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
2023-07-16 17:48:22 +08:00
|
|
|
|
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
|
2023-05-23 23:54:28 +08:00
|
|
|
|
// QQ群:605534569
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
using Furion;
|
|
|
|
|
|
|
2023-05-23 23:54:28 +08:00
|
|
|
|
using Mapster;
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
using MQTTnet;
|
2023-10-23 20:43:58 +08:00
|
|
|
|
using MQTTnet.Diagnostics;
|
2023-03-04 18:41:11 +08:00
|
|
|
|
using MQTTnet.Internal;
|
|
|
|
|
|
using MQTTnet.Protocol;
|
|
|
|
|
|
using MQTTnet.Server;
|
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
using ThingsGateway.Admin.Application;
|
|
|
|
|
|
using ThingsGateway.Foundation.Extension.ConcurrentQueue;
|
2023-07-12 21:16:38 +08:00
|
|
|
|
using ThingsGateway.Foundation.Extension.Generic;
|
2023-09-30 23:05:53 +08:00
|
|
|
|
using ThingsGateway.Foundation.Extension.String;
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-09-30 23:05:53 +08:00
|
|
|
|
namespace ThingsGateway.Plugin.Mqtt;
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MqttServer
|
|
|
|
|
|
/// </summary>
|
2023-04-15 20:48:56 +08:00
|
|
|
|
public class MqttServer : UpLoadBase
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
private readonly MqttServerProperty driverPropertys = new();
|
|
|
|
|
|
private readonly MqttClientVariableProperty variablePropertys = new();
|
2023-04-15 20:48:56 +08:00
|
|
|
|
private ConcurrentQueue<DeviceData> _collectDeviceRunTimes = new();
|
|
|
|
|
|
private ConcurrentQueue<VariableData> _collectVariableRunTimes = new();
|
2023-07-12 21:16:38 +08:00
|
|
|
|
private GlobalDeviceData _globalDeviceData;
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-04-15 20:48:56 +08:00
|
|
|
|
private MQTTnet.Server.MqttServer _mqttServer;
|
|
|
|
|
|
private RpcSingletonService _rpcCore;
|
2023-07-12 21:16:38 +08:00
|
|
|
|
private List<DeviceVariableRunTime> _uploadVariables = new();
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override Type DriverDebugUIType => null;
|
|
|
|
|
|
/// <inheritdoc/>
|
2023-05-02 21:58:11 +08:00
|
|
|
|
public override UpDriverPropertyBase DriverPropertys => driverPropertys;
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <inheritdoc/>
|
2023-07-12 21:16:38 +08:00
|
|
|
|
public override List<DeviceVariableRunTime> UploadVariables => _uploadVariables;
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <inheritdoc/>
|
2023-04-15 20:48:56 +08:00
|
|
|
|
public override VariablePropertyBase VariablePropertys => variablePropertys;
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override Task AfterStopAsync()
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
2023-08-07 15:09:53 +08:00
|
|
|
|
return Task.CompletedTask;
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <inheritdoc/>
|
2023-09-30 23:05:53 +08:00
|
|
|
|
public override async Task BeforStartAsync(CancellationToken cancellationToken)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
2023-06-06 19:37:04 +08:00
|
|
|
|
if (_mqttServer != null)
|
|
|
|
|
|
{
|
2023-08-07 15:09:53 +08:00
|
|
|
|
_mqttServer.ValidatingConnectionAsync += MqttServer_ValidatingConnectionAsync;
|
|
|
|
|
|
_mqttServer.InterceptingPublishAsync += MqttServer_InterceptingPublishAsync;
|
|
|
|
|
|
_mqttServer.LoadingRetainedMessageAsync += MqttServer_LoadingRetainedMessageAsync;
|
|
|
|
|
|
_mqttServer.InterceptingSubscriptionAsync += MqttServer_InterceptingSubscriptionAsync; ;
|
|
|
|
|
|
await _mqttServer.StartAsync();
|
|
|
|
|
|
|
2023-06-06 19:37:04 +08:00
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <inheritdoc/>
|
2023-09-30 23:05:53 +08:00
|
|
|
|
public override async Task ExecuteAsync(CancellationToken cancellationToken)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
////变化推送
|
|
|
|
|
|
var varList = _collectVariableRunTimes.ToListWithDequeue();
|
|
|
|
|
|
|
|
|
|
|
|
if (varList?.Count != 0)
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-04-15 20:48:56 +08:00
|
|
|
|
//分解List,避免超出mqtt字节大小限制
|
2023-07-15 22:42:34 +08:00
|
|
|
|
var varData = varList.ChunkTrivialBetter(driverPropertys.SplitSize);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
foreach (var item in varData)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-09-30 23:05:53 +08:00
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
|
|
.WithTopic($"{driverPropertys.VariableTopic}")
|
|
|
|
|
|
.WithPayload(item.GetSciptListValue(driverPropertys.BigTextScriptVariableModel)).Build();
|
|
|
|
|
|
await _mqttServer.InjectApplicationMessage(
|
2023-09-30 23:05:53 +08:00
|
|
|
|
new InjectedMqttApplicationMessage(message), cancellationToken);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2023-10-18 00:15:07 +08:00
|
|
|
|
LogMessage?.LogWarning(ex);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
catch (Exception ex)
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-10-18 00:15:07 +08:00
|
|
|
|
LogMessage?.LogWarning(ex);
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-15 20:48:56 +08:00
|
|
|
|
try
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-04-15 20:48:56 +08:00
|
|
|
|
////变化推送
|
|
|
|
|
|
var devList = _collectDeviceRunTimes.ToListWithDequeue();
|
|
|
|
|
|
if (devList?.Count != 0)
|
2023-03-17 16:04:24 +08:00
|
|
|
|
{
|
2023-04-15 20:48:56 +08:00
|
|
|
|
//分解List,避免超出mqtt字节大小限制
|
2023-07-15 22:42:34 +08:00
|
|
|
|
var varData = devList.ChunkTrivialBetter(driverPropertys.SplitSize);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
foreach (var item in varData)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-09-30 23:05:53 +08:00
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
|
|
.WithTopic($"{driverPropertys.DeviceTopic}")
|
|
|
|
|
|
.WithPayload(item.GetSciptListValue(driverPropertys.BigTextScriptDeviceModel)).Build();
|
|
|
|
|
|
await _mqttServer.InjectApplicationMessage(
|
2023-09-30 23:05:53 +08:00
|
|
|
|
new InjectedMqttApplicationMessage(message), cancellationToken);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2023-10-18 00:15:07 +08:00
|
|
|
|
LogMessage?.LogWarning(ex);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-17 16:04:24 +08:00
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
catch (Exception ex)
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-10-18 00:15:07 +08:00
|
|
|
|
LogMessage?.LogWarning(ex);
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|
2023-10-12 14:57:36 +08:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = GetRetainedMessages();
|
|
|
|
|
|
foreach (var item in data)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _mqttServer.UpdateRetainedMessageAsync(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2023-10-18 00:15:07 +08:00
|
|
|
|
LogMessage?.LogWarning(ex);
|
2023-10-12 14:57:36 +08:00
|
|
|
|
}
|
2023-05-12 16:31:57 +08:00
|
|
|
|
if (driverPropertys.CycleInterval > UploadDeviceThread.CycleInterval + 50)
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-05-29 01:32:37 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-09-30 23:05:53 +08:00
|
|
|
|
await Task.Delay(driverPropertys.CycleInterval - UploadDeviceThread.CycleInterval, cancellationToken);
|
2023-05-29 01:32:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
else
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-04-15 20:48:56 +08:00
|
|
|
|
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override bool IsConnected() => _mqttServer?.IsStarted == true;
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2023-04-15 20:48:56 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $" {nameof(MqttServer)} IP:{driverPropertys.IP} Port:{driverPropertys.Port}";
|
|
|
|
|
|
}
|
2023-08-07 15:09:53 +08:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_mqttServer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_mqttServer.ValidatingConnectionAsync -= MqttServer_ValidatingConnectionAsync;
|
|
|
|
|
|
_mqttServer.InterceptingPublishAsync -= MqttServer_InterceptingPublishAsync;
|
|
|
|
|
|
_mqttServer.LoadingRetainedMessageAsync -= MqttServer_LoadingRetainedMessageAsync;
|
|
|
|
|
|
_mqttServer.InterceptingSubscriptionAsync -= MqttServer_InterceptingSubscriptionAsync; ;
|
|
|
|
|
|
_mqttServer?.SafeDispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
_globalDeviceData?.AllVariables?.ForEach(a => a.VariableValueChange -= VariableValueChange);
|
|
|
|
|
|
_globalDeviceData?.CollectDevices?.ForEach(a =>
|
|
|
|
|
|
{
|
|
|
|
|
|
a.DeviceStatusChange -= DeviceStatusChange;
|
|
|
|
|
|
});
|
|
|
|
|
|
_uploadVariables = null;
|
|
|
|
|
|
_collectDeviceRunTimes.Clear();
|
|
|
|
|
|
_collectVariableRunTimes.Clear();
|
|
|
|
|
|
_collectDeviceRunTimes = null;
|
|
|
|
|
|
_collectVariableRunTimes = null;
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <inheritdoc/>
|
2023-06-06 19:37:04 +08:00
|
|
|
|
protected override void Init(UploadDeviceRunTime device)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
2023-10-23 20:43:58 +08:00
|
|
|
|
var log = new MqttNetEventLogger();
|
|
|
|
|
|
log.LogMessagePublished += Log_LogMessagePublished;
|
|
|
|
|
|
var mqttFactory = new MqttFactory(log);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder()
|
2023-08-07 15:09:53 +08:00
|
|
|
|
.WithDefaultEndpointBoundIPAddress(string.IsNullOrEmpty(driverPropertys.IP) ? null : IPAddress.Parse(driverPropertys.IP))
|
2023-04-15 20:48:56 +08:00
|
|
|
|
.WithDefaultEndpointPort(driverPropertys.Port)
|
|
|
|
|
|
.WithDefaultEndpoint()
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
_mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions);
|
|
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
_globalDeviceData = App.GetService<GlobalDeviceData>();
|
|
|
|
|
|
_rpcCore = App.GetService<RpcSingletonService>();
|
2023-04-15 20:48:56 +08:00
|
|
|
|
|
2023-07-12 21:16:38 +08:00
|
|
|
|
var tags = _globalDeviceData.AllVariables.Where(a => a.VariablePropertys.ContainsKey(device.Id))
|
2023-06-06 19:37:04 +08:00
|
|
|
|
.Where(b => GetPropertyValue(b, nameof(variablePropertys.Enable)).GetBoolValue())
|
2023-04-15 20:48:56 +08:00
|
|
|
|
.ToList();
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-04-15 20:48:56 +08:00
|
|
|
|
_uploadVariables = tags;
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-07-12 21:16:38 +08:00
|
|
|
|
_globalDeviceData.CollectDevices.Where(a => _uploadVariables.Select(b => b.DeviceId).Contains(a.Id)).ForEach(a =>
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-07-22 20:32:34 +08:00
|
|
|
|
a.DeviceStatusChange += DeviceStatusChange;
|
2023-04-15 20:48:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
_uploadVariables.ForEach(a =>
|
|
|
|
|
|
{
|
|
|
|
|
|
a.VariableValueChange += VariableValueChange;
|
|
|
|
|
|
});
|
2023-03-17 16:04:24 +08:00
|
|
|
|
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
2023-10-23 20:43:58 +08:00
|
|
|
|
private void Log_LogMessagePublished(object sender, MqttNetLogMessagePublishedEventArgs e)
|
|
|
|
|
|
{
|
2023-10-23 20:51:11 +08:00
|
|
|
|
if (e.LogMessage.Exception is not ArgumentNullException)
|
|
|
|
|
|
LogMessage.LogOut(e.LogMessage.Level, e.LogMessage.Source, e.LogMessage.Message, e.LogMessage.Exception);
|
2023-10-23 20:43:58 +08:00
|
|
|
|
}
|
2023-07-22 20:32:34 +08:00
|
|
|
|
private void DeviceStatusChange(CollectDeviceRunTime collectDeviceRunTime)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
_collectDeviceRunTimes.Enqueue(collectDeviceRunTime.Adapt<DeviceData>());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task MqttServer_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
|
|
|
|
|
|
{
|
2023-08-07 15:09:53 +08:00
|
|
|
|
if (!driverPropertys.DeviceRpcEnable || string.IsNullOrEmpty(arg.ClientId))
|
2023-04-15 20:48:56 +08:00
|
|
|
|
return;
|
|
|
|
|
|
if (arg.ApplicationMessage.Topic != driverPropertys.RpcWriteTopic)
|
|
|
|
|
|
return;
|
2023-08-27 15:59:57 +08:00
|
|
|
|
var rpcDatas = Encoding.UTF8.GetString(arg.ApplicationMessage.PayloadSegment).FromJsonString<MqttRpcNameVaueWithId>();
|
2023-08-22 11:23:03 +08:00
|
|
|
|
if (rpcDatas == null)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
return;
|
2023-08-22 11:23:03 +08:00
|
|
|
|
MqttRpcResult mqttRpcResult = new() { RpcId = rpcDatas.RpcId, Success = true };
|
2023-04-15 20:48:56 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-08-22 11:23:03 +08:00
|
|
|
|
foreach (var rpcData in rpcDatas.WriteInfos)
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
2023-03-17 16:04:24 +08:00
|
|
|
|
|
2023-08-22 11:23:03 +08:00
|
|
|
|
var tag = _uploadVariables.FirstOrDefault(a => a.Name == rpcData.Key);
|
|
|
|
|
|
if (tag != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rpcEnable = GetPropertyValue(tag, nameof(variablePropertys.VariableRpcEnable)).ToBoolean();
|
|
|
|
|
|
if (rpcEnable == true)
|
|
|
|
|
|
{
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-08-22 11:23:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
mqttRpcResult.Success = false;
|
2023-08-27 15:59:57 +08:00
|
|
|
|
mqttRpcResult.Message.Add(rpcData.Key, new OperResult("权限不足,变量不支持写入"));
|
2023-08-22 11:23:03 +08:00
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-08-22 11:23:03 +08:00
|
|
|
|
mqttRpcResult.Success = false;
|
2023-08-27 15:59:57 +08:00
|
|
|
|
mqttRpcResult.Message.Add(rpcData.Key, new OperResult("不存在该变量"));
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
2023-03-17 17:30:06 +08:00
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
|
2023-08-22 11:23:03 +08:00
|
|
|
|
var result = await _rpcCore.InvokeDeviceMethodAsync(ToString() + "-" + arg.ClientId,
|
|
|
|
|
|
rpcDatas.WriteInfos.Where(
|
|
|
|
|
|
a => !mqttRpcResult.Message.Any(b => b.Key == a.Key)).ToDictionary(a => a.Key, a => a.Value));
|
|
|
|
|
|
|
|
|
|
|
|
mqttRpcResult.Message.AddRange(result);
|
|
|
|
|
|
mqttRpcResult.Success = !mqttRpcResult.Message.Any(a => !a.Value.IsSuccess);
|
|
|
|
|
|
}
|
2023-04-15 20:48:56 +08:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2023-10-18 00:15:07 +08:00
|
|
|
|
LogMessage?.LogWarning(ex);
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var variableMessage = new MqttApplicationMessageBuilder()
|
|
|
|
|
|
.WithTopic($"{driverPropertys.RpcSubTopic}")
|
2023-08-27 15:59:57 +08:00
|
|
|
|
.WithPayload(mqttRpcResult.ToJsonString()).Build();
|
2023-04-15 20:48:56 +08:00
|
|
|
|
await _mqttServer.InjectApplicationMessage(
|
2023-08-22 11:23:03 +08:00
|
|
|
|
new InjectedMqttApplicationMessage(variableMessage));
|
2023-04-15 20:48:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
2023-03-04 18:41:11 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 15:09:53 +08:00
|
|
|
|
private Task MqttServer_InterceptingSubscriptionAsync(InterceptingSubscriptionEventArgs arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (arg.TopicFilter.Topic == driverPropertys.RpcWriteTopic)
|
|
|
|
|
|
{
|
|
|
|
|
|
arg.Response.ReasonCode = MqttSubscribeReasonCode.UnspecifiedError;
|
|
|
|
|
|
}
|
|
|
|
|
|
return CompletedTask.Instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Task MqttServer_LoadingRetainedMessageAsync(LoadingRetainedMessagesEventArgs arg)
|
2023-10-12 14:57:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
List<MqttApplicationMessage> Messages = GetRetainedMessages();
|
|
|
|
|
|
arg.LoadedRetainedMessages = Messages;
|
|
|
|
|
|
return CompletedTask.Instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<MqttApplicationMessage> GetRetainedMessages()
|
2023-08-07 15:09:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
//首次连接时的保留消息
|
|
|
|
|
|
//分解List,避免超出mqtt字节大小限制
|
|
|
|
|
|
var varData = _globalDeviceData.AllVariables.Adapt<List<VariableData>>().ChunkTrivialBetter(driverPropertys.SplitSize);
|
2023-10-12 14:57:36 +08:00
|
|
|
|
var devData = _globalDeviceData.CollectDevices.Adapt<List<DeviceData>>().ChunkTrivialBetter(driverPropertys.SplitSize);
|
2023-08-07 15:09:53 +08:00
|
|
|
|
List<MqttApplicationMessage> Messages = new();
|
|
|
|
|
|
foreach (var item in varData)
|
|
|
|
|
|
{
|
|
|
|
|
|
Messages.Add(new MqttApplicationMessageBuilder()
|
|
|
|
|
|
.WithTopic($"{driverPropertys.VariableTopic}")
|
|
|
|
|
|
.WithPayload(item.GetSciptListValue(driverPropertys.BigTextScriptVariableModel)).Build());
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var item in devData)
|
|
|
|
|
|
{
|
|
|
|
|
|
Messages.Add(new MqttApplicationMessageBuilder()
|
|
|
|
|
|
.WithTopic($"{driverPropertys.DeviceTopic}")
|
|
|
|
|
|
.WithPayload(item.GetSciptListValue(driverPropertys.BigTextScriptDeviceModel)).Build());
|
|
|
|
|
|
}
|
2023-10-12 14:57:36 +08:00
|
|
|
|
|
|
|
|
|
|
return Messages;
|
2023-08-07 15:09:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task MqttServer_ValidatingConnectionAsync(ValidatingConnectionEventArgs arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!arg.ClientId.StartsWith(driverPropertys.StartWithId))
|
|
|
|
|
|
{
|
|
|
|
|
|
arg.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var _openApiUserService = App.GetService<IOpenApiUserService>();
|
|
|
|
|
|
var userInfo = await _openApiUserService.GetUserByAccountAsync(arg.UserName);//获取用户信息
|
|
|
|
|
|
if (userInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
arg.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (userInfo.Password != arg.Password)
|
|
|
|
|
|
{
|
|
|
|
|
|
arg.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-09-30 23:05:53 +08:00
|
|
|
|
LogMessage?.LogInformation($"{ToString()}-{arg.ClientId}-客户端已连接成功");
|
2023-08-07 15:09:53 +08:00
|
|
|
|
}
|
2023-07-12 21:16:38 +08:00
|
|
|
|
private void VariableValueChange(DeviceVariableRunTime collectVariableRunTime)
|
2023-04-15 20:48:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
_collectVariableRunTimes.Enqueue(collectVariableRunTime.Adapt<VariableData>());
|
|
|
|
|
|
}
|
2023-03-04 18:41:11 +08:00
|
|
|
|
}
|