MQTT全局服务订阅,基本功能创建,标签打印等功能基本实现
This commit is contained in:
52
ZR.Admin.WebApi/Controllers/mqtt/MqttController.cs
Normal file
52
ZR.Admin.WebApi/Controllers/mqtt/MqttController.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MQTTnet.Protocol;
|
||||
using ZR.Common.MqttHelper;
|
||||
using ZR.Model.MES.wms;
|
||||
using ZR.Model.MES.wms.Dto;
|
||||
using ZR.Service.mes.wms.IService;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// agv 相关接口
|
||||
/// </summary>
|
||||
|
||||
[Route("/mqtt")]
|
||||
public class MqttController : BaseController
|
||||
{
|
||||
private readonly MqttService _mqttService;
|
||||
|
||||
public MqttController(MqttService mqttService)
|
||||
{
|
||||
_mqttService = mqttService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1. 发布信息
|
||||
/// </summary>
|
||||
/// <param name="topic">主题</param>
|
||||
/// <param name="payload">信息</param>
|
||||
/// <returns></returns>
|
||||
|
||||
[HttpPost("publish")]
|
||||
public async Task<IActionResult> PublishMessage(string topic, string payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 发布消息到MQTT代理服务器
|
||||
await _mqttService.PublishAsync(
|
||||
topic,
|
||||
payload,
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
false
|
||||
);
|
||||
|
||||
return Ok("消息已发布");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"发布消息失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<key id="6f790ae9-1260-4b42-ac56-6835d3555186" version="1">
|
||||
<creationDate>2025-05-13T00:45:18.9703791Z</creationDate>
|
||||
<activationDate>2025-05-13T00:45:18.9163673Z</activationDate>
|
||||
<expirationDate>2025-08-11T00:45:18.9163673Z</expirationDate>
|
||||
<descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
|
||||
<descriptor>
|
||||
<encryption algorithm="AES_256_CBC" />
|
||||
<validation algorithm="HMACSHA256" />
|
||||
<masterKey p4:requiresEncryption="true" xmlns:p4="http://schemas.asp.net/2015/03/dataProtection">
|
||||
<!-- Warning: the key below is in an unencrypted form. -->
|
||||
<value>dlysrp3IsJDOznQcS5WVSPFQ68bSJBN8cF8lnJ1+a0QzUsCf9KgzUDoQsgZ/9q/pyoKtTZJCnEoXO+m5nUaj3Q==</value>
|
||||
</masterKey>
|
||||
</descriptor>
|
||||
</descriptor>
|
||||
</key>
|
||||
@@ -11,6 +11,7 @@ using ZR.Admin.WebApi.Framework;
|
||||
using ZR.Admin.WebApi.Hubs;
|
||||
using ZR.Admin.WebApi.Middleware;
|
||||
using ZR.Common.Cache;
|
||||
using ZR.Common.MqttHelper;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -21,6 +22,12 @@ builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
//注入HttpContextAccessor
|
||||
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
// 注册MyMqttConfig(依赖ILogger和IConfiguration)
|
||||
builder.Services.AddSingleton<MyMqttConfig>();
|
||||
// 注册MqttService为单例服务,并作为后台服务运行 !!!! 这样注册就行了 ================
|
||||
builder.Services.AddSingleton<MqttService>();
|
||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<MqttService>());
|
||||
/// ===============================================================================
|
||||
// 跨域配置
|
||||
builder.Services.AddCors(builder.Configuration);
|
||||
// 显示logo
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
{
|
||||
//DOANtech123
|
||||
"MqttConfig": {
|
||||
"ClientId": "shgg-mes-server",
|
||||
"Server": "192.168.23.165",
|
||||
"Port": 1883,
|
||||
"Username": "admin",
|
||||
"Password": "123456",
|
||||
"Topics": [
|
||||
{
|
||||
"Topic": "devices/#",
|
||||
"QualityOfServiceLevel": "AtLeastOnce"
|
||||
},
|
||||
{
|
||||
"Topic": "system/alert",
|
||||
"QualityOfServiceLevel": "AtLeastOnce"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user