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

黑河是我国西北干旱区最重要的内陆河流之一,灌区分布及水利工程体系直接关系到流域农业发展、水资源配置与生态安全。 本资源包含黑河流域范围内的灌区空间分布矢量数据(Shapefile格式)与干支渠分布栅格图(TIF格式),可广泛应用于农业水资源管理、流域水文模拟、灌溉工程布局分析及生态水文研究等领域。 【数据内容】 灌区分布数据(Shapefile) 数据类型:矢量多边形(Polygon) 坐标系统:WGS 84 或 CGCS2000(具体可查看 .prj 文件)。 干支渠分布图(GeoTIFF) 数据类型:栅格图像(TIF) 分辨率:通常为10–30米,满足中尺度制图与分析; 图像内容:表示黑河流域干渠与支渠的空间路径分布,可作为水利网络基础图层; 内容描述:标识黑河流域主要灌区边界,包括各县(如张掖、高台、临泽、肃南等)所辖的骨干灌区、支渠灌区分布; 属性字段:灌区名称等; 应用价值:可用于构建灌溉水流路径、流量估算、水资源调度仿真模型等。 【典型应用场景】 流域灌溉调度研究:用于构建灌区供水模型,估算引水量与灌溉效率; 遥感与地理建模:与MODIS、Sentinel遥感数据叠加进行土地覆被分类或作物监测; 农业统计分析:与统计年鉴灌溉面积核对比对,服务于灌溉政策评估; 地图制图与展示:支持ArcGIS、QGIS、Mapbox等平台加载使用,可生成专题图; 水文模型输入:可作为SWAT、MIKE SHE 等模型的空间输入因子。 【附加说明】 文件命名清晰,包含 .shp, .shx, .dbf, .prj 等标准矢量格式; TIF 文件配有 .tfw 文件及标准色带,可直接叠加到DEM、水系图等背景图上; 可适配常用 GIS 软件(ArcGIS/QGIS)及建模工具; 数据来源规范,具有较高的空间精度与现势性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值