diff --git a/MyCore.Interfaces/DTO/MqttMessageDTO.cs b/MyCore.Interfaces/DTO/MqttMessageDTO.cs new file mode 100644 index 0000000..f104796 --- /dev/null +++ b/MyCore.Interfaces/DTO/MqttMessageDTO.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MyCore.Interfaces.DTO +{ + public class MqttMessageDTO + { + public string Topic { get; set; } + public string Message { get; set; } + public bool Online{ get; set; } + } +} diff --git a/MyCore/Controllers/Helpers/MQTTController.cs b/MyCore/Controllers/Helpers/MQTTController.cs index b10e1b7..6e81d1b 100644 --- a/MyCore/Controllers/Helpers/MQTTController.cs +++ b/MyCore/Controllers/Helpers/MQTTController.cs @@ -5,54 +5,72 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Mqtt.Client.AspNetCore.Services; using MQTTnet; using MQTTnet.Client; using MQTTnet.Client.Options; +using MyCore.Interfaces.DTO; namespace MyCore.Controllers { - [Authorize(Roles = "Admin")] + [Authorize] // TODO role (Roles = "Admin")] [Route("api/mqtt")] [ApiController] public class MQTTController : ControllerBase { - private string _mqttServer = "192.168.31.140"; - /// - /// It's a mqtt publish test ! :) - /// - [AllowAnonymous] - [HttpGet] - public void GetToPublishMqtt() + private readonly IMqttClientService _mqttClientService; + private readonly IMqttOnlineClientService _mqttOnlineClientService; + + public MQTTController(MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider) { - Client_Publish().ContinueWith(res => { - if (res.Status == TaskStatus.RanToCompletion) - { - Console.WriteLine("It's a success !"); - } - else - { - Console.WriteLine("We have an issue here.. "); - } - }); + this._mqttClientService = provider.MqttClientService; + this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; } - protected async Task Client_Publish() - { - var server = new MqttFactory().CreateMqttServer(); + /// + /// Publish mqtt test + /// + /// Message to send + [ProducesResponseType(typeof(bool), 200)] + [HttpPost] + public async Task PublishMessage([FromBody] MqttMessageDTO mqttMessageDTO) + { try { - var client1 = new MqttFactory().CreateMqttClient(); - await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer(_mqttServer).Build()); - var message = new MqttApplicationMessageBuilder().WithPayload("It's a test").WithTopic("IpAddress").WithRetainFlag().Build(); - await client1.PublishAsync(message); + if (mqttMessageDTO == null) + throw new KeyNotFoundException("message is null"); - await Task.Delay(500); + bool isSucess = false; + await MqttClientService.PublishMessage(mqttMessageDTO.Topic, mqttMessageDTO.Message).ContinueWith(res => { + + if (res.Status == TaskStatus.RanToCompletion) + { + isSucess = true; + } + }); + + if (mqttMessageDTO.Online) + { + await MqttClientOnlineService.PublishMessage(mqttMessageDTO.Topic, mqttMessageDTO.Message).ContinueWith(res => { + + if (res.Status == TaskStatus.RanToCompletion) + { + isSucess = true; + } + else + { + isSucess = false; + } + }); + } + + return new OkObjectResult(isSucess); } - finally + catch (Exception ex) { - await server.StopAsync(); + return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } diff --git a/MyCore/Startup.cs b/MyCore/Startup.cs index d641dd0..1a63328 100644 --- a/MyCore/Startup.cs +++ b/MyCore/Startup.cs @@ -203,7 +203,7 @@ namespace MyCore services.AddScoped(); services.AddScoped(); - services.AddMqttClientHostedService(); + services.AddMqttClientHostedService(); // Todo client files (a lot are useless) services.AddMqttClientOnlineHostedService(); }