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;
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;
public AIController(IHttpClientFactory httpClientFactory, ITsysMessageService tsysMessageService, IPushMsgService pushMsgService)
{
aIHelper = new AIProjectHelper(httpClientFactory);
_TsysMessageService = tsysMessageService;
_pushMsgService = pushMsgService;
}
///
/// 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("", 7, 10, 33379713109829);
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";
TpushMsgModel model = new TpushMsgModel
{
C_DevStoreCode = "d705ceb5-7473-4b19-91dd-d3eff223f05b",
C_MsgTypeCode = "MSG_TYPE_024",
Subject = item.Device_TypeName + "报警",
Msg = item.Event_Name??item.Event_Code,
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)
{
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);
}
}
}