Skip to content

Commit

Permalink
feat: Added GetParentTwincatProject
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuca committed Dec 17, 2023
1 parent 5a7d6c8 commit 999defa
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/TwinGet.AutomationInterface/Utils/TwincatUtils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file is licensed to you under MIT license.

using System.Xml.Serialization;
using TCatSysManagerLib;
using TwinGet.AutomationInterface.Exceptions;
using TwinGet.AutomationInterface.ProjectFileDeserialization;
using ITcSmTreeItemAlias = TCatSysManagerLib.ITcSmTreeItem9;
using ITcSysManagerAlias = TCatSysManagerLib.ITcSysManager15;

Expand All @@ -18,5 +20,76 @@ internal static class TwincatUtils

return plcProject;
}

/// <summary>
/// Try to find the TwinCAT project that the given PLC project belong to.
/// </summary>
/// <param name="plcProjectPath">The path to the <c>.plcproj</c> file.</param>
/// <param name="upwardDepth"></param>
/// <returns></returns>
/// <exception cref="FileNotFoundException"></exception>
public static string GetParentTwincatProject(string plcProjectPath, int upwardDepth = 5)
{
ArgumentException.ThrowIfNullOrEmpty(plcProjectPath, nameof(plcProjectPath));

if (!File.Exists(plcProjectPath))
{
throw new FileNotFoundException($"Provided plc project file path does not exists.", plcProjectPath);
}

// We first try to parse the plcproj file and throw if necessary.
string xmlContent = File.ReadAllText(plcProjectPath);
XmlSerializer serializer = new(typeof(PlcProjectData));

PlcProjectData plcProjectFile;
using (StringReader reader = new(xmlContent))
{
plcProjectFile = serializer.Deserialize(reader) as PlcProjectData ?? throw new InvalidProjectFileFormat("Could not deserialize the provided PLC project file.", plcProjectPath);
}

/// We recursively look up TwinCAT project files in parent folders with a depth of <param name="upwardDepth"/param>.
string parent = plcProjectPath;
for (int i = 0; i < upwardDepth - 1; i++)
{
parent = Directory.GetParent(parent)?.FullName ?? string.Empty;

if (string.IsNullOrEmpty(parent))
{
break;
}

string[] xaeCandidates = Directory.GetFiles(parent, $"*{TwincatConstants.TwincatXaeProjectExtension}");
string[] plcCandidates = Directory.GetFiles(parent, $"*{TwincatConstants.TwincatPlcProjectExtension}");
string[] twincatProjectCandidate = xaeCandidates.Concat(plcCandidates).ToArray();

if (twincatProjectCandidate.Length == 0) { continue; }

// We process each TwinCAT project file we found.
foreach (string tcCandidate in twincatProjectCandidate)
{
xmlContent = File.ReadAllText(tcCandidate);
serializer = new(typeof(TcSmProjectData));

using (StringReader reader = new(xmlContent))
{
TcSmProjectData? tcSmProject = serializer.Deserialize(reader) as TcSmProjectData;

if (tcSmProject is null) { continue; }

// We process each PLC project the TwinCAT project has.
foreach (ProjectElement plcProjectCandidate in tcSmProject.Project.Plc.Projects)
{
// Using GUID, if we find ourselves in the TwinCAT project candidate, we return the candidate.
if (plcProjectCandidate.GUID.Equals(plcProjectFile.PropertyGroup.ProjectGuid, StringComparison.OrdinalIgnoreCase))
{
return tcCandidate;
}
}
}
}
}

return "";
}
}
}
36 changes: 36 additions & 0 deletions test/TwinGet.AutomationInterface.Test/Utils/TwincatUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is licensed to you under MIT license.

using TwinGet.AutomationInterface.Test.TestUtils;
using TwinGet.AutomationInterface.Utils;

namespace TwinGet.AutomationInterface.Test.Utils
{
public class TwincatUtilsTests
{
[Fact]
public void GetParentTwincatProject_ShouldSucceed()
{
using TestProject testProject = new TestProject();
TestTwincatProject? testTcProject = null;
TestPlcProject? testPlcProject = null;

// Find a test TwinCAT project that has at least one PLC project.
foreach (TestTwincatProject tcProj in testProject.TwincatProjects)
{
if (tcProj.PlcProjects.Count > 0)
{
testTcProject = tcProj;
testPlcProject = tcProj.PlcProjects.First();
}
}

testTcProject.Should().NotBeNull();

string actual = TwincatUtils.GetParentTwincatProject(testPlcProject.AbsolutePath);
string expected = testTcProject.AbsolutePath;

expected.Should().NotBeNullOrEmpty();
expected.Should().Be(actual);
}
}
}

0 comments on commit 999defa

Please sign in to comment.