Class2.cs 11.1 KB
using System;

namespace Asa.RFID
{
    /// <summary>
    /// RFID读卡器
    /// </summary>
    public class HFReaderAll
    {
        private Log log;
        private string currIP;
        private string[] _ip;
        private HFReader[] reader;
        private byte[][] buffer;

        //private System.Threading.Thread tRecon;
        //private System.Threading.Thread tSave;
        //private System.Collections.Concurrent.ConcurrentQueue<string> log;

        public delegate void GetValueEvent(string s);
        public event GetValueEvent GetValue;
        public delegate void StatusEvent(int idx, string ip, bool status);
        public event StatusEvent ConnectStatus;
        public event StatusEvent ScanStatus;


        /// <summary>
        /// RFID
        /// </summary>
        public HFReaderAll()
        {

        }

        /// <summary>
        /// 日志目录
        /// </summary>
        public string LogPath { set; get; } = "";

        /// <summary>
        /// 扫描模式,true自动,false查找
        /// </summary>
        public bool ScanMode { set; get; } = true;

        /// <summary>
        /// 打开所有
        /// </summary>
        /// <param name="ip"></param>
        public void Open(params string[] ip)
        {
            log = new Log(LogPath, "RFID_ALL");
            log.OutInfo("Call Open");

            _ip = ip;
            reader = new HFReader[ip.Length];
            buffer = new byte[ip.Length][];
            for (int i = 0; i < buffer.Length; i++)
                buffer[i] = new byte[8];

            Connect();
        }

        /// <summary>
        /// 关闭所有
        /// </summary>
        public void Close()
        {
            log.OutInfo("ReaderAll Close");
            if (reader == null) return;
            for (int i = 0; i < reader.Length; i++)
            {
                if (reader[i] != null)
                {
                    if (ScanMode)
                        reader[i].AutoScanMode(false);
                    //else
                    //    reader[i].FindIDMode(false);
                    reader[i].Close();
                    log.OutInfo(reader[i].IP + " Close");
                    System.Threading.Thread.Sleep(10);
                }
            }
            reader = null;
            log.OutInfo("Close All OK");
            log.Exit();

        }

        /// <summary>
        /// 是否打开
        /// </summary>
        /// <returns></returns>
        public bool[] IsOpen()
        {
            if (reader == null) return null;

            bool[] rtn = new bool[reader.Length];
            for (int i = 0; i < rtn.Length; i++)
            {
                if (reader[i] == null)
                    rtn[i] = false;
                else
                    rtn[i] = reader[i].IsConn;
            }
            return rtn;
        }

        /// <summary>
        /// 是否打开
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool IsOpen(string ip)
        {
            int idx = Array.FindIndex(_ip, ss => ss == ip);
            if (idx == -1) return false;
            if (reader == null) return false;

            if (reader[idx] == null)
                return false;
            else
                return reader[idx].IsConn;
        }

        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public byte[] Read(string ip)
        {
            int idx = Array.FindIndex(_ip, ss => ss == ip);
            if (idx == -1)
            {
                log.OutInfo("ip " + ip + " 没有找到");
                return null;
            }
            return buffer[idx];
        }

        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public string ReadStr(string ip)
        {
            int idx = Array.FindIndex(_ip, ss => ss == ip);
            if (idx == -1)
            {
                log.OutInfo("ip " + ip + " 没有找到");
                return "";
            }

            string val;
            if (buffer[idx][1] == 0)
                val = "00";
            else
                val = (char)buffer[idx][1] + buffer[idx][2].ToString();
            return val;
        }

        /// <summary>
        /// 清零
        /// </summary>
        /// <param name="ip"></param>
        public void Clear(string ip)
        {
            int idx = Array.FindIndex(_ip, ss => ss == ip);
            if (idx == -1) return;
            buffer[idx] = new byte[8];

            reader[idx].AutoScanMode(false);
            System.Threading.Thread.Sleep(100);
            reader[idx].AutoScanMode(true);
        }




        private void Connect()
        {
            log.OutInfo("Call Connect");

            try
            {
                for (int i = 0; i < reader.Length; i++)
                {
                    reader[i] = new HFReader(_ip[i]);
                    reader[i].LogPath = LogPath;
                    reader[i].Received += Reader_Received;
                    reader[i].Open(ScanMode);
                    log.OutInfo(_ip[i] + " IsConn=" + reader[i].IsConn);
                    ConnectStatus?.Invoke(i, _ip[i], reader[i].IsConn);
                }

                for (int i = 0; i < reader.Length; i++)
                {
                    if (reader[i].IsConn)
                    {
                        System.Threading.Thread.Sleep(100);
                        if (ScanMode)
                        {
                            bool rtn = reader[i].AutoScanMode(true);
                            log.OutInfo(_ip[i] + " ScanMode=auto, Return=" + rtn);
                            ScanStatus?.Invoke(i, _ip[i], rtn);
                        }
                        else
                        {
                            log.OutInfo(_ip[i] + " ScanMode=find");
                        }
                    }
                    else
                    {
                        reader[i].Close();
                        reader[i] = null;
                        log.OutInfo(_ip[i] + " Close");
                        ScanStatus?.Invoke(i, _ip[i], false);
                    }
                }
            }
            catch (Exception ex)
            {
                log.OutError(ex.Message);
            }
            System.Threading.Thread.Sleep(50);

        }

        //private void Reconnect()
        //{
        //    while (true)
        //    {
        //        for (int i = 0; i < reader.Length; i++)
        //        {
        //            try
        //            {
        //                System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
        //                System.Net.NetworkInformation.PingReply result = ping.Send(_ip[i], 1000);
        //                ping.Dispose();

        //                if (result.Status == System.Net.NetworkInformation.IPStatus.Success)
        //                {
        //                    if (reader[i] == null)
        //                    {
        //                        reader[i] = new Reader(_ip[i]);
        //                        reader[i].Received += Reader_Received;
        //                        //reader[i].Connect(false);
        //                        if (reader[i].IsConn)
        //                        {
        //                            System.Threading.Thread.Sleep(100);
        //                            reader[i].AutoScanMode(true);
        //                        }
        //                        else
        //                        {
        //                            reader[i].Close();
        //                            reader[i] = null;
        //                        }
        //                    }
        //                    else
        //                    {
        //                        if (!reader[i].IsConn)
        //                        {
        //                            reader[i].Close();
        //                            reader[i] = new Reader(_ip[i]);
        //                            reader[i].Received += Reader_Received;
        //                            //reader[i].Connect(false);
        //                            if (reader[i].IsConn)
        //                            {
        //                                System.Threading.Thread.Sleep(100);
        //                                reader[i].AutoScanMode(true);
        //                            }
        //                            else
        //                            {
        //                                reader[i].Close();
        //                                reader[i] = null;
        //                            }
        //                        }
        //                    }
        //                }
        //                else
        //                {
        //                    //拔网线
        //                    if (reader[i] != null && reader[i].IsConn)
        //                    {
        //                        reader[i].Close();
        //                        reader[i] = null;
        //                    }
        //                }
        //            }
        //            catch (Exception)
        //            { }

        //            System.Threading.Thread.Sleep(50);
        //        }
        //        System.Threading.Thread.Sleep(5000);
        //    }
        //}

        private void Reader_Received(string ip, byte[] buff)
        {
            try
            {
                int idx = Array.FindIndex(_ip, ss => ss == ip);
                if (idx == -1)
                {
                    log.OutInfo("ip " + ip + " 没有找到");
                    return;
                }
                Array.Copy(buff, buffer[idx], buff.Length);

                string val;
                if (buff[1] == 0)
                    val = "00";
                else
                    val = (char)buff[1] + buff[2].ToString();
                log.OutString(ip + " Received " + val);




                //if (currIP != ip)
                //{
                //    currIP = ip;
                //    GetValue?.Invoke(ip + ", " + val);
                //}

            }
            catch (Exception ex)
            {
                log.OutError(ex.Message);
            }
        }

        private void SaveLog()
        {
            //if (string.IsNullOrWhiteSpace(LogPath)) return;
            //if (!LogPath.EndsWith("\\\\")) LogPath += "\\\\";
            //if (!System.IO.Directory.Exists(LogPath))
            //    System.IO.Directory.CreateDirectory(LogPath);

            //string _logFile = string.Format("{0}RFID_{1:yyyy-MM-dd}.log", LogPath, DateTime.Now);

            //while (true)
            //{
            //    System.Threading.Thread.Sleep(200);
            //    if (log.TryDequeue(out string result))
            //    {
            //        try
            //        {
            //            System.IO.File.AppendAllText(_logFile, result, System.Text.Encoding.UTF8);
            //        }
            //        catch (Exception)
            //        { }
            //    }
            //}
        }




    }
}