181 lines
9.3 KiB
C#
181 lines
9.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Prism.Container.DryIoc;
|
|
using Prism.Ioc;
|
|
using PLCSharp.Models;
|
|
using PLCSharp.VVMs.Connects;
|
|
using PLCSharp.VVMs.Cylinders;
|
|
using PLCSharp.VVMs.GlobalVariables;
|
|
using PLCSharp.VVMs.Homepage;
|
|
using PLCSharp.VVMs.MotionController;
|
|
using PLCSharp.VVMs.Recipe;
|
|
using PLCSharp.VVMs.Robots;
|
|
using PLCSharp.VVMs.Vision;
|
|
using PLCSharp.VVMs.Vision.Camera;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
|
|
namespace PLCSharp.Tests;
|
|
|
|
public class PortableConfigurationTests
|
|
{
|
|
[Fact]
|
|
public void CopyRestoresAllConfigurationInAnEmptyWorkingDatabase()
|
|
{
|
|
var sandbox = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
var source = Path.Combine(sandbox.FullName, "机器 A");
|
|
var target = Path.Combine(sandbox.FullName, "机器 B");
|
|
var image = Path.Combine(sandbox.FullName, "source.png");
|
|
WriteFixture(source, image);
|
|
var archive = Path.Combine(sandbox.FullName, "project.zip");
|
|
ZipFile.CreateFromDirectory(source, archive);
|
|
ZipFile.ExtractToDirectory(archive, target);
|
|
Directory.Delete(source, true);
|
|
File.Delete(image);
|
|
VerifyCopy(target);
|
|
}
|
|
finally { sandbox.Delete(true); }
|
|
}
|
|
|
|
[Fact]
|
|
public void ModuleSaveDetectsConflictAndCanRetryWithoutSavingWorkflowDraft()
|
|
{
|
|
var folder = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
using var project = ProjectDirectory.New();
|
|
var variable = new Variable { RecipeID = project.DefaultRecipeId, Name = "V" };
|
|
project.Data.Add(variable);
|
|
project.Save(folder.FullName);
|
|
project.Flows(project.DefaultRecipeId).Add(new PLCSharp.VVMs.Workflows.Workflow { Name = "Draft" });
|
|
var file = Path.Combine(folder.FullName, "configuration", "variables.json");
|
|
var before = File.ReadAllText(file);
|
|
File.WriteAllText(file, before + " ");
|
|
variable.Comment = "updated";
|
|
Assert.Throws<IOException>(() => project.Data.Save());
|
|
Assert.Equal(before + " ", File.ReadAllText(file));
|
|
File.WriteAllText(file, before);
|
|
project.Data.Save();
|
|
using var copy = ProjectDirectory.Open(folder.FullName);
|
|
Assert.Equal("updated", copy.Data.Variables.Single().Comment);
|
|
Assert.Empty(copy.Flows(copy.DefaultRecipeId));
|
|
var snapshot = Directory.GetFiles(folder.FullName, "*", SearchOption.AllDirectories)
|
|
.ToDictionary(path => path, File.ReadAllText);
|
|
copy.Save();
|
|
Assert.All(snapshot, pair => Assert.Equal(pair.Value, File.ReadAllText(pair.Key)));
|
|
}
|
|
finally { folder.Delete(true); }
|
|
}
|
|
|
|
[Fact]
|
|
public void FreshMachineDatabaseContainsOnlyAccountsAndLogs()
|
|
{
|
|
var folder = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
using var db = new MachineDataContext(Path.Combine(folder.FullName, "machine.db"));
|
|
Assert.Equal(new[] { "ErrorLogs", "Users" }, db.Model.GetEntityTypes()
|
|
.Select(t => t.GetTableName()).Order().ToArray());
|
|
}
|
|
finally
|
|
{
|
|
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
|
|
folder.Delete(true);
|
|
}
|
|
}
|
|
|
|
private static readonly byte[] png = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC");
|
|
|
|
internal static void WriteFixture(string source, string image)
|
|
{
|
|
File.WriteAllBytes(image, png);
|
|
using (var project = ProjectDirectory.New())
|
|
{
|
|
var db = project.Data;
|
|
var recipe = project.DefaultRecipeId;
|
|
var other = new Recipe { Name = "B" };
|
|
project.Recipes.Add(other);
|
|
project.Flows(recipe).Add(new PLCSharp.VVMs.Workflows.Workflow { RecipeID = recipe,
|
|
Name = "搬运", Code = "// portability fixture" });
|
|
var robot = new Robot { Name = "R1" };
|
|
var vision = new VisionFunction { RecipeID = recipe, Name = "检查" };
|
|
var input = new VisionFlow { Type = VisionFlowType.从文件获取图片 };
|
|
input.StringParams["Path"] = image;
|
|
vision.VisionFlows.Add(input);
|
|
db.AddRange(new Controller(), new Axis { Name = "X" }, new DI { Number = 3 }, new DQ { Number = 6 },
|
|
new Connect { Name = "PLC", IP_SerialPort = "192.0.2.1", Params = new Connect.ConnectParams { UnitId = 12, BaudRate = 115200 } }, robot, new Cylinder { Name = "夹具" },
|
|
new CameraBase { Name = "CAM1", Brand = CameraBrand.HikRobot },
|
|
new Variable { RecipeID = recipe, Name = "计数", Comment = "A", Type = VariableDataType.Int32, DefaultValueStr = "35", ValueStr = "42", RetainPersistent = true },
|
|
new Variable { RecipeID = other.ID, Name = "计数", Comment = "B" },
|
|
new SystemVariable { RecipeID = recipe, Name = "状态" },
|
|
new AxisPoint { RecipeID = recipe, Name = "定位", AxisXName = "X", X = 12.5 },
|
|
new Matrix { RecipeID = recipe, StartName = "定位" },
|
|
new InterpolationGroup { RecipeID = recipe, AxisXName = "X" },
|
|
new RobotPoint { RecipeID = recipe, RobotID = robot.ID, Name = "P1", X = 19.25 },
|
|
new RobotMatrix { RecipeID = recipe, RobotID = robot.ID, StartName = "P1" },
|
|
vision, new CanvasConfig { RecipeID = recipe, Rows = 3 }, new CustomControl { RecipeID = recipe, Name = "显示" });
|
|
db.Save();
|
|
// Insert original PNG bytes directly so the portability test can run without a camera SDK.
|
|
db.Database.ExecuteSqlInterpolated($"INSERT INTO ImageDatas (ID, RecipeID, Name, Mat) VALUES ({Guid.NewGuid()}, {recipe}, {"模板"}, {png})");
|
|
db.Database.ExecuteSqlInterpolated($"INSERT INTO LocalImageDatas (ID, FuncID, Name, Mat) VALUES ({Guid.NewGuid()}, {vision.ID}, {"局部"}, {png})");
|
|
project.Save(source);
|
|
db.AxisPoints.Single().X = 13.75;
|
|
db.Save(); // The regular module Save must update the directory as well.
|
|
}
|
|
}
|
|
|
|
internal static void VerifyCopy(string target)
|
|
{
|
|
Assert.Empty(Directory.GetFiles(target, "*.db*", SearchOption.AllDirectories));
|
|
using var copy = ProjectDirectory.Open(target);
|
|
Assert.Equal("// portability fixture", copy.Flows(copy.DefaultRecipeId).Single().Code);
|
|
var restored = copy.Data;
|
|
Assert.Equal("Data Source=:memory:", restored.Database.GetConnectionString());
|
|
var connection = restored.Connects.Single();
|
|
Assert.Equal("192.0.2.1", connection.IP_SerialPort);
|
|
Assert.Equal(12, connection.Params.UnitId);
|
|
Assert.Equal(115200, connection.Params.BaudRate);
|
|
Assert.Single(restored.Controllers);
|
|
Assert.Equal(3, restored.DI.Single().Number);
|
|
Assert.Equal(6, restored.DQ.Single().Number);
|
|
Assert.Equal("夹具", restored.Cylinders.Single().Name);
|
|
Assert.Equal("CAM1", restored.Cameras.Single().Name);
|
|
var point = restored.AxisPoints.Single();
|
|
Assert.Equal(13.75, point.X);
|
|
Assert.Equal(restored.Axes.Single().Name, point.AxisXName);
|
|
Assert.Equal(point.Name, restored.Matrices.Single().StartName);
|
|
Assert.Equal(point.AxisXName, restored.InterpolationGroups.Single().AxisXName);
|
|
Assert.Equal(restored.Robots.Single().ID, restored.RobotPoints.Single().RobotID);
|
|
Assert.Equal(restored.RobotPoints.Single().Name, restored.RobotMatrices.Single().StartName);
|
|
var counter = restored.Variables.Single(v => v.RecipeID == copy.DefaultRecipeId);
|
|
Assert.Equal("A", counter.Comment);
|
|
Assert.True(counter.RetainPersistent);
|
|
Assert.Equal("35", counter.DefaultValueStr);
|
|
Assert.Equal("42", counter.ValueStr);
|
|
Assert.Equal("B", restored.Variables.Single(v => v.RecipeID != copy.DefaultRecipeId).Comment);
|
|
IContainerExtension container = new DryIocContainerExtension();
|
|
container.RegisterInstance(restored);
|
|
var variables = new VariablesModel(container, null!, null!);
|
|
variables.LoadRecipe(copy.DefaultRecipeId);
|
|
Assert.Equal(42, (int)variables.Variables.Single().Value);
|
|
Assert.Equal("状态", variables.SystemVariables.Single().Name);
|
|
Assert.Null((object?)variables.SystemVariables.Single().Value);
|
|
var function = restored.VisionFunctions.Single();
|
|
var step = Assert.Single(function.VisionFlows);
|
|
Assert.Equal(png, Convert.FromBase64String(step.StringParams["ImageBase64"]));
|
|
Assert.Equal("source.png", step.StringParams["Path"]);
|
|
Assert.True(new PLCSharp.VVMs.Vision.VisionFlowHandler.Access.ImageFromFileHandler().Execute(function, step));
|
|
Assert.Equal(1, function.Src.Width);
|
|
Assert.Equal(1, function.Src.Height);
|
|
function.Src.Dispose();
|
|
Assert.Equal(function.ID, restored.LocalImageDatas.Select(i => i.FuncID).Single());
|
|
Assert.Equal(png, restored.ImageDatas.Select(i => i.SerializedMat).Single());
|
|
Assert.Equal(png, restored.LocalImageDatas.Select(i => i.SerializedMat).Single());
|
|
Assert.Equal(3, restored.CanvasConfigs.Single().Rows);
|
|
Assert.Equal("显示", restored.CustomControls.Single().Name);
|
|
Assert.Empty(restored.Users);
|
|
Assert.Empty(restored.ErrorLogs);
|
|
}
|
|
}
|