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

namespace GcDevicePc.Module
{
    class WTD624X
    {

        private GCModbus tcpmodbus = null;
        private bool workflag = false;
        private string ip = "";
        private ushort port = 502;

        public WTD624X(String module_ip, ushort module_port)
        {
            ip = module_ip;
            port = module_port;
        }

        public bool GetWorkFlag()
        {
            return workflag;
        }

        public int WTD624X_Open()
        {

            int ret = 0;
            int count = 3;
            try
            {
                tcpmodbus = new GCModbus();
                if (tcpmodbus != null)
                {
                    workflag = true;
                    UInt16[] buf = new UInt16[4] { 1, 1, 1, 1 };
                    ret = tcpmodbus.WriteMultipleRegs(11, 4, ip, port, ref buf);
                    while (ret != 0 && count > 0)
                    {
                        ret = tcpmodbus.WriteMultipleRegs(11, 4, ip, port, ref buf);
                        count--;
                    }
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return ret;
        }

        public int WTD624X_Close()
        {

            int ret = 0;
            try
            {
                ret = tcpmodbus.GCModbusClose();
                if (ret == 0)
                    workflag = false;
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return ret;
        }

        private void GetMaVal(float[] data, ref UInt16[] buf)
        {
            if (data[0] >= 0 && data[0] <= 150.0f)
                buf[0] = (UInt16)(data[0] * 20.0f / 150.0f * 4095 / 20.0f);
            if (data[1] >= 0 && data[1] <= 10.0f)
                buf[1] = (UInt16)(data[1] * 20.0f / 10.0f * 4095 / 20.0f);
            if (data[2] >= 0 && data[2] <= 20.0f)
                buf[2] = (UInt16)(data[2] * 20.0f / 20.0f * 4095 / 20.0f);
            if (data[3] >= 0 && data[3] <= 30.0f)
                buf[3] = (UInt16)(data[3] * 20.0f / 30.0f * 4095 / 20.0f);
        }

        public int UpdateData(float[] data)
        {
            UInt16[] buf = new UInt16[4] { 0, 0, 0, 0 };
            GetMaVal(data, ref buf);
            int ret = tcpmodbus.WriteMultipleRegs(1, 4, ip, port, ref buf);
            return ret;
        }

        public int UpdateIP(String IP, String Mask, String GW)
        {
            int ret = 0;
            string[] slocal_ip = IP.Split('.');
            string[] slocal_mask = Mask.Split('.');
            string[] slocal_gw = GW.Split('.');

            try
            {
                byte[] local_ip = new byte[4];
                byte[] local_mask = new byte[4];
                byte[] local_gw = new byte[4];
                UInt16[] localdata = new UInt16[6];

                for (int i = 0; i < 4;i++)
                {
                    local_ip[i] = Convert.ToByte(slocal_ip[i]);
                    local_mask[i] = Convert.ToByte(slocal_mask[i]);
                    local_gw[i] = Convert.ToByte(slocal_gw[i]);
                }

                localdata[0] = (ushort)((local_ip[1] & 0x00FF) | ((local_ip[0] << 8) & 0xFF00));
                localdata[1] = (ushort)((local_ip[3] & 0xFF) | ((local_ip[2] << 8) & 0xFF00));
                localdata[2] = (ushort)((local_mask[1] & 0xFF) | ((local_mask[0] << 8) & 0xFF00));
                localdata[3] = (ushort)((local_mask[3] & 0xFF) | ((local_mask[2] << 8) & 0xFF00));
                localdata[4] = (ushort)((local_gw[1] & 0xFF) | ((local_gw[0] << 8) & 0xFF00));
                localdata[5] = (ushort)((local_gw[3] & 0xFF) | ((local_gw[2] << 8) & 0xFF00));

                ret = tcpmodbus.WriteMultipleRegs(306, 6, ip, port, ref localdata);
                if (ret == 0)
                {
                    ip = IP;
                }
            }
            catch
            {
            }

            return ret;
        }

    }
}
