mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
Update mqtt controller
This commit is contained in:
parent
5b4355d43c
commit
fda53da013
13
MyCore.Interfaces/DTO/MqttMessageDTO.cs
Normal file
13
MyCore.Interfaces/DTO/MqttMessageDTO.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
||||
@ -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";
|
||||
/// <summary>
|
||||
/// It's a mqtt publish test ! :)
|
||||
/// </summary>
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
public void GetToPublishMqtt()
|
||||
private readonly IMqttClientService _mqttClientService;
|
||||
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||
|
||||
public MQTTController(MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
||||
{
|
||||
Client_Publish().ContinueWith(res => {
|
||||
this._mqttClientService = provider.MqttClientService;
|
||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Publish mqtt test
|
||||
/// </summary>
|
||||
/// <param name="mqttMessageDTO">Message to send</param>
|
||||
[ProducesResponseType(typeof(bool), 200)]
|
||||
[HttpPost]
|
||||
public async Task<ObjectResult> PublishMessage([FromBody] MqttMessageDTO mqttMessageDTO)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (mqttMessageDTO == null)
|
||||
throw new KeyNotFoundException("message is null");
|
||||
|
||||
bool isSucess = false;
|
||||
|
||||
await MqttClientService.PublishMessage(mqttMessageDTO.Topic, mqttMessageDTO.Message).ContinueWith(res => {
|
||||
|
||||
if (res.Status == TaskStatus.RanToCompletion)
|
||||
{
|
||||
Console.WriteLine("It's a success !");
|
||||
isSucess = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (mqttMessageDTO.Online)
|
||||
{
|
||||
await MqttClientOnlineService.PublishMessage(mqttMessageDTO.Topic, mqttMessageDTO.Message).ContinueWith(res => {
|
||||
|
||||
if (res.Status == TaskStatus.RanToCompletion)
|
||||
{
|
||||
isSucess = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("We have an issue here.. ");
|
||||
isSucess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected async Task Client_Publish()
|
||||
return new OkObjectResult(isSucess);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var server = new MqttFactory().CreateMqttServer();
|
||||
|
||||
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);
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
await server.StopAsync();
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -203,7 +203,7 @@ namespace MyCore
|
||||
services.AddScoped<DeviceDatabaseService>();
|
||||
services.AddScoped<LocationDatabaseService>();
|
||||
|
||||
services.AddMqttClientHostedService();
|
||||
services.AddMqttClientHostedService(); // Todo client files (a lot are useless)
|
||||
services.AddMqttClientOnlineHostedService();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user