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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user