AIController.cs 6.8 KB

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