国内“虚拟试妆”之路任重道远

关注最新技术动态

impinj固定式阅读器数据解析中Rfid筛选器实现

在做物联网仓库管理中用到RFID标签进行盘点和出库扫描检查,其中在仓库门口的impinj固定式阅读器对进出的货物进行检测,在读取rfid时候需要对epc标签进行过滤,实现2个过滤器,第一个是正则过滤,第二个是时间,在60秒内只读取一次,默认tag是一秒读200次,下面是过滤器的实现:输入

dataSource--tag信息类

device---设备配置类(包含读取时间和过滤条件的xml,在最后)

       

       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Org.LLRP.LTK.LLRPV1;
using Org.LLRP.LTK.LLRPV1.DataType;
using Org.LLRP.LTK.LLRPV1.Impinj;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Collections.ObjectModel;
using com.dn.Edgenode.Plugins.Data;
using com.dn.Edgenode.Plugins.Rfid.Impinj;
using com.dn.Edgenode.Plugins.Rfid.Impinj.Config;
using System.Text.RegularExpressions;

namespace Data
{
    public class TagFilter : Filter<NormalizedData>
    {
        private static ILog log = log4net.LogManager.GetLogger("com.dn.Edgenode.Log.Engine.Plugin");
        private Dictionary<string, DateTime> tagGroupTimeDic = new Dictionary<string, DateTime>();
        public object device{ get; set; }

        public override Func<NormalizedData, bool> Condition
        {
            get
            {
                return FilterEpcByConf;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        private bool FilterEpcByConf(NormalizedData dataSource)
        {
            ReaderSite rs = this.device as ReaderSite;
            TagData tag = dataSource as TagData;
            string epc = tag.Data["Epc"].ToString();
            bool re = false;

            #region Fiter by Regex rule
            try
            {
                if (!string.IsNullOrEmpty(rs.ReaderCfg.filter.rule.Value) && rs.ReaderCfg.filter.rule.operation != null)
                {
                    if (rs.ReaderCfg.filter.rule.operation == readerCfgFilterRuleOperation.Submit)
                    {
                        if (!Regex.IsMatch(epc, rs.ReaderCfg.filter.rule.Value))
                        {
                            return false;
                        }
                        else
                        {
                            //do nothing
                        }
                    }
                    else if (rs.ReaderCfg.filter.rule.operation == readerCfgFilterRuleOperation.None)
                    {
                        if (Regex.IsMatch(epc, rs.ReaderCfg.filter.rule.Value))
                        {
                            return false;
                        }
                        else
                        {
                            //do nothing
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                log.ErrorFormat("FilterEpcByConf: do fiter by rule error: {0}", ex.Message);
            }
            #endregion

            #region Fiter by Time
            try
            {
                string[] epcGroup = null;
                int readingTimeout = 0;
                //epc not in config ,Submit datas
                foreach (readerCfgTagGroup rg in rs.ReaderCfg.tagGroups)
                {
                    if (rg.Enabled == true && rg.Value.Contains(epc))
                    {
                        epcGroup = rg.Value.Split(';');
                        readingTimeout = rg.readingTimeout;
                        break;
                    }
                }
                //epc not in config or this tag's tagGroup is disanable ,Submit data
                if (epcGroup == null || epcGroup.Length == 0)
                {
                    return true;
                }

                if (!tagGroupTimeDic.ContainsKey(epc))
                {
                    //Submit data
                    re = true;
                    tagGroupTimeDic.Add(epc, DateTime.Now);

                }
                else
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime epcTime = tagGroupTimeDic[epc];
                    //find max time
                    foreach (string epcItem in epcGroup)
                    {
                        if (tagGroupTimeDic.ContainsKey(epcItem))
                        {
                            epcTime = tagGroupTimeDic[epcItem] < epcTime ? tagGroupTimeDic[epcItem] : epcTime;
                        }
                    }
                    //check time
                    if (dtNow.Subtract(epcTime).TotalMilliseconds > readingTimeout)
                    {
                        re = true;
                        foreach (string epcItem in epcGroup)
                        {
                            if (tagGroupTimeDic.ContainsKey(epcItem))
                            {
                                tagGroupTimeDic.Remove(epcItem);
                            }
                        }
                        tagGroupTimeDic[epc] = dtNow;
                    }
                    else
                    {
                        //do nothing
                        re = false;
                    }
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("FilterEpcByConf: do fiter by rule error: {0}", ex.Message);
            }
            #endregion

            return re;
        }
    }
}

posted on 2012-08-22 16:39  新技术动态  阅读(1005)  评论(1编辑  收藏  举报

导航