using System;
using System.Linq;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using ImageFormatter;
using irisHelper;

namespace IrisDoor
{
    public class DoorSystem
    {
        private string[] serialPorts;
        private static SerialPort _serialPort;
        private static string cardIDstr;
        private static byte[] cardID;

        private static int sleepTime = Convert.ToInt32(ConfigHelper.GetAppConfig("sleepTime").ToString());

        public DoorSystem() {
            if (ConfigHelper.GetAppConfig("doorSystemEnable") == "true")
            {
                //连接串口
                string serialPort = ConfigHelper.GetAppConfig("serialPort");
                if (connectSerialPort(serialPort) == -1)
                {
                    MessageBox.Show("Connect to serial port failed！");
                    Application.Exit();//连接失败，关闭程序
                }
            }
        }

        /// <summary>
        /// 按名称连接串口
        /// </summary>
        /// <param name="sPort"></param>
        public int connectSerialPort(string sPort)
        {
            try
            {
                serialPorts = SerialPort.GetPortNames();
                if (serialPorts.Count() > 0)
                {
                    for (int i = 0; i < serialPorts.Count(); i++)
                    {
                        if (serialPorts[i] == sPort)
                        {
                            //新建串口对象
                            _serialPort = new SerialPort();

                            //设置串口参数 
                            _serialPort.PortName = serialPorts[i];
                            _serialPort.BaudRate = 9600;
                            _serialPort.Parity = Parity.None;
                            _serialPort.DataBits = 8;
                            _serialPort.StopBits = StopBits.One;
                            _serialPort.Handshake = Handshake.None;
                            _serialPort.ReadTimeout = 500;
                            _serialPort.WriteTimeout = 500;

                            //打开串口
                            _serialPort.Open();
                            
                            i = 9999;
                            return 0;
                        }
                    }
                    return -1;
                }
                else
                {
                    MessageBox.Show("no serial，closeing！");
                    return -1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("serial connection failed：" + ex.Message);
                return -1;
            }
        }

        //给门发送开门信号
        public static void open()
        {
            cardIDstr = "01 05 00 00 FF 00 8C 3A";
            cardID = BitmapFactory.StrToHexBytes(cardIDstr);
            _serialPort.Write(cardID, 0, cardID.Length);

            
            Thread.Sleep(sleepTime);

            cardIDstr = "01 05 00 00 00 00 CD CA ";
            cardID = BitmapFactory.StrToHexBytes(cardIDstr);
            _serialPort.Write(cardID, 0, cardID.Length);

            _serialPort.DiscardOutBuffer();
            
        }
        
        
    }
}