using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Ropin.Core.Extensions.Redis; using Ropin.Core.Extensions; using Ropin.Inspection.Api.Common.Options; using Ropin.Inspection.Api.Common.Token; using Ropin.Inspection.Service.Interface; using Ropin.Inspection.Service.SYS.Interface; using Ropin.Inspection.Service; using System.Net.Http; using Ropin.Inspection.Common.Helper; using Microsoft.AspNetCore.Authorization; using Ropin.Inspection.Api.Common; using System.Threading.Tasks; using Ropin.Inspection.Model; using System.Collections.Generic; using System; using Newtonsoft.Json; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; using Ropin.Inspection.Service.VMC.Interface; using NPOI.SS.Formula.Functions; using MySqlX.XDevAPI.Relational; namespace Ropin.Inspection.Api.Controllers.Base { public class AIController : BaseController { private readonly IHttpClientFactory _httpClientFactory; private readonly AIProjectHelper aIHelper; private readonly ITsysMessageService _TsysMessageService; private readonly IPushMsgService _pushMsgService; private readonly IVmcCameraService _vmcCameraService; private string IsUpdateAI = "false"; private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(AIController)); public AIController(IHttpClientFactory httpClientFactory, ITsysMessageService tsysMessageService, IPushMsgService pushMsgService, IConfiguration configuration, IVmcCameraService vmcCameraService) { aIHelper = new AIProjectHelper(httpClientFactory); _TsysMessageService = tsysMessageService; _pushMsgService = pushMsgService; string IsUpdate = configuration.GetSection("AIIsUpdate")?.Value; IsUpdateAI = !string.IsNullOrEmpty(IsUpdate) ? IsUpdate : "false"; _vmcCameraService = vmcCameraService; } /// /// AI登录 /// /// [HttpGet("AiLogin")] [AllowAnonymous] public async Task AiLogin() { var token = await aIHelper.GetToken(); return token; } /// /// AI-获取历史报警记录 /// /// [HttpGet("AiHistoricAlmrecord/{Devno}/{Page}/{pageSize}")] [AllowAnonymous] public async Task>> AiHistoricAlmrecord(string Devno, int Page = 1, int pageSize = 10, long? entityId = null) { var data = await aIHelper.GetHistoricAlmrecord(Devno, Page, pageSize, entityId); return new ApiResult>(data, ReturnCode.Success); } /// /// AI-获取报警处理列表 /// /// [HttpGet("AiAlmDispose/{Devno}/{Page}/{pageSize}")] [AllowAnonymous] public async Task>> AiAlmDispose(string Devno, int Page = 1, int pageSize = 10, long? entityId = null) { var data = await aIHelper.GetAlmDispose(Devno, Page, pageSize, entityId); return new ApiResult>(data, ReturnCode.Success); } /// /// AI-报警数据保存到环保消息表数据 /// /// [HttpGet("AiAlmSaveMessage")] [AllowAnonymous] public async Task AiAlmSaveMessage() { //40124884581189;33379713109829 var data = await aIHelper.GetAlmDispose("", 1, 10, 40124884581189); //var data = await aIHelper.GetHistoricAlmrecord("", 1, 10, 40124884581189); if (data != null) { foreach (var item in data.Items) { if (item != null) { var textJson = new { PanoramaId = item.PanoramaId, PanoramaUrl = item.PanoramaUrl, RoiJson = item.RoiJson, NonRoiJson = item.NonRoiJson, DetectionFrameJson = item.DetectionFrameJson }; var datas = JsonConvert.SerializeObject(textJson); MessageFile messageFile = new MessageFile(); messageFile.Text = datas; messageFile.Type = "FILE_TYP_006"; string msg = item.Event_Name; if (string.IsNullOrEmpty(msg)) { msg = item.Event_Code; } TpushMsgModel model = new TpushMsgModel { C_DevStoreCode = "d705ceb5-7473-4b19-91dd-d3eff223f05b", C_MsgTypeCode = "MSG_TYPE_024", Subject = item.Device_TypeName + "报警", Msg = msg, UserName = "AI盒子", UserMobile = "", CreateOn = item.StartTime?.ToString("yyyy-MM-dd HH:mm:ss"), GenerationType = 1, msgStatus = 1, FileList = new List { messageFile } }; bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject); if (bol && IsUpdateAI == "true") { DateTime updTime = DateTime.Now; var updData = new { Id = item.Id, ConfrimContent = "环保测试数据,自动确认", Mistake = false, Confirmed = true, ConfirmTime = updTime, ConfirmUserName = "环保同步程序", Closed = true, CloseContent = "环保测试数据,自动关闭", EndTime = updTime, CloseUserName = "环保同步程序" }; var dataStr = JsonConvert.SerializeObject(updData); bool result = await aIHelper.UpdateAlm(dataStr); //bool result = await aIHelper.UpdateAlmIsDelete(item.Id); } } } } //TsysMessageSearchModel searchModel = new TsysMessageSearchModel(); //searchModel.IsPagination = true; //searchModel.C_MsgTypeCode = "MSG_TYPE_024"; //var msgData = await _TsysMessageService.GetConditionAsync(searchModel); return new ApiResult(ReturnCode.Success); } [HttpPost] [AllowAnonymous] [Route("/device/business/generalApp/pushData/alarmData/{deviceNo}")] public async Task> AlarmData([FromRoute] string deviceNo, [FromBody] AlarmDataModel alarmDataModel) { var devIds = await _vmcCameraService.GetCameraByTDH(alarmDataModel.deviceNo); int row = 0; foreach (var x in devIds) { List messageFiles = new List(); var textJson = new { PanoramaId = alarmDataModel.panoramaId, PanoramaUrl = alarmDataModel.panoramaUrl, RoiJson = alarmDataModel.roiJson, NonRoiJson = alarmDataModel.nonRoiJson, DetectionFrameJson = alarmDataModel.detectionFrameJson }; var datas = JsonConvert.SerializeObject(textJson); MessageFile messageFile = new MessageFile(); messageFile.Text = datas; messageFile.Type = "FILE_TYP_006"; messageFiles.Add(messageFile); TpushMsgModel model = new TpushMsgModel { C_DevStoreCode = x, C_MsgTypeCode = "MSG_TYPE_024", Subject = "AI盒子报警", Msg = alarmDataModel.alarmType, UserName = "AI盒子", UserMobile = "", CreateOn = alarmDataModel.captureTime, GenerationType = 1, msgStatus = 1, FileList = messageFiles }; bool bol = await _pushMsgService.PushAlarmMsgAsync(model, model.Subject); log.Info($"执行发送结果=[{bol}]"); if (bol) { row++; } } return ApiResultModel.GetSuccess(row); //return await Task.FromResult(ApiResultModel.GetSuccess(new object { })); } /// /// 注册 /// /// 设备编号 /// /// [HttpPost] [AllowAnonymous] [Route("/api/base/deviceServerSdk/register/{deviceNo}")] public async Task> Register([FromRoute] string deviceNo, [FromBody] RegisterModel registerModel) { return await Task.FromResult(ApiResultModel.GetSuccess(new object { })); } /// /// 状态变化 /// /// 设备编号 /// /// [HttpPost] [AllowAnonymous] [Route("/api/base/deviceServerSdk/statusChange/{deviceNo}")] public async Task> StatusChange([FromRoute] string deviceNo, [FromBody] StatusChangeModel statusChangeModel) { return await Task.FromResult(ApiResultModel.GetSuccess(new object { })); } /// /// 心跳 /// /// /// [HttpPost] [AllowAnonymous] [Route("/api/base/deviceServerSdk/heartbeat")] public async Task> Heartbeat([FromBody] HeartbeatModel heartbeatModel) { return await Task.FromResult(ApiResultModel.GetSuccess(new object { })); } } public class RegisterModel { public string username { get; set; } public string password { get; set; } } public class StatusChangeModel { public int status { get; set; } } public class HeartbeatModel { public DateTime startTime { get; set; } } /// /// api结果模型 /// public class ApiResultModel { /// /// api状态枚举 /// public int Code { get; set; } /// /// 成功、错误消息 /// public string? Msg { get; set; } } /// /// api结果模型,带着数据 /// /// public class ApiResultModel : ApiResultModel where T : new() { /// /// 返回数据 /// public T? Data { get; set; } /// /// 获取成功返回对象 /// /// 返回数据 /// public static ApiResultModel GetSuccess(T data) { ApiResultModel apiResult = new ApiResultModel(); apiResult.Data = data; apiResult.Code = 0; apiResult.Msg = "成功"; return apiResult; } } }