25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

65 satır
2.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Mvc;
  9. using MQTTnet.Server.Mqtt;
  10. namespace MQTTnet.Server.Controllers
  11. {
  12. [Authorize]
  13. [ApiController]
  14. public class RetainedApplicationMessagesController : Controller
  15. {
  16. private readonly MqttServerService _mqttServerService;
  17. public RetainedApplicationMessagesController(MqttServerService mqttServerService)
  18. {
  19. _mqttServerService = mqttServerService ?? throw new ArgumentNullException(nameof(mqttServerService));
  20. }
  21. [Route("api/v1/retainedApplicationMessages")]
  22. [HttpGet]
  23. public async Task<ActionResult<IList<MqttApplicationMessage>>> GetRetainedApplicationMessages()
  24. {
  25. return new ObjectResult(await _mqttServerService.GetRetainedApplicationMessagesAsync());
  26. }
  27. [Route("api/v1/retainedApplicationMessages/{topic}")]
  28. [HttpGet]
  29. public async Task<ActionResult<MqttApplicationMessage>> GetRetainedApplicationMessage(string topic)
  30. {
  31. topic = HttpUtility.UrlDecode(topic);
  32. var applicationMessage = (await _mqttServerService.GetRetainedApplicationMessagesAsync()).FirstOrDefault(c => c.Topic == topic);
  33. if (applicationMessage == null)
  34. {
  35. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  36. }
  37. return new ObjectResult(applicationMessage);
  38. }
  39. [Route("api/v1/retainedApplicationMessages")]
  40. [HttpDelete]
  41. public async Task<ActionResult> DeleteRetainedApplicationMessages()
  42. {
  43. await _mqttServerService.ClearRetainedApplicationMessagesAsync();
  44. return StatusCode((int)HttpStatusCode.NoContent);
  45. }
  46. [Route("api/v1/retainedApplicationMessages/{topic}")]
  47. [HttpDelete]
  48. public async Task<ActionResult> DeleteRetainedApplicationMessage(string topic)
  49. {
  50. topic = HttpUtility.UrlDecode(topic);
  51. await _mqttServerService.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithRetainFlag().Build());
  52. return StatusCode((int)HttpStatusCode.NoContent);
  53. }
  54. }
  55. }