diff --git a/IoTSharp/Extensions/DataExtension.cs b/IoTSharp/Extensions/DataExtension.cs index 13100dc35e1fbf3af36a1c75c4e8be6217e81132..c5e47d407d576da5a2a93ada1c3c82a49e746057 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 aa2d9ffa2fb72c37b32c018b4461fb37a3ca4b1c..bfd6422ff0a622ffd7436c5bd841b5a02e13abb6 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" )