Aerotech系列(5)Aerotech.A3200控制器实现

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值