Browse Source

fix: 更改运算逻辑与存储运算结果方式

master
fajiao 2 years ago
parent
commit
d90a7cebaf
  1. 48
      Cis.Application/Cis.Application.xml
  2. 54
      Cis.Application/Core/Center/CameraDataCenter.cs
  3. 18
      Cis.Application/Core/Component/MarkSeacher/IMarkSearcherServer.cs
  4. 91
      Cis.Application/Core/Component/MarkSeacher/MarkSearcherServer.cs
  5. 68
      Cis.Application/Core/Component/MarkSeacher/Seacher/MarkSearcherBase.cs
  6. 4
      Cis.Application/Core/Service/MarkSearchService.cs
  7. 10
      Cis.Web.Entry/Program.cs

48
Cis.Application/Cis.Application.xml

@ -606,6 +606,13 @@
<param name="markLabelId"></param>
<returns></returns>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.IMarkSearcherServer.GetMarkLabelCalcResultList(System.Int64)">
<summary>
获取标签计算结果列表
</summary>
<param name="cameraId"></param>
<returns></returns>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.IMarkSearcherServer.ActivateSearcherAsync(System.Int64)">
<summary>
激活相机进入运算
@ -658,6 +665,13 @@
<param name="markLabelId"></param>
<returns></returns>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.IMarkSearcherServer.GetMarkLabelCalcResultListAsync(System.Int64)">
<summary>
获取标签计算结果列表
</summary>
<param name="cameraId"></param>
<returns></returns>
</member>
<member name="T:Cis.Application.Core.Component.MarkSeacher.MarkSearcherServer">
<summary>
追踪标签服务类
@ -673,6 +687,11 @@
MarkSearcherDict 原子操作锁
</summary>
</member>
<member name="P:Cis.Application.Core.Component.MarkSeacher.MarkSearcherServer.MsMlRWLock">
<summary>
MarkSearcherDict 中 MarkLabel 原子操作锁
</summary>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.MarkSearcherServer.ActivateSearcherAtom(System.Int64)">
<summary>
ActivateSearcher 原子操作
@ -687,6 +706,22 @@
<param name="cameraId"></param>
<returns></returns>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.MarkSearcherServer.ActivateMarkLabelAtom(System.Int64,System.Int64)">
<summary>
ActivateMarkLabel 原子操作
</summary>
<param name="cameraId"></param>
<param name="markLabelId"></param>
<returns></returns>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.MarkSearcherServer.DeactivateMarkLabelAtom(System.Int64,System.Int64)">
<summary>
DeactivateMarkLabel 原子操作
</summary>
<param name="cameraId"></param>
<param name="markLabelId"></param>
<returns></returns>
</member>
<member name="P:Cis.Application.Core.Component.MarkSeacher.MarkSearcherBase.CameraCalcParams">
<summary>
当前相机计算参数
@ -699,7 +734,12 @@
</member>
<member name="P:Cis.Application.Core.Component.MarkSeacher.MarkSearcherBase.MarkLabelCalcParamsDict">
<summary>
{cameraId, MarkLabelCalcParams}
{markLabelId, MarkLabelCalcParams}
</summary>
</member>
<member name="P:Cis.Application.Core.Component.MarkSeacher.MarkSearcherBase.MarkLabelCalcResultDict">
<summary>
{markLabelId, MarkLabelCalcParams}
</summary>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.MarkSearcherBase.IsCameraRotate(EC.Helper.CameraSDK.PtzInfo)">
@ -792,6 +832,12 @@
<param name="markLabelId"></param>
<returns></returns>
</member>
<member name="M:Cis.Application.Core.Component.MarkSeacher.MarkSearcherBase.UpdateCameraCalcParamsAsync(EC.Helper.CameraSDK.PtzInfo)">
<summary>
更新相机计算参数
</summary>
<param name="ptzInfo"></param>
</member>
<member name="M:Cis.Application.Startup.ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary>
配置应用所需服务,在该方法中可以添加应用所需要的功能或服务

54
Cis.Application/Core/Center/CameraDataCenter.cs

@ -63,20 +63,20 @@ public class CameraDataCenter : ISingleton
// 启动 CameraPtzThread
RefreshCameraPtzThread.Start();
//// 初始化 MarkSearcherThread
//RefreshMarkSearcherThread = new Thread(async () =>
//{
// while (true)
// {
// await RefreshMarkSearchers();
// Thread.Sleep(_options.MarkSearcher.LoopInterval);
// }
//})
//{
// IsBackground = true// 设置后台线程
//};
//// 启动 MarkSearcherThread
//RefreshMarkSearcherThread.Start();
// 初始化 MarkSearcherThread
RefreshMarkSearcherThread = new Thread(async () =>
{
while (true)
{
await RefreshMarkSearchers();
Thread.Sleep(_options.MarkSearcher.LoopInterval);
}
})
{
IsBackground = true// 设置后台线程
};
// 启动 MarkSearcherThread
RefreshMarkSearcherThread.Start();
}
/// <summary>
@ -135,32 +135,8 @@ public class CameraDataCenter : ISingleton
if (string.IsNullOrEmpty(ptzInfoStr)) return;
PtzInfo ptzInfo = ptzInfoStr.ToObject<PtzInfo>();
if (ptzInfo == null) return;
markSearcher.UpdateCameraCalcParams(ptzInfo);
List<MarkLabelCalcResult> resultList = await markSearcher.SearchAsync();
SearchResultListDict[cameraId] = resultList;
await markSearcher.UpdateCameraCalcParamsAsync(ptzInfo);
}
#endregion Loop
#region Base Method
public List<MarkLabelCalcResult> GetMarkLabelCalcResultList(long cameraId)
{
bool ret = SearchResultListDict.TryGetValue(cameraId, out List<MarkLabelCalcResult> resultList);
return ret ? resultList : new List<MarkLabelCalcResult>();
}
#endregion Base Method
#region Base Method Async
public async Task<List<MarkLabelCalcResult>> GetMarkLabelCalcResultListAsync(long cameraId)
{
return await Task.Run(() =>
{
return GetMarkLabelCalcResultList(cameraId);
});
}
#endregion Base Method Async
}

18
Cis.Application/Core/Component/MarkSeacher/IMarkSearcherServer.cs

@ -59,9 +59,16 @@ public interface IMarkSearcherServer
/// <returns></returns>
public bool IsExistsMarkLabel(long cameraId, long markLabelId);
/// <summary>
/// 获取标签计算结果列表
/// </summary>
/// <param name="cameraId"></param>
/// <returns></returns>
public List<MarkLabelCalcResult> GetMarkLabelCalcResultList(long cameraId);
#endregion Base Method
#region Base Method
#region Base Method Async
/// <summary>
/// 激活相机进入运算
@ -115,5 +122,12 @@ public interface IMarkSearcherServer
/// <returns></returns>
public Task<bool> IsExistsMarkLabelAsync(long cameraId, long markLabelId);
#endregion Base Method
/// <summary>
/// 获取标签计算结果列表
/// </summary>
/// <param name="cameraId"></param>
/// <returns></returns>
public Task<List<MarkLabelCalcResult>> GetMarkLabelCalcResultListAsync(long cameraId);
#endregion Base Method Async
}

91
Cis.Application/Core/Component/MarkSeacher/MarkSearcherServer.cs

@ -31,6 +31,11 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
/// </summary>
private ReaderWriterLockSlim MsDictRWLock { get; } = new();
/// <summary>
/// MarkSearcherDict 中 MarkLabel 原子操作锁
/// </summary>
private ReaderWriterLockSlim MsMlRWLock { get; } = new();
#endregion Attr
public MarkSearcherServer(
@ -51,7 +56,7 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
{
try
{
MsDictRWLock.EnterWriteLock();
MsDictRWLock.TryEnterWriteLock(2000);
return ActivateSearcherAtom(cameraId);
}
finally
@ -126,7 +131,7 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
{
try
{
MsDictRWLock.EnterWriteLock();
MsDictRWLock.TryEnterWriteLock(2000);
return DeactivateSearcherAtom(cameraId);
}
finally
@ -170,6 +175,25 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
}
public bool ActivateMarkLabel(long cameraId, long markLabelId)
{
try
{
MsMlRWLock.TryEnterWriteLock(2000);
return ActivateMarkLabelAtom(cameraId, markLabelId);
}
finally
{
MsMlRWLock.ExitWriteLock();
}
}
/// <summary>
/// ActivateMarkLabel 原子操作
/// </summary>
/// <param name="cameraId"></param>
/// <param name="markLabelId"></param>
/// <returns></returns>
private bool ActivateMarkLabelAtom(long cameraId, long markLabelId)
{
bool ret = MarkSearcherDict.TryGetValue(cameraId, out MarkSearcherBase markSearcher);
if (!ret) return false;
@ -190,6 +214,25 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
}
public bool DeactivateMarkLabel(long cameraId, long markLabelId)
{
try
{
MsMlRWLock.TryEnterWriteLock(2000);
return DeactivateMarkLabelAtom(cameraId, markLabelId);
}
finally
{
MsMlRWLock.ExitWriteLock();
}
}
/// <summary>
/// DeactivateMarkLabel 原子操作
/// </summary>
/// <param name="cameraId"></param>
/// <param name="markLabelId"></param>
/// <returns></returns>
private bool DeactivateMarkLabelAtom(long cameraId, long markLabelId)
{
bool ret = MarkSearcherDict.TryGetValue(cameraId, out MarkSearcherBase markSearcher);
if (!ret) return false;
@ -203,64 +246,54 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
&& searcher.IsExistsMarkLabelCalcParams(markLabelId);
}
public List<MarkLabelCalcResult> GetMarkLabelCalcResultList(long cameraId)
{
bool ret = MarkSearcherDict.TryGetValue(cameraId, out MarkSearcherBase markSearcher);
return ret ? markSearcher.GetMarkLabelCalcResultList() : new List<MarkLabelCalcResult>();
}
#endregion Base Method
#region Base Method Async
public async Task<bool> ActivateSearcherAsync(long cameraId)
{
return await Task.Run(() =>
{
return ActivateSearcher(cameraId);
});
return await Task.Run(() => ActivateSearcher(cameraId));
}
public async Task<bool> DeactivateSearcherAsync(long cameraId)
{
return await Task.Run(() =>
{
return DeactivateSearcher(cameraId);
});
return await Task.Run(() => DeactivateSearcher(cameraId));
}
public async Task<bool> IsExistsSearcherAsync(long cameraId)
{
return await Task.Run(() =>
{
return IsExistsSearcher(cameraId);
});
return await Task.Run(() => IsExistsSearcher(cameraId));
}
public async Task<MarkSearcherBase> GetSearcherAsync(long cameraId)
{
return await Task.Run(() =>
{
return GetSearcher(cameraId);
});
return await Task.Run(() => GetSearcher(cameraId));
}
public async Task<bool> ActivateMarkLabelAsync(long cameraId, long markLabelId)
{
return await Task.Run(() =>
{
return ActivateMarkLabel(cameraId, markLabelId);
});
return await Task.Run(() => ActivateMarkLabel(cameraId, markLabelId));
}
public async Task<bool> DeactivateMarkLabelAsync(long cameraId, long markLabelId)
{
return await Task.Run(() =>
{
return DeactivateMarkLabel(cameraId, markLabelId);
});
return await Task.Run(() => DeactivateMarkLabel(cameraId, markLabelId));
}
public async Task<bool> IsExistsMarkLabelAsync(long cameraId, long markLabelId)
{
return await Task.Run(() =>
return await Task.Run(() => IsExistsMarkLabel(cameraId, markLabelId));
}
public async Task<List<MarkLabelCalcResult>> GetMarkLabelCalcResultListAsync(long cameraId)
{
return IsExistsMarkLabel(cameraId, markLabelId);
});
return await Task.Run(() => GetMarkLabelCalcResultList(cameraId));
}
#endregion Base Method Async

68
Cis.Application/Core/Component/MarkSeacher/Seacher/MarkSearcherBase.cs

@ -21,9 +21,14 @@ public abstract class MarkSearcherBase
protected Matrix<double> World2CameraMatrix { get; set; }
/// <summary>
/// {cameraId, MarkLabelCalcParams}
/// {markLabelId, MarkLabelCalcParams}
/// </summary>
private ConcurrentDictionary<long, MarkLabelCalcParams> MarkLabelCalcParamsDict { get; set; } = new();
protected ConcurrentDictionary<long, MarkLabelCalcParams> MarkLabelCalcParamsDict { get; set; } = new();
/// <summary>
/// {markLabelId, MarkLabelCalcParams}
/// </summary>
protected ConcurrentDictionary<long, MarkLabelCalcResult> MarkLabelCalcResultDict { get; set; } = new();
#endregion Attr
@ -144,39 +149,38 @@ public abstract class MarkSearcherBase
/// 计算标签位置过程
/// </summary>
/// <returns></returns>
public List<MarkLabelCalcResult> Search()
protected void Search()
{
List<MarkLabelCalcResult> resultList = new();
if (World2CameraMatrix == null || MarkLabelCalcParamsDict.IsEmpty)
return resultList;
return;
foreach (MarkLabelCalcParams item in MarkLabelCalcParamsDict.Values)
{
MarkLabelCalcResult labelCalcResult = SearchMarkLabel(item);
resultList.Add(labelCalcResult);
MarkLabelCalcResult result = SearchMarkLabel(item);
if (!MarkLabelCalcParamsDict.ContainsKey(item.Id)) return;
MarkLabelCalcResultDict[item.Id] = result;
}
return resultList;
}
public async Task<List<MarkLabelCalcResult>> SearchAsync()
protected async Task SearchAsync()
{
List<MarkLabelCalcResult> resultList = new();
if (World2CameraMatrix == null || MarkLabelCalcParamsDict.IsEmpty)
return resultList;
return;
List<Task> tasks = new();
foreach (MarkLabelCalcParams item in MarkLabelCalcParamsDict.Values)
{
tasks.Add(Task.Run(() =>
{
resultList.Add(SearchMarkLabel(item));
MarkLabelCalcResult result = SearchMarkLabel(item);
if (!MarkLabelCalcParamsDict.ContainsKey(item.Id)) return;
MarkLabelCalcResultDict[item.Id] = result;
}));
}
await Task.WhenAll(tasks);
return resultList;
}
private MarkLabelCalcResult SearchMarkLabel(MarkLabelCalcParams item)
protected MarkLabelCalcResult SearchMarkLabel(MarkLabelCalcParams item)
{
Matrix<double> labelC2WMatrix = ConvertCameraToWorld(item);
Matrix<double> labelPointMatrix = new DenseMatrix(3, 1, new double[]
@ -255,6 +259,7 @@ public abstract class MarkSearcherBase
{
CameraCalcParams.PtzInfo = ptzInfo;
World2CameraMatrix = ConvertWorldToCamera(CameraCalcParams);
Search();
}
}
@ -265,7 +270,9 @@ public abstract class MarkSearcherBase
/// <returns></returns>
public bool AddMarkLabelCalcParams(MarkLabelCalcParams labelCalcParams)
{
return MarkLabelCalcParamsDict.TryAdd(labelCalcParams.Id, labelCalcParams);
long markLabelId = labelCalcParams.Id;
return MarkLabelCalcParamsDict.TryAdd(markLabelId, labelCalcParams)
&& MarkLabelCalcResultDict.TryAdd(markLabelId, SearchMarkLabel(labelCalcParams));
}
/// <summary>
@ -275,7 +282,8 @@ public abstract class MarkSearcherBase
/// <returns></returns>
public bool DeleteMarkLabelCalcParams(long markLabelId)
{
return MarkLabelCalcParamsDict.TryRemove(markLabelId, out _);
return MarkLabelCalcParamsDict.TryRemove(markLabelId, out _)
&& MarkLabelCalcResultDict.TryRemove(markLabelId, out _);
}
/// <summary>
@ -288,5 +296,33 @@ public abstract class MarkSearcherBase
return MarkLabelCalcParamsDict.ContainsKey(markLabelId);
}
public List<MarkLabelCalcResult> GetMarkLabelCalcResultList()
{
return MarkLabelCalcResultDict.Values.ToList();
}
#endregion Base Method
#region Base Method Async
/// <summary>
/// 更新相机计算参数
/// </summary>
/// <param name="ptzInfo"></param>
public async Task UpdateCameraCalcParamsAsync(PtzInfo ptzInfo)
{
if (IsCameraRotate(ptzInfo))
{
CameraCalcParams.PtzInfo = ptzInfo;
World2CameraMatrix = ConvertWorldToCamera(CameraCalcParams);
await SearchAsync();
}
}
public async Task<List<MarkLabelCalcResult>> GetMarkLabelCalcResultListAsync()
{
return await Task.Run(GetMarkLabelCalcResultList);
}
#endregion Base Method Async
}

4
Cis.Application/Core/Service/MarkSearchService.cs

@ -7,14 +7,12 @@ public class MarkSearchService : IDynamicApiController, ITransient
{
#region Attr
private readonly CameraDataCenter _cameraDataCenter;
private readonly IMarkSearcherServer _markSearcherServer;
#endregion Attr
public MarkSearchService(CameraDataCenter cameraDataCenter, IMarkSearcherServer markSearcherServer)
{
_cameraDataCenter = cameraDataCenter;
_markSearcherServer = markSearcherServer;
}
@ -57,6 +55,6 @@ public class MarkSearchService : IDynamicApiController, ITransient
[HttpGet]
public async Task<List<MarkLabelCalcResult>> GetMarkLabelCalcResultList(long cameraId)
{
return await _cameraDataCenter.GetMarkLabelCalcResultListAsync(cameraId);
return await _markSearcherServer.GetMarkLabelCalcResultListAsync(cameraId);
}
}

10
Cis.Web.Entry/Program.cs

@ -1,5 +1,15 @@
using Furion.Logging;
// 处理未捕获的异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// 启动 webServer
Serve.Run(RunOptions.Default.AddWebComponent<WebComponent>().WithArgs(args));
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Error(sender?.ToString(), e);
}
public class WebComponent : IWebComponent
{
public void Load(WebApplicationBuilder builder, ComponentContext componentContext)

Loading…
Cancel
Save