HuBingJie
10 天以前 21078521d79f0eb5535cc34be7917a3fd1123b8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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<IRgvoperainformService, Dt_Task_hty>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        public RgvoperainformController(IRgvoperainformService service, IHttpContextAccessor httpContextAccessor) : base(service)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <returns></returns>
        [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; }
        }
 
        /// <summary>
        /// 设备操作
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        [HttpPost, Route("DeviceOperation"), AllowAnonymous]
        public WebResponseContent DeviceOperation()
        {
            try
            {
                // 读取原始JSON
                var json = new StreamReader(HttpContext.Request.Body).ReadToEndAsync().Result;
 
                // 解析为动态对象
                var requestData = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
 
                if (requestData == null)
                {
                    return new WebResponseContent { Status = false, Message = "参数不能为空" };
                }
 
                // 获取参数数组
                var delKeys = requestData.DelKeys?.ToObject<string[]>();
                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 };
            }
        }
 
        /// <summary>
        /// RGV一键操作
        /// </summary>
        /// <returns></returns>
        [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; }
        }
    }
}