mqtt test

This commit is contained in:
Mac mini git server 2026-03-22 14:31:52 +08:00
parent 55cce81912
commit 09098c020a
2 changed files with 63 additions and 3 deletions

View File

@ -32,11 +32,68 @@ namespace HHWMS.Web
try
{
// Parse your message.Payload here.
// For example, based on specific business logic.
if (!string.IsNullOrEmpty(message.Payload))
{
try
{
var jObj = System.Text.Json.Nodes.JsonNode.Parse(message.Payload) as System.Text.Json.Nodes.JsonObject;
if (jObj != null)
{
var parsedObj = new System.Text.Json.Nodes.JsonObject();
string[] fieldsToExtract = new[] {
"setSpindleSpeed", "setFeedRate", "spindleSpeedAndFeedRate",
"currentProduceCount", "expectProduceCount", "boottime",
"runtime", "cuttime", "statusInfo", "timestamp"
};
bool hasAnyField = false;
foreach (var field in fieldsToExtract)
{
if (jObj.ContainsKey(field))
{
// Clone the node to avoid "node already has a parent" exception
var nodeValue = jObj[field];
if (nodeValue != null)
{
var clonedNode = System.Text.Json.Nodes.JsonNode.Parse(nodeValue.ToJsonString());
parsedObj.Add(field, clonedNode);
}
else
{
parsedObj.Add(field, null);
}
hasAnyField = true;
}
}
if (hasAnyField)
{
message.ParsedData = parsedObj.ToJsonString();
message.ProcessResult = "Success (Parsed)";
}
else
{
message.ProcessResult = "Success (No matching fields)";
}
}
else
{
message.ProcessResult = "Success (Not JSON Object)";
}
}
catch (Exception parseEx)
{
message.ProcessResult = $"Parse JSON Error: {parseEx.Message}";
}
}
else
{
message.ProcessResult = "Success (Empty payload)";
}
message.IsProcessed = true;
message.ProcessTime = DateTime.Now;
message.ProcessResult = "Success";
_unitwork.Add(message);
}

View File

@ -30,5 +30,8 @@ namespace HHWMS.Web.Models
[Display(Name = "Mqtt.ProcessResult", Description = "处理结果")]
public string ProcessResult { get; set; }
[Display(Name = "Mqtt.ParsedData", Description = "解析后数据")]
public string ParsedData { get; set; }
}
}