AIController.cs 13 KB

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