From 849ac9cbc6711577641b9ecd6e415b881d7d5583 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Wed, 12 Aug 2020 21:02:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E7=A9=BA=E5=80=BC?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp/Extensions/DataExtension.cs | 8 +++++--- IoTSharp/Storage/EFStorage.cs | 9 ++++++--- IoTSharp/Storage/ShardingStorage.cs | 9 ++++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/IoTSharp/Extensions/DataExtension.cs b/IoTSharp/Extensions/DataExtension.cs index 83026e83..5dc5add9 100644 --- a/IoTSharp/Extensions/DataExtension.cs +++ b/IoTSharp/Extensions/DataExtension.cs @@ -57,7 +57,7 @@ namespace IoTSharp.Extensions { try { - if (kp.Key != null) + if (kp.Key != null && kp.Value !=null) { var tl = from tx in devdata where tx.KeyName == kp.Key && tx.DataSide == dataSide select tx; if (tl.Any()) @@ -74,6 +74,10 @@ namespace IoTSharp.Extensions t2.FillKVToMe(kp); } } + else + { + exceptions.Add($"Key:{ kp.Key}","Key is null or value is null"); + } } catch (Exception ex) { @@ -123,8 +127,6 @@ namespace IoTSharp.Extensions } internal static void FillKVToMe(this T tdata, KeyValuePair kp) where T : IDataStorage { - - switch (Type.GetTypeCode(kp.Value.GetType())) { case TypeCode.Boolean: diff --git a/IoTSharp/Storage/EFStorage.cs b/IoTSharp/Storage/EFStorage.cs index e372e6b3..6dc2c193 100644 --- a/IoTSharp/Storage/EFStorage.cs +++ b/IoTSharp/Storage/EFStorage.cs @@ -91,9 +91,12 @@ namespace IoTSharp.Storage { msg.MsgBody.ToList().ForEach(kp => { - var tdata = new TelemetryData() { DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1) }; - tdata.FillKVToMe(kp); - _dbContext.Set().Add(tdata); + if (kp.Value != null) + { + var tdata = new TelemetryData() { DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1) }; + tdata.FillKVToMe(kp); + _dbContext.Set().Add(tdata); + } }); var result1 = await _dbContext.SaveAsync(msg.MsgBody, msg.DeviceId, msg.DataSide); result1.exceptions?.ToList().ForEach(ex => diff --git a/IoTSharp/Storage/ShardingStorage.cs b/IoTSharp/Storage/ShardingStorage.cs index a6cd0132..c32c5a05 100644 --- a/IoTSharp/Storage/ShardingStorage.cs +++ b/IoTSharp/Storage/ShardingStorage.cs @@ -104,9 +104,12 @@ namespace IoTSharp.Storage var lst = new List(); msg.MsgBody.ToList().ForEach(kp => { - var tdata = new TelemetryData() { DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1) }; - tdata.FillKVToMe(kp); - lst.Add(tdata); + if (kp.Value != null) + { + var tdata = new TelemetryData() { DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1) }; + tdata.FillKVToMe(kp); + lst.Add(tdata); + } }); int ret = db.Insert(lst); _logger.LogInformation($"新增({msg.DeviceId})遥测数据{ret}"); -- GitLab