using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WTModbus;
using System.Configuration;
namespace GcDevicePc.ProThread
{
    public class SwitchController
    {
        #region 单实例
        private static SwitchController instance = null;
        private static object _lock = new object();
        public static SwitchController GetInstance(ushort parity, int ubaudrate, byte uport, int utimeout)
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new SwitchController(parity, ubaudrate, uport, utimeout);
                    }
                }
            }
            return instance;
        }
        #endregion

        public RTUMaster rtumodbus;

        public SwitchController(ushort parity, int ubaudrate, byte uport, int utimeout)
        {
            try
            {
                rtumodbus = new RTUMaster(uport, ubaudrate, parity, utimeout);

            }
            catch (Exception)
            {
                
            }

        }

        public bool OpenModbusRtu()
        {
            try
            {
                rtumodbus.ModbusOpen();

                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }

        /// <summary>
        /// 关闭所有管路
        /// </summary>
        public void SwitchAllClose()
        {
            for (int i = 1; i < 16; i++)
            {
                if (i < 7)
                {
                    rtumodbus.WriteSingleCoil(31, (ushort)(i + 16), (ushort)0);
                }
                else if (i > 6 && i < 13)
                {
                    rtumodbus.WriteSingleCoil(32, (ushort)(i + 10), (ushort)0);
                }
                else
                {
                    rtumodbus.WriteSingleCoil(33, (ushort)(i + 4), (ushort)0);
                }

            }
        }

        /// <summary>
        /// 打开指定管路
        /// </summary>
        /// <param name="v"></param>
        public void SwitchOpen(int v)
        {

            if (v < 7)
            {
                rtumodbus.WriteSingleCoil(31, (ushort)(v + 16), (ushort)65280);
            }
            else if (v > 6 && v < 13)
            {
                rtumodbus.WriteSingleCoil(32, (ushort)(v + 10), (ushort)65280);
            }
            else
            {
                rtumodbus.WriteSingleCoil(33, (ushort)(v + 4), (ushort)65280);
            }
        }

        /// <summary>
        /// 关闭指定管路
        /// </summary>
        /// <param name="v"></param>
        public void SwitchClose(int v)
        {
            if (v < 7)
            {
                rtumodbus.WriteSingleCoil(31, (ushort)(v + 16), (ushort)0);
            }
            else if (v > 6 && v < 13)
            {
                rtumodbus.WriteSingleCoil(32, (ushort)(v + 10), (ushort)0);
            }
            else
            {
                rtumodbus.WriteSingleCoil(33, (ushort)(v + 4), (ushort)0);
            }

        }



    }
}
