基于读取车载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; } } }