298 lines
10 KiB
C#
298 lines
10 KiB
C#
using Infrastructure;
|
||
using Infrastructure.Cache;
|
||
using Infrastructure.CloudSdk.minio;
|
||
using Infrastructure.Helpers;
|
||
using Microsoft.DotNet.InternalAbstractions;
|
||
using Microsoft.Extensions.Logging;
|
||
using Newtonsoft.Json;
|
||
using OpenAuth.App.BaseApp.Base;
|
||
using OpenAuth.App.BasicQueryService;
|
||
using OpenAuth.App.Interface;
|
||
using OpenAuth.App.ServiceApp.Algo;
|
||
using OpenAuth.App.ServiceApp.Algo.Request;
|
||
using OpenAuth.Repository;
|
||
using OpenAuth.Repository.Domain;
|
||
using OpenAuth.WebApi;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace OpenAuth.App.ServiceApp
|
||
{
|
||
/// <summary>
|
||
/// 消息推送模块
|
||
/// </summary>
|
||
public class LasaPlatformPushApp : SqlSugarBaseApp<LasaPlatform, SugarDbContext>
|
||
{
|
||
private readonly ILogger<LasaPlatformPushApp> _logger;
|
||
public LasaPlatformPushApp(ILogger<LasaPlatformPushApp> logger,ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<LasaPlatform> repository, IAuth auth): base(unitWork, repository, auth)
|
||
{
|
||
_logger = logger;
|
||
}
|
||
/// <summary>
|
||
/// 获取推送平台
|
||
/// </summary>
|
||
/// <param name="page"></param>
|
||
/// <param name="limit"></param>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public async Task<Response<PageInfo<List<LasaPlatform>>>> GetPlatformList(int page, int limit, string key)
|
||
{
|
||
RefAsync<int> totalCount = 0;
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
var list = await db.LasaPlatform.AsQueryable()
|
||
.Where((a) => a.IsDelete == false)
|
||
.WhereIF(!string.IsNullOrEmpty(key), (a) => a.PlatformName.Contains(key))
|
||
.ToPageListAsync(page, limit, totalCount);
|
||
return new Response<PageInfo<List<LasaPlatform>>>
|
||
{
|
||
Result = new PageInfo<List<LasaPlatform>> { Items = list, Total = totalCount }
|
||
};
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 添加平台信息
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
/// <returns></returns>
|
||
public async Task<Response<bool>> AddPlatform(LasaPlatform info)
|
||
{
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
info.Id = Guid.NewGuid().ToString();
|
||
info.IsDelete = false;
|
||
await db.LasaPlatform.InsertAsync(info);
|
||
if (db.Commit())
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "添加成功"
|
||
};
|
||
else
|
||
return new Response<bool>
|
||
{
|
||
Result = false,
|
||
Message = "添加失败"
|
||
};
|
||
}
|
||
}
|
||
//修改平台信息
|
||
public async Task<Response<bool>> UpdatePlatform(LasaPlatform info)
|
||
{
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
// 编辑
|
||
await db.LasaPlatform.UpdateAsync(u => new LasaPlatform
|
||
{
|
||
Url = info.Url,
|
||
PlatformName = info.PlatformName,
|
||
RoutingKey = info.RoutingKey,
|
||
Exchange = info.Exchange
|
||
}, u => u.Id == info.Id);
|
||
// 提交事务
|
||
if (db.Commit())
|
||
{
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "修改成功"
|
||
};
|
||
}
|
||
else
|
||
{
|
||
return new Response<bool>
|
||
{
|
||
Result = false,
|
||
Message = "修改失败"
|
||
};
|
||
}
|
||
}
|
||
}
|
||
//删除平台信息
|
||
public async Task<Response<bool>> DeletePlatform(string id)
|
||
{
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
await db.LasaPlatform.DeleteByIdAsync(id);
|
||
if (db.Commit())
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "删除成功"
|
||
};
|
||
else
|
||
return new Response<bool>
|
||
{
|
||
Result = false,
|
||
Message = "删除失败"
|
||
};
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加ai算法任务信息
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
/// <returns></returns>
|
||
public async Task<Response<bool>> AddTaskAi(LasaTaskAi info)
|
||
{
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
if (string.IsNullOrEmpty(info.Id))
|
||
info.Id = Guid.NewGuid().ToString();
|
||
await db.LasaTaskAi.InsertAsync(info);
|
||
if (db.Commit())
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "添加成功"
|
||
};
|
||
else
|
||
return new Response<bool>
|
||
{
|
||
Result = false,
|
||
Message = "添加失败"
|
||
};
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 添加ai算法推送log
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
/// <returns></returns>
|
||
public async Task<Response<bool>> AddTaskAi(LasaTaskAiLog info)
|
||
{
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
if (string.IsNullOrEmpty(info.Id))
|
||
info.Id = Guid.NewGuid().ToString();
|
||
await db.LasaTaskAiLog.InsertAsync(info);
|
||
if (db.Commit())
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "添加成功"
|
||
};
|
||
else
|
||
return new Response<bool>
|
||
{
|
||
Result = false,
|
||
Message = "添加失败"
|
||
};
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取推送记录
|
||
/// </summary>
|
||
/// <param name="page"></param>
|
||
/// <param name="limit"></param>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public async Task<Response<PageInfo<List<LasaTaskAiLog>>>> GetAiLogList(int page, int limit, string key)
|
||
{
|
||
RefAsync<int> totalCount = 0;
|
||
using (var db = UnitWork.CreateContext())
|
||
{
|
||
var list = await db.LasaTaskAiLog.AsQueryable()
|
||
.WhereIF(!string.IsNullOrEmpty(key), (a) => a.Data.Contains(key))
|
||
.ToPageListAsync(page, limit, totalCount);
|
||
return new Response<PageInfo<List<LasaTaskAiLog>>>
|
||
{
|
||
Result = new PageInfo<List<LasaTaskAiLog>> { Items = list, Total = totalCount }
|
||
};
|
||
}
|
||
}
|
||
|
||
|
||
public async Task<Response<bool>> AddImgTopic(string payload)
|
||
{
|
||
using var db = UnitWork.CreateContext();
|
||
|
||
var aiinfo = JsonConvert.DeserializeObject<AiImgReq>(payload);
|
||
if (aiinfo == null)
|
||
{
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "添加成功"
|
||
};
|
||
}
|
||
|
||
_logger.LogInformation("标签信息:{tag}", JsonConvert.SerializeObject(aiinfo.tag));
|
||
_logger.LogInformation("aiid:{aiid}", aiinfo.aiid);
|
||
|
||
var config = ConfigHelper.GetConfigRoot();
|
||
var imageBaseUrl = $"http://{config["Minio:Endpoint"]}/{config["Minio:BucketName"]}/";
|
||
|
||
var (lat, lng) = GetDroneLocation(aiinfo);
|
||
|
||
// 一次性加载模型标签
|
||
var modelDict = (await db.LasaModelLabel.AsQueryable().Where(r => r.PId == aiinfo.aiid).ToListAsync())
|
||
.ToDictionary(x => x.EnumValue, x => x.Name);
|
||
|
||
foreach (var tag in aiinfo.tag)
|
||
{
|
||
if (tag.confidence < 0.3)
|
||
continue;
|
||
|
||
var tagKey = tag.class_id.ToString();
|
||
var achievement = await db.LasaAiAchievement
|
||
.GetFirstAsync(r => r.TaskId == aiinfo.taskid && r.Tag == tagKey);
|
||
if (achievement == null)
|
||
{
|
||
await db.LasaAiAchievement.InsertAsync(new LasaAiAchievement
|
||
{
|
||
Id = Guid.NewGuid().ToString(),
|
||
CreateTime = DateTime.Now,
|
||
TaskId = aiinfo.taskid,
|
||
AlgoId = aiinfo.aiid,
|
||
AiModel = "yolo12x",
|
||
Tag = tag.class_id.ToString(),
|
||
Title = modelDict.TryGetValue(tag.class_id, out var name) ? name : string.Empty,
|
||
ConfidenceLevel = (float)Math.Round(tag.confidence, 2) * 100,
|
||
Cover = imageBaseUrl + aiinfo.path,
|
||
Lat = (float)lat,
|
||
Lng = (float)lng
|
||
});
|
||
}
|
||
await db.LasaAiAchievementDetail.InsertAsync(new LasaAiAchievementDetail
|
||
{
|
||
Id = Guid.NewGuid().ToString(),
|
||
AiAchievementId = achievement.Id,
|
||
Image = imageBaseUrl + aiinfo.path,
|
||
Lat = lat,
|
||
Lng = lng
|
||
});
|
||
}
|
||
|
||
if (db.Commit())
|
||
return new Response<bool>
|
||
{
|
||
Result = true,
|
||
Message = "添加成功"
|
||
};
|
||
else
|
||
return new Response<bool>
|
||
{
|
||
Result = false,
|
||
Message = "添加失败"
|
||
};
|
||
}
|
||
private static (double lat, double lng) GetDroneLocation(AiImgReq aiinfo)
|
||
{
|
||
if (aiinfo.drone_info?.data == null)
|
||
return (0.0, 0.0);
|
||
|
||
return (
|
||
aiinfo.drone_info.data.latitude ?? 0.0,
|
||
aiinfo.drone_info.data.longitude ?? 0.0
|
||
);
|
||
}
|
||
}
|
||
}
|