1
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using Evergreen.Health.Application.Health.Devices.Contracts;
|
||||
|
||||
namespace Evergreen.Health.Application.Health.Devices;
|
||||
|
||||
public sealed class DeviceModuleRegistry
|
||||
{
|
||||
public IReadOnlyList<IHealthDeviceModule> Modules { get; }
|
||||
private readonly IReadOnlyDictionary<string, IHealthDeviceModule> _parameterOwners;
|
||||
|
||||
public DeviceModuleRegistry(IEnumerable<IHealthDeviceModule> modules)
|
||||
{
|
||||
Modules = modules.ToArray();
|
||||
if (Modules.Select(module => module.Key).Distinct(StringComparer.OrdinalIgnoreCase).Count() != Modules.Count)
|
||||
throw new InvalidOperationException("设备模块 Key 重复");
|
||||
var owners = new Dictionary<string, IHealthDeviceModule>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var module in Modules)
|
||||
foreach (var parameter in module.Parameters)
|
||||
if (!owners.TryAdd(parameter.Key, module))
|
||||
throw new InvalidOperationException($"设备参数重复注册:{parameter.Key}");
|
||||
_parameterOwners = owners;
|
||||
}
|
||||
|
||||
public IHealthDeviceModule ForParameter(string parameterType)
|
||||
=> _parameterOwners.TryGetValue(parameterType, out var module)
|
||||
? module : throw new InvalidOperationException($"不支持的参数类型:{parameterType}");
|
||||
|
||||
public IHealthDeviceModule? ForEntities(IEnumerable<string> entityIds)
|
||||
{
|
||||
var ids = entityIds.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
return Modules.Select(module => new { Module = module,
|
||||
Score = module.EntityIds.Count(id => !string.IsNullOrWhiteSpace(id) && ids.Contains(id)) })
|
||||
.Where(item => item.Score > 0).OrderByDescending(item => item.Score)
|
||||
.Select(item => item.Module).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user