diff --git a/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs b/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs
index 15179af..bad9528 100644
--- a/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs
+++ b/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs
@@ -1,4 +1,5 @@
-using Microsoft.CodeAnalysis;
+using JiShe.CollectBus.Analyzers.Shared;
+using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
@@ -40,18 +41,37 @@ namespace JiShe.CollectBus.IncrementalGenerator
private static ClassDeclarationSyntax GetClassDeclaration(GeneratorSyntaxContext context)
{
var classDecl = (ClassDeclarationSyntax)context.Node;
- var attributeType = context.SemanticModel.Compilation.GetTypeByMetadataName(AttributeFullName);
+ var semanticModel = context.SemanticModel;
- foreach (var attribute in classDecl.AttributeLists.SelectMany(al => al.Attributes))
+ // 获取类符号
+ var classSymbol = semanticModel.GetDeclaredSymbol(classDecl) as INamedTypeSymbol;
+ if (classSymbol == null) return null;
+
+ // 检查是否包含 SourceAnalyzers 特性
+ var sourceAnalyzerAttr = classSymbol.GetAttributes().FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString() == AttributeFullName);
+
+ // 必须包含 EntityType 参数
+ if (sourceAnalyzerAttr == null ||
+ sourceAnalyzerAttr.ConstructorArguments.Length == 0)
{
- var symbol = context.SemanticModel.GetSymbolInfo(attribute).Symbol;
- if (symbol is IMethodSymbol ctor &&
- SymbolEqualityComparer.Default.Equals(ctor.ContainingType, attributeType))
- {
- return classDecl;
- }
+ return null;
}
- return null;
+
+ return classDecl;
+
+ //var classDecl = (ClassDeclarationSyntax)context.Node;
+ //var attributeType = context.SemanticModel.Compilation.GetTypeByMetadataName(AttributeFullName);
+
+ //foreach (var attribute in classDecl.AttributeLists.SelectMany(al => al.Attributes))
+ //{
+ // var symbol = context.SemanticModel.GetSymbolInfo(attribute).Symbol;
+ // if (symbol is IMethodSymbol ctor &&
+ // SymbolEqualityComparer.Default.Equals(ctor.ContainingType, attributeType))
+ // {
+ // return classDecl;
+ // }
+ //}
+ //return null;
}
///
@@ -154,6 +174,25 @@ namespace JiShe.CollectBus.IncrementalGenerator
Compilation compilation,
HashSet processedTypes)
{
+ // 获取 SourceAnalyzers 特性的 EntityType 参数
+ var sourceAnalyzerAttr = classSymbol.GetAttributes()
+ .FirstOrDefault(attr =>
+ attr.AttributeClass?.ToDisplayString() == AttributeFullName);
+
+ // 解析 EntityType 枚举值
+ string entityTypeValue = "EntityTypeEnum.Other"; // 默认值
+ if (sourceAnalyzerAttr != null &&
+ sourceAnalyzerAttr.ConstructorArguments.Length > 0)
+ {
+ var arg = sourceAnalyzerAttr.ConstructorArguments[0];
+ if (arg.Kind == TypedConstantKind.Enum &&
+ arg.Type is INamedTypeSymbol enumType)
+ {
+ int enumValue = (int)arg.Value!;
+ entityTypeValue = GetEnumMemberName(enumType, enumValue);
+ }
+ }
+
var code = new StringBuilder();
code.AppendLine("// ");
code.AppendLine("#nullable enable");
@@ -185,12 +224,14 @@ namespace JiShe.CollectBus.IncrementalGenerator
//类名称
code.AppendLine($" public string EntityName {{get;}} = \"{classSymbol.Name}\";");
-
+ // 添加 EntityType 属性
+ code.AppendLine($" public EntityTypeEnum? EntityType {{ get; }} = {entityTypeValue};");
+
foreach (var prop in propList)
{
// 安全类型转换
if (prop.Type is not ITypeSymbol propType) continue;
-
+
if (propType is INamedTypeSymbol namedType)
{
GenerateStandardAccessors(prop, namedType, code);
@@ -262,7 +303,7 @@ namespace JiShe.CollectBus.IncrementalGenerator
: $"obj.{propName}.{elements[i].Name}";
}
}
-
+
///
/// 处理System.Tuple类型的访问器生成
///
@@ -300,7 +341,7 @@ namespace JiShe.CollectBus.IncrementalGenerator
}
}
}
-
+
///
/// 增强的工厂类实现
///
@@ -381,7 +422,7 @@ namespace JiShe.CollectBus.IncrementalGenerator
$"Get{prop.Name}_{element.Name}(targetEntity),");
}
}
-
+
}
code.AppendLine(" _ => throw new ArgumentException($\"Unknown property: {propertyName}\")");
@@ -425,7 +466,7 @@ namespace JiShe.CollectBus.IncrementalGenerator
code.AppendLine($" targetEntity, ({elementType})value);");
code.AppendLine(" break;");
}
- }
+ }
}
code.AppendLine(" default:");
@@ -475,10 +516,10 @@ namespace JiShe.CollectBus.IncrementalGenerator
/// 生成当前类属性信息集合
///
private static void GenerateEntityMemberInfoList(
- IEnumerable propList,
- StringBuilder code,
- Compilation compilation,
- INamedTypeSymbol classSymbol)
+ IEnumerable propList,
+ StringBuilder code,
+ Compilation compilation,
+ INamedTypeSymbol classSymbol)
{
code.AppendLine(" public List MemberList { get; } = new()");
code.AppendLine(" {");
@@ -487,8 +528,10 @@ namespace JiShe.CollectBus.IncrementalGenerator
foreach (var prop in propList)
{
- var propType = prop.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
- var parentType = prop.ContainingType.ToDisplayString();
+ var entityType = prop.ContainingType.ToDisplayString();//entity 实体类型名称
+ var propType = prop.Type;//实体属性的类型
+ var propTypeName = propType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
+ var declaredTypeName = propType.Name; // 直接获取类型名称(如 "Int32")
// 处理主属性
var propAttributes = prop.GetAttributes()
@@ -503,9 +546,10 @@ namespace JiShe.CollectBus.IncrementalGenerator
mainMember.Append(
$"new EntityMemberInfo(" +
$"\"{prop.Name}\", " +
- $"typeof({propType}), " +
- $"(e) => Get{prop.Name}(({parentType})e), " +
- $"(e, v) => Set{prop.Name}(({parentType})e, ({propType})v))");
+ $"typeof({propTypeName}), " +
+ $"\"{declaredTypeName}\", " +
+ $"(e) => Get{prop.Name}(({entityType})e), " +
+ $"(e, v) => Set{prop.Name}(({entityType})e, ({propTypeName})v))");
if (attributeInitializers.Any())
{
@@ -516,20 +560,22 @@ namespace JiShe.CollectBus.IncrementalGenerator
initializerLines.Add(mainMember.ToString());
- // 处理元组元素(假设不需要处理元组元素的特性)
+ // 处理元组元素,(暂不需要处理元组元素的特性)
if (prop.Type is INamedTypeSymbol { IsTupleType: true } tupleType)
{
foreach (var element in tupleType.TupleElements)
{
- var elementType = element.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
- var elementName = element.Name;
+ var elementType = element.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);//元组元素的类型
+ var elementName = element.Name;//元组元素名称
+ var elementDeclaredName = element.Type.Name;//元组元素类型名称
initializerLines.Add(
- $"new EntityMemberInfo(" +
- $"\"{prop.Name}.{elementName}\", " +
- $"typeof({elementType}), " +
- $"(e) => Get{prop.Name}_{elementName}(({parentType})e), " +
- $"(e, v) => Set{prop.Name}_{elementName}(({parentType})e, ({elementType})v))");
+ $"new EntityMemberInfo(" +
+ $"\"{prop.Name}.{elementName}\", " +
+ $"typeof({elementType}), " +
+ $"\"{elementDeclaredName}\", " +
+ $"(e) => Get{prop.Name}_{elementName}(({entityType})e), " +
+ $"(e, v) => Set{prop.Name}_{elementName}(({entityType})e, ({elementType})v))");
}
}
}
@@ -621,5 +667,23 @@ namespace JiShe.CollectBus.IncrementalGenerator
{
return attribute.AttributeClass?.ToDisplayString() == "System.Runtime.CompilerServices.CompilerGeneratedAttribute";
}
+
+ ///
+ /// 获取枚举的参数
+ ///
+ ///
+ ///
+ ///
+ private static string GetEnumMemberName(INamedTypeSymbol enumType, int value)
+ {
+ foreach (var member in enumType.GetMembers().OfType())
+ {
+ if (member.ConstantValue is int intValue && intValue == value)
+ {
+ return $"{enumType.ToDisplayString()}.{member.Name}";
+ }
+ }
+ return $"{enumType.ToDisplayString()}.Other";
+ }
}
}
\ No newline at end of file
diff --git a/modules/JiShe.CollectBus.IoTDB/Attributes/EntityTypeAttribute.cs b/modules/JiShe.CollectBus.IoTDB/Attributes/EntityTypeAttribute.cs
deleted file mode 100644
index 89a4e38..0000000
--- a/modules/JiShe.CollectBus.IoTDB/Attributes/EntityTypeAttribute.cs
+++ /dev/null
@@ -1,19 +0,0 @@
-using JiShe.CollectBus.IoTDB.Enums;
-
-namespace JiShe.CollectBus.IoTDB.Attributes
-{
- ///
- /// IoTDB实体类型特性
- ///
- [AttributeUsage(AttributeTargets.Class)]
- public class EntityTypeAttribute : System.Attribute
- {
- public EntityTypeEnum EntityType { get; }
-
-
- public EntityTypeAttribute(EntityTypeEnum entityType)
- {
- EntityType = entityType;
- }
- }
-}
diff --git a/modules/JiShe.CollectBus.IoTDB/Attributes/TableNameOrTreePathAttribute.cs b/modules/JiShe.CollectBus.IoTDB/Attributes/TableNameOrTreePathAttribute.cs
index ba0ca12..5f986b5 100644
--- a/modules/JiShe.CollectBus.IoTDB/Attributes/TableNameOrTreePathAttribute.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Attributes/TableNameOrTreePathAttribute.cs
@@ -1,5 +1,4 @@
-using JiShe.CollectBus.IoTDB.Enums;
-
+
namespace JiShe.CollectBus.IoTDB.Attributes
{
///
diff --git a/modules/JiShe.CollectBus.IoTDB/Model/Class1.cs b/modules/JiShe.CollectBus.IoTDB/Model/Class1.cs
new file mode 100644
index 0000000..9bcd5ff
--- /dev/null
+++ b/modules/JiShe.CollectBus.IoTDB/Model/Class1.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace JiShe.CollectBus.IoTDB.Model
+{
+ internal class Class1
+ {
+ }
+}
diff --git a/modules/JiShe.CollectBus.IoTDB/Model/IoTEntity.cs b/modules/JiShe.CollectBus.IoTDB/Model/IoTEntity.cs
index 40403ed..f2d55a5 100644
--- a/modules/JiShe.CollectBus.IoTDB/Model/IoTEntity.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Model/IoTEntity.cs
@@ -1,4 +1,5 @@
using JiShe.CollectBus.Common.Attributes;
+using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.IoTDB.Attributes;
namespace JiShe.CollectBus.IoTDB.Model
@@ -20,6 +21,12 @@ namespace JiShe.CollectBus.IoTDB.Model
[TAGColumn]
public string ProjectId { get; set; }
+ ///
+ /// 数据类型
+ ///
+ [TAGColumn]
+ public string DataType { get; set; } = IOTDBDataTypeConst.Data;
+
///
/// 设备类型集中器、电表、水表、流量计、传感器等
///
diff --git a/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntity.cs b/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntity.cs
index a5c98cf..376b677 100644
--- a/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntity.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntity.cs
@@ -1,14 +1,12 @@
using JiShe.CollectBus.Analyzers.Shared;
-using JiShe.CollectBus.IoTDB.Attributes;
-using JiShe.CollectBus.IoTDB.Enums;
+using JiShe.CollectBus.IoTDB.Attributes;
namespace JiShe.CollectBus.IoTDB.Model
{
///
/// Table模型单项数据实体
- ///
- [EntityType(EntityTypeEnum.TableModel)]
- [SourceAnalyzers]
+ ///
+ [SourceAnalyzers(EntityTypeEnum.TableModel)]
public class TableModelSingleMeasuringEntity : IoTEntity
{
///
diff --git a/modules/JiShe.CollectBus.IoTDB/Model/TreeModelSingleMeasuringEntity.cs b/modules/JiShe.CollectBus.IoTDB/Model/TreeModelSingleMeasuringEntity.cs
index 6244cdf..56a6c54 100644
--- a/modules/JiShe.CollectBus.IoTDB/Model/TreeModelSingleMeasuringEntity.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Model/TreeModelSingleMeasuringEntity.cs
@@ -1,14 +1,12 @@
using JiShe.CollectBus.Analyzers.Shared;
-using JiShe.CollectBus.IoTDB.Attributes;
-using JiShe.CollectBus.IoTDB.Enums;
+using JiShe.CollectBus.IoTDB.Attributes;
namespace JiShe.CollectBus.IoTDB.Model
{
///
/// Tree模型单项数据实体
///
- [EntityType(EntityTypeEnum.TreeModel)]
- [SourceAnalyzers]
+ [SourceAnalyzers(EntityTypeEnum.TreeModel)]
public class TreeModelSingleMeasuringEntity : IoTEntity
{
///
diff --git a/modules/JiShe.CollectBus.IoTDB/Provider/DeviceMetadata.cs b/modules/JiShe.CollectBus.IoTDB/Provider/DeviceMetadata.cs
index a093bb7..f48d218 100644
--- a/modules/JiShe.CollectBus.IoTDB/Provider/DeviceMetadata.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Provider/DeviceMetadata.cs
@@ -1,5 +1,5 @@
using Apache.IoTDB;
-using JiShe.CollectBus.IoTDB.Enums;
+using JiShe.CollectBus.Analyzers.Shared;
namespace JiShe.CollectBus.IoTDB.Provider
{
@@ -9,9 +9,9 @@ namespace JiShe.CollectBus.IoTDB.Provider
public class DeviceMetadata
{
///
- /// IoTDB实体类型枚举
+ /// 实体类型枚举
///
- public EntityTypeEnum EntityType { get; set; }
+ public EntityTypeEnum? EntityType { get; set; }
///
/// 是否有单测量值
diff --git a/modules/JiShe.CollectBus.IoTDB/Provider/DevicePathBuilder.cs b/modules/JiShe.CollectBus.IoTDB/Provider/DevicePathBuilder.cs
index 6a1a596..57e1b09 100644
--- a/modules/JiShe.CollectBus.IoTDB/Provider/DevicePathBuilder.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Provider/DevicePathBuilder.cs
@@ -15,7 +15,7 @@ namespace JiShe.CollectBus.IoTDB.Provider
///
public static string GetDevicePath(T entity) where T : IoTEntity
{
- return $"root.{entity.SystemName.ToLower()}.`{entity.ProjectId}`.`{entity.DeviceType}`.`{entity.DeviceId}`";
+ return $"root.{entity.SystemName.ToLower()}.`{entity.ProjectId}`.`{entity.DeviceType}`.{entity.DataType}.`{entity.DeviceId}`";
}
diff --git a/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs b/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs
index c88403f..bacbf61 100644
--- a/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs
+++ b/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs
@@ -195,6 +195,8 @@ namespace JiShe.CollectBus.IoTDB.Provider
}
);
+ metadata.EntityType = accessor.EntityType;
+
return await Task.FromResult(metaData);
}
@@ -262,26 +264,31 @@ namespace JiShe.CollectBus.IoTDB.Provider
var devicePaths = new HashSet();
List tempColumnNames = new List();
tempColumnNames.AddRange(metadata.ColumnNames);
-
+
var accessor = SourceEntityAccessorFactory.GetAccessor();
- var entityTypeAttribute = typeof(T).GetCustomAttribute();
+ var memberCache = new Dictionary(); // 缓存优化查询
+ // 预构建成员缓存(Key: NameOrPath)
+ foreach (var member in accessor.MemberList)
+ {
+ memberCache[member.NameOrPath] = member;
+ }
- if (entityTypeAttribute == null)
+ if (accessor.EntityType == null || metadata.EntityType == null)
{
throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 {nameof(T)}的EntityType 没有指定,属于异常情况,-101");
}
- if (metadata.EntityType != entityTypeAttribute.EntityType)
+ if (metadata.EntityType != accessor.EntityType)
{
throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 {nameof(T)}的EntityType 和{nameof(DeviceMetadata)}的 EntityType 不一致,属于异常情况,-102");
}
- if (metadata.EntityType == Enums.EntityTypeEnum.TreeModel && _runtimeContext.UseTableSessionPool == true)
+ if (metadata.EntityType == EntityTypeEnum.TreeModel && _runtimeContext.UseTableSessionPool == true)
{
throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 tree模型不能使用table模型Session连接,属于异常情况,-103");
}
- else if (metadata.EntityType == Enums.EntityTypeEnum.TableModel && _runtimeContext.UseTableSessionPool == false)
+ else if (metadata.EntityType == EntityTypeEnum.TableModel && _runtimeContext.UseTableSessionPool == false)
{
throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 table模型不能使用tree模型Session连接,属于异常情况,-104");
}
@@ -299,13 +306,31 @@ namespace JiShe.CollectBus.IoTDB.Provider
var rowValues = new List
string EntityName { get; }
+ ///
+ /// 实体类型
+ ///
+ EntityTypeEnum? EntityType { get;}
+
///
/// 获取属性值
///
diff --git a/shared/JiShe.CollectBus.Analyzers.Shared/SourceAnalyzersAttribute.cs b/shared/JiShe.CollectBus.Analyzers.Shared/SourceAnalyzersAttribute.cs
index 0b33a6a..ad02607 100644
--- a/shared/JiShe.CollectBus.Analyzers.Shared/SourceAnalyzersAttribute.cs
+++ b/shared/JiShe.CollectBus.Analyzers.Shared/SourceAnalyzersAttribute.cs
@@ -8,5 +8,12 @@ namespace JiShe.CollectBus.Analyzers.Shared
[AttributeUsage(AttributeTargets.Class)]
public class SourceAnalyzersAttribute : Attribute
{
+ public EntityTypeEnum EntityType { get; }
+
+
+ public SourceAnalyzersAttribute(EntityTypeEnum entityType)
+ {
+ EntityType = entityType;
+ }
}
}
diff --git a/shared/JiShe.CollectBus.Common/Consts/IOTDBDataTypeConst.cs b/shared/JiShe.CollectBus.Common/Consts/IOTDBDataTypeConst.cs
new file mode 100644
index 0000000..7c076c9
--- /dev/null
+++ b/shared/JiShe.CollectBus.Common/Consts/IOTDBDataTypeConst.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace JiShe.CollectBus.Common.Consts
+{
+ ///
+ /// IOTDB数据树类型
+ ///
+ public class IOTDBDataTypeConst
+ {
+ ///
+ /// 数据
+ ///
+ public const string Data = "Data";
+
+ ///
+ /// 事件
+ ///
+ public const string Event = "Event";
+
+ ///
+ /// 状态
+ ///
+ public const string Status = "Status";
+ }
+}
diff --git a/shared/JiShe.CollectBus.Common/Consts/T37612012PacketItemCodeConst.cs b/shared/JiShe.CollectBus.Common/Consts/T37612012PacketItemCodeConst.cs
index 5123fab..1d93ff8 100644
--- a/shared/JiShe.CollectBus.Common/Consts/T37612012PacketItemCodeConst.cs
+++ b/shared/JiShe.CollectBus.Common/Consts/T37612012PacketItemCodeConst.cs
@@ -151,27 +151,7 @@ namespace JiShe.CollectBus.Common.Consts
public const string Ic = "0C_49_Ic"; // 当前电压、电流相位角
}
-
- ///
- /// IOTDB数据树类型
- ///
- public class IOTDBDataType
- {
- ///
- /// 数据
- ///
- public const string Data = "Data";
-
- ///
- /// 事件
- ///
- public const string Event = "Event";
-
- ///
- /// 状态
- ///
- public const string Status = "Status";
- }
+
///
/// 集中器状态字段