71 lines
4.4 KiB
C#
71 lines
4.4 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace PLCSharp.Models;
|
|
|
|
public static class ProjectInspection
|
|
{
|
|
private static readonly string[] Kinds = ["Recipes", "Workflows", .. ProjectConfiguration.Groups.Values.SelectMany(t => t)];
|
|
|
|
public static JsonNode Query(ProjectDirectory project, string operation, JsonObject args)
|
|
{
|
|
if (operation == "scriptApi")
|
|
return WorkflowScriptApi.Read((string)args["topic"], args["workflowId"] == null ? null : Guid.Parse((string)args["workflowId"]));
|
|
if (operation == "references")
|
|
{
|
|
string type = (string)args["kind"], target = (string)args["id"];
|
|
var nested = type is "VisionLocalVariables" or "ControlMeasurements" or "InterpolationSegments" or "InterpolationPoints"
|
|
? project.DescribeMember(type, Guid.Parse(target), (string)args["segmentName"]) : null;
|
|
if (nested != null && string.IsNullOrEmpty((string)args["memberName"])) throw new ArgumentException("请指定子对象名称。");
|
|
return WorkflowReferences.Find(project, type, target, nested: nested, memberName: (string)args["memberName"]);
|
|
}
|
|
if (operation == "summary")
|
|
return JsonSerializer.SerializeToNode(new { project.Id, project.DefaultRecipeId, project.Root, source = "directory",
|
|
kinds = Kinds, nestedKinds = new { VisionLocalVariables = "VisionFunctions.VisionParams.Variables",
|
|
ControlMeasurements = "CustomControls.RangeValues", InterpolationSegments = "InterpolationGroups.Interpolations",
|
|
InterpolationPoints = "InterpolationGroups.Interpolations.SerializedInterpolations" }, recipes = project.Recipes.Select(r => new { r.ID, r.Name }) });
|
|
string kind = (string)args["kind"];
|
|
if (!Kinds.Contains(kind)) throw new ArgumentException("Unknown object kind.");
|
|
var rows = Rows(project, kind);
|
|
if (operation == "get")
|
|
{
|
|
var id = Guid.Parse((string)args["id"]);
|
|
return rows.SingleOrDefault(row => Guid.Parse((string)row["ID"]) == id)?.DeepClone()
|
|
?? throw new ArgumentException("Object not found.");
|
|
}
|
|
if (operation != "list") throw new ArgumentException("Unknown operation.");
|
|
if ((string)args["recipeId"] is string filter)
|
|
{
|
|
var recipe = Guid.Parse(filter);
|
|
rows = rows.Where(row => Guid.TryParse((string)row[kind == "Recipes" ? "ID" : "RecipeID"], out var owner) && owner == recipe);
|
|
}
|
|
int offset = (int?)args["offset"] ?? 0, limit = (int?)args["limit"] ?? 50;
|
|
if (offset < 0 || limit is < 1 or > 200) throw new ArgumentException("Invalid page bounds.");
|
|
var items = rows.Select(r => new { id = (string)r["ID"], name = (string)r["Name"], recipeId = (string)r["RecipeID"] }).ToArray();
|
|
return JsonSerializer.SerializeToNode(new { total = items.Length, items = items.Skip(offset).Take(limit) });
|
|
}
|
|
|
|
private static IEnumerable<JsonObject> Rows(ProjectDirectory p, string kind)
|
|
{
|
|
if (kind == "Recipes")
|
|
return p.Recipes.Select(r => JsonSerializer.SerializeToNode(new { r.ID, r.Name, r.Comment }).AsObject());
|
|
if (kind == "Workflows")
|
|
return p.Recipes.SelectMany(r => p.Flows(r.ID).Select(w => JsonSerializer.SerializeToNode(new
|
|
{ w.ID, RecipeID = r.ID, w.Name, w.Comment, w.Code, w.AutomaticExecution, w.CycleDelayTime }).AsObject()));
|
|
if (kind == "Variables")
|
|
return p.Data.Variables.ToList().Select(v => JsonSerializer.SerializeToNode(new { v.ID, v.RecipeID, v.Name,
|
|
v.Comment, v.Type, v.DefaultValueStr, v.ValueStr, v.RetainPersistent }).AsObject());
|
|
if (kind == "SystemVariables")
|
|
return p.Data.SystemVariables.ToList().Select(v => JsonSerializer.SerializeToNode(new { v.ID, v.RecipeID, v.Name,
|
|
v.Comment, v.Type, v.DefaultValueStr, v.ValueStr, v.RetainPersistent }).AsObject());
|
|
var table = ProjectConfiguration.Capture(p.Data, [kind], embedInputs: false)[kind].AsArray();
|
|
if (kind == "LocalImageDatas")
|
|
{
|
|
var owners = p.Data.VisionFunctions.Select(f => new { f.ID, f.RecipeID }).ToDictionary(f => f.ID, f => f.RecipeID);
|
|
foreach (var row in table)
|
|
if (owners.TryGetValue(Guid.Parse((string)row["FuncID"]), out var owner)) row["RecipeID"] = owner.ToString();
|
|
}
|
|
return table.Select(n => n.AsObject());
|
|
}
|
|
}
|