using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using GcDevicePc.Common;

namespace GcDevicePc.ProThread
{
    class HMIControl
    {
        //设备参数
        string strIP = null;
        ushort netPost = 0;
        bool setflag = false;

        GCModbus m_modbus = new GCModbus();

        public HMIControl(string ip, ushort post)
        {
            if (ip != null && post > 0)
            {
                this.strIP = ip;
                this.netPost = post;
                this.setflag = true;
            }
            else
            {
                setflag = false;
            }
        }

        ~HMIControl()
        {
            this.strIP = null;
            this.netPost = 0;
            this.setflag = false;
        }

        public int WriteSingleVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            if (this.setflag)
                ret = m_modbus.WriteHoldingReg(147, mystate, this.strIP, this.netPost);
            return ret;
        }

        public int WriteBatVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            if (this.setflag)
                ret = m_modbus.WriteHoldingReg(148, mystate, this.strIP, this.netPost);
            return ret;
        }

        public int WriteSTDBatVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            if (this.setflag)
                ret = m_modbus.WriteHoldingReg(151, mystate, this.strIP, this.netPost);
            return ret;
        }

        public int WriteOpenVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            if (this.setflag)
                ret = m_modbus.WriteHoldingReg(150, mystate, this.strIP, this.netPost);
            return ret;
        }

        /// <summary>
        /// 写入更新指令
        /// </summary>
        public void WriteUpdateVal()
        {
            ushort mystate = (ushort)0x5;
            int ret = 0;
            int count = 0;

            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(156, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    m_modbus.WriteHoldingReg(156, mystate, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
            }
        }


        public int WriteStartVal()
        {
            int ret = -1;
            if (this.setflag)
                ret = m_modbus.WriteSingleCoil(30, (ushort)65280, this.strIP, this.netPost);
            return ret;
        }

        public int WriteStopVal()
        {
            int ret = -1;
            if (this.setflag)
                ret = m_modbus.WriteSingleCoil(31, (ushort)65280, this.strIP, this.netPost);
            return ret;
        }

        public int WriteEndVal()
        {
            int ret = -1;
            if (this.setflag)
                ret = m_modbus.WriteSingleCoil(33, (ushort)65280, this.strIP, this.netPost);
            return ret;     
        }

        public int WriteCancalVal()
        {
            int ret = -1;
            if (this.setflag)
                ret = m_modbus.WriteSingleCoil(32, (ushort)65280, this.strIP, this.netPost);
            return ret;
        }

        /// <summary>
        /// 氢空开关测试
        /// </summary>
        /// <param name="val"></param>
        public void HAirSwitch(int val)
        {
            int ret;
            ushort switchaddr = 155;
            ushort writestatue = 0;
            byte[] nowdata = new byte[1];
            int count = 0;
            if (this.setflag)
            {
                switch (val)
                {
                    case 1:
                        switchaddr = 20;
                        break;
                    case 2:
                        switchaddr = 21;
                        break;
                    case 3:
                        switchaddr = 22;
                        break;
                    case 4:
                        switchaddr = 23;
                        break;
                    default:
                        switchaddr = 20;
                        break;
                }

                ret = m_modbus.ReadCoilStatus(switchaddr, 1, this.strIP, this.netPost, ref nowdata);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadCoilStatus(switchaddr, 1, this.strIP, this.netPost, ref nowdata);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }

                if(ret == 0)
                {
                    if (nowdata[0] == 0)
                    {
                        writestatue = (ushort)65280;
                    }
                    else
                    {
                        writestatue = 0;
                    }

                    ret = m_modbus.WriteSingleCoil(switchaddr, writestatue, this.strIP, this.netPost);
                    while (ret != 0)
                    {
                        count++;
                        ret = m_modbus.WriteSingleCoil(switchaddr, writestatue, this.strIP, this.netPost);
                        Thread.Sleep(100);
                        if (count == 3)
                            break;
                    }
                }
                else
                {

                }
               
            }
                
        }
     
        /// <summary>
        /// 点火
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        public void FireUPWork(int local, ushort time1, ushort time2)
        {
            ushort mystate = (ushort)0x1;
            ushort []buf = new ushort[2];
            int ret = 0;
            int count = 0;
            buf[0] = time1;
            buf[1] = time2;
           // buf[2] = mystate;

            ushort fireaddr = 155;

            if (this.setflag)
            {
                switch(local)
                {
                    case 0:
                        fireaddr = 155;
                        break;
                    case 1:
                        fireaddr = 157;
                        break;
                    case 2:
                        fireaddr = 158;
                        break;
                    default:
                        fireaddr = 155;
                        break;
                }

                ret = m_modbus.WriteMultipleRegs(153, 2, this.strIP, this.netPost, ref buf);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteMultipleRegs(153, 2, this.strIP, this.netPost, ref buf);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
                count = 0;
                ret = m_modbus.WriteHoldingReg(fireaddr, mystate, this.strIP, this.netPost );
                while(ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(fireaddr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
        }

        public bool TCD_Switch(ushort port)
        {
            int ret = 0;
            ushort addr = 0;
            int count = 0;
            byte [] nowdata = new byte[1];
            ushort writestatue = 0;
            if (this.setflag)
            {
                switch (port)
                {
                    case 0:
                        addr = 11;
                        break;
                    case 1:
                        addr = 12;
                        break;
                    case 2:
                        addr = 13;
                        break;
                    default:
                        addr = 11;
                        break;
                }

                ret = m_modbus.ReadCoilStatus(addr,1,this.strIP, this.netPost, ref nowdata);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadCoilStatus(addr, 1, this.strIP, this.netPost, ref nowdata);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
                if(ret == 0 && nowdata[0] == 0)
                {
                    writestatue = (ushort)65280;
                }
                else
                {
                    writestatue = 0;
                }

                count = 0;
                ret = m_modbus.WriteSingleCoil(addr, writestatue, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(addr, writestatue, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }

               
            }

            return (ret == 0) ? true: false;   
        }

        /// <summary>
        /// 设置TCD1 DAC值
        /// </summary>
        /// <param name="val"></param>
        public bool TCD_SetDAC(ushort port, ushort val)
        {
            ushort mystate = val;
            int ret = 0;
            int count = 0;
            ushort addr = 0;

            if (this.setflag)
            {
                switch (port)
                {
                    case 0:
                        addr = 132;
                        break;
                    case 1:
                        addr = 134;
                        break;
                    case 2:
                        addr = 136;
                        break;
                    default:
                        addr = 132;
                        break;
                }

                ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
            }

            return (ret == 0) ? true : false;   
        }


        public bool SetBatCount(ushort val)
        {
            ushort mystate = val;
            int ret = 0;
            int count = 0;
            ushort addr = 10;

            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
            }
            return (ret == 0) ? true : false;
        }

    }
}
