using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimensCNC
{
   public  class ETH_S7SimensPLCLib
    {

        public static string cncIP;
        int receiveData;

        byte[] sendCommands;

        byte[] buffer;//发送报文容器
        byte[] datas;//接收报文容器

        static IAsyncResult connResult = null;

        Func<byte[], int, object> func;

        public ETH_S7SimensPLCLib()
        {

        }

        public Socket PLCSocket { get; set; }

        /// <summary>
        /// 链接函数
        /// </summary>
        public bool ETH_S7SimensConnect(string adrip, ushort port, int overtime)
        {
            cncIP = adrip;
            IPAddress ip = IPAddress.Parse(adrip);
            int ports = Convert.ToInt32(port);
            PLCSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connResult = PLCSocket.BeginConnect(ip, ports, null, null);
            Thread.Sleep(200);
            connResult.AsyncWaitHandle.WaitOne(5, true);  //等待2秒
            if (connResult.IsCompleted)
            {
                PLCHandShark();
                return true;
            }
            else
            {

                return false;
            }
        }

        /// <summary>
        /// 断开设备连接
        /// </summary>
        /// <param name="no">设备编号</param>
        public void ETH_S7SimensDisConnect(string no)
        {
            PLCSocket.Shutdown(SocketShutdown.Both);
            PLCSocket.Close();

        }

        /// <summary>
        /// 三次握手
        /// </summary>
        /// <summary>
        /// plc握手
        /// </summary>
        void PLCHandShark()
        {
            PLCSocket.Send(ETH_S7SimensPLCProtocol.PLC_FIRST_HAND_SHANK);
            buffer = new byte[1024];
            receiveData = PLCSocket.Receive(buffer);
            PLCSocket.Send(ETH_S7SimensPLCProtocol.PLC_SECONDE_HAND_SHANK);
            buffer = new byte[1024];
            receiveData = PLCSocket.Receive(buffer);
        }

        /// <summary>
        /// 获取报警信息
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetPLCAlarmMsg(int i, ref object result)
        {
            ETH_S7SimensPLCProtocol.PLC_READBYTE[ETH_S7SimensPLCProtocol.PLC_READBYTE.Length - 1] = (byte)(i * 8);
            PLCSocket.Send(ETH_S7SimensPLCProtocol.PLC_READBYTE);
            Thread.Sleep(10);
            buffer = new byte[1024 * 8];
            receiveData = PLCSocket.Receive(buffer);

            if (receiveData > 0)
            {
                datas = new byte[receiveData];
                Array.Copy(buffer, datas, receiveData);
                var s = BitConverter.ToInt16(datas.Skip(datas.Length - 2).Take(2).ToArray(), 0);
                if (s > 0)
                {
                    if ((s & 0x01) == 1)
                    {
                        result=((700000 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x02) == 0x02)
                    {
                        result=((700001 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x04) == 0x04)
                    {
                        result=((700002 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x08) == 0x08)
                    {
                        result=((700003 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x10) == 0x10)
                    {
                        result=((700004 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x20) == 0x20)
                    {
                        result=((700005 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x40) == 0x40)
                    {
                        result=((700006 + i * 8).ToString() + "\r\n");
                    }
                    if ((s & 0x80) == 0x80)
                    {
                        result=((700007 + i * 8).ToString() + "\r\n");
                    }
                };

                return true;

            }
            else
            {
                return false;
            }
        }

    }
}
