解决IoTDB动态单侧点记录入库的问题。
This commit is contained in:
parent
085a67b20a
commit
99f96d889e
@ -20,7 +20,7 @@ namespace JiShe.CollectBus.IoTDBProvider
|
||||
/// <summary>
|
||||
/// 测量值集合,用于构建Table的测量值,也就是columnNames参数
|
||||
/// </summary>
|
||||
public List<string> ColumnNames { get; } = new();
|
||||
public List<string> ColumnNames { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 列类型集合,用于构建Table的列类型,也就是columnCategories参数
|
||||
|
||||
@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -143,27 +144,37 @@ namespace JiShe.CollectBus.IoTDBProvider
|
||||
var timestamps = new List<long>();
|
||||
var values = new List<List<object>>();
|
||||
var devicePaths = new HashSet<string>();
|
||||
List<string> tempColumnNames = new List<string>();
|
||||
tempColumnNames.AddRange(metadata.ColumnNames);
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
timestamps.Add(entity.Timestamps);
|
||||
var rowValues = new List<object>();
|
||||
foreach (var measurement in metadata.ColumnNames)
|
||||
foreach (var measurement in tempColumnNames)
|
||||
{
|
||||
|
||||
PropertyInfo propertyInfo = typeof(T).GetProperty(measurement);
|
||||
if (propertyInfo == null)
|
||||
{
|
||||
throw new Exception($"{nameof(BuildTablet)} 构建表模型{typeof(T).Name}时,没有找到{measurement}属性,属于异常情况。");
|
||||
throw new Exception($"{nameof(BuildTablet)} 构建表模型{typeof(T).Name}时,没有找到{measurement}属性,属于异常情况,-101。");
|
||||
}
|
||||
|
||||
var value = propertyInfo.GetValue(entity);
|
||||
if (propertyInfo.IsDefined(typeof(SingleMeasuringAttribute), false) && value != null)//表示当前对象是单测点模式,
|
||||
if (propertyInfo.IsDefined(typeof(SingleMeasuringAttribute), false) && value != null)//表示当前对象是单测点模式
|
||||
{
|
||||
Type tupleType = value.GetType();
|
||||
Type[] tupleArgs = tupleType.GetGenericArguments();
|
||||
Type item2Type = tupleArgs[1]; // T 的实际类型
|
||||
var item1 = tupleType.GetProperty("Item1")!.GetValue(value);
|
||||
var item2 = tupleType.GetProperty("Item2")!.GetValue(value);
|
||||
if (item1 == null || item2 == null)
|
||||
{
|
||||
throw new Exception($"{nameof(BuildTablet)} 构建表模型{typeof(T).Name}时,单测点模式构建失败,没有获取测点名称或者测点值,-102。");
|
||||
}
|
||||
|
||||
var indexOf = metadata.ColumnNames.IndexOf(measurement);
|
||||
metadata.ColumnNames[indexOf] = (string)item1!;
|
||||
|
||||
rowValues.Add(item2);
|
||||
|
||||
@ -389,12 +400,34 @@ namespace JiShe.CollectBus.IoTDBProvider
|
||||
/// <returns></returns>
|
||||
private DeviceMetadata GetMetadata<T>() where T : IoTEntity
|
||||
{
|
||||
return _metadataCache.GetOrAdd(typeof(T), type =>
|
||||
{
|
||||
var columns = CollectColumnMetadata(type);
|
||||
|
||||
var columns = CollectColumnMetadata(typeof(T));
|
||||
var metadata = BuildDeviceMetadata(columns);
|
||||
return metadata;
|
||||
});
|
||||
|
||||
return _metadataCache.AddOrUpdate(
|
||||
typeof(T),
|
||||
addValueFactory: t => metadata, // 如果键不存在,用此值添加
|
||||
updateValueFactory: (t, existingValue) =>
|
||||
{
|
||||
var columns = CollectColumnMetadata(t);
|
||||
var metadata = BuildDeviceMetadata(columns);
|
||||
|
||||
//对现有值 existingValue 进行修改,返回新值
|
||||
existingValue.ColumnNames = metadata.ColumnNames;
|
||||
return existingValue;
|
||||
}
|
||||
);
|
||||
|
||||
//return _metadataCache.GetOrAdd(typeof(T), type =>
|
||||
//{
|
||||
// var columns = CollectColumnMetadata(type);
|
||||
// var metadata = BuildDeviceMetadata(columns);
|
||||
// //if (metadata.IsSingleMeasuring)
|
||||
// //{
|
||||
// // _metadataCache.Remove(typeof(T));
|
||||
// //}
|
||||
// return metadata;
|
||||
//});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -434,7 +467,7 @@ namespace JiShe.CollectBus.IoTDBProvider
|
||||
//warning: 单侧点模式注意事项
|
||||
//Entity实体 字段类型是 Tuple<string,T>,Item1=>测点名称,Item2=>测点值,泛型
|
||||
//只有一个Filed字段。
|
||||
//MeasuringName 默认为 SingleMeasuringAttribute.FieldName。
|
||||
//MeasuringName 默认为 SingleMeasuringAttribute.FieldName,以便于在获取对应的Value的时候重置为 Item1 的值。
|
||||
|
||||
Type tupleType = prop.PropertyType;
|
||||
Type[] tupleArgs = tupleType.GetGenericArguments();
|
||||
|
||||
@ -54,6 +54,11 @@ namespace JiShe.CollectBus.IoTDBProvider.Provider
|
||||
public async Task<int> InsertAsync(Tablet tablet)
|
||||
{
|
||||
var result = await _sessionPool.InsertAlignedTabletAsync(tablet);
|
||||
if (result != 0)
|
||||
{
|
||||
throw new Exception($"{nameof(TableSessionPoolAdapter)} ");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user