25개 이상의 토픽을 선택하실 수 없습니다.
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- using System.Web;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using MQTTnet.Server.Mqtt;
-
- namespace MQTTnet.Server.Controllers
- {
- [Authorize]
- [ApiController]
- public class RetainedApplicationMessagesController : ControllerBase
- {
- private readonly MqttServerService _mqttServerService;
-
- public RetainedApplicationMessagesController(MqttServerService mqttServerService)
- {
- _mqttServerService = mqttServerService ?? throw new ArgumentNullException(nameof(mqttServerService));
- }
-
- [Route("api/v1/retainedApplicationMessages")]
- [HttpGet]
- public async Task<ActionResult<IList<MqttApplicationMessage>>> GetRetainedApplicationMessages()
- {
- return new ObjectResult(await _mqttServerService.GetRetainedApplicationMessagesAsync());
- }
-
- [Route("api/v1/retainedApplicationMessages/{topic}")]
- [HttpGet]
- public async Task<ActionResult<MqttApplicationMessage>> GetRetainedApplicationMessage(string topic)
- {
- topic = HttpUtility.UrlDecode(topic);
-
- var applicationMessage = (await _mqttServerService.GetRetainedApplicationMessagesAsync()).FirstOrDefault(c => c.Topic == topic);
- if (applicationMessage == null)
- {
- return new StatusCodeResult((int)HttpStatusCode.NotFound);
- }
-
- return new ObjectResult(applicationMessage);
- }
-
- [Route("api/v1/retainedApplicationMessages")]
- [HttpDelete]
- public async Task<ActionResult> DeleteRetainedApplicationMessages()
- {
- await _mqttServerService.ClearRetainedApplicationMessagesAsync();
- return StatusCode((int)HttpStatusCode.NoContent);
- }
-
- [Route("api/v1/retainedApplicationMessages/{topic}")]
- [HttpDelete]
- public async Task<ActionResult> DeleteRetainedApplicationMessage(string topic)
- {
- topic = HttpUtility.UrlDecode(topic);
-
- await _mqttServerService.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithRetainFlag().Build());
- return StatusCode((int)HttpStatusCode.NoContent);
- }
- }
- }
|