From f036e9546d9b9bff0983e3d4594eff06235b6497 Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Fri, 11 Apr 2025 14:50:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8D=95=E4=BE=A7=E7=82=B9?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/SampleAppService.cs | 6 +-- .../AFNEntity/SingleMeasuringAFNDataEntity.cs | 4 +- .../Attribute/SingleMeasuringAttribute.cs | 2 +- .../Provider/IoTDBProvider.cs | 42 ++++++------------- .../Provider/SessionPoolAdapter.cs | 3 +- .../Provider/TableSessionPoolAdapter.cs | 8 +++- 6 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/JiShe.CollectBus.Application/Samples/SampleAppService.cs b/src/JiShe.CollectBus.Application/Samples/SampleAppService.cs index 551dc60..f243e05 100644 --- a/src/JiShe.CollectBus.Application/Samples/SampleAppService.cs +++ b/src/JiShe.CollectBus.Application/Samples/SampleAppService.cs @@ -128,16 +128,16 @@ public class SampleAppService : CollectBusAppService, ISampleAppService /// /// [HttpGet] - public async Task TestSingleMeasuringAFNData(string measuring, object value) + public async Task TestSingleMeasuringAFNData(string measuring, string value) { - SingleMeasuringAFNDataEntity meter = new SingleMeasuringAFNDataEntity() + var meter = new SingleMeasuringAFNDataEntity() { SystemName = "energy", DeviceId = "402440506", DeviceType = "Ammeter", ProjectCode = "10059", Timestamps = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), - SingleMeasuring = new Tuple(measuring, value) + SingleMeasuring = new Tuple(measuring, value) }; await _iotDBProvider.InsertAsync(meter); } diff --git a/src/JiShe.CollectBus.Domain/IotSystems/AFNEntity/SingleMeasuringAFNDataEntity.cs b/src/JiShe.CollectBus.Domain/IotSystems/AFNEntity/SingleMeasuringAFNDataEntity.cs index f53ca21..751e9e3 100644 --- a/src/JiShe.CollectBus.Domain/IotSystems/AFNEntity/SingleMeasuringAFNDataEntity.cs +++ b/src/JiShe.CollectBus.Domain/IotSystems/AFNEntity/SingleMeasuringAFNDataEntity.cs @@ -10,12 +10,12 @@ namespace JiShe.CollectBus.IotSystems.AFNEntity /// /// AFN单项数据实体 /// - public class SingleMeasuringAFNDataEntity:IoTEntity + public class SingleMeasuringAFNDataEntity : IoTEntity { /// /// 单项数据对象 /// [SingleMeasuring(nameof(SingleMeasuring))] - public Tuple SingleMeasuring { get; set; } + public Tuple SingleMeasuring { get; set; } } } diff --git a/src/JiShe.CollectBus.IoTDBProvider/Attribute/SingleMeasuringAttribute.cs b/src/JiShe.CollectBus.IoTDBProvider/Attribute/SingleMeasuringAttribute.cs index 9a85a85..ba5af69 100644 --- a/src/JiShe.CollectBus.IoTDBProvider/Attribute/SingleMeasuringAttribute.cs +++ b/src/JiShe.CollectBus.IoTDBProvider/Attribute/SingleMeasuringAttribute.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace JiShe.CollectBus.IoTDBProvider { /// - /// 用于标识当前实体为单侧点模式,单侧点模式只有一个Filed标识字段,类型是Tuple,Item1=>测点名称,Item2=>测点值 + /// 用于标识当前实体为单侧点模式,单侧点模式只有一个Filed标识字段,类型是Tuple,Item1=>测点名称,Item2=>测点值,泛型 /// [AttributeUsage(AttributeTargets.Property)] public class SingleMeasuringAttribute : Attribute diff --git a/src/JiShe.CollectBus.IoTDBProvider/Provider/IoTDBProvider.cs b/src/JiShe.CollectBus.IoTDBProvider/Provider/IoTDBProvider.cs index 297d250..acfab09 100644 --- a/src/JiShe.CollectBus.IoTDBProvider/Provider/IoTDBProvider.cs +++ b/src/JiShe.CollectBus.IoTDBProvider/Provider/IoTDBProvider.cs @@ -150,6 +150,7 @@ namespace JiShe.CollectBus.IoTDBProvider var rowValues = new List(); foreach (var measurement in metadata.ColumnNames) { + PropertyInfo propertyInfo = typeof(T).GetProperty(measurement); if (propertyInfo == null) { @@ -157,18 +158,14 @@ namespace JiShe.CollectBus.IoTDBProvider } var value = propertyInfo.GetValue(entity); - if (propertyInfo.IsDefined(typeof(SingleMeasuringAttribute), false))//表示当前对象是单测点模式, + if (propertyInfo.IsDefined(typeof(SingleMeasuringAttribute), false) && value != null)//表示当前对象是单测点模式, { - Tuple valueTuple = (Tuple)value!; - //获得当前Field对应的是的在measurement column中的序号,然后直接更新 metadata 中datetype对应序号的数据类型 - var indexOf = metadata.ColumnNames.IndexOf(measurement); - metadata.ColumnNames[indexOf] = valueTuple.Item1; - Type item2Type = valueTuple.Item2.GetType(); - metadata.DataTypes[indexOf] = GetDataTypeFromTypeName(valueTuple.Item2.GetType().Name); + Type tupleType = value.GetType(); + Type[] tupleArgs = tupleType.GetGenericArguments(); + Type item2Type = tupleArgs[1]; // T 的实际类型 + var item2 = tupleType.GetProperty("Item2")!.GetValue(value); - rowValues.Add(valueTuple.Item2); - - //TSDataType singleMeasuringTSDataType = + rowValues.Add(item2); } else @@ -435,34 +432,21 @@ namespace JiShe.CollectBus.IoTDBProvider if (singleMeasuringAttribute != null && column == null) { //warning: 单侧点模式注意事项 - //Entity实体 字段类型是 Tuple,Item1=>测点名称,Item2=>测点值 + //Entity实体 字段类型是 Tuple,Item1=>测点名称,Item2=>测点值,泛型 //只有一个Filed字段。 //MeasuringName 默认为 SingleMeasuringAttribute.FieldName。 - //DateTye 默认为 string,在获取对用的Value值的时候在进行重置。 + + Type tupleType = prop.PropertyType; + Type[] tupleArgs = tupleType.GetGenericArguments(); column = new ColumnInfo( singleMeasuringAttribute.FieldName, ColumnCategory.FIELD, - GetDataTypeFromTypeName(singleMeasuringAttribute.FieldName), + GetDataTypeFromTypeName(tupleArgs[1].Name), true ); } - - ////按优先级顺序检查属性,避免重复反射 - //column = prop.GetCustomAttribute() is not null ? new ColumnInfo( - // name: prop.Name, //使用属性名 - // category: ColumnCategory.TAG, - // dataType: GetDataTypeFromTypeName(prop.PropertyType.Name) - //) : prop.GetCustomAttribute() is not null ? new ColumnInfo( - // prop.Name, - // ColumnCategory.ATTRIBUTE, - // GetDataTypeFromTypeName(prop.PropertyType.Name) - //) : prop.GetCustomAttribute() is not null ? new ColumnInfo( - // prop.Name, - // ColumnCategory.FIELD, - // GetDataTypeFromTypeName(prop.PropertyType.Name) - //) : null; - + if (column.HasValue) { columns.Add(column.Value); diff --git a/src/JiShe.CollectBus.IoTDBProvider/Provider/SessionPoolAdapter.cs b/src/JiShe.CollectBus.IoTDBProvider/Provider/SessionPoolAdapter.cs index 81a69a9..006b1c3 100644 --- a/src/JiShe.CollectBus.IoTDBProvider/Provider/SessionPoolAdapter.cs +++ b/src/JiShe.CollectBus.IoTDBProvider/Provider/SessionPoolAdapter.cs @@ -53,7 +53,8 @@ namespace JiShe.CollectBus.IoTDBProvider.Provider /// public async Task InsertAsync(Tablet tablet) { - return await _sessionPool.InsertAlignedTabletAsync(tablet); + var result = await _sessionPool.InsertAlignedTabletAsync(tablet); + return result; } /// diff --git a/src/JiShe.CollectBus.IoTDBProvider/Provider/TableSessionPoolAdapter.cs b/src/JiShe.CollectBus.IoTDBProvider/Provider/TableSessionPoolAdapter.cs index 3676199..22de1b0 100644 --- a/src/JiShe.CollectBus.IoTDBProvider/Provider/TableSessionPoolAdapter.cs +++ b/src/JiShe.CollectBus.IoTDBProvider/Provider/TableSessionPoolAdapter.cs @@ -51,7 +51,13 @@ namespace JiShe.CollectBus.IoTDBProvider.Provider /// public async Task InsertAsync(Tablet tablet) { - return await _sessionPool.InsertAsync(tablet); + var result = await _sessionPool.InsertAsync(tablet); + if (result != 0) + { + throw new Exception($"{nameof(TableSessionPoolAdapter)} "); + } + + return result; } ///