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

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

@ -63,20 +63,20 @@ public class CameraDataCenter : ISingleton
// 启动 CameraPtzThread // 启动 CameraPtzThread
RefreshCameraPtzThread.Start(); RefreshCameraPtzThread.Start();
//// 初始化 MarkSearcherThread // 初始化 MarkSearcherThread
//RefreshMarkSearcherThread = new Thread(async () => RefreshMarkSearcherThread = new Thread(async () =>
//{ {
// while (true) while (true)
// { {
// await RefreshMarkSearchers(); await RefreshMarkSearchers();
// Thread.Sleep(_options.MarkSearcher.LoopInterval); Thread.Sleep(_options.MarkSearcher.LoopInterval);
// } }
//}) })
//{ {
// IsBackground = true// 设置后台线程 IsBackground = true// 设置后台线程
//}; };
//// 启动 MarkSearcherThread // 启动 MarkSearcherThread
//RefreshMarkSearcherThread.Start(); RefreshMarkSearcherThread.Start();
} }
/// <summary> /// <summary>
@ -135,32 +135,8 @@ public class CameraDataCenter : ISingleton
if (string.IsNullOrEmpty(ptzInfoStr)) return; if (string.IsNullOrEmpty(ptzInfoStr)) return;
PtzInfo ptzInfo = ptzInfoStr.ToObject<PtzInfo>(); PtzInfo ptzInfo = ptzInfoStr.ToObject<PtzInfo>();
if (ptzInfo == null) return; if (ptzInfo == null) return;
markSearcher.UpdateCameraCalcParams(ptzInfo); await markSearcher.UpdateCameraCalcParamsAsync(ptzInfo);
List<MarkLabelCalcResult> resultList = await markSearcher.SearchAsync();
SearchResultListDict[cameraId] = resultList;
} }
#endregion Loop #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> /// <returns></returns>
public bool IsExistsMarkLabel(long cameraId, long markLabelId); public bool IsExistsMarkLabel(long cameraId, long markLabelId);
/// <summary>
/// 获取标签计算结果列表
/// </summary>
/// <param name="cameraId"></param>
/// <returns></returns>
public List<MarkLabelCalcResult> GetMarkLabelCalcResultList(long cameraId);
#endregion Base Method #endregion Base Method
#region Base Method #region Base Method Async
/// <summary> /// <summary>
/// 激活相机进入运算 /// 激活相机进入运算
@ -115,5 +122,12 @@ public interface IMarkSearcherServer
/// <returns></returns> /// <returns></returns>
public Task<bool> IsExistsMarkLabelAsync(long cameraId, long markLabelId); 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> /// </summary>
private ReaderWriterLockSlim MsDictRWLock { get; } = new(); private ReaderWriterLockSlim MsDictRWLock { get; } = new();
/// <summary>
/// MarkSearcherDict 中 MarkLabel 原子操作锁
/// </summary>
private ReaderWriterLockSlim MsMlRWLock { get; } = new();
#endregion Attr #endregion Attr
public MarkSearcherServer( public MarkSearcherServer(
@ -51,7 +56,7 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
{ {
try try
{ {
MsDictRWLock.EnterWriteLock(); MsDictRWLock.TryEnterWriteLock(2000);
return ActivateSearcherAtom(cameraId); return ActivateSearcherAtom(cameraId);
} }
finally finally
@ -126,7 +131,7 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
{ {
try try
{ {
MsDictRWLock.EnterWriteLock(); MsDictRWLock.TryEnterWriteLock(2000);
return DeactivateSearcherAtom(cameraId); return DeactivateSearcherAtom(cameraId);
} }
finally finally
@ -170,6 +175,25 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
} }
public bool ActivateMarkLabel(long cameraId, long markLabelId) 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); bool ret = MarkSearcherDict.TryGetValue(cameraId, out MarkSearcherBase markSearcher);
if (!ret) return false; if (!ret) return false;
@ -190,6 +214,25 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
} }
public bool DeactivateMarkLabel(long cameraId, long markLabelId) 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); bool ret = MarkSearcherDict.TryGetValue(cameraId, out MarkSearcherBase markSearcher);
if (!ret) return false; if (!ret) return false;
@ -203,64 +246,54 @@ public class MarkSearcherServer : IMarkSearcherServer, ISingleton
&& searcher.IsExistsMarkLabelCalcParams(markLabelId); && 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 #endregion Base Method
#region Base Method Async #region Base Method Async
public async Task<bool> ActivateSearcherAsync(long cameraId) public async Task<bool> ActivateSearcherAsync(long cameraId)
{ {
return await Task.Run(() => return await Task.Run(() => ActivateSearcher(cameraId));
{
return ActivateSearcher(cameraId);
});
} }
public async Task<bool> DeactivateSearcherAsync(long cameraId) public async Task<bool> DeactivateSearcherAsync(long cameraId)
{ {
return await Task.Run(() => return await Task.Run(() => DeactivateSearcher(cameraId));
{
return DeactivateSearcher(cameraId);
});
} }
public async Task<bool> IsExistsSearcherAsync(long cameraId) public async Task<bool> IsExistsSearcherAsync(long cameraId)
{ {
return await Task.Run(() => return await Task.Run(() => IsExistsSearcher(cameraId));
{
return IsExistsSearcher(cameraId);
});
} }
public async Task<MarkSearcherBase> GetSearcherAsync(long cameraId) public async Task<MarkSearcherBase> GetSearcherAsync(long cameraId)
{ {
return await Task.Run(() => return await Task.Run(() => GetSearcher(cameraId));
{
return GetSearcher(cameraId);
});
} }
public async Task<bool> ActivateMarkLabelAsync(long cameraId, long markLabelId) public async Task<bool> ActivateMarkLabelAsync(long cameraId, long markLabelId)
{ {
return await Task.Run(() => return await Task.Run(() => ActivateMarkLabel(cameraId, markLabelId));
{
return ActivateMarkLabel(cameraId, markLabelId);
});
} }
public async Task<bool> DeactivateMarkLabelAsync(long cameraId, long markLabelId) public async Task<bool> DeactivateMarkLabelAsync(long cameraId, long markLabelId)
{ {
return await Task.Run(() => return await Task.Run(() => DeactivateMarkLabel(cameraId, markLabelId));
{
return DeactivateMarkLabel(cameraId, markLabelId);
});
} }
public async Task<bool> IsExistsMarkLabelAsync(long cameraId, long 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 #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; } protected Matrix<double> World2CameraMatrix { get; set; }
/// <summary> /// <summary>
/// {cameraId, MarkLabelCalcParams} /// {markLabelId, MarkLabelCalcParams}
/// </summary> /// </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 #endregion Attr
@ -144,39 +149,38 @@ public abstract class MarkSearcherBase
/// 计算标签位置过程 /// 计算标签位置过程
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public List<MarkLabelCalcResult> Search() protected void Search()
{ {
List<MarkLabelCalcResult> resultList = new();
if (World2CameraMatrix == null || MarkLabelCalcParamsDict.IsEmpty) if (World2CameraMatrix == null || MarkLabelCalcParamsDict.IsEmpty)
return resultList; return;
foreach (MarkLabelCalcParams item in MarkLabelCalcParamsDict.Values) foreach (MarkLabelCalcParams item in MarkLabelCalcParamsDict.Values)
{ {
MarkLabelCalcResult labelCalcResult = SearchMarkLabel(item); MarkLabelCalcResult result = SearchMarkLabel(item);
resultList.Add(labelCalcResult); 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) if (World2CameraMatrix == null || MarkLabelCalcParamsDict.IsEmpty)
return resultList; return;
List<Task> tasks = new(); List<Task> tasks = new();
foreach (MarkLabelCalcParams item in MarkLabelCalcParamsDict.Values) foreach (MarkLabelCalcParams item in MarkLabelCalcParamsDict.Values)
{ {
tasks.Add(Task.Run(() => 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); await Task.WhenAll(tasks);
return resultList;
} }
private MarkLabelCalcResult SearchMarkLabel(MarkLabelCalcParams item) protected MarkLabelCalcResult SearchMarkLabel(MarkLabelCalcParams item)
{ {
Matrix<double> labelC2WMatrix = ConvertCameraToWorld(item); Matrix<double> labelC2WMatrix = ConvertCameraToWorld(item);
Matrix<double> labelPointMatrix = new DenseMatrix(3, 1, new double[] Matrix<double> labelPointMatrix = new DenseMatrix(3, 1, new double[]
@ -255,6 +259,7 @@ public abstract class MarkSearcherBase
{ {
CameraCalcParams.PtzInfo = ptzInfo; CameraCalcParams.PtzInfo = ptzInfo;
World2CameraMatrix = ConvertWorldToCamera(CameraCalcParams); World2CameraMatrix = ConvertWorldToCamera(CameraCalcParams);
Search();
} }
} }
@ -265,7 +270,9 @@ public abstract class MarkSearcherBase
/// <returns></returns> /// <returns></returns>
public bool AddMarkLabelCalcParams(MarkLabelCalcParams labelCalcParams) 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> /// <summary>
@ -275,7 +282,8 @@ public abstract class MarkSearcherBase
/// <returns></returns> /// <returns></returns>
public bool DeleteMarkLabelCalcParams(long markLabelId) public bool DeleteMarkLabelCalcParams(long markLabelId)
{ {
return MarkLabelCalcParamsDict.TryRemove(markLabelId, out _); return MarkLabelCalcParamsDict.TryRemove(markLabelId, out _)
&& MarkLabelCalcResultDict.TryRemove(markLabelId, out _);
} }
/// <summary> /// <summary>
@ -288,5 +296,33 @@ public abstract class MarkSearcherBase
return MarkLabelCalcParamsDict.ContainsKey(markLabelId); return MarkLabelCalcParamsDict.ContainsKey(markLabelId);
} }
public List<MarkLabelCalcResult> GetMarkLabelCalcResultList()
{
return MarkLabelCalcResultDict.Values.ToList();
}
#endregion Base Method #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 #region Attr
private readonly CameraDataCenter _cameraDataCenter;
private readonly IMarkSearcherServer _markSearcherServer; private readonly IMarkSearcherServer _markSearcherServer;
#endregion Attr #endregion Attr
public MarkSearchService(CameraDataCenter cameraDataCenter, IMarkSearcherServer markSearcherServer) public MarkSearchService(CameraDataCenter cameraDataCenter, IMarkSearcherServer markSearcherServer)
{ {
_cameraDataCenter = cameraDataCenter;
_markSearcherServer = markSearcherServer; _markSearcherServer = markSearcherServer;
} }
@ -57,6 +55,6 @@ public class MarkSearchService : IDynamicApiController, ITransient
[HttpGet] [HttpGet]
public async Task<List<MarkLabelCalcResult>> GetMarkLabelCalcResultList(long cameraId) 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)); 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 class WebComponent : IWebComponent
{ {
public void Load(WebApplicationBuilder builder, ComponentContext componentContext) public void Load(WebApplicationBuilder builder, ComponentContext componentContext)

Loading…
Cancel
Save