添加mqtt服务单例
This commit is contained in:
@@ -48,3 +48,4 @@ bld/
|
||||
[Ll]ogs/
|
||||
/JY.DAL/obj/Release/JY.DAL.csproj.AssemblyReference.cache
|
||||
/JY.Inspection/obj/Debug/JY.Inspection.csproj.AssemblyReference.cache
|
||||
/JY.Inspection/.vs/JY.Inspection.csproj.dtbcache.json
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace JY.Inspection.Common
|
||||
{
|
||||
public class MqttSingleton
|
||||
{
|
||||
// 单例实例
|
||||
private static readonly Lazy<MqttSingleton> _instance =
|
||||
new Lazy<MqttSingleton>(() => new MqttSingleton());
|
||||
|
||||
public static MqttSingleton Instance => _instance.Value;
|
||||
|
||||
// 内部成员
|
||||
private IMqttClient _client;
|
||||
private MqttClientOptions _options;
|
||||
private bool _isInitialized = false;
|
||||
|
||||
// 事件:收到消息时触发
|
||||
public event Action<string, string> MessageReceived;
|
||||
|
||||
// 私有构造函数
|
||||
private MqttSingleton() { }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化 MQTT 客户端(只需调用一次)
|
||||
/// </summary>
|
||||
/// <param name="brokerHost">MQTT 服务端地址</param>
|
||||
/// <param name="port">端口,默认1883</param>
|
||||
/// <param name="clientId">客户端ID,为空则自动生成</param>
|
||||
/// <param name="username">用户名(可选)</param>
|
||||
/// <param name="password">密码(可选)</param>
|
||||
/// <param name="useTls">是否启用TLS加密</param>
|
||||
public async Task InitializeAsync(
|
||||
string brokerHost,
|
||||
int port = 1883,
|
||||
string clientId = null,
|
||||
string username = null,
|
||||
string password = null,
|
||||
bool useTls = false)
|
||||
{
|
||||
if (_isInitialized) return;
|
||||
|
||||
// 创建客户端
|
||||
var factory = new MqttFactory();
|
||||
_client = factory.CreateMqttClient();
|
||||
|
||||
// 构建连接选项
|
||||
var optionsBuilder = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer(brokerHost, port)
|
||||
.WithClientId(string.IsNullOrEmpty(clientId) ? $"WinForm_{Guid.NewGuid()}" : clientId)
|
||||
.WithCleanSession();
|
||||
|
||||
if (!string.IsNullOrEmpty(username))
|
||||
{
|
||||
optionsBuilder.WithCredentials(username, password);
|
||||
}
|
||||
|
||||
if (useTls)
|
||||
{
|
||||
optionsBuilder.WithTls();
|
||||
}
|
||||
|
||||
_options = optionsBuilder.Build();
|
||||
|
||||
// 绑定事件
|
||||
_client.ConnectedAsync += async e =>
|
||||
{
|
||||
Console.WriteLine("MQTT 已连接");
|
||||
};
|
||||
|
||||
_client.DisconnectedAsync += async e =>
|
||||
{
|
||||
Console.WriteLine("MQTT 已断开,尝试重连...");
|
||||
// 自动重连
|
||||
await Task.Delay(3000).ContinueWith(_ => ConnectAsync());
|
||||
};
|
||||
|
||||
_client.ApplicationMessageReceivedAsync += async e =>
|
||||
{
|
||||
var topic = e.ApplicationMessage.Topic;
|
||||
var payload = e.ApplicationMessage.PayloadSegment == null
|
||||
? null
|
||||
: Encoding.UTF8.GetString(e.ApplicationMessage.PayloadSegment.Array);
|
||||
|
||||
MessageReceived?.Invoke(topic, payload);
|
||||
};
|
||||
|
||||
// 连接
|
||||
await ConnectAsync();
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接MQTT服务端
|
||||
/// </summary>
|
||||
private async Task ConnectAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_client.IsConnected)
|
||||
{
|
||||
await _client.ConnectAsync(_options, System.Threading.CancellationToken.None);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"MQTT连接失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="topic">主题</param>
|
||||
/// <param name="payload">消息内容</param>
|
||||
/// <param name="qos">QoS等级,默认1</param>
|
||||
public async Task SendAsync(string topic, string payload, int qos = 1)
|
||||
{
|
||||
if (!_client.IsConnected)
|
||||
{
|
||||
await ConnectAsync();
|
||||
}
|
||||
|
||||
if (_client.IsConnected)
|
||||
{
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic(topic)
|
||||
.WithPayload(payload)
|
||||
.WithQualityOfServiceLevel((MqttQualityOfServiceLevel)qos)
|
||||
.WithRetainFlag(false)
|
||||
.Build();
|
||||
|
||||
await _client.PublishAsync(message, System.Threading.CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅主题
|
||||
/// </summary>
|
||||
public async Task SubscribeAsync(string topic)
|
||||
{
|
||||
if (_client.IsConnected)
|
||||
{
|
||||
await _client.SubscribeAsync(new MqttTopicFilterBuilder()
|
||||
.WithTopic(topic)
|
||||
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
if (_client != null && _client.IsConnected)
|
||||
{
|
||||
await _client.DisconnectAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前是否已连接
|
||||
/// </summary>
|
||||
public bool IsConnected => _client?.IsConnected ?? false;
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,9 @@
|
||||
<Reference Include="MiniExcel, Version=1.36.1.0, Culture=neutral, PublicKeyToken=e7310002a53eac39, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MiniExcel.1.36.1\lib\net45\MiniExcel.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MQTTnet, Version=4.3.7.1207, Culture=neutral, PublicKeyToken=fdb7629f2e364a63, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MQTTnet.4.3.7.1207\lib\net48\MQTTnet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -182,6 +185,7 @@
|
||||
<Compile Include="Common\MessageBoxTimeOut.cs" />
|
||||
<Compile Include="Common\AlarmForm.cs" />
|
||||
<Compile Include="Common\DeleteLog.cs" />
|
||||
<Compile Include="Common\MqttSingleton.cs" />
|
||||
<Compile Include="Common\PLCAlarmParse.cs" />
|
||||
<Compile Include="Common\Global.cs" />
|
||||
<Compile Include="Common\StrUtil.cs" />
|
||||
|
||||
@@ -344,7 +344,7 @@ namespace JY.Inspection.Mes
|
||||
throw new ArgumentNullException("MES员工登录请求信息为空");
|
||||
}
|
||||
|
||||
// TODO MES登录请求地址
|
||||
// MES登录请求地址
|
||||
DateTime currentTime = DateTime.Now;
|
||||
string reqUrl = Global.systemConfig.LoginMesUrl;
|
||||
MesCallResult<RespLoginMes> resp = new MesCallResult<RespLoginMes>();
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="10.0.9" targetFramework="net48" />
|
||||
<package id="Microsoft.IO.RecyclableMemoryStream" version="3.0.1" targetFramework="net48" />
|
||||
<package id="MiniExcel" version="1.36.1" targetFramework="net48" />
|
||||
<package id="MQTTnet" version="4.3.7.1207" targetFramework="net48" />
|
||||
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net452" />
|
||||
<package id="NModbus4" version="2.1.0" targetFramework="net452" />
|
||||
<package id="NPOI" version="2.5.5" targetFramework="net452" />
|
||||
|
||||
Reference in New Issue
Block a user