From 019f428f0a41c83648e5e1e13d9eb52426206d2d Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 4 Jul 2019 20:27:48 +0200 Subject: [PATCH] MQTTnet.Server: Add HTTP API for publishing of messages. --- .../Controllers/ClientsController.cs | 2 +- .../Controllers/MessagesController.cs | 40 +++++++++++++++++++ .../RetainedApplicationMessagesController.cs | 2 +- .../Controllers/SessionsController.cs | 2 +- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 Source/MQTTnet.Server/Controllers/MessagesController.cs diff --git a/Source/MQTTnet.Server/Controllers/ClientsController.cs b/Source/MQTTnet.Server/Controllers/ClientsController.cs index 6898375..bd9795a 100644 --- a/Source/MQTTnet.Server/Controllers/ClientsController.cs +++ b/Source/MQTTnet.Server/Controllers/ClientsController.cs @@ -13,7 +13,7 @@ namespace MQTTnet.Server.Controllers { [Authorize] [ApiController] - public class ClientsController : ControllerBase + public class ClientsController : Controller { private readonly MqttServerService _mqttServerService; diff --git a/Source/MQTTnet.Server/Controllers/MessagesController.cs b/Source/MQTTnet.Server/Controllers/MessagesController.cs new file mode 100644 index 0000000..89eb385 --- /dev/null +++ b/Source/MQTTnet.Server/Controllers/MessagesController.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using MQTTnet.Server.Mqtt; + +namespace MQTTnet.Server.Controllers +{ + [Authorize] + [ApiController] + public class MessagesController : Controller + { + private readonly MqttServerService _mqttServerService; + + public MessagesController(MqttServerService mqttServerService) + { + _mqttServerService = mqttServerService ?? throw new ArgumentNullException(nameof(mqttServerService)); + } + + [Route("api/v1/messages")] + [HttpPost] + public async Task PostMessage(MqttApplicationMessage message) + { + await _mqttServerService.PublishAsync(message); + return Ok(); + } + + [Route("api/v1/messages/{*topic}")] + [HttpPost] + public Task PostMessage(string topic, string payload) + { + var message = new MqttApplicationMessageBuilder() + .WithTopic(topic) + .WithPayload(payload) + .Build(); + + return PostMessage(message); + } + } +} diff --git a/Source/MQTTnet.Server/Controllers/RetainedApplicationMessagesController.cs b/Source/MQTTnet.Server/Controllers/RetainedApplicationMessagesController.cs index 9c9f273..030d141 100644 --- a/Source/MQTTnet.Server/Controllers/RetainedApplicationMessagesController.cs +++ b/Source/MQTTnet.Server/Controllers/RetainedApplicationMessagesController.cs @@ -12,7 +12,7 @@ namespace MQTTnet.Server.Controllers { [Authorize] [ApiController] - public class RetainedApplicationMessagesController : ControllerBase + public class RetainedApplicationMessagesController : Controller { private readonly MqttServerService _mqttServerService; diff --git a/Source/MQTTnet.Server/Controllers/SessionsController.cs b/Source/MQTTnet.Server/Controllers/SessionsController.cs index 463c004..5fd0638 100644 --- a/Source/MQTTnet.Server/Controllers/SessionsController.cs +++ b/Source/MQTTnet.Server/Controllers/SessionsController.cs @@ -13,7 +13,7 @@ namespace MQTTnet.Server.Controllers { [Authorize] [ApiController] - public class SessionsController : ControllerBase + public class SessionsController : Controller { private readonly MqttServerService _mqttServerService;