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.
 
 
 
 

80 satır
2.6 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. using MQTTnet.Server.Status;
  11. namespace MQTTnet.Server.Controllers
  12. {
  13. [Authorize]
  14. [ApiController]
  15. public class SessionsController : ControllerBase
  16. {
  17. private readonly MqttServerService _mqttServerService;
  18. public SessionsController(MqttServerService mqttServerService)
  19. {
  20. _mqttServerService = mqttServerService ?? throw new ArgumentNullException(nameof(mqttServerService));
  21. }
  22. [Route("api/v1/sessions")]
  23. [HttpGet]
  24. public async Task<ActionResult<IList<IMqttSessionStatus>>> GetSessions()
  25. {
  26. return new ObjectResult(await _mqttServerService.GetSessionStatusAsync());
  27. }
  28. [Route("api/v1/sessions/{clientId}")]
  29. [HttpGet]
  30. public async Task<ActionResult<IMqttClientStatus>> GetSession(string clientId)
  31. {
  32. clientId = HttpUtility.UrlDecode(clientId);
  33. var session = (await _mqttServerService.GetSessionStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  34. if (session == null)
  35. {
  36. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  37. }
  38. return new ObjectResult(session);
  39. }
  40. [Route("api/v1/sessions/{clientId}")]
  41. [HttpDelete]
  42. public async Task<ActionResult> DeleteSession(string clientId)
  43. {
  44. clientId = HttpUtility.UrlDecode(clientId);
  45. var session = (await _mqttServerService.GetSessionStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  46. if (session == null)
  47. {
  48. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  49. }
  50. await session.DeleteAsync();
  51. return StatusCode((int)HttpStatusCode.NoContent);
  52. }
  53. [Route("api/v1/sessions/{clientId}/pendingApplicationMessages")]
  54. [HttpDelete]
  55. public async Task<ActionResult> DeletePendingApplicationMessages(string clientId)
  56. {
  57. clientId = HttpUtility.UrlDecode(clientId);
  58. var session = (await _mqttServerService.GetSessionStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  59. if (session == null)
  60. {
  61. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  62. }
  63. await session.ClearPendingApplicationMessagesAsync();
  64. return StatusCode((int)HttpStatusCode.NoContent);
  65. }
  66. }
  67. }