plcsharp/PLCSharpWpf/Models/IoRename.cs

79 lines
4.1 KiB
C#

using PLCSharp.VVMs.Cylinders;
using PLCSharp.VVMs.MotionController;
using System.Text.Json.Nodes;
namespace PLCSharp.Models;
public sealed partial class ProjectDirectory
{
private static string[] CylinderFields(string kind) => kind switch
{
"DI" => ["InA", "InB"], "DQ" => ["OutA", "OutB"],
_ => throw new ArgumentException("请选择 DI 或 DQ。")
};
private void CheckHardwareName(string kind, Guid id, string before, string after, bool inverse)
{
if ((!inverse && string.IsNullOrWhiteSpace(after)) || after != after?.Trim() || before == after)
throw new ArgumentException("请输入不同的新名称,首尾不能有空白。");
var rows = kind switch
{
"DI" => Data.DI.Select(p => new { p.ID, p.Name }),
"DQ" => Data.DQ.Select(p => new { p.ID, p.Name }),
"Robots" => Data.Robots.Select(p => new { p.ID, p.Name }),
"Cylinders" => Data.Cylinders.Select(p => new { p.ID, p.Name }),
"Connects" => Data.Connects.Select(p => new { p.ID, p.Name }),
_ => throw new ArgumentException("未知硬件类型。")
};
if (!rows.Any(p => p.ID == id && p.Name == before) || rows.Any(p => p.ID != id &&
((before != null && before != "" && p.Name == before) || (after != null && after != "" && p.Name == after))))
throw new ProjectConflictException("设备已更改或同类设备存在重名。");
}
internal ProjectRename PlanIoRename(string kind, Guid id, string newName)
{
var fields = CylinderFields(kind);
CheckSavedRecipes();
var hardware = JsonNode.Parse(baseline[ConfigPath("hardware")]);
var old = (string)new PointField(kind, id, "Name").Row(hardware)["Name"];
CheckHardwareName(kind, id, old, newName, false);
var links = new List<PointField>();
foreach (var row in hardware["Cylinders"].AsArray())
foreach (var field in fields)
{
var name = (string)row[field];
if (name == newName) throw new ProjectConflictException($"气缸 {row["Name"]} 的 {field} 已引用新名称。");
if (!string.IsNullOrEmpty(old) && name == old)
links.Add(new("Cylinders", Guid.Parse((string)row["ID"]), field));
}
return PlanReferences(kind, id, Guid.Empty, old, newName) with { CylinderReferences = links };
}
internal Action PrepareIoView(ProjectRename plan, ControllersModel controllers, CylinderModel cylinders)
{
IEnumerable<object> channels = plan.Kind == "DI" ? controllers.DI : controllers.DQ;
var entries = channels.Select(c => Data.Entry(c)).ToList();
var target = entries.SingleOrDefault(e => (Guid)e.Property("ID").CurrentValue == plan.ObjectId);
if (target == null || (string)target.Property("Name").CurrentValue != plan.OldName ||
entries.Any(e => e != target && e.Property("Name").CurrentValue is string name && name.Length > 0 &&
(name == plan.OldName || name == plan.NewName)))
throw new ProjectConflictException("编辑器中的通道名称已改变,请先保存。");
var links = plan.CylinderReferences.ToHashSet();
var updates = new List<Action> { () => target.Property("Name").CurrentValue = plan.NewName };
foreach (var cylinder in cylinders.Cylinders)
foreach (var field in CylinderFields(plan.Kind))
{
var property = Data.Entry(cylinder).Property(field);
var name = (string)property.CurrentValue;
bool referencesOld = !string.IsNullOrEmpty(name) && name == plan.OldName;
if ((!string.IsNullOrEmpty(name) && name == plan.NewName) ||
links.Remove(new("Cylinders", cylinder.ID, field)) != referencesOld)
throw new ProjectConflictException("编辑器中的气缸绑定已改变,请先保存。");
if (referencesOld) updates.Add(() => property.CurrentValue = plan.NewName);
}
if (links.Count != 0) throw new ProjectConflictException("气缸列表已改变,请先保存。");
return () => updates.ForEach(update => update());
}
}