AIController.cs 12 KB

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