Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Editor/Scripts/SceneMenuItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ private static bool ValidateLoadSnapshot()
[MenuItem("Open Commissioning/Scene/Create Configuration", priority = 20)]
private static void CreateConfiguration()
{
SceneConfiguration.Create();
SceneConfigurationManager.Save();
}

[MenuItem("Open Commissioning/Scene/Load Configuration", priority = 21)]
private static void LoadConfiguration()
{
SceneConfiguration.Load();
SceneConfigurationManager.Load();
}

[MenuItem("Open Commissioning/Settings/Apply Default Layers", priority = 100)]
Expand Down
7 changes: 0 additions & 7 deletions Runtime/Scripts/Communication/Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public abstract class Client : MonoBehaviour, IClient, IConfigAsset, IAfterFixed
public IPropertyReadOnly<bool> IsConnected => _isConnected;
public abstract IClientBuffer Buffer { get; }
public string RootName => _rootName;
public bool Verbose
{
get => _verbose;
set => _verbose = value;
}

public float TimeScale
{
Expand All @@ -33,8 +28,6 @@ public float TimeScale
[Header("Settings")]
[SerializeField]
protected string _rootName = "MAIN";
[SerializeField]
protected bool _verbose;

[HideInInspector]
[SerializeField]
Expand Down
43 changes: 18 additions & 25 deletions Runtime/Scripts/Communication/Client/TwinCAT/TcAdsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ namespace OC.Communication.TwinCAT
public class TcAdsClient : Client
{
public override IClientBuffer Buffer => _adsClientBuffer;
public bool AutoConnect => _autoConnect;
public string NetId => _netId;
public int Port => _port;

public TcAdsClientConfig Config
{
get => _config;
set => _config = value;
}

[Header("ADS")]
[SerializeField]
private bool _autoConnect;
[SerializeField]
private string _netId = "Local";
[SerializeField]
[Tooltip("Port range [301...399], [851...899]")]
private int _port = 851;
private TcAdsClientConfig _config = TcAdsClientConfig.Default;

private TcClientStateHandler _clientStateHandler;
private IClientBuffer _adsClientBuffer;
Expand All @@ -37,7 +35,7 @@ public class TcAdsClient : Client
base.Start();
_clientStateHandler = new TcClientStateHandler();
_clientStateHandler.OnStateChanged += OnClientStateChanged;
_clientStateHandler.Connect(_netId);
_clientStateHandler.Connect(_config.NetId);
}

public override void BeforeFixedUpdate()
Expand All @@ -54,7 +52,7 @@ public override void AfterFixedUpdate()

private void OnClientStateChanged(TcAdsExtension.TcClientState state)
{
if (!_autoConnect) return;
if (!_config.Reconnect) return;

switch (state)
{
Expand All @@ -77,26 +75,26 @@ public override void Connect()

try
{
_ = ValidatePort(_port);
_ = ValidatePort(_config.Port);

if (!_clientStateHandler.IsRunning)
{
throw new Exception("Target system isn't in run mode");
}

var tcAdsBufferType = GetTcAdsBufferType(_port);
var tcAdsBufferType = GetTcAdsBufferType(_config.Port);

switch (tcAdsBufferType)
{
case TcAdsBufferType.None:
throw new Exception($"Port:{_port} is out of range [301...399], [851...899]");
throw new Exception($"Port:{_config.Port} is out of range [301...399], [851...899]");
case TcAdsBufferType.Array:
_adsClientBuffer = new TcAdsTaskBuffer(this);
_adsClientBuffer.Connect(_netId, _port);
_adsClientBuffer.Connect(_config.NetId, _config.Port);
break;
case TcAdsBufferType.SumCommand:
_adsClientBuffer = new TcAdsSumCommandBuffer(this);
_adsClientBuffer.Connect(_netId, _port);
_adsClientBuffer.Connect(_config.NetId, _config.Port);
break;
default:
throw new ArgumentOutOfRangeException();
Expand All @@ -107,7 +105,7 @@ public override void Connect()
catch (Exception exception)
{
_isConnected.Value = false;
Logging.Logger.Log(LogType.Error, $"TcAdsClient failed to connect: {_netId}:{_port}. {exception.Message}. \n{exception.Source}{exception.StackTrace}");
Logging.Logger.Log(LogType.Error, $"TcAdsClient failed to connect: {_config.NetId}:{_config.Port}. {exception.Message}. \n{exception.Source}{exception.StackTrace}");
}
}

Expand Down Expand Up @@ -177,7 +175,7 @@ private bool ValidatePort(int port)
{
> 300 and < 400 => true,
> 850 and < 900 => true,
_ => throw new Exception($"Port:{_port} is out of range [301...399], [851...899]")
_ => throw new Exception($"Port:{_config.Port} is out of range [301...399], [851...899]")
};
}

Expand All @@ -193,8 +191,7 @@ private TcAdsBufferType GetTcAdsBufferType(int port)

public override XElement GetAsset()
{
var config = new TcAdsClientConfig(this);
var element = config.ToXElement<TcAdsClientConfig>();
var element = _config.ToXElement<TcAdsClientConfig>();
element.SetAttributeValue("Name", name);
return element;
}
Expand All @@ -203,11 +200,7 @@ public override void SetAsset(XElement xElement)
{
try
{
var config = xElement.FromXElement<TcAdsClientConfig>();
_autoConnect = config.Reconnect;
_netId = config.NetId;
_port = config.Port;
_verbose = config.Verbose;
_config = xElement.FromXElement<TcAdsClientConfig>();
}
catch (Exception exception)
{
Expand Down
74 changes: 55 additions & 19 deletions Runtime/Scripts/Communication/Client/TwinCAT/TcAdsClientConfig.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,70 @@
using System;
using UnityEngine;

namespace OC.Communication.TwinCAT
{
[Serializable]
public struct TcAdsClientConfig
public class TcAdsClientConfig
{
public string Name;
public bool Reconnect;
public string NetId;
public int Port;
public bool Verbose;
public string Name
{
get => _name;
set => _name = value;
}

public TcAdsClientConfig(string name, bool reconnect, string netId, int port, bool verbose = false)
public bool Reconnect
{
Name = name;
Reconnect = reconnect;
NetId = netId;
Port = port;
Verbose = verbose;
get => _reconnect;
set => _reconnect = value;
}

public TcAdsClientConfig(TcAdsClient tcAdsClient)
public string NetId
{
Name = tcAdsClient.name;
Reconnect = tcAdsClient.AutoConnect;
NetId = tcAdsClient.NetId;
Port = tcAdsClient.Port;
Verbose = tcAdsClient.Verbose;
get => _netId;
set => _netId = value;
}

public static TcAdsClientConfig Default => new ("Client", true, "Local", 351);
public int Port
{
get => _port;
set => _port = value;
}

public bool ClearBuffer
{
get => _clearBuffer;
set => _clearBuffer = value;
}

public bool Verbose
{
get => _verbose;
set => _verbose = value;
}

[SerializeField]
private string _name;
[SerializeField]
private bool _reconnect;
[SerializeField]
private string _netId;
[Tooltip("Port range [301...399], [851...899]")]
[SerializeField]
private int _port;
[Tooltip("If Disconnected, process data will be overwrite with null")]
[SerializeField]
private bool _clearBuffer;
[SerializeField]
private bool _verbose;

public static TcAdsClientConfig Default => new ()
{
Name = "Client",
Reconnect = true,
NetId = "Local",
Port = 351,
ClearBuffer = true,
Verbose = false
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TcAdsSumCommandBuffer : IClientBuffer
private readonly List<ClientVariable> _outputVariables = new();
private readonly List<IAdsSymbol> _inputSymbols = new();
private readonly List<IAdsSymbol> _outputSymbols = new();
private readonly Client _client;
private readonly TcAdsClient _client;
private AdsClient _adsClient;
private string _netId;
private int _port;
Expand All @@ -33,7 +33,7 @@ public class TcAdsSumCommandBuffer : IClientBuffer

private bool _isConnected;

public TcAdsSumCommandBuffer(Client client)
public TcAdsSumCommandBuffer(TcAdsClient client)
{
_client = client;
}
Expand All @@ -54,7 +54,7 @@ public void Connect(string netId, int port)

public void Disconnect()
{
WriteNullData();
if (_client.Config.ClearBuffer) WriteNullData();
Clear();
_adsClient?.Disconnect();
_isConnected = false;
Expand Down Expand Up @@ -121,14 +121,14 @@ private void Initialize()
{
_inputSymbols.Add(adsSymbol);
_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");
if (_client.Config.Verbose) Logging.Logger.Log(LogType.Log, Logging.Logger.VERBOSE_TAG + " <color=green>Find ADS Variable:</color>" + $"{symbol.InstancePath} : {symbol.TypeName} : TcOutput");
break;
}
case 0xF020:
{
_outputSymbols.Add(adsSymbol);
_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");
if (_client.Config.Verbose) Logging.Logger.Log(LogType.Log, Logging.Logger.VERBOSE_TAG + " <color=green>Find ADS Variable:</color>" + $"{symbol.InstancePath} : {symbol.TypeName} : TcInput");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TcAdsTaskBuffer : IClientBuffer
private readonly List<ClientVariable> _outputVariables = new();
private readonly List<IAdsSymbol> _inputSymbols = new();
private readonly List<IAdsSymbol> _outputSymbols = new();
private readonly Client _client;
private readonly TcAdsClient _client;
private AdsClient _adsClient;
private string _netId;
private int _port;
Expand All @@ -33,7 +33,7 @@ public class TcAdsTaskBuffer : IClientBuffer
private byte[] _outputBuffer;
private bool _isConnected;

public TcAdsTaskBuffer(Client client)
public TcAdsTaskBuffer(TcAdsClient client)
{
_client = client;
}
Expand All @@ -54,7 +54,7 @@ public void Connect(string netId, int port)

public void Disconnect()
{
WriteNullData();
if (_client.Config.ClearBuffer) WriteNullData();
Clear();
_adsClient?.Disconnect();
_isConnected = false;
Expand Down Expand Up @@ -172,7 +172,7 @@ private void AddSymbol(IAdsSymbol symbol)
throw new ArgumentOutOfRangeException();
}

if (_client.Verbose) Logging.Logger.Log(LogType.Log, Logging.Logger.VERBOSE_TAG + " <color=green>Find ADS Variable:</color>" + $"{symbol.InstancePath} : {symbol.TypeName} : {direction}");
if (_client.Config.Verbose) Logging.Logger.Log(LogType.Log, Logging.Logger.VERBOSE_TAG + " <color=green>Find ADS Variable:</color>" + $"{symbol.InstancePath} : {symbol.TypeName} : {direction}");
}
catch (Exception exception)
{
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Scripts/System/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static void RuntimeInit()

private void OnEnable()
{
SceneConfiguration.LoadFromDefaultPath();
SceneConfigurationManager.LoadFile();
}

private void Start()
Expand All @@ -43,7 +43,7 @@ private void Start()
[Button]
public void SaveConfig()
{
SceneConfiguration.SaveInDefaultPath();
SceneConfigurationManager.SaveFile();
}

public void ConnectClients()
Expand Down
Loading