基于读取车载RFID标签自动开关车库门c#算法实现

最近做物联网自动识别技术,遇到一个算法难题,如下发给大家分享:

1 问题描述

捕获

2.流程图稍后上,

3.编码实现

编码实现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 com.dn.
{
   
    internal 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)
        {
            bool re = false;
            ReaderSite rs = this.device as ReaderSite;
            string epc = dataSource.Data["Epc"].ToString();

            #region Fiter by Regex rule
            if (!string.IsNullOrEmpty(rs.ReaderCfg.filter.rule.Value) && !rs.ReaderCfg.filter.rule.operation.Equals(null))
            {
                if (Regex.IsMatch(epc, rs.ReaderCfg.filter.rule.Value))
                {
                    if (rs.ReaderCfg.filter.rule.operation == readerCfgFilterRuleOperation.Submit)
                    {
                        //do nothing
                        // continue Fiter by Time.
                    }
                    else if (rs.ReaderCfg.filter.rule.operation == readerCfgFilterRuleOperation.None)
                    {
                        //Ignored Fiter by Time
                        return false;
                    }
                }
            }
            #endregion

            #region Fiter by Time

            readerCfgTagGroup tagsArray = rs.ReaderCfg.tagGroups.Where(p => p.tag.Contains(epc)).FirstOrDefault();

            //epc not in config or this tag's tagGroup is disanable ,Submit data
            if (tagsArray == null || tagsArray.Enabled == false || tagsArray.tag == null || tagsArray.tag.Length == 0)
            {
                re = true;
            }
            else
            {
                DateTime dtNow = DateTime.Now;

                if (!tagGroupTimeDic.ContainsKey(epc))
                {
                    //Submit data
                    re = true;
                }
                else
                {
                    DateTime epcTime = tagGroupTimeDic[epc];
                    //find max time, it is the last time of reading the any of tags in the group.
                    foreach (string epcItem in tagsArray.tag)
                    {
                        if (tagGroupTimeDic.ContainsKey(epcItem))
                        {
                            epcTime = tagGroupTimeDic[epcItem] > epcTime ? tagGroupTimeDic[epcItem] : epcTime;
                        }
                    }

                    //check time
                    if (dtNow.Subtract(epcTime).TotalMilliseconds > tagsArray.readingTimeout)
                    {
                        re = true;

                        // the timespan is greater than reading timeout, it means the group of tags has left the reader before already.
                        // so, remove all.(so the act will be as the same as the firs seen of them for the reader.)
                        foreach (string epcItem in tagsArray.tag)
                        {
                            if (tagGroupTimeDic.ContainsKey(epcItem))
                            {
                                tagGroupTimeDic.Remove(epcItem);
                            }
                        }
                    }
                    else
                    {
                        //do nothing
                        re = false;
                    }
                }

                // add/update the reading time.
                tagGroupTimeDic[epc] = dtNow;
            }

            if (!re)
            {
                log.InfoFormat("EPC Ignored: {0}", epc);
            }

            #endregion

            return re;
        }
    }
}

 

转载于:https://www.cnblogs.com/sung/archive/2012/08/23/2652647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值