RFIDManager.cs 8.7 KB
using Asa.RFID;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public  class RFIDManager
    {
        public static Dictionary<string, Reader> RFReaderMap = new Dictionary<string, Reader>();

        private static List<string> RfIPList = new List<string>();
        private static System.Timers.Timer conTimer = null;

        public  static void ConnectRFIOList(List<string> rfioNameList)
        {
            try { 
            if (conTimer == null)
            {
                conTimer = new System.Timers.Timer();
                conTimer.AutoReset = true;
                conTimer.Interval = 60000;
                conTimer.Elapsed += ConTimer_Elapsed;
            }
            conTimer.Enabled = false;
            RfIPList = new List<string>(rfioNameList);
            foreach (string ip in rfioNameList)
            {
                ConnectionIP(ip);
            }
            if (RfIPList.Count > 0)
            {
                conTimer.Start();
            }
            }catch(Exception ex)
            {
                LogUtil.error("ConnectRFIOList出错:" + ex.ToString());
            }
        }
        private static bool isProcess = false;
        private static DateTime lastTime = DateTime.Now;
        private static void ConTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            TimeSpan span = DateTime.Now - lastTime;
            if (span.TotalMinutes < RfIPList.Count && isProcess)
            {
                return;
            }
            isProcess = true;
            lastTime = DateTime.Now;
            try
            {
                List<string> list = new List<string>(RfIPList);
                if (list.Count > 0)
                {
                    LogUtil.info("开始重连RFIP模块 ------------");
                    foreach (string ip in list)
                    {
                        ConnectionIP(ip);
                    }
                    LogUtil.info("结束重连RFIP模块 ------------");
                }
                GC.Collect();
            }
            catch (Exception ex)
            {
                LogUtil.error("RFIP ConTimer_Elapsed 出错: " + ex.ToString());
            }
            isProcess = false;
        }

        internal static void ConnectionIP(string rfid)
        {
            Reader rfidReader = null;
            if (RFReaderMap.ContainsKey(rfid))
            {
                rfidReader = RFReaderMap[rfid];
                if (null != rfidReader)
                {
                    rfidReader.Close();
                    rfidReader = null;
                }
                RFReaderMap.Remove(rfid);
            }

            string logName = "RFIP模块[" + rfid + "] ";
            try
            { 
                LogUtil.debug("开始连接" + logName + ",尝试重连3次");
                for (int i = 1; i <= 3; i++)
                {
                    rfidReader = new Reader();
                    //  rfidReader.LocalIP = "192.168.100.101";
                    rfidReader.RemoteIP = rfid; 
                    bool result = rfidReader.Connect();
                    if (result)
                    {
                        rfidReader.AutoScan(true);
                        LogUtil.info("第【" + i + "】次连接 " + logName + " 成功:");
                        Thread.Sleep(10);
                        RFReaderMap.Add(rfid, rfidReader);
                        if (RfIPList.Contains(rfid))
                        {
                            RfIPList.Remove(rfid);
                        }
                        break;
                    }
                    else
                    {
                        LogUtil.error("第【" + i + "】次连接 " + logName + " 失败:" + "");
                    }
                    Thread.Sleep(5); 
                }

            }
            catch (Exception error)
            {
                LogUtil.error("连接RFIP模块 " + logName + " 出错:" + error.ToString());
            }
        }
        public  static void CloseAllConnection()
        {
            foreach (Reader reader in RFReaderMap.Values)
            {
                reader.Close();
            }
        }
        public static bool WriteData(string IP, RFIDData obj)
        { 
            byte[] sendData = obj.ToData();
            return WriteData(IP, sendData, 3);
        }
        public static RFIDData ReadData(string IP)
        { 
            byte[] reviceData = ReadData(IP, 3);
            if (reviceData != null)
            {
                return new RFIDData(reviceData);
            }
            return null;
        }
        public static string SearchIP(string localIp)
        {
            string ip = "";
            try
            {
                Reader rfReader = new Reader();
                rfReader.LocalIP = localIp;
                ip = rfReader.FindIP();
            }
            catch (Exception ex)
            { 
            }
            return ip;
        }
        public static bool FindRFID(string IP)
        {
            try
            {
                Reader rfReader = getRfReader(IP);
                if (rfReader != null)
                {
                    return rfReader.FindRFID();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("RFIP FindRFID [" + IP + "] 出错 :" + ex.ToString());
            }
            return false;
        }
        public static byte[] ReadData(string IP, int reReadCount = 1,int startIndex=0,int readLength=8)
        {
            try
            {
                Reader rfReader = getRfReader(IP);
                if (rfReader == null)
                {
                    return null;
                }
                for (int i = 1; i <= reReadCount; i++)
                {
                    byte[] reviceData = rfReader.Read(startIndex, readLength);
                    if (reviceData != null)
                    {
                        return reviceData;
                    }
                    LogUtil.error("RFIP ReadData [" + IP + "] 第" + i + "次失败 ");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("RFIP ReadData [" + IP + "] 出错 :" + ex.ToString());
            }
            return null;
        }
        public static bool WriteData(string IP, byte[] data, int reWriteCount = 1)
        {
            try
            {
                Reader rfReader = getRfReader(IP);
                if (rfReader == null)
                {
                    return false;
                }
                for (int i = 1; i <= reWriteCount; i++)
                {
                    bool result = rfReader.Write(data);
                    if (result)
                    {
                        return result;
                    }
                    LogUtil.error("RFIP WriteData [" + IP + "] 第" + i + "次失败:");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("RFIP WriteData [" + IP + "]   出错 :" + ex.ToString());
            }
            return false;
        }
        private static Reader getRfReader(string IP)
        {
            Reader rfReader = null;
            if (RFReaderMap.ContainsKey(IP))
            {
                rfReader = RFReaderMap[IP];
            }
            else
            {
                LogUtil.error("getRfReader 没有连接RFIP模块:" + IP);
            }
            return rfReader;
        }
    }
    public class RFIDData
    {
        /// <summary>
        /// RFID类型,区分是料架还是托盘,托盘=E
        /// </summary>
        public char  RFType= 'E';
        /// <summary>
        /// 托盘编号,从1-32
        /// </summary>
        public int Num = 0;

        public RFIDData (int num,char t='E')
        {
            this.RFType = t;
            this.Num = num;
        }

        public RFIDData(byte[] data)
        {
            try
            {
                RFType =(char)data[1]; 
                Num = Convert.ToInt32(data[2]); 
            }
            catch (Exception ex)
            {
                LogUtil.error("RFIP 数据【" + data + "】 获取编码失败");
            }
        }
        public byte[] ToData(int dataLength=10)
        {
            byte[] sendData = new byte[dataLength];
            for (int i = 0; i < sendData.Length; i++)
            {
                sendData[i] = 0x00;
            }
            sendData[0] = (byte)RFType;
            sendData[1] = (byte)Num;
            return sendData;
        }
        public string ToStr()
        {
            return "Type=["+RFType+"],Num=["+Num+"]";
        }
    }
}