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

namespace DeviceCommand.Devices
{
    /// <summary>
    /// 环境箱（型号：RLT1000） 一拖三
    /// </summary>
    [ACPCommand]
    public class RLT1000 : ModbusTcp
    {
        private const byte SlaveId = 1;

        #region 读取

        public async Task<ushort> 读取状态监控(CancellationToken ct = default)
        {
            ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 5, 1, ct);
            return re[0];
        }

        public async Task<float> 读取当前温度(CancellationToken ct = default)
        {
            try
            {
                ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 10, 2, ct);
                return ConvertToFloat(re);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public async Task<float> 读取当前湿度(CancellationToken ct = default)
        {
            ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 16, 2, ct);
            return ConvertToFloat(re);
        }

        public async Task<float> 读取当前水温(CancellationToken ct = default)
        {
            try
            {
                ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 22, 2, ct);
                return ConvertToFloat(re);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public async Task<float> 读取当前1号水流量值(CancellationToken ct = default)
        {
            ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 28, 2, ct);
            return ConvertToFloat(re);
        }

        public async Task<float> 读取当前2号水流量值(CancellationToken ct = default)
        {
            ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 34, 2, ct);
            return ConvertToFloat(re);
        }

        public async Task<float> 读取当前3号水流量值(CancellationToken ct = default)
        {
            ushort[] re = await ReadHoldingRegistersAsync(SlaveId, 40, 2, ct);
            return ConvertToFloat(re);
        }

        #endregion

        #region 读写（控制命令）

        public async Task 切换为本地模式(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 147, 0, ct);
        }

        public async Task 切换为远程模式(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 147, 1, ct);
        }

        public async Task 远程模式关机(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 148, 0, ct);
        }

        public async Task 远程模式开机(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 148, 1, ct);
        }

        public async Task 远程模式复位结束(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 149, 0, ct);
        }

        public async Task 远程模式复位(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 149, 1, ct);
        }

        public async Task 切换为程序模式(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 153, 0, ct);
        }

        public async Task 切换为定值模式(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 153, 1, ct);
        }

        public async Task 定值温度设定(float 温度, CancellationToken ct = default)
        {
            var tmp = ConvertFromFloat(温度);
            await WriteMultipleRegistersAsync(SlaveId, 154, tmp, ct);
        }

        public async Task 定值湿度设定(float 湿度, CancellationToken ct = default)
        {
            var tmp = ConvertFromFloat(湿度);
            await WriteMultipleRegistersAsync(SlaveId, 158, tmp, ct);
        }

        public async Task 定值水温设定(float 水温, CancellationToken ct = default)
        {
            var tmp = ConvertFromFloat(水温);
            await WriteMultipleRegistersAsync(SlaveId, 162, tmp, ct);
        }

        public async Task 定值1号水流量设定(float 水流量, CancellationToken ct = default)
        {
            var tmp = ConvertFromFloat(水流量);
            await WriteMultipleRegistersAsync(SlaveId, 164, tmp, ct);
        }

        public async Task 定值2号水流量设定(float 水流量, CancellationToken ct = default)
        {
            var tmp = ConvertFromFloat(水流量);
            await WriteMultipleRegistersAsync(SlaveId, 166, tmp, ct);
        }

        public async Task 定值3号水流量设定(float 水流量, CancellationToken ct = default)
        {
            var tmp = ConvertFromFloat(水流量);
            await WriteMultipleRegistersAsync(SlaveId, 168, tmp, ct);
        }

        public async Task 水温模式不使用(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 1181, 0, ct);
        }

        public async Task 水温模式使用(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 1181, 1, ct);
        }

        public async Task 环境箱温度模式不使用(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 1182, 0, ct);
        }

        public async Task 环境箱温度模式使用(CancellationToken ct = default)
        {
            await WriteSingleRegisterAsync(SlaveId, 1182, 1, ct);
        }

        #endregion

        #region 数据转换助手方法

        private float ConvertToFloat(ushort[] values)
        {
            if (values == null || values.Length < 2) return 0f;

            byte[] bytes = new byte[4];
            bytes[3] = (byte)(values[0] >> 8);
            bytes[2] = (byte)(values[0] & 0xFF);
            bytes[1] = (byte)(values[1] >> 8);
            bytes[0] = (byte)(values[1] & 0xFF);
            return BitConverter.ToSingle(bytes, 0);
        }

        private ushort[] ConvertFromFloat(float value)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            ushort[] result = new ushort[2];
            result[0] = (ushort)((bytes[3] << 8) | bytes[2]);
            result[1] = (ushort)((bytes[1] << 8) | bytes[0]);
            return result;
        }

        #endregion
    }
}