using System.Collections; using System.Text.Json.Nodes; using Microsoft.EntityFrameworkCore; using PLCSharp.VVMs.Cylinders; using PLCSharp.VVMs.Connects; using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.Robots; using PLCSharp.VVMs.Robots.Epson; using PLCSharp.VVMs.Vision.Camera; namespace PLCSharp.Models; public partial class GlobalModel { private Action PrepareObjectView(ObjectEdit edit) { var row = edit.After ?? edit.Before; if (row["RecipeID"] != null && Guid.Parse((string)row["RecipeID"]) != CurrentRecipe.ID) return null; Robot robot = edit.Kind == "RobotPoints" ? RobotModel.Robots.SingleOrDefault(r => r.ID == Guid.Parse((string)row["RobotID"])) : null; var controller = ControllersModel.Controller; var entity = ProjectConfiguration.Entity(Project.Data, edit.Kind); var key = entity.FindPrimaryKey().Properties.Single().PropertyInfo; IList list = edit.Kind switch { "Variables" => VariablesModel.Variables, "AxisPoints" => ControllersModel.AxisPoints, "RobotPoints" => robot?.Points, "Axes" => ControllersModel.Axes, "DI" => ControllersModel.DI, "DQ" => ControllersModel.DQ, "Robots" => RobotModel.Robots, "Cylinders" => CylindersModel.Cylinders, "Cameras" => VisionsModel.Cameras, "Connects" => Connects.Connects, _ => null }; object live = edit.Kind == "Controllers" ? controller : list?.Cast().SingleOrDefault(o => (Guid)key.GetValue(o) == edit.Id); var properties = entity.GetProperties().Where(p => edit.Before == null || edit.After == null || !JsonNode.DeepEquals(edit.Before[p.GetColumnName()], edit.After[p.GetColumnName()])).ToArray(); if (edit.Before != null && live != null) { var before = ProjectDirectory.ObjectRow(entity, ProjectDirectory.MaterializeObject(entity, edit.Before)); var current = ProjectDirectory.ObjectRow(entity, live); foreach (var p in properties) if (!JsonNode.DeepEquals(before[p.GetColumnName()], current[p.GetColumnName()])) throw new ProjectConflictException($"编辑器中 {p.GetColumnName()} 有未保存的修改。"); } if (live is Connect { Connected: true } or CameraBase { Connected: true } || live is EpsonRobot epson && (epson.ClientCtrl.Connected || epson.ClientCommand.Connected) || (edit.Kind is "Controllers" or "Axes" or "DI" or "DQ") && controller.Connected) throw new ProjectConflictException("请先断开正在使用的设备连接。"); var next = edit.After == null ? null : ProjectDirectory.MaterializeObject(entity, edit.After); void ApplyFields(object target) { foreach (var p in properties) p.PropertyInfo.SetValue(target, p.PropertyInfo.GetValue(next)); } void Remove() { if (live != null) list?.Remove(live); } return () => { if (edit.Kind == "Controllers") { if (next == null || edit.Before == null || properties.Any(p => p.Name == "Type")) controller.ForgetReviewedDriver(); if (next != null) ApplyFields(controller); else { var empty = new Controller(); foreach (var p in properties) p.PropertyInfo.SetValue(controller, p.PropertyInfo.GetValue(empty)); } return; } if (list == null) return; // A saved robot point may belong to a robot without a configured driver. if (edit.Kind == "Connects") { if (Connects.SelectedConnect == live) Connects.SelectedConnect = null; Remove(); if (next != null) Connects.AddConfigured((Connect)next); return; } if (edit.Kind == "Robots") { if (live is EpsonRobot old) old.EndPolling(); Remove(); if (next is Robot { Type: RobotType.Epson } definition) { var r = EpsonRobot.Create(definition); foreach (var p in entity.GetProperties()) p.PropertyInfo.SetValue(r, p.PropertyInfo.GetValue(definition)); r.Model = RobotModel; RobotModel.Robots.Add(r); RobotModel.LoadReviewedPoints(r, CurrentRecipe.ID); } return; } if (edit.Kind == "Variables" || edit.Kind == "Cylinders") { var tracked = next == null ? null : Project.Data.Find(entity.ClrType, edit.Id); if (tracked is Variable variable) variable.RestoreValue(Project.Data); if (tracked == null) Remove(); else if (live == null) list.Add(tracked); if (edit.Kind == "Variables" && next == null && VariablesModel.SelectedVariable == live) VariablesModel.SelectedVariable = null; return; } IList channels = edit.Kind switch { "Axes" => controller.Axes, "DI" => controller.DI, "DQ" => controller.DQ, _ => null }; if (next == null) { Remove(); if (live != null) channels?.Remove(live); if (live != null && ControllersModel.SelectedAxis == live) ControllersModel.SelectedAxis = null; if (live != null && ControllersModel.SelectedAxisPoint == live) ControllersModel.SelectedAxisPoint = null; if (live != null && robot?.SelectedPoint == live) robot.SelectedPoint = null; if (live != null && VisionsModel.SelectedCamera == live) VisionsModel.SelectedCamera = null; } else if (live == null) { live = next is CameraBase camera ? VisionsModel.ReviewedCamera(camera) : next; list.Add(live); channels?.Add(live); } else ApplyFields(live); if (edit.Kind is "AxisPoints" or "Axes") ControllersModel.RefreshReviewedAxes(edit.Kind, edit.Id, (string)row["Name"]); if (edit.Kind == "RobotPoints") foreach (var matrix in robot.RobotMatrices.Where(m => m.StartName == (string)row["Name"] || m.XEndName == (string)row["Name"] || m.YEndName == (string)row["Name"])) robot.Create(matrix); }; } }