25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 lines
1.9 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 ClientsController : ControllerBase
  14. {
  15. private readonly MqttServerService _mqttServerService;
  16. public ClientsController(MqttServerService mqttServerService)
  17. {
  18. _mqttServerService = mqttServerService ?? throw new ArgumentNullException(nameof(mqttServerService));
  19. }
  20. [Route("api/v1/clients")]
  21. [HttpGet]
  22. public async Task<ActionResult<IList<IMqttClientStatus>>> GetClients()
  23. {
  24. return new ObjectResult(await _mqttServerService.GetClientStatusAsync());
  25. }
  26. [Route("api/v1/clients/{clientId}")]
  27. [HttpGet]
  28. public async Task<ActionResult<IMqttClientStatus>> GetClient(string clientId)
  29. {
  30. clientId = HttpUtility.UrlDecode(clientId);
  31. var client = (await _mqttServerService.GetClientStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  32. if (client == null)
  33. {
  34. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  35. }
  36. return new ObjectResult(client);
  37. }
  38. [Route("api/v1/clients/{clientId}")]
  39. [HttpDelete]
  40. public async Task<ActionResult> DeleteClient(string clientId)
  41. {
  42. clientId = HttpUtility.UrlDecode(clientId);
  43. var client = (await _mqttServerService.GetClientStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  44. if (client == null)
  45. {
  46. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  47. }
  48. await client.DisconnectAsync();
  49. return StatusCode((int)HttpStatusCode.NoContent);
  50. }
  51. }
  52. }