获取插件继承属性时,去除不重写特性的属性

This commit is contained in:
Kimdiego2098
2023-11-19 14:15:58 +08:00
parent 4f188ea6cc
commit d63c3aaa80
11 changed files with 25 additions and 45 deletions

View File

@@ -21,7 +21,7 @@ namespace ThingsGateway.Foundation.Extension.String;
public static class StringExtensions
{
/// <summary>
/// 转换布尔值
/// 转换布尔值,注意 10onoff truefalse 都会对应转换
/// </summary>
/// <param name="value"></param>
/// <returns></returns>

View File

@@ -267,7 +267,19 @@ public class DriverPluginService : ISingleton
}, true);
return data;
}
static T GetCustomAttributeRecursive<T>(PropertyInfo property) where T : Attribute
{
var attribute = property.GetCustomAttribute<T>(false);
if (attribute == null && property.DeclaringType.BaseType != null)
{
var baseProperty = property.DeclaringType.BaseType.GetProperties().FirstOrDefault(p => p.Name == property.Name);
if (baseProperty != null)
{
attribute = GetCustomAttributeRecursive<T>(baseProperty);
}
}
return attribute;
}
/// <summary>
/// 获取插件的属性值
/// </summary>
@@ -278,7 +290,7 @@ public class DriverPluginService : ISingleton
var data = _serviceScope.ServiceProvider.GetService<MemoryCache>().GetOrCreate($"{nameof(GetDriverProperties)}", cacheKey, c =>
{
var data = driver.DriverPropertys?.GetType().GetProperties().SelectMany(it =>
new[] { new { memberInfo = it, attribute = it.GetCustomAttribute<DevicePropertyAttribute>() } })
new[] { new { memberInfo = it, attribute = GetCustomAttributeRecursive<DevicePropertyAttribute>(it) } })
.Where(x => x.attribute != null).ToList()
.SelectMany(it => new[]
{
@@ -305,7 +317,7 @@ public class DriverPluginService : ISingleton
var data = _serviceScope.ServiceProvider.GetService<MemoryCache>().GetOrCreate($"{nameof(GetDriverVariableProperties)}", cacheKey, c =>
{
var data = driver.VariablePropertys?.GetType().GetProperties()?.SelectMany(it =>
new[] { new { memberInfo = it, attribute = it.GetCustomAttribute<VariablePropertyAttribute>() } })
new[] { new { memberInfo = it, attribute = GetCustomAttributeRecursive<DevicePropertyAttribute>(it) } })
?.Where(x => x.attribute != null).ToList()
?.SelectMany(it => new[]
{
@@ -380,7 +392,7 @@ public class DriverPluginService : ISingleton
/// <param name="deviceProperties">插件属性,检索相同名称的属性后写入</param>
public void SetDriverProperties(DriverBase driver, List<DependencyProperty> deviceProperties)
{
var pluginPropertys = driver.DriverPropertys?.GetType().GetProperties().Where(a => a.GetCustomAttribute(typeof(DevicePropertyAttribute)) != null)?.ToList();
var pluginPropertys = driver.DriverPropertys?.GetType().GetProperties().Where(a => GetCustomAttributeRecursive<DevicePropertyAttribute>(a) != null)?.ToList();
foreach (var propertyInfo in pluginPropertys ?? new())
{
var deviceProperty = deviceProperties.FirstOrDefault(x => x.PropertyName == propertyInfo.Name);

View File

@@ -17,7 +17,7 @@ using System.Text;
namespace ThingsGateway.Gateway.Application;
/// <summary>
/// <see cref="HistoryValue"/> Master规则
/// <see cref="Encoding"/> Master规则
/// </summary>
public class EncodingMapper : IRegister
{

View File

@@ -12,10 +12,8 @@
namespace ThingsGateway.Gateway.Core;
/// <summary>
/// 历史报警表
/// 报警变量
/// </summary>
[IgnoreSqlTable]
[SugarTable("historyAlarm", TableDescription = "历史报警表")]
public class AlarmVariable : PrimaryIdEntity
{
/// <inheritdoc cref="DeviceVariable.Name"/>

View File

@@ -59,7 +59,7 @@ public partial class QuestDB : UpLoadBaseWithCache<DeviceData, QuestDBHistoryVal
{
_config = new TypeAdapterConfig();
_config.ForType<DeviceVariableRunTime, QuestDBHistoryValue>()
.Map(dest => dest.Value, (src) => ValueReturn(src))
.Map(dest => dest.Value, src => src.Value == null ? string.Empty : src.Value.ToString() ?? string.Empty)
.Map(dest => dest.CollectTime, (src) => src.CollectTime < DateTime.MinValue ? DateTime.MinValue.ToUniversalTime() : src.CollectTime.ToUniversalTime())//注意sqlsugar插入时无时区直接utc时间
.Map(dest => dest.CreateTime, (src) => DateTime.UtcNow)
;//注意sqlsugar插入时无时区直接utc时间

View File

@@ -57,5 +57,5 @@ public class QuestDBHistoryValue
/// 变量值
/// </summary>
[Description("变量值")]
public double Value { get; set; }
public string Value { get; set; }
}

View File

@@ -22,7 +22,6 @@ using System.ComponentModel.DataAnnotations;
using System.Reflection;
using ThingsGateway.Foundation.Extension.Generic;
using ThingsGateway.Foundation.Extension.String;
using Yitter.IdGenerator;
@@ -39,18 +38,6 @@ public partial class QuestDB : UpLoadBaseWithCache<DeviceData, QuestDBHistoryVal
private readonly QuestDBVariableProperty _variablePropertys = new();
private volatile bool success = true;
private static object ValueReturn(DeviceVariableRunTime src)
{
var data = src.Value?.ToString()?.ToBool();
if (data != null)
{
return data;
}
else
{
return src.Value;
}
}
protected override void AddCache(List<CacheItem> cacheItems, IEnumerable<QuestDBHistoryValue> dev)
{

View File

@@ -58,5 +58,5 @@ public class TDHistoryValue : STable
/// </summary>
[Description("变量值")]
[SugarColumn(Length = 18, DecimalDigits = 2)]
public double Value { get; set; }
public string Value { get; set; }
}

View File

@@ -59,7 +59,7 @@ public partial class TDengineDB : UpLoadBaseWithCache<DeviceData, TDHistoryValue
{
_config = new TypeAdapterConfig();
_config.ForType<DeviceVariableRunTime, TDHistoryValue>()
.Map(dest => dest.Value, (src) => ValueReturn(src));
.Map(dest => dest.Value, src => src.Value == null ? string.Empty : src.Value.ToString() ?? string.Empty);
base.Init(client);
}

View File

@@ -22,7 +22,6 @@ using System.ComponentModel.DataAnnotations;
using System.Reflection;
using ThingsGateway.Foundation.Extension.Generic;
using ThingsGateway.Foundation.Extension.String;
using Yitter.IdGenerator;
@@ -39,19 +38,6 @@ public partial class TDengineDB : UpLoadBaseWithCache<DeviceData, TDHistoryValue
private readonly TDengineDBVariableProperty _variablePropertys = new();
private volatile bool success = true;
private static object ValueReturn(DeviceVariableRunTime src)
{
var data = src.Value?.ToString()?.ToBool();
if (data != null)
{
return data;
}
else
{
return src.Value;
}
}
protected override void AddCache(List<CacheItem> cacheItems, IEnumerable<TDHistoryValue> dev)
{
var data = dev.ChunkBetter(_driverPropertys.CacheItemCount);

View File

@@ -8,12 +8,9 @@
</PropertyGroup>
<ItemGroup>
<_WebToolingArtifacts Remove="Properties\PublishProfiles\linux64_net6.pubxml" />
<_WebToolingArtifacts Remove="Properties\PublishProfiles\linux64_net7.pubxml" />
<_WebToolingArtifacts Remove="Properties\PublishProfiles\win64_net6.pubxml" />
<_WebToolingArtifacts Remove="Properties\PublishProfiles\win64_net7.pubxml" />
<_WebToolingArtifacts Remove="Properties\PublishProfiles\linux64_net6.pubxml" />
<_WebToolingArtifacts Remove="Properties\PublishProfiles\linux64_net8.pubxml" />
</ItemGroup>
<ItemGroup>