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;
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";
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, 100, 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("pushData/alarmData/{deviceNo}")]
public async Task> AlarmData([FromRoute] string deviceNo, [FromBody] AlarmDataModel alarmDataModel)
{
var devIds = await _vmcCameraService.GetCameraByTDH(alarmDataModel.deviceNo);
devIds.ForEach(async x =>
{
List messageFiles = new List();
messageFiles.Add(new MessageFile
{
Text = alarmDataModel.panoramaDataId,
Type = "FILE_TYP_006"
});
messageFiles.Add(new MessageFile
{
Text = alarmDataModel.snapshotDataId,
Type = "FILE_TYP_006"
});
messageFiles.Add(new MessageFile
{
Text = alarmDataModel.relatedSnapshotDataId,
Type = "FILE_TYP_006"
});
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);
});
return new ApiResult