解决IoTDB数据查询映射异常的问题

This commit is contained in:
ChenYi 2025-04-22 22:11:55 +08:00
parent 5647385582
commit 26f6796409
3 changed files with 45 additions and 20 deletions

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using Apache.IoTDB;
@ -195,7 +197,7 @@ namespace JiShe.CollectBus.IoTDB.Provider
{
try
{
var query =await BuildQuerySQL<T>(options);
var query = await BuildQuerySQL<T>(options);
var sessionDataSet = await CurrentSession.ExecuteQueryStatementAsync(query);
@ -205,10 +207,10 @@ namespace JiShe.CollectBus.IoTDB.Provider
Items = await ParseResults<T>(sessionDataSet, options.PageSize),
PageIndex = options.PageIndex,
PageSize = options.PageSize,
};
result.HasNext = result.TotalCount > 0? result.TotalCount < result.PageSize : false;
result.HasNext = result.TotalCount > 0 ? result.TotalCount < result.PageSize : false;
return result;
}
@ -415,7 +417,7 @@ namespace JiShe.CollectBus.IoTDB.Provider
private async Task<string> BuildQuerySQL<T>(IoTDBQueryOptions options) where T : IoTEntity
{
var metadata = await GetMetadata<T>();
var sb = new StringBuilder("SELECT TIME,");
var sb = new StringBuilder("SELECT TIME as Timestamps,");
sb.AppendJoin(", ", metadata.ColumnNames);
sb.Append($" FROM {options.TableNameOrTreePath}");
@ -472,7 +474,7 @@ namespace JiShe.CollectBus.IoTDB.Provider
{
return condition.Operator switch
{
">" => condition.IsNumber ? $"{condition.Field} > {condition.Value}": $"{condition.Field} > '{condition.Value}'",
">" => condition.IsNumber ? $"{condition.Field} > {condition.Value}" : $"{condition.Field} > '{condition.Value}'",
"<" => condition.IsNumber ? $"{condition.Field} < {condition.Value}" : $"{condition.Field} < '{condition.Value}'",
"=" => condition.IsNumber ? $"{condition.Field} = {condition.Value}" : $"{condition.Field} = '{condition.Value}'",
_ => throw new NotSupportedException($"{nameof(TranslateCondition)} 将查询条件转换为SQL语句时操作符 {condition.Operator} 属于异常情况")
@ -510,6 +512,8 @@ namespace JiShe.CollectBus.IoTDB.Provider
var metadata = await GetMetadata<T>();
var properties = typeof(T).GetProperties();
metadata.ColumnNames.Insert(0, "Timestamps");
metadata.DataTypes.Insert(0, TSDataType.TIMESTAMP);
while (dataSet.HasNext() && results.Count < pageSize)
{
@ -523,19 +527,21 @@ namespace JiShe.CollectBus.IoTDB.Provider
{
int indexOf = metadata.ColumnNames.IndexOf(measurement);
var value = record.Values[indexOf];
TSDataType tSDataType = metadata.DataTypes[indexOf];
var prop = properties.FirstOrDefault(p =>
p.Name.Equals(measurement, StringComparison.OrdinalIgnoreCase));
if (prop != null)
{
if (measurement.EndsWith("time"))
if (prop != null && !(value is System.DBNull))
{
dynamic tempValue = GetTSDataValue(tSDataType,value);
if (measurement.ToLower().EndsWith("time"))
{
var tempValue = TimestampHelper.ConvertToDateTime(Convert.ToInt64(value), TimestampUnit.Nanoseconds);
typeof(T).GetProperty(measurement)?.SetValue(entity, value);
typeof(T).GetProperty(measurement)?.SetValue(entity, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds));
}
else
{
typeof(T).GetProperty(measurement)?.SetValue(entity, value);
typeof(T).GetProperty(measurement)?.SetValue(entity, tempValue);
}
}
@ -760,5 +766,28 @@ namespace JiShe.CollectBus.IoTDB.Provider
["DECIMAL"] = "0.0",
["STRING"] = string.Empty
};
/// <summary>
/// IoTDB 数据类型与.net类型映射
/// </summary>
/// <param name="tSDataType"></param>
/// <param name="value"></param>
/// <returns></returns>
private dynamic GetTSDataValue(TSDataType tSDataType, object value) =>
tSDataType switch
{
TSDataType.BOOLEAN => Convert.ToBoolean(value),
TSDataType.INT32 => Convert.ToInt32(value),
TSDataType.INT64 => Convert.ToInt64(value),
TSDataType.FLOAT => Convert.ToDouble(value),
TSDataType.DOUBLE => Convert.ToDouble(value),
TSDataType.TEXT => Convert.ToString(value),
TSDataType.NONE => null,
TSDataType.TIMESTAMP => Convert.ToInt64(value),
TSDataType.DATE => Convert.ToDateTime(value),
TSDataType.BLOB => Convert.ToByte(value),
TSDataType.STRING => Convert.ToString(value),
_ => Convert.ToString(value)
};
}
}

View File

@ -6,6 +6,7 @@ using JiShe.CollectBus.Application.Contracts;
using JiShe.CollectBus.Common.BuildSendDatas;
using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.Common.DeviceBalanceControl;
using JiShe.CollectBus.Common.Encrypt;
using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Common.Helpers;
@ -712,7 +713,7 @@ namespace JiShe.CollectBus.ScheduledMeterReading
}
string taskMark = CommonHelper.GetTaskMark((int)aFN, fn, ammeterInfo.MeteringCode, builderResponse.MSA);
var meterReadingRecords = new MeterReadingTelemetryPacketInfo()
{
SystemName = SystemType,
@ -729,13 +730,14 @@ namespace JiShe.CollectBus.ScheduledMeterReading
//Seq = builderResponse.Seq,
MSA = builderResponse.MSA,
ItemCode = tempItem,
TaskMark = CommonHelper.GetTaskMark((int)aFN, fn, ammeterInfo.MeteringCode, builderResponse.MSA),
TaskMark = taskMark,
IsSend = false,
ManualOrNot = false,
Pn = ammeterInfo.MeteringCode,
IssuedMessageId = GuidGenerator.Create().ToString(),
IssuedMessageHexString = Convert.ToHexString(builderResponse.Data),
IsReceived = false,
ScoreValue = $"{ammeterInfo.FocusAddress}.{taskMark}".Md5Fun(),
};
taskList.Add(meterReadingRecords);

View File

@ -24,13 +24,7 @@ namespace JiShe.CollectBus.IotSystems.MeterReadingRecords
/// 排序索引分数值具体值可以根据不同业务场景进行定义例如时间戳、或者某一个固定的标识1
/// </summary>
[FIELDColumn]
public string ScoreValue
{
get
{
return $"{DeviceId}.{TaskMark}".Md5Fun();
}
}
public string ScoreValue { get; set; }
/// <summary>
/// 是否手动操作