using Common;
using Common.Attributes;
using DeviceCommand.Base;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceCommand.Device
{
    /// <summary>
    /// 功率分析仪（型号：HIOKI PW8001）
    /// </summary>

    public class PW8001 : Tcp
    {
        /// <summary>
        /// 构造函数：SCPI 通信使用端口 23
        /// </summary>
        public PW8001()
        {
            Port = 23;
        }

        /// <summary>
        /// 执行引擎注入的设备实例会话键
        /// </summary>
        public string? SessionKey { get; set; }

        #region 1. IEEE 488.2 基础标准命令

        public virtual async Task<string> 查询机器信息(CancellationToken ct = default)
        {
            return await WriteReadAsync("*IDN?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询硬件选项(CancellationToken ct = default)
        {
            return await WriteReadAsync("*OPT?\r\n", "\n", ct);
        }

        public virtual async Task 复位仪器(CancellationToken ct = default)
        {
            await SendAsync("*RST\r\n", ct);
        }

        public virtual async Task 清除状态(CancellationToken ct = default)
        {
            await SendAsync("*CLS\r\n", ct);
        }

        #endregion

        #region 2. 仪器系统与测试模式设置设置 (System & Mode)

        public virtual async Task 设置测试模式_WIDE(CancellationToken ct = default)
        {
            await SendAsync(":MODE WIDE\r\n", ct);
        }

        public virtual async Task 设置测试模式_IEC(CancellationToken ct = default)
        {
            await SendAsync(":MODE IEC\r\n", ct);
        }

        public virtual async Task<string> 查询测试模式(CancellationToken ct = default)
        {
            return await WriteReadAsync(":MODE?\r\n", "\n", ct);
        }

        public virtual async Task 设置同步源(string 源, CancellationToken ct = default)
        {
            await SendAsync($":SYNC:SOURce {源}\r\n", ct);
        }

        public virtual async Task<string> 查询同步源(CancellationToken ct = default)
        {
            return await WriteReadAsync(":SYNC:SOURce?\r\n", "\n", ct);
        }

        public virtual async Task 设置零点调整(string 状态, CancellationToken ct = default)
        {
            await SendAsync($":ZERO {状态}\r\n", ct);
        }

        public virtual async Task<string> 查询零点调整(CancellationToken ct = default)
        {
            return await WriteReadAsync(":ZERO?\r\n", "\n", ct);
        }

        public virtual async Task 设置零点抑制(string 状态, CancellationToken ct = default)
        {
            await SendAsync($":ZSP {状态}\r\n", ct);
        }

        public virtual async Task<string> 查询零点抑制(CancellationToken ct = default)
        {
            return await WriteReadAsync(":ZSP?\r\n", "\n", ct);
        }

        public virtual async Task 设置响应消息头(string 状态, CancellationToken ct = default)
        {
            await SendAsync($":HEADer {状态}\r\n", ct);
        }

        public virtual async Task<string> 查询响应消息头(CancellationToken ct = default)
        {
            return await WriteReadAsync(":HEADer?\r\n", "\n", ct);
        }

        public virtual async Task 设置按键锁定(string 状态, CancellationToken ct = default)
        {
            await SendAsync($":KLOCk {状态}\r\n", ct);
        }

        public virtual async Task<string> 查询按键锁定(CancellationToken ct = default)
        {
            return await WriteReadAsync(":KLOCk?\r\n", "\n", ct);
        }

        #endregion

        #region 3. 积分控制 (Integration)

        public virtual async Task 设置积分控制(string 模式, CancellationToken ct = default)
        {
            await SendAsync($":INTEG:CONTROL {模式}\r\n", ct);
        }

        public virtual async Task<string> 查询积分控制(CancellationToken ct = default)
        {
            return await WriteReadAsync(":INTEG:CONTROL?\r\n", "\n", ct);
        }

        public virtual async Task 设置积分模式(string 通道号, string 模式, CancellationToken ct = default)
        {
            await SendAsync($":INTEG:MODE{通道号} {模式}\r\n", ct);
        }

        public virtual async Task<string> 查询积分模式(string 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":INTEG:MODE{通道号}?\r\n", "\n", ct);
        }

        #endregion

        #region 4. 测量值查询 (Measurement Query)

        // ---------------- 基础测量（不含变比） ----------------

        public virtual async Task<string> 查询电压_不含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:U{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电流_不含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:I{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询功率_不含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:P{通道号}?\r\n", "\n", ct);
        }

        // ---------------- 基础测量（含变比 CT） ----------------

        public virtual async Task<string> 查询电压_含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:U{通道号}:CT?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电流_含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:I{通道号}:CT?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询功率_含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:P{通道号}:CT?\r\n", "\n", ct);
        }

        // ---------------- 其他高阶参数测量 ----------------

        public virtual async Task<string> 查询有功功率积分(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:WP{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电压THD(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:UTHD{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电流THD(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:ITHD{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询线电压U12(CancellationToken ct = default)
        {
            return await WriteReadAsync(":MEAS:U12?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询总功率P123(CancellationToken ct = default)
        {
            return await WriteReadAsync(":MEAS:P123?\r\n", "\n", ct);
        }

        // ---------------- 整流平均值 ----------------

        public virtual async Task<string> 查询电压整流平均值(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:UMN{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电流整流平均值(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:IMN{通道号}?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电压整流平均值_含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:UMN{通道号}:CT?\r\n", "\n", ct);
        }

        public virtual async Task<string> 查询电流整流平均值_含变比(int 通道号, CancellationToken ct = default)
        {
            return await WriteReadAsync($":MEAS:IMN{通道号}:CT?\r\n", "\n", ct);
        }

        #endregion

        #region 5. 自定义透传指令

        /// <summary>
        /// 发送自定义命令
        /// </summary>
        public virtual async Task 发送自定义命令(string 命令, CancellationToken ct = default)
        {
            await SendAsync($"{命令}\r\n", ct);
        }

        #endregion
    }
}