AIController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using Ropin.Core.Extensions.Redis;
  5. using Ropin.Core.Extensions;
  6. using Ropin.Inspection.Api.Common.Options;
  7. using Ropin.Inspection.Api.Common.Token;
  8. using Ropin.Inspection.Service.Interface;
  9. using Ropin.Inspection.Service.SYS.Interface;
  10. using Ropin.Inspection.Service;
  11. using System.Net.Http;
  12. using Ropin.Inspection.Common.Helper;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Ropin.Inspection.Api.Common;
  15. using System.Threading.Tasks;
  16. using Ropin.Inspection.Model;
  17. using System.Collections.Generic;
  18. using System;
  19. using Newtonsoft.Json;
  20. using Microsoft.AspNetCore.Identity;
  21. using Microsoft.Extensions.Configuration;
  22. using Ropin.Inspection.Service.VMC.Interface;
  23. using NPOI.SS.Formula.Functions;
  24. using MySqlX.XDevAPI.Relational;
  25. using Newtonsoft.Json.Linq;
  26. using System.Xml;
  27. namespace Ropin.Inspection.Api.Controllers.Base
  28. {
  29. public class AIController : BaseController
  30. {
  31. private readonly IHttpClientFactory _httpClientFactory;
  32. private readonly AIProjectHelper aIHelper;
  33. private readonly ITsysMessageService _TsysMessageService;
  34. private readonly IPushMsgService _pushMsgService;
  35. private readonly IVmcCameraService _vmcCameraService;
  36. private string IsUpdateAI = "false";
  37. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(AIController));
  38. public AIController(IHttpClientFactory httpClientFactory, ITsysMessageService tsysMessageService, IPushMsgService pushMsgService, IConfiguration configuration, IVmcCameraService vmcCameraService)
  39. {
  40. aIHelper = new AIProjectHelper(httpClientFactory);
  41. _TsysMessageService = tsysMessageService;
  42. _pushMsgService = pushMsgService;
  43. string IsUpdate = configuration.GetSection("AIIsUpdate")?.Value;
  44. IsUpdateAI = !string.IsNullOrEmpty(IsUpdate) ? IsUpdate : "false";
  45. _vmcCameraService = vmcCameraService;
  46. }
  47. /// <summary>
  48. /// AI登录
  49. /// </summary>
  50. /// <returns></returns>
  51. [HttpGet("AiLogin")]
  52. [AllowAnonymous]
  53. public async Task<AILoginOutput> AiLogin()
  54. {
  55. var token = await aIHelper.GetToken();
  56. return token;
  57. }
  58. /// <summary>
  59. /// AI-获取历史报警记录
  60. /// </summary>
  61. /// <returns></returns>
  62. [HttpGet("AiHistoricAlmrecord/{Devno}/{Page}/{pageSize}")]
  63. [AllowAnonymous]
  64. public async Task<ApiResult<AISqlSugarPagedList<AlmRecordOutput>>> AiHistoricAlmrecord(string Devno, int Page = 1, int pageSize = 10, long? entityId = null)
  65. {
  66. var data = await aIHelper.GetHistoricAlmrecord(Devno, Page, pageSize, entityId);
  67. return new ApiResult<AISqlSugarPagedList<AlmRecordOutput>>(data, ReturnCode.Success);
  68. }
  69. /// <summary>
  70. /// AI-获取报警处理列表
  71. /// </summary>
  72. /// <returns></returns>
  73. [HttpGet("AiAlmDispose/{Devno}/{Page}/{pageSize}")]
  74. [AllowAnonymous]
  75. public async Task<ApiResult<AISqlSugarPagedList<AlmRecordOutput>>> AiAlmDispose(string Devno, int Page = 1, int pageSize = 10, long? entityId = null)
  76. {
  77. var data = await aIHelper.GetAlmDispose(Devno, Page, pageSize, entityId);
  78. return new ApiResult<AISqlSugarPagedList<AlmRecordOutput>>(data, ReturnCode.Success);
  79. }
  80. /// <summary>
  81. /// AI-报警数据保存到环保消息表数据
  82. /// </summary>
  83. /// <returns></returns>
  84. [HttpGet("AiAlmSaveMessage")]
  85. [AllowAnonymous]
  86. public async Task<ApiResult> AiAlmSaveMessage()
  87. {
  88. //40124884581189;33379713109829
  89. var data = await aIHelper.GetAlmDispose("", 1, 10, 40124884581189);
  90. //var data = await aIHelper.GetHistoricAlmrecord("", 1, 10, 40124884581189);
  91. if (data != null)
  92. {
  93. foreach (var item in data.Items)
  94. {
  95. if (item != null)
  96. {
  97. var textJson = new
  98. {
  99. PanoramaId = item.PanoramaId,
  100. PanoramaUrl = item.PanoramaUrl,
  101. RoiJson = item.RoiJson,
  102. NonRoiJson = item.NonRoiJson,
  103. DetectionFrameJson = item.DetectionFrameJson
  104. };
  105. var datas = JsonConvert.SerializeObject(textJson);
  106. MessageFile messageFile = new MessageFile();
  107. messageFile.Text = datas;
  108. messageFile.Type = "FILE_TYP_006";
  109. string msg = item.Event_Name;
  110. if (string.IsNullOrEmpty(msg))
  111. {
  112. msg = item.Event_Code;
  113. }
  114. TpushMsgModel model = new TpushMsgModel
  115. {
  116. C_DevStoreCode = "d705ceb5-7473-4b19-91dd-d3eff223f05b",
  117. C_MsgTypeCode = "MSG_TYPE_024",
  118. Subject = item.Device_TypeName + "报警",
  119. Msg = msg,
  120. UserName = "AI盒子",
  121. UserMobile = "",
  122. CreateOn = item.StartTime?.ToString("yyyy-MM-dd HH:mm:ss"),
  123. GenerationType = 1,
  124. msgStatus = 1,
  125. FileList = new List<MessageFile> { messageFile }
  126. };
  127. bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject);
  128. if (bol && IsUpdateAI == "true")
  129. {
  130. DateTime updTime = DateTime.Now;
  131. var updData = new
  132. {
  133. Id = item.Id,
  134. ConfrimContent = "环保测试数据,自动确认",
  135. Mistake = false,
  136. Confirmed = true,
  137. ConfirmTime = updTime,
  138. ConfirmUserName = "环保同步程序",
  139. Closed = true,
  140. CloseContent = "环保测试数据,自动关闭",
  141. EndTime = updTime,
  142. CloseUserName = "环保同步程序"
  143. };
  144. var dataStr = JsonConvert.SerializeObject(updData);
  145. bool result = await aIHelper.UpdateAlm(dataStr);
  146. //bool result = await aIHelper.UpdateAlmIsDelete(item.Id);
  147. }
  148. }
  149. }
  150. }
  151. //TsysMessageSearchModel searchModel = new TsysMessageSearchModel();
  152. //searchModel.IsPagination = true;
  153. //searchModel.C_MsgTypeCode = "MSG_TYPE_024";
  154. //var msgData = await _TsysMessageService.GetConditionAsync(searchModel);
  155. return new ApiResult(ReturnCode.Success);
  156. }
  157. [HttpPost]
  158. [AllowAnonymous]
  159. [Route("/device/business/generalApp/pushData/alarmData/{deviceNo}")]
  160. public async Task<ApiResultModel<object>> AlarmData([FromRoute] string deviceNo, [FromBody] JObject alarmDataModel)
  161. {
  162. Console.WriteLine("进入/device/business/generalApp/pushData/alarmData/JObject" + deviceNo);
  163. log.Info($"进入AI[/device/business/generalApp/pushData/alarmData/{deviceNo}],参数:【{JsonConvert.SerializeObject(alarmDataModel)}】");
  164. AlarmDataModel alarmData= JsonConvert.DeserializeObject<AlarmDataModel>(alarmDataModel.ToString());
  165. var devIds = await _vmcCameraService.GetCameraByTDH(alarmData.deviceNo);
  166. log.Info($"获取到的设备ID:【{JsonConvert.SerializeObject(devIds)}】");
  167. int row = 0;
  168. foreach (var x in devIds)
  169. {
  170. List<MessageFile> messageFiles = new List<MessageFile>();
  171. var textJson = new
  172. {
  173. PanoramaId = alarmData.panoramaId,
  174. PanoramaUrl = alarmData.panoramaUrl,
  175. RoiJson = alarmData.roiJson,
  176. NonRoiJson = alarmData.nonRoiJson,
  177. DetectionFrameJson = alarmData.detectionFrameJson
  178. };
  179. var datas = JsonConvert.SerializeObject(textJson);
  180. MessageFile messageFile = new MessageFile();
  181. messageFile.Text = datas;
  182. messageFile.Type = "FILE_TYP_006";
  183. messageFiles.Add(messageFile);
  184. TpushMsgModel model = new TpushMsgModel
  185. {
  186. C_DevStoreCode = x,
  187. C_MsgTypeCode = "MSG_TYPE_024",
  188. Subject = "AI盒子报警",
  189. Msg = alarmData.alarmType,
  190. UserName = "AI盒子",
  191. UserMobile = "",
  192. CreateOn = alarmData.captureTime,
  193. GenerationType = 1,
  194. msgStatus = 1,
  195. FileList = messageFiles
  196. };
  197. bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject);
  198. log.Info($"执行发送结果=[{bol}]");
  199. if (bol) { row++; }
  200. }
  201. return ApiResultModel<object>.GetSuccess(row);
  202. //return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
  203. }
  204. //[HttpPost]
  205. //[AllowAnonymous]
  206. //[Route("/device/business/generalApp/pushData/alarmData/{deviceNo}")]
  207. //public async Task<ApiResultModel<object>> AlarmData([FromRoute] string deviceNo, AlarmDataModel alarmDataModel)
  208. //{
  209. // Console.WriteLine("进入/device/business/generalApp/pushData/alarmData/" + deviceNo);
  210. // var devIds = await _vmcCameraService.GetCameraByTDH(alarmDataModel.deviceNo);
  211. // int row = 0;
  212. // foreach (var x in devIds)
  213. // {
  214. // List<MessageFile> messageFiles = new List<MessageFile>();
  215. // var textJson = new
  216. // {
  217. // PanoramaId = alarmDataModel.panoramaId,
  218. // PanoramaUrl = alarmDataModel.panoramaUrl,
  219. // RoiJson = alarmDataModel.roiJson,
  220. // NonRoiJson = alarmDataModel.nonRoiJson,
  221. // DetectionFrameJson = alarmDataModel.detectionFrameJson
  222. // };
  223. // var datas = JsonConvert.SerializeObject(textJson);
  224. // MessageFile messageFile = new MessageFile();
  225. // messageFile.Text = datas;
  226. // messageFile.Type = "FILE_TYP_006";
  227. // messageFiles.Add(messageFile);
  228. // TpushMsgModel model = new TpushMsgModel
  229. // {
  230. // C_DevStoreCode = x,
  231. // C_MsgTypeCode = "MSG_TYPE_024",
  232. // Subject = "AI盒子报警",
  233. // Msg = alarmDataModel.alarmType,
  234. // UserName = "AI盒子",
  235. // UserMobile = "",
  236. // CreateOn = alarmDataModel.captureTime,
  237. // GenerationType = 1,
  238. // msgStatus = 1,
  239. // FileList = messageFiles
  240. // };
  241. // bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject);
  242. // log.Info($"执行发送结果=[{bol}]");
  243. // if (bol) { row++; }
  244. // }
  245. // return ApiResultModel<object>.GetSuccess(row);
  246. // //return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
  247. //}
  248. /// <summary>
  249. /// 注册
  250. /// </summary>
  251. /// <param name="deviceNo">设备编号</param>
  252. /// <param name="registerModel"></param>
  253. /// <returns></returns>
  254. [HttpPost]
  255. [AllowAnonymous]
  256. [Route("/api/base/deviceServerSdk/register/{deviceNo}")]
  257. public async Task<ApiResultModel<object>> Register([FromRoute] string deviceNo, [FromBody] RegisterModel registerModel)
  258. {
  259. return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
  260. }
  261. /// <summary>
  262. /// 状态变化
  263. /// </summary>
  264. /// <param name="deviceNo">设备编号</param>
  265. /// <param name="statusChangeModel"></param>
  266. /// <returns></returns>
  267. [HttpPost]
  268. [AllowAnonymous]
  269. [Route("/api/base/deviceServerSdk/statusChange/{deviceNo}")]
  270. public async Task<ApiResultModel<object>> StatusChange([FromRoute] string deviceNo, [FromBody] StatusChangeModel statusChangeModel)
  271. {
  272. return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
  273. }
  274. /// <summary>
  275. /// 心跳
  276. /// </summary>
  277. /// <param name="heartbeatModel"></param>
  278. /// <returns></returns>
  279. [HttpPost]
  280. [AllowAnonymous]
  281. [Route("/api/base/deviceServerSdk/heartbeat")]
  282. public async Task<ApiResultModel<object>> Heartbeat([FromBody] HeartbeatModel heartbeatModel)
  283. {
  284. return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
  285. }
  286. }
  287. public class RegisterModel
  288. {
  289. public string username { get; set; }
  290. public string password { get; set; }
  291. }
  292. public class StatusChangeModel
  293. {
  294. public int status { get; set; }
  295. }
  296. public class HeartbeatModel
  297. {
  298. public DateTime startTime { get; set; }
  299. }
  300. /// <summary>
  301. /// api结果模型
  302. /// </summary>
  303. public class ApiResultModel
  304. {
  305. /// <summary>
  306. /// api状态枚举
  307. /// </summary>
  308. public int Code { get; set; }
  309. /// <summary>
  310. /// 成功、错误消息
  311. /// </summary>
  312. public string? Msg { get; set; }
  313. }
  314. /// <summary>
  315. /// api结果模型,带着数据
  316. /// </summary>
  317. /// <typeparam name="T"></typeparam>
  318. public class ApiResultModel<T> : ApiResultModel where T : new()
  319. {
  320. /// <summary>
  321. /// 返回数据
  322. /// </summary>
  323. public T? Data { get; set; }
  324. /// <summary>
  325. /// 获取成功返回对象
  326. /// </summary>
  327. /// <param name="data">返回数据</param>
  328. /// <returns></returns>
  329. public static ApiResultModel<T> GetSuccess(T data)
  330. {
  331. ApiResultModel<T> apiResult = new ApiResultModel<T>();
  332. apiResult.Data = data;
  333. apiResult.Code = 0;
  334. apiResult.Msg = "成功";
  335. return apiResult;
  336. }
  337. }
  338. }