using BPASmartClient.Peripheral;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartClient.EventBus;
using BPASmartClient.Modbus;
using static BPASmartClient.EventBus.EventBus;
using BPASmartClient.Model;
using BPASmartClient.Model.PLC;

namespace BPASmartClient.PLC
{
    public class PLCMachine : BasePeripheral
    {
        ModbusTcp modbusTcp = new ModbusTcp();
        //public string IpAddress { get; set; }
        //public int Port { get; set; }
        //public string PLCReadAddress { get; set; }

        //private List<PLCReadParameter> plcReadParameters;

        public override void Init()
        {
            //plcReadParameters = PLCReadParameter.DeSerialize(PLCReadAddress);

            Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort); })); //PLC 设备连接

            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                IsConnected = modbusTcp.Connected;
                if (!IsConnected) IsWork = false;
                while (IsConnected)
                {
                    IsWork = true;
                    foreach (var par in variables)
                    {
                        if (par?.Address.Length > 0)
                        {
                            var res = modbusTcp.Read(par.Address, (ushort)par.ReadLeng);
                            if (status.ContainsKey(par.Address))
                            {
                                status[par.Address] = res;
                            }
                            else
                            {
                                status.TryAdd(par.Address, res);
                            }
                        }

                        //ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
                        //object readData = new object();
                        //switch (par.CmdType)
                        //{
                        //    case CommandType.Coils:
                        //        modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
                        //        break;
                        //    case CommandType.HoldingRegisters:
                        //        readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
                        //        break;
                        //}
                        //status[par.Address] = readData;
                    }


                    //foreach (var par in plcReadParameters)
                    //{
                    //    ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
                    //    object readData = new object();
                    //    switch (par.CmdType)
                    //    {
                    //        case CommandType.Coils:
                    //            modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
                    //            break;
                    //        case CommandType.HoldingRegisters:
                    //            readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
                    //            break;
                    //    }
                    //    status[par.Address] = readData;
                    //}

                    Thread.Sleep(500);
                }
                Thread.Sleep(1000);
            }), $"设备[{DeviceId}]PLC读取线程", true);

            //读取数据
            //    EventBus.EventBus.GetInstance().Subscribe<ReadModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            //{
            //    if (@event == null) return;
            //    var par = @event as ReadModel;
            //    ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
            //    object readData = new object();
            //    if (par.Address.ToUpper().Contains("M"))
            //    {
            //        modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
            //    }
            //    else if (par.Address.ToUpper().Contains("VW"))
            //    {
            //        readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
            //    }
            //    callBack?.Invoke(readData);
            //});

            //写入数据
            EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                var par = @event as WriteModel;

                modbusTcp.Write(par?.Address, par?.Value);
                //ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
                //if (par.Address.ToUpper().Contains("M"))
                //{
                //    modbusTcp.Write(address, CommandType.Coils, par.Value);
                //}
                //else if (par.Address.ToUpper().Contains("VW"))
                //{
                //    modbusTcp.Write(address, CommandType.HoldingRegisters, par.Value);
                //}
            });
        }

        public override void Start()
        {

        }

        public override void Stop()
        {

        }

        protected override void InitStatus()
        {

        }
    }
}