using System;
using System.Text;
using Aerotech.A3200.Callbacks;
using Aerotech.A3200.Commands;
using Aerotech.A3200.Configuration;
using Aerotech.A3200.DataCollection;
using Aerotech.A3200.Exceptions;
using Aerotech.A3200.Information;
using Aerotech.A3200.Parameters;
using Aerotech.A3200.Properties;
using Aerotech.A3200.Status;
using Aerotech.A3200.SystemDLLWrapper;
using Aerotech.A3200.Tasks;
using Aerotech.A3200.Variables;
using Aerotech.Common;
namespace Aerotech.A3200;
public class Controller : INamed<string>, IDisposable
{
private ControllerHandle controllerHandle;
private bool disposed;
private readonly Data data;
private readonly ControllerInformation information;
private readonly ControllerParameters parameters;
private readonly TasksCollection tasks;
private readonly ControlCenter controlCenter;
private readonly TaskSelectionCommands commands;
private readonly ControllerVariableContainer variables;
private readonly LoadedProgramCollection loadedPrograms;
private readonly CallbackRegistrar callbackRegistrar;
private static object mutex = new object();
private static Controller connectedController;
private static SystemConfiguration systemConfiguration;
string INamed<string>.Name => "A3200";
public Data DataCollection => data;
public ControllerInformation Information => information;
public ControllerParameters Parameters => parameters;
public TasksCollection Tasks => tasks;
public ControlCenter ControlCenter => controlCenter;
public TaskSelectionCommands Commands => commands;
public ControllerVariableContainer Variables => variables;
public LoadedProgramCollection LoadedPrograms => loadedPrograms;
internal ControllerHandle Handle => controllerHandle;
internal CallbackRegistrar CallbackRegistrar => callbackRegistrar;
internal int Number => 0;
public static Controller ConnectedController => connectedController;
public static SystemConfiguration Configuration
{
get
{
if (systemConfiguration == null)
{
systemConfiguration = new SystemConfiguration();
}
return systemConfiguration;
}
}
public static bool IsRunning
{
get
{
int pdwInitialized_ = 0;
ExceptionResolver.ResolveThrow(Wrapper.AerSysIsSystemInitialized(ref pdwInitialized_));
return pdwInitialized_ != 1;
}
}
internal Controller(IntPtr controllerHandle)
: this(new ControllerHandle(controllerHandle))
{
}
internal Controller(ControllerHandle controllerHandle)
{
this.controllerHandle = controllerHandle;
information = new ControllerInformation(this);
parameters = new ControllerParameters(this, new ControllerParameterCreator(this));
tasks = new TasksCollection(this);
callbackRegistrar = new CallbackRegistrar(this);
commands = new TaskSelectionCommands(this);
data = new Data(this);
controlCenter = new ControlCenter(this);
variables = new ControllerVariableContainer(this);
loadedPrograms = new LoadedProgramCollection(this);
Information.ControllerResetPre += delegate (object sender, ControllerEventArgs eventArgs)
{
eventArgs.Controller.Parameters.InvalidateCache();
eventArgs.Controller.Information.invalidateCache();
};
}
~Controller()
{
Dispose(disposing: false);
}
public void Reset()
{
if (controllerHandle == null)
{
throw new InvalidOperationException(Resources.ControllerNotConnectedException);
}
InitializationWrapper(controllerHandle.Value, smc: true);
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
public void ChangePassword(string oldPassword, string newPassword)
{
if (oldPassword == null)
{
throw new ArgumentNullException("oldPassword");
}
if (newPassword == null)
{
throw new ArgumentNullException("newPassword");
}
ExceptionResolver.ResolveThrow(Wrapper.AerSysSetPassword(controllerHandle.Value, oldPassword, newPassword));
}
protected virtual void Dispose(bool disposing)
{
if (!disposed && disposing)
{
information.stopResetThread();
Wrapper.AerEventWaitCancel(controllerHandle.Value);
controlCenter.UnsubscribeAll();
CallbackRegistrar.ShutDown();
((IDisposable)DataCollection.Poller).Dispose();
controllerHandle.Dispose();
variables.Dispose();
disposed = true;
}
}
public static Controller Connect()
{
Disconnect();
IntPtr phAerCtrl_ = IntPtr.Zero;
int pdwConfigMode_ = 0;
ExceptionResolver.ResolveThrow(Wrapper.AerSysConnect(ref phAerCtrl_, ref pdwConfigMode_));
try
{
_SYS_INIT_INFO pSysInitInfo_ = default(_SYS_INIT_INFO);
Wrapper.AerSysServerIsInitialized(phAerCtrl_, ref pSysInitInfo_);
InitializationWrapper(phAerCtrl_, !pSysInitInfo_.bSmcIsInitialized);
}
catch
{
Wrapper.AerSysDisconnect(phAerCtrl_);
throw;
}
lock (mutex)
{
connectedController = new Controller(phAerCtrl_);
DiagPacketPoller.Threader.ResetInterval();
DiagPacketPoller.Threader.Spooler.InitQueue(1);
TaskStatesPoller.Threader.ResetInterval();
TaskStatesPoller.Threader.Spooler.InitQueue(1);
return connectedController;
}
}
private static void InitializationWrapper(IntPtr handle, bool smc)
{
int pdwInitErrorState_ = 0;
ErrorData peRcWarning_ = default(ErrorData);
StringBuilder stringBuilder = new StringBuilder(4096);
if (smc)
{
ErrorData errorData = Wrapper.AerSysInitialize(handle, null, bResetFieldbus_: true, ref pdwInitErrorState_, ref peRcWarning_, stringBuilder);
string errorMessage = ExceptionResolver.GetErrorMessage(errorData, stringBuilder.ToString());
ExceptionResolver.ResolveThrow(errorData, errorMessage);
}
}
public static void Disconnect()
{
DiagPacketPoller.Threader.StopAll();
TaskStatesPoller.Threader.StopAll();
lock (mutex)
{
if (connectedController != null)
{
connectedController.Dispose();
}
connectedController = null;
DiagPacketPoller.Threader.Spooler.InitQueue(0);
TaskStatesPoller.Threader.Spooler.InitQueue(0);
}
}
}
06-09
798

07-27
711

05-28
1216

07-28
1206
