using System.Linq; using System.Threading.Tasks; using HHWMS.Web.Models; using HHWMS.Web.Services; using Microsoft.AspNetCore.Mvc; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Mvc; namespace HHWMS.Web.Areas.System.Controllers { [Area("System")] [Route("api/mqtt")] [ApiController] public class MqttRuntimeController : BaseController { private readonly IUnitWork _unitwork; private readonly MqttRuntimeState _runtimeState; private readonly MqttRuntimeStateRefresher _runtimeStateRefresher; public MqttRuntimeController( IUnitWork unitwork, MqttRuntimeState runtimeState, MqttRuntimeStateRefresher runtimeStateRefresher) { _unitwork = unitwork; _runtimeState = runtimeState; _runtimeStateRefresher = runtimeStateRefresher; } [HttpGet("status")] public IActionResult Status() { var enabledSubscriptionCount = _unitwork.GetCount(x => x.IsEnabled); return Ok(new { enabled = _runtimeState.Enabled, enabledSubscriptionCount, lastChangedAt = _runtimeState.LastChangedAt, lastChangedBy = _runtimeState.LastChangedBy }); } [HttpPost("refresh")] public async Task Refresh() { await _runtimeStateRefresher.RefreshFromSubscriptionsAsync(Wtm?.LoginUserInfo?.ITCode, HttpContext.RequestAborted); return Status(); } [HttpPost("subscriptions/{id:int}/enabled")] public async Task SetSubscriptionEnabled(int id, [FromQuery] bool enabled) { var subscription = _unitwork.Find(x => x.ID == id).FirstOrDefault(); if (subscription == null) { return NotFound(); } subscription.IsEnabled = enabled; _unitwork.Update(subscription); await _runtimeStateRefresher.RefreshFromSubscriptionsAsync(Wtm?.LoginUserInfo?.ITCode, HttpContext.RequestAborted); return Status(); } } }