using MES.Utility.Network;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq; 
using 常用工具集.Base;

namespace 常用工具集.ViewModel._01PLC通信调试
{
    public class ModbusRTUViewModel : ViewModelBase
    {
        public int SlaveId { get; set; } = 1;
        //串口号下拉列表数据
        public List<string> SerialList { get; set; }
        public int SerialIndex { get; set; } = 0;

        /// <summary>
        /// 波特率
        /// </summary>
        public List<int> BaudRateList { get; set; }
        public int BaudRateIndex { get; set; } = 0;
        //校验
        public List<string> ParityList { get; set; }
        public int ParityIndex { get; set; } = 0;

        //数据为
        public List<int> DataBitList { get; set; }
        public int DataBitIndex { get; set; } = 0;
        //停止位
        public List<string> StopBitList { get; set; }
        public int StopBitIndex { get; set; } = 0;

        public bool Enabled1 { get; set; } = true;
        public bool Enabled2 { get; set; } = false;
        public int Address { get; set; } = 4001;
        public int StringLength { get; set; } = 100;
        public string ReadedValue { get; set; } = "";
        public string Message { get; set; } = "";

        public string SendedBytesValue { get; set; } = "";
        public string RecivedBytesValue { get; set; } = "";
        public string Int16Value { get; set; } = "";
        public string Int32Value { get; set; } = "";
        public string RealValue { get; set; } = "";
        public string StringValue { get; set; } = "";
        public string DoubleValue { get; set; } = "";
        public string LongValue { get; set; } = "";

        //STRING  DOUBLE  INT64  INT32  INT16  REAL
        private ModbusHelper modbus;

        public DelegateCommand ConnectCmd { get; set; }
        public DelegateCommand DisconnectCmd { get; set; }
        public DelegateCommand WriteCmd { get; set; }
        public DelegateCommand ReadCmd { get; set; }
        public ModbusRTUViewModel()
        {
            //串口号
            string[] portNames = SerialPort.GetPortNames();
            if (portNames != null && portNames.Length > 0)
            {
                SerialList = portNames.ToList();
            }
            //波特率
            BaudRateList = new List<int>() { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200, 128000 };
            BaudRateIndex = 6;
            //校验
            ParityList = new List<string> { "None", "Odd", "Even", "Mark", "Space" };
            ParityIndex = 0;
            //数据位
            DataBitList = new List<int> { 5, 6, 7, 8 };
            DataBitIndex = 3;
            //停止位
            StopBitList = new List<string> { "None", "One", "Two", "OnePointFive" };
            StopBitIndex = 1;
            ConnectCmd = new DelegateCommand(ConnectCmdFunc);
            DisconnectCmd = new DelegateCommand(DisconnectCmdFunc);
            ReadCmd = new DelegateCommand(ReadCmdFunc);
            WriteCmd = new DelegateCommand(WriteCmdFunc);
        }

        private void ConnectCmdFunc(object obj)
        {
            try
            {
                SharpModbus.SerialSettings setting = new SharpModbus.SerialSettings();
                setting.PortName = SerialList[SerialIndex];
                setting.BaudRate = BaudRateList[BaudRateIndex];
                setting.DataBits = DataBitList[DataBitIndex];
                setting.Parity = (Parity)ParityIndex;
                setting.StopBits = (StopBits)StopBitIndex;

                modbus = new ModbusHelper(setting, 500, (byte)SlaveId);
                modbus.OnSended += Modbus_OnSended;
                modbus.OnRecived += Modbus_OnRecived;
                Enabled1 = false;
                Enabled2 = true;
            }
            catch
            {
                return;
            }
        }



        private void DisconnectCmdFunc(object obj)
        {
            modbus.OnSended -= Modbus_OnSended;
            modbus.OnRecived -= Modbus_OnRecived;
            modbus.Dispose();
            modbus = null;
            Enabled1 = true;
            Enabled2 = false;
        }

        private void ReadCmdFunc(object obj)
        {
            string cmd = obj.ToString();

            ReadedValue = "";
            Message = "";
            SendedBytesValue = "";
            RecivedBytesValue = "";
            if (cmd == "INT16")
            {
                ushort value;
                bool flag = modbus.ReadHoldRegisterInt16(Address, out value);
                if (!flag)
                {
                    Message = "读取失败";
                    DisconnectCmdFunc(null);
                    return;
                }
                Message = "读取成功";
                ReadedValue = value.ToString();
            }
            else if (cmd == "INT32")
            {
                int value;
                bool flag = modbus.ReadHoldRegisterInt32(Address, out value);
                if (!flag)
                {
                    Message = "读取失败";
                    DisconnectCmdFunc(null);
                    return;
                }
                Message = "读取成功";
                ReadedValue = value.ToString();
            }
            else if (cmd == "INT64")
            {
                long value;
                bool flag = modbus.ReadHoldRegisterInt64(Address, out value);
                if (!flag)
                {
                    Message = "读取失败";
                    DisconnectCmdFunc(null);
                    return;
                }
                Message = "读取成功";
                ReadedValue = value.ToString();
            }
            else if (cmd == "REAL")
            {
                float value;
                bool flag = modbus.ReadHoldRegisterFloat(Address, out value);
                if (!flag)
                {
                    Message = "读取失败";
                    DisconnectCmdFunc(null);
                    return;
                }
                Message = "读取成功";
                ReadedValue = value.ToString();
            }
            else if (cmd == "DOUBLE")
            {
                double value;
                bool flag = modbus.ReadHoldRegisterDouble(Address, out value);
                if (!flag)
                {
                    Message = "读取失败";
                    DisconnectCmdFunc(null);
                    return;
                }
                Message = "读取成功";
                ReadedValue = value.ToString();
            }
            else if (cmd == "STRING")
            {
                string value;
                bool flag = modbus.ReadHoldRegisterString(Address, StringLength, out value);
                if (!flag)
                {
                    Message = "读取失败";
                    DisconnectCmdFunc(null);
                    return;
                }
                Message = "读取成功";
                ReadedValue = value.ToString();
            }

            //  DOUBLE         

        }

        private void WriteCmdFunc(object obj)
        {
            string cmd = obj.ToString();
            ReadedValue = "";
            Message = "";
            SendedBytesValue = "";
            RecivedBytesValue = "";
            if (cmd == "INT16")
            {
                ushort value;
                bool flag = ushort.TryParse(Int16Value, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = modbus.WriteHoldRegisterInt16(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "INT32")
            {
                int value;
                bool flag = int.TryParse(Int32Value, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = modbus.WriteHoldRegisterInt32(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "INT64")
            {
                long value;
                bool flag = long.TryParse(LongValue, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = modbus.WriteHoldRegisterInt64(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "REAL")
            {
                float value;
                bool flag = float.TryParse(RealValue, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = modbus.WriteHoldRegisterFloat(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "DOUBLE")
            {
                double value;
                bool flag = double.TryParse(DoubleValue, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = modbus.WriteHoldRegisterDouble(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "STRING")
            {
                bool flag = modbus.WriteHoldRegisterString(Address, StringLength, StringValue);
                Message = flag ? "写入成功" : "写入失败";
            }
        }


        private void Modbus_OnRecived(byte[] bytes, string hexString)
        {
            RecivedBytesValue = hexString;
        }

        private void Modbus_OnSended(byte[] bytes, string hexString)
        {
            SendedBytesValue = hexString;
        }
    }
}
