Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

80 строки
2.5 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 ClientsController : Controller
  16. {
  17. private readonly MqttServerService _mqttServerService;
  18. public ClientsController(MqttServerService mqttServerService)
  19. {
  20. _mqttServerService = mqttServerService ?? throw new ArgumentNullException(nameof(mqttServerService));
  21. }
  22. [Route("api/v1/clients")]
  23. [HttpGet]
  24. public async Task<ActionResult<IList<IMqttClientStatus>>> GetClients()
  25. {
  26. return new ObjectResult(await _mqttServerService.GetClientStatusAsync());
  27. }
  28. [Route("api/v1/clients/{clientId}")]
  29. [HttpGet]
  30. public async Task<ActionResult<IMqttClientStatus>> GetClient(string clientId)
  31. {
  32. clientId = HttpUtility.UrlDecode(clientId);
  33. var client = (await _mqttServerService.GetClientStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  34. if (client == null)
  35. {
  36. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  37. }
  38. return new ObjectResult(client);
  39. }
  40. [Route("api/v1/clients/{clientId}")]
  41. [HttpDelete]
  42. public async Task<ActionResult> DeleteClient(string clientId)
  43. {
  44. clientId = HttpUtility.UrlDecode(clientId);
  45. var client = (await _mqttServerService.GetClientStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  46. if (client == null)
  47. {
  48. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  49. }
  50. await client.DisconnectAsync();
  51. return StatusCode((int)HttpStatusCode.NoContent);
  52. }
  53. [Route("api/v1/clients/{clientId}/statistics")]
  54. [HttpDelete]
  55. public async Task<ActionResult> DeleteClientStatistics(string clientId)
  56. {
  57. clientId = HttpUtility.UrlDecode(clientId);
  58. var client = (await _mqttServerService.GetClientStatusAsync()).FirstOrDefault(c => c.ClientId == clientId);
  59. if (client == null)
  60. {
  61. return new StatusCodeResult((int)HttpStatusCode.NotFound);
  62. }
  63. client.ResetStatistics();
  64. return StatusCode((int)HttpStatusCode.NoContent);
  65. }
  66. }
  67. }