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.

ClientsController.cs 1.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }