plcsharp/PLCSharpWpf/Models/GlobalModel.ObjectEdit.cs

170 lines
9.5 KiB
C#

using System.Collections;
using PLCSharp.Core.Prism;
using PLCSharp.VVMs.Vision;
using PLCSharp.VVMs.Homepage;
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;
if (edit.Kind == "Recipes") return () =>
{
if (edit.After == null && RecipeSelected?.ID == edit.Id) RecipeSelected = null;
if (edit.After == null && CurrentRecipe.ID == edit.Id)
{
CurrentRecipe = Recipes.Single(r => r.ID == Project.DefaultRecipeId);
foreach (var r in Recipes) r.Current = r.ID == CurrentRecipe.ID;
LoadRecipe();
}
};
if (edit.Kind == "Workflows") return () =>
{
if (edit.Action == "create") Project.Flows(CurrentRecipe.ID).Single(f => f.ID == edit.Id).Start(this);
if (edit.Action == "delete" && WorkflowsModel.SelectedTask?.ID == edit.Id) WorkflowsModel.SelectedTask = null;
};
Robot robot = edit.Kind is "RobotPoints" or "RobotMatrices" ? 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
{
"SystemVariables" => VariablesModel.SystemVariables, "Matrices" => ControllersModel.Matrices,
"RobotMatrices" => robot?.RobotMatrices, "InterpolationGroups" => ControllersModel.InterpolationGroups,
"ImageDatas" => VisionsModel.ImageDatas, "VisionFunctions" => VisionsModel.VisionFunctions,
"LocalImageDatas" => VisionsModel.VisionFunctions.SingleOrDefault(f => f.ID == Guid.Parse((string)row["FuncID"]))?.LocalImageDatas,
"CustomControls" => CustomControls, "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 : edit.Kind == "CanvasConfigs" && CurrentCanvasConfig?.ID == edit.Id ? CurrentCanvasConfig : list?.Cast<object>().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 (edit.Kind == "CanvasConfigs")
{
CurrentCanvasConfig = next == null ? new CanvasConfig { RecipeID = CurrentRecipe.ID } : Project.Data.CanvasConfigs.Find(edit.Id);
_EventAggregator.GetEvent<MessageEvent>().Publish(new() { Target = "UIReLoad" });
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 is "Variables" or "Cylinders" or "SystemVariables" or "LocalImageDatas")
{
var tracked = next == null ? null : Project.Data.Find(entity.ClrType, edit.Id);
if (tracked is Variable variable) variable.RestoreValue(Project.Data);
if (tracked is SystemVariable system) system.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)
{
if (live != null && VisionsModel.SelectedVisionFunction == live) VisionsModel.SelectedVisionFunction = null;
if (live != null && SelectedCustomControl == live) SelectedCustomControl = 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;
if (live is VisionFunction function)
{
function.VisionsModel = VisionsModel;
function.GlobalModel = this;
function.ImageDatas = VisionsModel.ImageDatas;
foreach (var local in Project.Data.LocalImageDatas.Where(l => l.FuncID == function.ID)) function.LocalImageDatas.Add(local);
}
list.Add(live);
channels?.Add(live);
}
else ApplyFields(live);
if (next != null && live is Matrix matrixItem) ControllersModel.Create(matrixItem);
if (next != null && live is RobotMatrix robotMatrix) robot?.Create(robotMatrix);
if (next != null && live is InterpolationGroup group)
{
group.AxisX = ControllersModel.Axes.FirstOrDefault(axis => axis.Name == group.AxisXName);
group.AxisY = ControllersModel.Axes.FirstOrDefault(axis => axis.Name == group.AxisYName);
}
if (edit.Kind == "CustomControls") _EventAggregator.GetEvent<MessageEvent>().Publish(new() { Target = "UIReLoad" });
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);
};
}
}