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>
    /// 直流电源——阿美泰克直流电源
    /// </summary>
    [ATSCommand]
    [DeviceCategory("阿美泰克直流电源")]
    public class AMETEKSGX : Tcp
    {
        /// <summary>
        /// 信号量锁
        /// </summary>
        public SemaphoreSlim semaphoreSlimLock { get; set; } = new(1, 1);

        /// <summary>
        /// 切换为远程模式 阿美泰克直流电源
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task Set_RemoteMode(Tcp client, CancellationToken ct = default)
        {
            try
            {
                await SendAsync(client, $"SYSTem:LOCAL? OFF\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }

        /// <summary>
        /// 切换为本地模式
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task Set_LocalMode(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"SYSTem:LOCAL? ON\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }



        /// <summary>
        /// 设定输出电压值
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="voltage">电压</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_General_OutputVoltage(Tcp client, int voltage, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"SOURce:VOLTage {voltage}\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 设置最大电压
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="voltage">输出频率</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_MaxVoltage(Tcp client, float voltage, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"SOURce:VOLTage:LIMit {voltage}\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 返回AMETEK自身的电压值
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">取消令牌</param>
        /// <returns>返回值是：AMETEK自身的电流值 (Amps)</returns>
        public async Task<string> QueryVoltage(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //查询语法与命令语法相同，只需在命令后添加“？”即可
                return await WriteRead(client, $"SOURce:VOLTage?\r\n", "\n", ct: ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 设置输出电流
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="current">输出频率</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_OutputCurrent(Tcp client, float current, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"SOURce:CURRent{current}\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 设置最大电流
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="current">输出频率</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_MaxCurrent(Tcp client, float current, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"SOURce:CURRent:LIMit {current}\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }

        /// <summary>
        /// 返回AMETEK自身的电流值
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">取消令牌</param>
        /// <returns>返回值是：AMETEK自身的电流值 (Amps)</returns>
        public async Task<string> QueryCurrent(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //查询语法与命令语法相同，只需在命令后添加“？”即可
                return await WriteRead(client, $"SOURce:CURRent?\r\n", "\n", ct: ct);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }

        /// <summary>
        /// 设置电源的开启
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_Power_ON(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                //1/0 或 ON/OFF都可以
                await SendAsync(client, $"OUTPut:STATe ON\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }


        /// <summary>
        /// 设置电源的关闭
        /// </summary>
        /// <param name="client">设备</param>
        /// <param name="ct">支持中途取消发送指令</param>
        /// <returns></returns>
        public async Task Set_Power_OFF(Tcp client, CancellationToken ct = default)
        {
            await semaphoreSlimLock.WaitAsync(ct);
            try
            {
                await SendAsync(client, $"OUTPut:STATe OFF\n", ct);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                semaphoreSlimLock.Release();
            }
        }
    }
}
