選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

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