using PLCSharp.VVMs.Connects; using PLCSharp.VVMs.Robots; using System.Text.Json.Nodes; namespace PLCSharp.Models; internal interface INamedDevice { Guid ID { get; } string Name { get; } void SetReviewedName(string value); } public sealed partial class ProjectDirectory { internal ProjectRename PlanDeviceRename(string kind, Guid id, string newName) { if (kind is not ("Cylinders" or "Robots" or "Connects")) throw new ArgumentException("请选择 Cylinders、Robots 或 Connects。"); CheckSavedRecipes(); var hardware = JsonNode.Parse(baseline[ConfigPath("hardware")]); var oldName = (string)new PointField(kind, id, "Name").Row(hardware)["Name"]; CheckHardwareName(kind, id, oldName, newName, false); return PlanReferences(kind, id, Guid.Empty, oldName, newName); } } public partial class GlobalModel { private Action PrepareDeviceView(ProjectRename plan) { IEnumerable devices = plan.Kind switch { "Cylinders" => CylindersModel.Cylinders, "Robots" => RobotModel.Robots, "Connects" => Connects.Connects, _ => throw new ArgumentException("未知设备类型。") }; var target = devices.SingleOrDefault(d => d.ID == plan.ObjectId); if (devices.Any(d => d.ID != plan.ObjectId && !string.IsNullOrEmpty(d.Name) && (d.Name == plan.OldName || d.Name == plan.NewName))) throw new ProjectConflictException("编辑器中存在同名设备。"); // Undefined devices are saved definitions without a live driver instance. if (target == null && (plan.Kind == "Robots" && Project.Data.Robots.Find(plan.ObjectId)?.Type == RobotType.Undefined || plan.Kind == "Connects" && Project.Data.Connects.Find(plan.ObjectId)?.Type == ProtocolType.Undefined)) return null; if (target == null || target.Name != plan.OldName) throw new ProjectConflictException("编辑器设备列表已改变,请先保存并重新预览。"); return () => target.SetReviewedName(plan.NewName); } }