From f73254e04fe308d7f87e9593e4e62baa1688c247 Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Wed, 14 May 2025 11:33:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96IoTDB=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=A4=84=E7=90=86=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=A7=94=E6=89=98=E8=BF=9B=E8=A1=8C=E5=AE=9E=E7=8E=B0=E5=80=BC?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E6=8B=BC=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Options/QueryCondition.cs | 43 +++++++++++++-- .../Provider/IoTDBProvider.cs | 10 ++-- .../Samples/SampleAppService.cs | 37 +++++++++++-- .../BasicScheduledMeterReadingService.cs | 53 ++++++++++--------- ...nergySystemScheduledMeterReadingService.cs | 39 +++++++++++++- ...ataInfo.cs => DeviceTableModelDataInfo.cs} | 2 +- .../Devices/DeviceTreeModelDataInfo.cs | 22 ++++++++ web/JiShe.CollectBus.Host/appsettings.json | 18 +++---- 8 files changed, 173 insertions(+), 51 deletions(-) rename services/JiShe.CollectBus.Domain/IotSystems/Devices/{DeviceDataInfo.cs => DeviceTableModelDataInfo.cs} (93%) create mode 100644 services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTreeModelDataInfo.cs diff --git a/modules/JiShe.CollectBus.IoTDB/Options/QueryCondition.cs b/modules/JiShe.CollectBus.IoTDB/Options/QueryCondition.cs index 40dd443..f5249b0 100644 --- a/modules/JiShe.CollectBus.IoTDB/Options/QueryCondition.cs +++ b/modules/JiShe.CollectBus.IoTDB/Options/QueryCondition.cs @@ -1,4 +1,7 @@ -namespace JiShe.CollectBus.IoTDB.Options +using JiShe.CollectBus.Common.Extensions; +using JiShe.CollectBus.Common.Helpers; + +namespace JiShe.CollectBus.IoTDB.Options { /// /// 查询条件 @@ -19,10 +22,44 @@ /// 是否数值,如果是数值,则进行数值比较,否则进行字符串比较 /// public bool IsNumber { get; set; } = false; - + + private object _rawValue; /// /// 值 /// - public object Value { get; set; } + public object Value + { + get => ApplyValueConversion(_rawValue); + set => _rawValue = value; + } + + /// + /// 值转换 + /// + /// + /// + private object ApplyValueConversion(object rawValue) + { + string declaredTypeName = rawValue.GetType().Name; + + Func converter = GetQueryConditionValue(declaredTypeName); + return converter(rawValue); + } + + /// + /// 查询条件值转换委托 + /// + /// + /// + private Func GetQueryConditionValue(string declaredTypeName) + { + return declaredTypeName?.ToUpper() switch + { + "DATETIME" => v => v != null ? ((DateTime)v).ToUniversalTime().Ticks : null, + "BOOLEAN" => v => v != null && (bool)v ? 1 : 0, + "STRING" => v => v != null ? $"'{v}'" : "''", + _ => v => v + }; + } } } diff --git a/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs b/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs index 791240d..a1a69a8 100644 --- a/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs +++ b/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs @@ -519,16 +519,16 @@ namespace JiShe.CollectBus.IoTDB.Provider /// /// private string TranslateCondition(QueryCondition condition) - { + { 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.Field} > {condition.Value}", + "<" => $"{condition.Field} < {condition.Value}", + "=" => $"{condition.Field} = {condition.Value}", _ => throw new NotSupportedException($"{nameof(TranslateCondition)} 将查询条件转换为SQL语句时操作符 {condition.Operator} 属于异常情况") }; } - + /// /// 获取查询条件的总数量 /// diff --git a/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs b/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs index 8d9ac21..14c7fea 100644 --- a/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs +++ b/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs @@ -1,4 +1,5 @@ -using FreeSql.Internal.CommonProvider; +using DeviceDetectorNET.Parser.Device; +using FreeSql.Internal.CommonProvider; using JiShe.CollectBus.Ammeters; using JiShe.CollectBus.Application.Contracts; using JiShe.CollectBus.Common.Consts; @@ -262,10 +263,7 @@ public class SampleAppService : CollectBusAppService, ISampleAppService, IKafkaS Timestamps = time.GetDateTimeOffset().ToUnixTimeMilliseconds(), SingleMeasuring = ("measuring", true) }; - - - - + QueryCondition conditions = new QueryCondition() { Field = "DeviceId", @@ -331,6 +329,35 @@ public class SampleAppService : CollectBusAppService, ISampleAppService, IKafkaS }; _dbContext.UseTableSessionPool = true; await _iotDBProvider.InsertAsync(meter); + + var meter3 = new TableModelSingleMeasuringEntity() + { + SystemName = "energy", + DeviceId = "402440506", + DeviceType = "Ammeter", + ProjectId = "10059", + Timestamps = time.GetDateTimeOffset().ToUnixTimeMilliseconds(), + SingleColumn = ("DeviceResult", true) + }; + _dbContext.UseTableSessionPool = true; + + QueryCondition conditions = new QueryCondition() + { + Field = "DeviceId", + Operator = "=", + Value = meter.DeviceId + }; + + + var query = new IoTDBQueryOptions() + { + TableNameOrTreePath = meter.DevicePath, + PageIndex = 1, + PageSize = 1, + Conditions = new List() { conditions }, + }; + + var pageResult = await _iotDBProvider.QueryAsync(query); } /// diff --git a/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs b/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs index 2991fe8..b7da236 100644 --- a/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs +++ b/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs @@ -323,7 +323,7 @@ namespace JiShe.CollectBus.ScheduledMeterReading /// public virtual async Task InitAmmeterCacheData(string gatherCode = "") { - //return; + return; try { @@ -332,34 +332,35 @@ namespace JiShe.CollectBus.ScheduledMeterReading _ = _dataChannelManage.ScheduledMeterTaskReadingAsync(DataChannelManage.TaskDataChannel.Reader); - //此处代码不要删除 -#if DEBUG - var redisCacheDeviceInfoHashKeyTemp = $"CollectBus:Energy:JiSheCollectBus2:DeviceInfo"; +// //此处代码不要删除 +//#if DEBUG +// var redisCacheDeviceInfoHashKeyTemp = $"CollectBus:Energy:JiSheCollectBus2:DeviceInfo"; - var timer1 = Stopwatch.StartNew(); - Dictionary> keyValuePairsTemps = FreeRedisProvider.Instance.HGetAll>(redisCacheDeviceInfoHashKeyTemp); - List meterInfos = new List(); - List focusAddressDataLista = new List(); - foreach (var item in keyValuePairsTemps) - { - foreach (var subItem in item.Value) - { - if (subItem.MeterType == MeterTypeEnum.Ammeter && subItem.TimeDensity == 15) - { - meterInfos.Add(subItem); - focusAddressDataLista.Add(subItem.MeterId.ToString()); - } - } - } +// var timer1 = Stopwatch.StartNew(); +// Dictionary> keyValuePairsTemps = FreeRedisProvider.Instance.HGetAll>(redisCacheDeviceInfoHashKeyTemp); +// List meterInfos = new List(); +// List focusAddressDataLista = new List(); +// foreach (var item in keyValuePairsTemps) +// { +// foreach (var subItem in item.Value) +// { +// if (subItem.MeterType == MeterTypeEnum.Ammeter && subItem.TimeDensity == 15) +// { +// meterInfos.Add(subItem); +// focusAddressDataLista.Add(subItem.MeterId.ToString()); +// } +// } +// } - timer1.Stop(); - _logger.LogError($"电表初始化读取数据总共花费时间{timer1.ElapsedMilliseconds}毫秒"); - DeviceGroupBalanceControl.InitializeCache(focusAddressDataLista, _kafkaOptions.NumPartitions); - return; -#else - var meterInfos = await GetAmmeterInfoList(gatherCode); -#endif +// timer1.Stop(); +// _logger.LogError($"电表初始化读取数据总共花费时间{timer1.ElapsedMilliseconds}毫秒"); +// DeviceGroupBalanceControl.InitializeCache(focusAddressDataLista, _kafkaOptions.NumPartitions); +// return; +//#else +// var meterInfos = await GetAmmeterInfoList(gatherCode); +//#endif + var meterInfos = await GetAmmeterInfoList(gatherCode); if (meterInfos == null || meterInfos.Count <= 0) { _logger.LogError($"{nameof(InitAmmeterCacheData)} 初始化电表缓存数据时,电表数据为空"); diff --git a/services/JiShe.CollectBus.Application/ScheduledMeterReading/EnergySystemScheduledMeterReadingService.cs b/services/JiShe.CollectBus.Application/ScheduledMeterReading/EnergySystemScheduledMeterReadingService.cs index 57eb3ec..562d2b1 100644 --- a/services/JiShe.CollectBus.Application/ScheduledMeterReading/EnergySystemScheduledMeterReadingService.cs +++ b/services/JiShe.CollectBus.Application/ScheduledMeterReading/EnergySystemScheduledMeterReadingService.cs @@ -98,8 +98,43 @@ namespace JiShe.CollectBus.ScheduledMeterReading //[Route($"ammeter/list")] public override async Task> GetAmmeterInfoList(string gatherCode = "V4-Gather-8890") { + //442400040 + //442400039 + + List ammeterInfos = new List(); + + ammeterInfos.Add(new DeviceInfo() + { + Baudrate = 2400, + FocusAddress = "442400040", + Name = "保利单箱电表1", + FocusId = 95778, + DatabaseBusiID = 1, + MeteringCode = 1, + MeterAddress = "442405000040", + MeterId = 127136, + TypeName = 3, + DataTypes = "449,503,581,582,583,584,585,586,587,588,589,590,591,592,593,594,597,598,599,600,601,602,603,604,605,606,607,608,661,663,677,679", + TimeDensity = 15, + BrandType = "DDS1980", + }); + + ammeterInfos.Add(new DeviceInfo() + { + Baudrate = 2400, + FocusAddress = "442400039", + Name = "保利单箱电表2", + FocusId = 95778, + DatabaseBusiID = 1, + MeteringCode = 1, + MeterAddress = "442405000039", + MeterId = 127236, + TypeName = 3, + DataTypes = "449,503,581,582,583,584,585,586,587,588,589,590,591,592,593,594,597,598,599,600,601,602,603,604,605,606,607,608,661,663,677,679", + TimeDensity = 15, + BrandType = "DDS1980", + }); - //List ammeterInfos = new List(); //ammeterInfos.Add(new DeviceInfo() //{ // Baudrate = 2400, @@ -132,7 +167,7 @@ namespace JiShe.CollectBus.ScheduledMeterReading // BrandType = "DDS1980", //}); - //return ammeterInfos; + return ammeterInfos; try { diff --git a/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceDataInfo.cs b/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTableModelDataInfo.cs similarity index 93% rename from services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceDataInfo.cs rename to services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTableModelDataInfo.cs index a3a0e21..4ced50b 100644 --- a/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceDataInfo.cs +++ b/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTableModelDataInfo.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace JiShe.CollectBus.IotSystems.Devices { /// - /// 设备树模型数据信息 + /// 设备表型数据信息 /// [SourceAnalyzers(EntityTypeEnum.TableModel)] public class DeviceTreeModelDataInfo: IoTEntity diff --git a/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTreeModelDataInfo.cs b/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTreeModelDataInfo.cs new file mode 100644 index 0000000..27a59c7 --- /dev/null +++ b/services/JiShe.CollectBus.Domain/IotSystems/Devices/DeviceTreeModelDataInfo.cs @@ -0,0 +1,22 @@ +using JiShe.CollectBus.Analyzers.Shared; +using JiShe.CollectBus.IoTDB.Attributes; +using JiShe.CollectBus.IoTDB.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.CollectBus.IotSystems.Devices +{ + /// + /// 设备树模型数据信息 + /// + [SourceAnalyzers(EntityTypeEnum.TableModel)] + public class DeviceTableModelDataInfo : IoTEntity + { + + [FIELDColumn] + public bool xfdsa { get; set; } + } +} diff --git a/web/JiShe.CollectBus.Host/appsettings.json b/web/JiShe.CollectBus.Host/appsettings.json index a3ed9de..3ed39cf 100644 --- a/web/JiShe.CollectBus.Host/appsettings.json +++ b/web/JiShe.CollectBus.Host/appsettings.json @@ -35,13 +35,13 @@ "CorsOrigins": "http://localhost:4200,http://localhost:3100" }, "ConnectionStrings": { - "Default": "mongodb://mongo_PmEeF3:lixiao1980@192.168.1.9:27017/JiSheCollectBus?authSource=admin&maxPoolSize=400&minPoolSize=10&waitQueueTimeoutMS=5000", - "Kafka": "192.168.1.9:29092,192.168.1.9:39092,192.168.1.9:49092", + "Default": "mongodb://mongo_PmEeF3:lixiao1980@192.168.5.9:27017/JiSheCollectBus?authSource=admin&maxPoolSize=400&minPoolSize=10&waitQueueTimeoutMS=5000", + "Kafka": "192.168.5.9:29092,192.168.5.9:39092,192.168.5.9:49092", "PrepayDB": "server=118.190.144.92;database=jishe.sysdb;uid=sa;pwd=admin@2023;Encrypt=False;Trust Server Certificate=False", - "EnergyDB": "server=rm-wz9hw529i3j1e3b5fbo.sqlserver.rds.aliyuncs.com,3433;database=db_energy;uid=yjdb;pwd=Kdjdhf+9*7ad222LL;Encrypt=False;Trust Server Certificate=False" + "EnergyDB": "server=118.190.144.92;database=db_energy;uid=sa;pwd=admin@2023;Encrypt=False;Trust Server Certificate=False" }, "Redis": { - "Configuration": "192.168.1.9:6380,password=1q2w3e!@#,syncTimeout=30000,abortConnect=false,connectTimeout=30000,allowAdmin=true", + "Configuration": "192.168.5.9:6380,password=1q2w3e!@#,syncTimeout=30000,abortConnect=false,connectTimeout=30000,allowAdmin=true", "MaxPoolSize": "50", "DefaultDB": "14", "HangfireDB": "13" @@ -71,7 +71,7 @@ } ], "Kafka": { - "BootstrapServers": "192.168.1.9:29092,192.168.1.9:39092,192.168.1.9:49092", + "BootstrapServers": "192.168.5.9:29092,192.168.5.9:39092,192.168.5.9:49092", "EnableFilter": true, "EnableAuthorization": false, "SecurityProtocol": "SaslPlaintext", @@ -85,7 +85,7 @@ "IoTDBOptions": { "UserName": "root", "Password": "root", - "ClusterList": [ "192.168.1.9:6667" ], + "ClusterList": [ "192.168.5.9:6667" ], "PoolSize": 32, "DataBaseName": "energy", "OpenDebugMode": false, @@ -103,19 +103,19 @@ }, "Nodes": [ { - "Host": "192.168.1.9", + "Host": "192.168.5.9", "Port": 9042, "DataCenter": "dc1", "Rack": "RAC1" }, { - "Host": "192.168.1.9", + "Host": "192.168.5.9", "Port": 9043, "DataCenter": "dc1", "Rack": "RAC2" }, { - "Host": "192.168.1.9", + "Host": "192.168.5.9", "Port": 9044, "DataCenter": "dc1", "Rack": "RAC2"