using Common.Attributes;
using DeviceCommand.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Common.Attributes.ATSCommandAttribute;

namespace DeviceCommand.Device
{
    /// <summary>
    /// 万用表（型号：Truevolt 系列）
    /// </summary>
    [ATSCommand]
    [DeviceCategory("万用表")] // 添加分类属性
    public class KeySight_Truevolt : Tcp
    {

        public SemaphoreSlim semaphoreSlimLock { get; set; } = new(1, 1);

        /// <summary>
        /// 耦合模式（AC DC）
        /// </summary>
        public enum Coupling_Mode
        {
            AC,
            DC,
        }

        #region 测量电压
        /// <summary>
        /// 复位仪器
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task SYSTem_PRESet(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"SYSTem:PRESet\n", ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        ///   设置电压量程（直流电压DC）
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="range">量程</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_VoltageDCRange(Tcp client, float range, CancellationToken ct = default)
        {
            if (range > 1000)
            {
                throw new ArgumentNullException($"{range}超出规定的最大量程");
            }
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //CONFigure[:VOLTage]:{AC|DC}[{<range>|AUTO|MIN|MAX|DEF}[, {<resolution>|MIN|MAX|DEF}]]
                await SendAsync(client, $"CONFigure:VOLTage:DC:RANGe {range}\n", ct); // 使用 \n 作为 SCPI 命令结束符
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 读取默认配置电压测量结果
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">取消令牌</param>
        /// <returns>返回值是：定通道电源输出的实际电压值 (Amps)</returns>
        public async Task<string> Default_QueryVoltage(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                return await WriteRead(client, $"MEAS:VOLT:DC? AUTO\r\n", "\n", ct: ct);

            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }
        /// <summary>
        /// 读取自定义量程电压测量结果
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="range">量程</param>
        /// <param name="ct">取消令牌</param>
        /// <returns>返回值是：定通道电源输出的实际电压值 (Amps)</returns>
        public async Task<string> Range_QueryVoltage(Tcp client, float range, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //MEASure[:VOLTage]:{AC|DC}? [{<range>|AUTO|MIN|MAX|DEF} MIN/MAX仪器默认最小和最大值
                return await WriteRead(client, $"MEAS:VOLT:DC?{range}\r\n", "\n", ct: ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }

        /// <summary>
        /// 触发并读取结果
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">取消令牌</param>
        /// <returns>返回值是：定通道电源输出的实际电压值 (Amps)</returns>
        public async Task<string> Read_Voltage(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //MEASure[:VOLTage]:{AC|DC}? [{<range>|AUTO|MIN|MAX|DEF} MIN/MAX仪器默认最小和最大值
                return await WriteRead(client, $"READ?\r\n", "\n", ct: ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 启用自动输入阻抗模式
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task StartIMPAuto_VoltageDC(Tcp client, CancellationToken ct = default)
        {

            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //CONFigure[:VOLTage]:{AC|DC}[{<range>|AUTO|MIN|MAX|DEF}[, {<resolution>|MIN|MAX|DEF}]]
                await SendAsync(client, $"VOLTage:DC:IMPedance:AUTO ON\n", ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }



        /// <summary>
        /// 关闭自动输入阻抗模式
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task StopIMPAuto_VoltageDC(Tcp client, CancellationToken ct = default)
        {

            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //CONFigure[:VOLTage]:{AC|DC}[{<range>|AUTO|MIN|MAX|DEF}[, {<resolution>|MIN|MAX|DEF}]]
                await SendAsync(client, $"VOLTage:DC:IMPedance:AUTO OFF\n", ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 启动DC电压和比例测量禁用或启用自动归零模式
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Start_VoltageDCAUTO_ZERO(Tcp client, CancellationToken ct = default)
        {

            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //CONFigure[:VOLTage]:{AC|DC}[{<range>|AUTO|MIN|MAX|DEF}[, {<resolution>|MIN|MAX|DEF}]]
                await SendAsync(client, $"VOLTage:DC:ZERO:AUTO ON\n", ct); // 使用 \n 作为 SCPI 命令结束符
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 关闭DC电压和比例测量禁用或启用自动归零模式
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="range">量程</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Stop_VoltageDCAUTO_ZERO(Tcp client, CancellationToken ct = default)
        {

            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //CONFigure[:VOLTage]:{AC|DC}[{<range>|AUTO|MIN|MAX|DEF}[, {<resolution>|MIN|MAX|DEF}]]
                await SendAsync(client, $"VOLTage:DC:ZERO:AUTO OFF\n", ct); // 使用 \n 作为 SCPI 命令结束符
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }
        #endregion

    }
}
