AIController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. namespace Ropin.Inspection.Api.Controllers.Base
  24. {
  25. public class AIController : BaseController
  26. {
  27. private readonly IHttpClientFactory _httpClientFactory;
  28. private readonly AIProjectHelper aIHelper;
  29. private readonly ITsysMessageService _TsysMessageService;
  30. private readonly IPushMsgService _pushMsgService;
  31. private readonly IVmcCameraService _vmcCameraService;
  32. private string IsUpdateAI = "false";
  33. public AIController(IHttpClientFactory httpClientFactory, ITsysMessageService tsysMessageService, IPushMsgService pushMsgService, IConfiguration configuration, IVmcCameraService vmcCameraService)
  34. {
  35. aIHelper = new AIProjectHelper(httpClientFactory);
  36. _TsysMessageService = tsysMessageService;
  37. _pushMsgService = pushMsgService;
  38. string IsUpdate = configuration.GetSection("AIIsUpdate")?.Value;
  39. IsUpdateAI = !string.IsNullOrEmpty(IsUpdate) ? IsUpdate : "false";
  40. _vmcCameraService = vmcCameraService;
  41. }
  42. /// <summary>
  43. /// AI登录
  44. /// </summary>
  45. /// <returns></returns>
  46. [HttpGet("AiLogin")]
  47. [AllowAnonymous]
  48. public async Task<AILoginOutput> AiLogin()
  49. {
  50. var token = await aIHelper.GetToken();
  51. return token;
  52. }
  53. /// <summary>
  54. /// AI-获取历史报警记录
  55. /// </summary>
  56. /// <returns></returns>
  57. [HttpGet("AiHistoricAlmrecord/{Devno}/{Page}/{pageSize}")]
  58. [AllowAnonymous]
  59. public async Task<ApiResult<AISqlSugarPagedList<AlmRecordOutput>>> AiHistoricAlmrecord(string Devno, int Page = 1, int pageSize = 10, long? entityId = null)
  60. {
  61. var data = await aIHelper.GetHistoricAlmrecord(Devno, Page, pageSize, entityId);
  62. return new ApiResult<AISqlSugarPagedList<AlmRecordOutput>>(data, ReturnCode.Success);
  63. }
  64. /// <summary>
  65. /// AI-获取报警处理列表
  66. /// </summary>
  67. /// <returns></returns>
  68. [HttpGet("AiAlmDispose/{Devno}/{Page}/{pageSize}")]
  69. [AllowAnonymous]
  70. public async Task<ApiResult<AISqlSugarPagedList<AlmRecordOutput>>> AiAlmDispose(string Devno, int Page = 1, int pageSize = 10, long? entityId = null)
  71. {
  72. var data = await aIHelper.GetAlmDispose(Devno, Page, pageSize, entityId);
  73. return new ApiResult<AISqlSugarPagedList<AlmRecordOutput>>(data, ReturnCode.Success);
  74. }
  75. /// <summary>
  76. /// AI-报警数据保存到环保消息表数据
  77. /// </summary>
  78. /// <returns></returns>
  79. [HttpGet("AiAlmSaveMessage")]
  80. [AllowAnonymous]
  81. public async Task<ApiResult> AiAlmSaveMessage()
  82. {
  83. //40124884581189;33379713109829
  84. var data = await aIHelper.GetAlmDispose("", 1, 100, 40124884581189);
  85. //var data = await aIHelper.GetHistoricAlmrecord("", 1, 10, 40124884581189);
  86. if (data != null)
  87. {
  88. foreach (var item in data.Items)
  89. {
  90. if (item != null)
  91. {
  92. var textJson = new
  93. {
  94. PanoramaId = item.PanoramaId,
  95. PanoramaUrl = item.PanoramaUrl,
  96. RoiJson = item.RoiJson,
  97. NonRoiJson = item.NonRoiJson,
  98. DetectionFrameJson = item.DetectionFrameJson
  99. };
  100. var datas = JsonConvert.SerializeObject(textJson);
  101. MessageFile messageFile = new MessageFile();
  102. messageFile.Text = datas;
  103. messageFile.Type = "FILE_TYP_006";
  104. string msg = item.Event_Name;
  105. if (string.IsNullOrEmpty(msg))
  106. {
  107. msg = item.Event_Code;
  108. }
  109. TpushMsgModel model = new TpushMsgModel
  110. {
  111. C_DevStoreCode = "d705ceb5-7473-4b19-91dd-d3eff223f05b",
  112. C_MsgTypeCode = "MSG_TYPE_024",
  113. Subject = item.Device_TypeName + "报警",
  114. Msg = msg,
  115. UserName = "AI盒子",
  116. UserMobile = "",
  117. CreateOn = item.StartTime?.ToString("yyyy-MM-dd HH:mm:ss"),
  118. GenerationType = 1,
  119. msgStatus = 1,
  120. FileList = new List<MessageFile> { messageFile }
  121. };
  122. bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject);
  123. if (bol && IsUpdateAI == "true")
  124. {
  125. DateTime updTime = DateTime.Now;
  126. var updData = new
  127. {
  128. Id = item.Id,
  129. ConfrimContent = "环保测试数据,自动确认",
  130. Mistake = false,
  131. Confirmed = true,
  132. ConfirmTime = updTime,
  133. ConfirmUserName = "环保同步程序",
  134. Closed = true,
  135. CloseContent = "环保测试数据,自动关闭",
  136. EndTime = updTime,
  137. CloseUserName = "环保同步程序"
  138. };
  139. var dataStr = JsonConvert.SerializeObject(updData);
  140. bool result = await aIHelper.UpdateAlm(dataStr);
  141. //bool result = await aIHelper.UpdateAlmIsDelete(item.Id);
  142. }
  143. }
  144. }
  145. }
  146. //TsysMessageSearchModel searchModel = new TsysMessageSearchModel();
  147. //searchModel.IsPagination = true;
  148. //searchModel.C_MsgTypeCode = "MSG_TYPE_024";
  149. //var msgData = await _TsysMessageService.GetConditionAsync(searchModel);
  150. return new ApiResult(ReturnCode.Success);
  151. }
  152. [HttpPost]
  153. [AllowAnonymous]
  154. [Route("pushData/alarmData/{deviceNo}")]
  155. public async Task<ApiResult<object>> AlarmData([FromRoute] string deviceNo, [FromBody] AlarmDataModel alarmDataModel)
  156. {
  157. var devIds = await _vmcCameraService.GetCameraByTDH(alarmDataModel.deviceNo);
  158. devIds.ForEach(async x =>
  159. {
  160. List<MessageFile> messageFiles = new List<MessageFile>();
  161. messageFiles.Add(new MessageFile
  162. {
  163. Text = alarmDataModel.panoramaDataId,
  164. Type = "FILE_TYP_006"
  165. });
  166. messageFiles.Add(new MessageFile
  167. {
  168. Text = alarmDataModel.snapshotDataId,
  169. Type = "FILE_TYP_006"
  170. });
  171. messageFiles.Add(new MessageFile
  172. {
  173. Text = alarmDataModel.relatedSnapshotDataId,
  174. Type = "FILE_TYP_006"
  175. });
  176. TpushMsgModel model = new TpushMsgModel
  177. {
  178. C_DevStoreCode = x,
  179. C_MsgTypeCode = "MSG_TYPE_024",
  180. Subject = "AI盒子报警",
  181. Msg = alarmDataModel.alarmType,
  182. UserName = "AI盒子",
  183. UserMobile = "",
  184. CreateOn = alarmDataModel.captureTime,
  185. GenerationType = 1,
  186. msgStatus = 1,
  187. FileList = messageFiles
  188. };
  189. bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject);
  190. });
  191. return new ApiResult<object>(new object { });
  192. }
  193. }
  194. }