123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- 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;
- }
- /// <summary>
- /// AI登录
- /// </summary>
- /// <returns></returns>
- [HttpGet("AiLogin")]
- [AllowAnonymous]
- public async Task<AILoginOutput> AiLogin()
- {
- var token = await aIHelper.GetToken();
- return token;
- }
- /// <summary>
- /// AI-获取历史报警记录
- /// </summary>
- /// <returns></returns>
- [HttpGet("AiHistoricAlmrecord/{Devno}/{Page}/{pageSize}")]
- [AllowAnonymous]
- public async Task<ApiResult<AISqlSugarPagedList<AlmRecordOutput>>> AiHistoricAlmrecord(string Devno, int Page = 1, int pageSize = 10, long? entityId = null)
- {
- var data = await aIHelper.GetHistoricAlmrecord(Devno, Page, pageSize, entityId);
- return new ApiResult<AISqlSugarPagedList<AlmRecordOutput>>(data, ReturnCode.Success);
- }
- /// <summary>
- /// AI-获取报警处理列表
- /// </summary>
- /// <returns></returns>
- [HttpGet("AiAlmDispose/{Devno}/{Page}/{pageSize}")]
- [AllowAnonymous]
- public async Task<ApiResult<AISqlSugarPagedList<AlmRecordOutput>>> AiAlmDispose(string Devno, int Page = 1, int pageSize = 10, long? entityId = null)
- {
- var data = await aIHelper.GetAlmDispose(Devno, Page, pageSize, entityId);
- return new ApiResult<AISqlSugarPagedList<AlmRecordOutput>>(data, ReturnCode.Success);
- }
- /// <summary>
- /// AI-报警数据保存到环保消息表数据
- /// </summary>
- /// <returns></returns>
- [HttpGet("AiAlmSaveMessage")]
- [AllowAnonymous]
- public async Task<ApiResult> 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> { 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<ApiResultModel<object>> AlarmData([FromRoute] string deviceNo, [FromBody] AlarmDataModel alarmDataModel)
- {
- var devIds = await _vmcCameraService.GetCameraByTDH(alarmDataModel.deviceNo);
- int row = 0;
- foreach (var x in devIds)
- {
- List<MessageFile> messageFiles = new List<MessageFile>();
- 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<object>.GetSuccess(row);
- //return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
- }
- /// <summary>
- /// 注册
- /// </summary>
- /// <param name="deviceNo">设备编号</param>
- /// <param name="registerModel"></param>
- /// <returns></returns>
- [HttpPost]
- [AllowAnonymous]
- [Route("/api/base/deviceServerSdk/register/{deviceNo}")]
- public async Task<ApiResultModel<object>> Register([FromRoute] string deviceNo, [FromBody] RegisterModel registerModel)
- {
- return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
- }
- /// <summary>
- /// 状态变化
- /// </summary>
- /// <param name="deviceNo">设备编号</param>
- /// <param name="statusChangeModel"></param>
- /// <returns></returns>
- [HttpPost]
- [AllowAnonymous]
- [Route("/api/base/deviceServerSdk/statusChange/{deviceNo}")]
- public async Task<ApiResultModel<object>> StatusChange([FromRoute] string deviceNo, [FromBody] StatusChangeModel statusChangeModel)
- {
- return await Task.FromResult(ApiResultModel<object>.GetSuccess(new object { }));
- }
- /// <summary>
- /// 心跳
- /// </summary>
- /// <param name="heartbeatModel"></param>
- /// <returns></returns>
- [HttpPost]
- [AllowAnonymous]
- [Route("/api/base/deviceServerSdk/heartbeat")]
- public async Task<ApiResultModel<object>> Heartbeat([FromBody] HeartbeatModel heartbeatModel)
- {
- return await Task.FromResult(ApiResultModel<object>.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; }
- }
-
- /// <summary>
- /// api结果模型
- /// </summary>
- public class ApiResultModel
- {
- /// <summary>
- /// api状态枚举
- /// </summary>
- public int Code { get; set; }
- /// <summary>
- /// 成功、错误消息
- /// </summary>
- public string? Msg { get; set; }
- }
- /// <summary>
- /// api结果模型,带着数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class ApiResultModel<T> : ApiResultModel where T : new()
- {
- /// <summary>
- /// 返回数据
- /// </summary>
- public T? Data { get; set; }
- /// <summary>
- /// 获取成功返回对象
- /// </summary>
- /// <param name="data">返回数据</param>
- /// <returns></returns>
- public static ApiResultModel<T> GetSuccess(T data)
- {
- ApiResultModel<T> apiResult = new ApiResultModel<T>();
- apiResult.Data = data;
- apiResult.Code = 0;
- apiResult.Msg = "成功";
- return apiResult;
- }
- }
- }
|