Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RetainedApplicationMessagesController.cs 2.2 KiB

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