复原算法调用
parent
cb0008e09d
commit
a811ec3566
|
|
@ -2533,7 +2533,7 @@ namespace OpenAuth.App.ServiceApp
|
|||
};
|
||||
}
|
||||
|
||||
public async Task<Response<bool>> CallAiModelOld(CallAiModel req)
|
||||
public async Task<Response<bool>> CallAiModel(CallAiModel req)
|
||||
{
|
||||
_logger.LogDebug("成功调用CallAIModel");
|
||||
var task = await Repository
|
||||
|
|
@ -2680,179 +2680,7 @@ namespace OpenAuth.App.ServiceApp
|
|||
}
|
||||
|
||||
|
||||
public async Task<Response<bool>> CallAiModel(CallAiModel req)
|
||||
{
|
||||
_logger.LogDebug("成功调用CallAIModel");
|
||||
var task = await Repository
|
||||
.ChangeRepository<SugarRepositiry<LasaTask>>()
|
||||
.GetByIdAsync(req.TaskId);
|
||||
using var db = Repository.AsSugarClient();
|
||||
try
|
||||
{
|
||||
db.Ado.BeginTran();
|
||||
if (task.FlightTaskType.Equals(1)) // 手飞任务
|
||||
{
|
||||
var aiInspection = new LasaAiInspection
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TaskId = req.TaskId,
|
||||
AlgoInstanceId = req.AlgoInstanceId,
|
||||
};
|
||||
if (!string.IsNullOrEmpty(req.WarningContent))
|
||||
{
|
||||
aiInspection.WarningTitle = req.WarningTitle;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(req.WarningContent))
|
||||
{
|
||||
aiInspection.WarningContent = req.WarningContent;
|
||||
}
|
||||
|
||||
// 关于多次调用问题
|
||||
var count = await db.Queryable<LasaAiInspection>().Where(x => x.TaskId == req.TaskId).CountAsync();
|
||||
if (count == 0)
|
||||
{
|
||||
// 设置任务智能巡检标识
|
||||
task.AIInspection = "true";
|
||||
task.Status = 3; // 表示智能巡检中
|
||||
await db.Updateable(task).ExecuteCommandAsync();
|
||||
await db.Insertable(aiInspection).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 计划任务
|
||||
var aiInspection = await Repository
|
||||
.ChangeRepository<SugarRepositiry<LasaAiInspection>>()
|
||||
.AsQueryable().Where(x => x.TaskId == req.TaskId).FirstAsync();
|
||||
req.AlgoInstanceId = aiInspection.AlgoInstanceId;
|
||||
}
|
||||
|
||||
var algoInstances = await db
|
||||
.Queryable<LasaAlgoInstance>()
|
||||
.Where(x => x.Id == req.AlgoInstanceId)
|
||||
.ToListAsync();
|
||||
var tagsIds = algoInstances.Select(x => x.Tags).ToList();
|
||||
// todo 关于存在多个算法的处理 查询多个算法
|
||||
var algoIds = algoInstances.First().AlgoIds.Split(",").ToArray();
|
||||
var algos = await db
|
||||
.Queryable<LasaAlgorithmsRepository>()
|
||||
.Where(x => algoIds.Contains(x.Id)).ToListAsync();
|
||||
//创建多模型数据结构
|
||||
var models = new List<ModelConfig>();
|
||||
foreach (var algo in algos)
|
||||
{
|
||||
// 当前算法对应的 tag
|
||||
var algoInstance = algoInstances.First(a => a.AlgoIds.Contains(algo.Id));
|
||||
|
||||
var tagIds = algoInstance.Tags.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var labels = await db.Queryable<LasaModelLabel>()
|
||||
.Where(l => tagIds.Contains(l.Id) && l.PId == algo.Id)
|
||||
.ToListAsync();
|
||||
|
||||
Dictionary<string, ModelTag> tags = new Dictionary<string, ModelTag>();
|
||||
|
||||
foreach (var label in labels)
|
||||
{
|
||||
tags[label.EnumValue.ToString()] = new ModelTag
|
||||
{
|
||||
name = label.Name,
|
||||
reliability = label.Reliability,
|
||||
select = true,
|
||||
color = label.Color
|
||||
};
|
||||
}
|
||||
|
||||
var model = new ModelConfig
|
||||
{
|
||||
path = algo.Path,
|
||||
encryption_key = algo.SecretKey,
|
||||
tags = tags,
|
||||
conf_thres = algo.ConfThres,
|
||||
imgsz = algo.Imgsz,
|
||||
device = "cuda:0",
|
||||
line_width = 2,
|
||||
enabled = algo.Enable
|
||||
};
|
||||
|
||||
models.Add(model);
|
||||
}
|
||||
var x = SnowFlakeSingle.instance;
|
||||
//var pushUrl = $"rtmp://box.wisestcity.com:1935/live/{x.NextId()}";
|
||||
var config = ConfigHelper.GetConfigRoot();
|
||||
var pushUrl = config["AIModelApi:PushUrl"];
|
||||
// 取得无人机sn + 1
|
||||
if (string.IsNullOrEmpty(req.UavSn))
|
||||
{
|
||||
var lasaDronePort = await Repository.AsSugarClient().Queryable<LasaDronePort>()
|
||||
.LeftJoin<LasaUav>((a, b) => a.Id == b.PId)
|
||||
.Where((a, b) => a.Id == task.TaskDronePort).Select<dynamic>(
|
||||
(a, b) => new
|
||||
{
|
||||
UavSn = b.Sn,
|
||||
b.TypeId
|
||||
}
|
||||
).FirstAsync();
|
||||
pushUrl += lasaDronePort.UavSn + "1";
|
||||
}
|
||||
else
|
||||
{
|
||||
pushUrl += req.UavSn + 1;
|
||||
}
|
||||
var reqBody = new StartDetectionResp
|
||||
{
|
||||
rtmp_url = req.RtmpUrl,
|
||||
push_url = pushUrl,
|
||||
taskname = task.TaskName,
|
||||
taskid = req.TaskId,
|
||||
models = models
|
||||
};
|
||||
var taskRecord = new LasaTask()
|
||||
{
|
||||
Id = req.TaskId,
|
||||
Status = 6, // 智能巡检状态
|
||||
PushUrl = pushUrl,
|
||||
AIInspection = "true",
|
||||
};
|
||||
await db.Updateable(taskRecord).IgnoreNullColumns().ExecuteCommandAsync();
|
||||
|
||||
_logger.LogDebug($"发送的json:{JsonConvert.SerializeObject(reqBody)}");
|
||||
var content = new StringContent(JsonConvert.SerializeObject(reqBody), Encoding.UTF8, "application/json");
|
||||
var url = config["AIModelApi:Url"];
|
||||
using var httpClient = new HttpClient();
|
||||
var response = await httpClient.PostAsync($"{url}/api/tasks/creat", content);
|
||||
//var response = await httpClient.PostAsync("http://192.168.10.131:9025/start_detection", content);
|
||||
if (response.IsSuccessStatusCode)//低一哦成功记录当前任务数据
|
||||
{
|
||||
var responseStr = await response.Content.ReadAsStringAsync();
|
||||
var root = JsonNode.Parse(responseStr)?.AsObject();
|
||||
var taskId = root?["data"]?["task_id"]?.ToString();
|
||||
if (!string.IsNullOrEmpty(taskId))
|
||||
{
|
||||
_redisCacheContext.HashSetAsync($"ai:task:{taskId}", new[]{
|
||||
new HashEntry("TaskId", taskId),
|
||||
new HashEntry("Status", "running"),
|
||||
new HashEntry("CreateTime", DateTime.Now.ToString("O"))
|
||||
});
|
||||
}
|
||||
}
|
||||
_logger.LogDebug($"成功调用{response.IsSuccessStatusCode}");
|
||||
db.Ado.CommitTran();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Ado.RollbackTran();
|
||||
throw ex;
|
||||
//throw new Exception("调用智能巡检失败");
|
||||
}
|
||||
|
||||
return new Response<bool>()
|
||||
{
|
||||
Result = true
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public async Task<Response<dynamic>> EndHandFlyTask(string taskid)
|
||||
{
|
||||
var savedTask = await Repository.ChangeRepository<SugarRepositiry<LasaTask>>().AsQueryable()
|
||||
|
|
|
|||
Loading…
Reference in New Issue