From d77d7848eec6a41d02bfab366eec88465a3854bb Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Mon, 5 Sep 2022 22:45:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93playload=20=E4=B8=BA=E7=A9=BA=E6=97=B6?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E6=8A=A5=E9=94=99=EF=BC=8C=20=E4=B9=9F?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E5=8F=91=E9=80=81=E7=A9=BA=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp/Extensions/DataExtension.cs | 2 +- IoTSharp/Handlers/MQTTServerHandler.cs | 27 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/IoTSharp/Extensions/DataExtension.cs b/IoTSharp/Extensions/DataExtension.cs index 13100dc3..c5e47d40 100644 --- a/IoTSharp/Extensions/DataExtension.cs +++ b/IoTSharp/Extensions/DataExtension.cs @@ -273,7 +273,7 @@ namespace IoTSharp.Extensions } public static Dictionary ConvertPayloadToDictionary(this MqttApplicationMessage msg) { - return JToken.Parse(msg.ConvertPayloadToString())?.JsonToDictionary(); + return JToken.Parse(msg.ConvertPayloadToString()??"{}")?.JsonToDictionary(); } public static Dictionary JsonToDictionary(this JToken jojb) diff --git a/IoTSharp/Handlers/MQTTServerHandler.cs b/IoTSharp/Handlers/MQTTServerHandler.cs index aa2d9ffa..bfd6422f 100644 --- a/IoTSharp/Handlers/MQTTServerHandler.cs +++ b/IoTSharp/Handlers/MQTTServerHandler.cs @@ -120,7 +120,10 @@ namespace IoTSharp.Handlers { try { - keyValues = e.ApplicationMessage.ConvertPayloadToDictionary(); + if (e.ApplicationMessage.Payload?.Length > 0) + { + keyValues = e.ApplicationMessage.ConvertPayloadToDictionary(); + } } catch (Exception ex) { @@ -129,17 +132,31 @@ namespace IoTSharp.Handlers } if (tpary[2] == "telemetry") { - _queue.PublishTelemetryData(new PlayloadData() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData }); + if (keyValues.Count > 0) + { + _queue.PublishTelemetryData(new PlayloadData() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData }); + } + else + { + _logger.LogWarning( $"空的遥测数据 {topic}, ClientId:{e.ClientId}"); + } } else if (tpary[2] == "attributes") { - if (tpary.Length > 3 && tpary[3] == "request") + if (keyValues.Count > 0) { - await RequestAttributes(tpary, clientid, e.ApplicationMessage.ConvertPayloadToDictionary(), device); + if (tpary.Length > 3 && tpary[3] == "request") + { + await RequestAttributes(tpary, clientid, e.ApplicationMessage.ConvertPayloadToDictionary(), device); + } + else + { + _queue.PublishAttributeData(new PlayloadData() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.AttributeData }); + } } else { - _queue.PublishAttributeData(new PlayloadData() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.AttributeData }); + _logger.LogWarning($"空的属性数据 {topic}, ClientId:{e.ClientId}"); } } else if (tpary[2] == "status" ) -- GitLab