Skip to content

Commit

Permalink
Bug fixes for M470-M472
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishamm committed Dec 11, 2024
1 parent 79383ed commit ced44eb
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions src/DuetControlServer/Codes/Handlers/MCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -569,68 +569,72 @@ public static class MCodes

// Query object model
case 409:
if (code.TryGetInt('I', out int iVal) && iVal > 0)
{
if (code.TryGetInt('I', out int iVal) && iVal > 0)
return new Message(MessageType.Error, "M409 I1 is reserved for internal purposes only");
}

if (code.TryGetString('K', out string? key) && (!code.TryGetInt('R', out int rParam) || rParam == 0))
{
if (!key.TrimStart('#').StartsWith("network") && !key.TrimStart('#').StartsWith("volumes"))
{
return new Message(MessageType.Error, "M409 I1 is reserved for internal purposes only");
// Only return query results for network and volume keys as part of M409
break;
}

if (code.TryGetString('K', out string? key) && (!code.TryGetInt('R', out int rParam) || rParam == 0))
// Wait until pending codes have finished
if (!await Processor.FlushAsync(code))
{
if (!key.TrimStart('#').StartsWith("network") && !key.TrimStart('#').StartsWith("volumes"))
{
// Only return query results for network and volume keys as part of M409
break;
}
throw new OperationCanceledException();
}

// Retrieve filtered OM data. At present, flags are ignored
code.TryGetString('F', out string? flags);
using JsonDocument queryResult = JsonSerializer.SerializeToDocument(Filter.GetFiltered(key + ".**"), JsonHelper.DefaultJsonOptions);
// Retrieve filtered OM data. At present, flags are ignored
code.TryGetString('F', out string? flags);
using JsonDocument queryResult = JsonSerializer.SerializeToDocument(Filter.GetFiltered(key + ".**"), JsonHelper.DefaultJsonOptions);

// Get down to the requested depth
JsonElement result = queryResult.RootElement;
if (key is not null)
// Get down to the requested depth
JsonElement result = queryResult.RootElement;
if (key is not null)
{
foreach (string depth in key.Split('.'))
{
foreach (string depth in key.Split('.'))
if (result.ValueKind == JsonValueKind.Object)
{
if (result.ValueKind == JsonValueKind.Object)
foreach (var subItem in result.EnumerateObject())
{
foreach (var subItem in result.EnumerateObject())
{
result = subItem.Value;
break;
}
result = subItem.Value;
break;
}
}
}
}

// Generate final OM response
object finalResult;
if (result.ValueKind == JsonValueKind.Array)
{
finalResult = new
{
key,
flags = flags ?? string.Empty,
result,
next = 0
};
}
else
// Generate final OM response
object finalResult;
if (result.ValueKind == JsonValueKind.Array)
{
finalResult = new
{
finalResult = new
{
key,
flags = flags ?? string.Empty,
result
};
}
return new Message(MessageType.Success, JsonSerializer.Serialize(finalResult, JsonHelper.DefaultJsonOptions));
key,
flags = flags ?? string.Empty,
result,
next = 0
};
}
else
{
break;
finalResult = new
{
key,
flags = flags ?? string.Empty,
result
};
}
return new Message(MessageType.Success, JsonSerializer.Serialize(finalResult, JsonHelper.DefaultJsonOptions));
}
else
{
break;
}

// Create Directory on SD-Card
Expand All @@ -641,6 +645,7 @@ public static class MCodes
try
{
Directory.CreateDirectory(physicalPath);
return new Message();
}
catch (Exception e)
{
Expand Down Expand Up @@ -679,6 +684,7 @@ public static class MCodes
{
throw new FileNotFoundException();
}
return new Message();
}
catch (Exception e)
{
Expand All @@ -704,6 +710,7 @@ public static class MCodes
{
File.Delete(physicalPath);
}
return new Message();
}
catch (Exception e)
{
Expand Down

0 comments on commit ced44eb

Please sign in to comment.