using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using System.Xml.Linq; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseController; using WIDESEAWCS_DTO.TaskInfo; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Service; namespace WIDESEAWCS_Server.Controllers.RgvOper { [Route("api/Rgvoperainform")] [ApiController] public class RgvoperainformController : ApiBaseController { private readonly IHttpContextAccessor _httpContextAccessor; public RgvoperainformController(IRgvoperainformService service, IHttpContextAccessor httpContextAccessor) : base(service) { _httpContextAccessor = httpContextAccessor; } /// /// 获取设备状态 /// /// [HttpPost, Route("GetDeviceStatusDto"), AllowAnonymous] public WebResponseContent GetDeviceStatusDto([FromBody] DeviceStatusRequest request) { return Service.GetDeviceStatusDto(request.Off, request.MonitorType); } // 添加请求模型类 public class DeviceStatusRequest { public int Off { get; set; } public string MonitorType { get; set; } } /// /// 设备操作 /// /// /// [HttpPost, Route("DeviceOperation"), AllowAnonymous] public WebResponseContent DeviceOperation() { try { // 读取原始JSON var json = new StreamReader(HttpContext.Request.Body).ReadToEndAsync().Result; // 解析为动态对象 var requestData = Newtonsoft.Json.JsonConvert.DeserializeObject(json); if (requestData == null) { return new WebResponseContent { Status = false, Message = "参数不能为空" }; } // 获取参数数组 var delKeys = requestData.DelKeys?.ToObject(); bool extra = requestData.Extra ?? false; // 检查参数数量(2-3个参数) if (delKeys == null || delKeys.Length < 2 || delKeys.Length > 3) { return new WebResponseContent { Status = false, Message = "参数数量错误,需要2-3个参数" }; } // 转换设备名称为首字母大写 string deviceName = delKeys[0]; if (!string.IsNullOrEmpty(deviceName)) { // 方法2:使用三元运算符 deviceName = deviceName.Length >= 3 ? deviceName.Substring(0, 3).ToUpper() + deviceName.Substring(3) : deviceName.ToUpper(); } string operationType = delKeys[1]; // 处理第三个参数(可选) int parameter = 101012; // 默认值(移除了前导0,因为0101012是八进制) if (delKeys.Length == 3) { // 安全地解析第三个参数 if (int.TryParse(delKeys[2].ToString(), out int parsedParam)) { parameter = parsedParam; } else { return new WebResponseContent { Status = false, Message = "第三个参数必须是有效的整数" }; } } return Service.DeviceOperation(deviceName, operationType, parameter); } catch (Exception ex) { return new WebResponseContent { Status = false, Message = ex.Message }; } } /// /// RGV一键操作 /// /// [HttpPost, Route("OneClickOperation"), AllowAnonymous] public WebResponseContent OneClickOperation([FromBody] OneClickOperationRequest request) { return Service.OneClickOperation(request.monitorType, request.operationType); } //添加一键操作请求模型类 public class OneClickOperationRequest { public string monitorType { get; set; } public string operationType { get; set; } } } }