Skip to content

Commit

Permalink
Merge pull request #6 from OpenCommissioning/development
Browse files Browse the repository at this point in the history
Hotfix
  • Loading branch information
Preliy authored Nov 18, 2024
2 parents cb0a61b + 7ec0d9f commit 58ef4a4
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 25 deletions.
53 changes: 53 additions & 0 deletions Editor/Scripts/SamplesAssetPostprocessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEngine;
using UnityEditor;
using System.IO;

namespace OC.Editor
{
public class SamplesAssetPostprocessor : AssetPostprocessor
{
private const string DATA_PATH = "Demo/Data/RFID";
private const string SAMPLE_IDENTIFIER = "Samples/Open Commissioning/";
private static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{
if (Directory.Exists(Path.Combine(Application.streamingAssetsPath, DATA_PATH))) return;
if (importedAssets.Length == 0 || !importedAssets[0].StartsWith("Assets/"+SAMPLE_IDENTIFIER)) return;

var splits = importedAssets[0].Split('/');
var versionString = splits[3];

var sourceFullPath = Path.Combine(Path.GetFullPath(Application.dataPath), "Samples/Open Commissioning/", versionString, DATA_PATH);

MoveTemplateFiles(sourceFullPath);

var deletePath = Directory.GetParent(sourceFullPath)!.FullName;
FileUtil.DeleteFileOrDirectory(deletePath);
FileUtil.DeleteFileOrDirectory(deletePath + ".meta");
AssetDatabase.Refresh();
}

private static void MoveTemplateFiles(string sourceFullPath)
{
var targetDirectory = Path.Combine(Application.streamingAssetsPath, DATA_PATH);

if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory);

try
{
foreach (var file in Directory.GetFiles(sourceFullPath))
{
var targetFileName = Path.Combine(targetDirectory, Path.GetFileName(file));
File.Move(file, targetFileName);
}
}
catch (System.Exception e)
{
Debug.LogError($"Error moving files to StreamingAssets: {e.Message}");
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/SamplesAssetPostprocessor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ with a `Payload Tag` is created using a [`Source`](#source) component, a corresp
named after the [`Payload`](#payload)
_unique ID_. This file initially contains default values derived from a template.
The locations for both the template file and the generated tag file are defined in the _Directory Id_ property of
the component, which corresponds to the index of the Product `Data Directory Manager`.
the component, which corresponds to the index of the Product `Data Directory Manager`. When the path begins with the prefix `streamingassets:`,
the path will be interpreted as relative to the [StreamingAssets](https://docs.unity3d.com/Manual/StreamingAssets.html) folder.

![PayloadTag_Inspector.png](Documentation%2FImages%2FPayloadTag_Inspector.png)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public void Disconnect()
public void Read()
{
if (!_isConnected) return;
if (!_adsClient.IsConnected) return;
if (_inputSymbols.Count == 0) return;

_adsClient.ReadWrite(
0xF080,
Expand All @@ -80,6 +82,7 @@ public void Write()
{
if (!_isConnected) return;
if (!_adsClient.IsConnected) return;
if (_outputSymbols.Count == 0) return;

_adsClient.ReadWrite(
0xF081,
Expand Down Expand Up @@ -107,22 +110,25 @@ private void Initialize()

foreach (var symbol in symbols)
{
if (string.IsNullOrEmpty(symbol.Comment)) continue;
if (!symbol.Comment.ToLower().Contains("{simulation-interface}")) continue;
if (!symbol.InstancePath.Contains(_client.RootName)) continue;
if (!symbol.Attributes.TryGetAttribute("simulation_interface", out _)) continue;
if (!symbol.Attributes.TryGetAttribute("TcAddressType", out var attribute)) continue;

if (symbol.Attributes.TryGetAttribute("TcAddressType", out var attribute))
switch (attribute.Value.ToLower())
{
if (attribute.Value.ToLower() == "output")
case "output":
{
_inputSymbols.Add((IAdsSymbol)symbol);
_inputSize += symbol.ByteSize;
if (_client.Verbose) Logging.Logger.Log(LogType.Log, Logging.Logger.VERBOSE_TAG + " <color=green>Find ADS Variable:</color>" + $"{symbol.InstancePath} : {symbol.TypeName} : TcOutput");
break;
}
else if (attribute.Value.ToLower() == "input")
case "input":
{
_outputSymbols.Add((IAdsSymbol)symbol);
_outputSize += symbol.ByteSize;
if (_client.Verbose) Logging.Logger.Log(LogType.Log, Logging.Logger.VERBOSE_TAG + " <color=green>Find ADS Variable:</color>" + $"{symbol.InstancePath} : {symbol.TypeName} : TcInput");
break;
}
}
}
Expand Down
33 changes: 19 additions & 14 deletions Runtime/Scripts/Communication/Client/TwinCAT/TcAdsTaskBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,25 @@ public void Disconnect()
_isConnected = false;
Logging.Logger.Log(LogType.Log, $"TcAds Task Client is <color=red>disconnected</color> from {_netId}:{_port}");
}


public void Read()
{
if (!_isConnected) return;
if (!_adsClient.IsConnected) return;
if (_inputBuffer.Length == 0) return;

_adsClient.Read(_addrGroup, ADDR_OFFSET_INPUTS, _inputBuffer, 0, _inputBuffer.Length);
}

public void Write()
{
if (!_isConnected) return;
if (!_adsClient.IsConnected) return;
if (_inputBuffer.Length == 0) return;

_adsClient.Write(_addrGroup, ADDR_OFFSET_OUTPUTS, _outputBuffer, 0, _outputBuffer.Length);
}

private void Initialize()
{
var symbolLoaderSettings = new SymbolLoaderSettings(SymbolsLoadMode.Flat);
Expand Down Expand Up @@ -113,19 +131,6 @@ private void CreateVairables(byte[] buffer, List<IAdsSymbol> symbols, List<Clien
}
}

public void Read()
{
if (!_isConnected) return;
_adsClient.Read(_addrGroup, ADDR_OFFSET_INPUTS, _inputBuffer, 0, _inputBuffer.Length);
}

public void Write()
{
if (!_isConnected) return;
if (!_adsClient.IsConnected) return;
_adsClient.Write(_addrGroup, ADDR_OFFSET_OUTPUTS, _outputBuffer, 0, _outputBuffer.Length);
}

private void WriteNullData()
{
if (!_isConnected) return;
Expand Down
10 changes: 6 additions & 4 deletions Runtime/Scripts/Data/ProductDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static class ProductDataFactory
private const string TEMPLATE_NAME = "Type";
private const string DATA_NAME = "ID";
private const string TAG_UNIQUE_ID = "UniqueId";
private const string STREAMING_ASSETS_PREFIX = "streamingassets:";


public static void CreateProductData(this PayloadTag payloadTag, Dictionary<string, string> content = null)
{
try
Expand All @@ -29,14 +29,16 @@ public static void CreateProductData(this PayloadTag payloadTag, Dictionary<stri
continue;
}
string directory;
var configuredPath = ProductDataDirectoryManager.Instance.ProductDataDirectories[directoryId].Path;

if (Path.IsPathRooted(ProductDataDirectoryManager.Instance.ProductDataDirectories[directoryId].Path))
if (configuredPath.StartsWith(STREAMING_ASSETS_PREFIX, StringComparison.OrdinalIgnoreCase))
{
directory = ProductDataDirectoryManager.Instance.ProductDataDirectories[directoryId].Path;
var relativePath = configuredPath.Substring(STREAMING_ASSETS_PREFIX.Length).TrimStart('/','\\');
directory = Path.Combine(Application.streamingAssetsPath, relativePath);
}
else
{
directory = $"{Application.dataPath}\\{ProductDataDirectoryManager.Instance.ProductDataDirectories[directoryId].Path}";
directory = configuredPath;
}

CreateProductDataFile(payloadTag, directory, content);
Expand Down
2 changes: 1 addition & 1 deletion Samples/Demo/0.1 MaterialFlow.unity
Original file line number Diff line number Diff line change
Expand Up @@ -3851,7 +3851,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
_productDataDirectories:
- Name: RFID
Path: Samples\Open Commissioning\1.0.1\Demo\Data\RFID
Path: streamingassets:Demo/Data/RFID
--- !u!1 &1629764384
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 58ef4a4

Please sign in to comment.