You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
12 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using GRreader;
using MaterialTraceability.Common;
using MaterialTraceability.Entity.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaterialTraceability.Business
{
public class CFliterRFID
{
/// <summary>
/// 通过读取次数找最好标签
/// </summary>
/// <param name="rfidlist"></param>
/// <returns></returns>
public TagInfo fliterBycount(List<TagInfo> rfidlist)
{
try
{
int maxIndex = 0;
if (rfidlist.Count < 1)
{
LogHelper.RfidLog("过滤标签太少,无法获取");
return null;
}
else if (rfidlist.Count == 1)
{
///LogHelper.Info("过滤标签太少,无法获取");
return rfidlist[0];
}
else
{
int first = rfidlist[0].Count;
for (int i = 1; i < rfidlist.Count; i++)
{
if (first < rfidlist[i].Count)
{
maxIndex = i;
first = rfidlist[i].Count;
}
LogHelper.RfidLog(rfidlist[i].Antana + "号天线读到标签:" + rfidlist[i].EPCstring + ",信号强度:" + rfidlist[i].RSSI + ",读取次数:" + rfidlist[i].Count);
}
}
return rfidlist[maxIndex];
}
catch (Exception ex)
{
LogHelper.RfidLog("过滤标签异常读到标签数量为" + rfidlist.Count + ex.ToString());
return null;
}
}
/// <summary>
/// 通过信号强度读取标签
/// </summary>
/// <param name="rfidlist"></param>
/// <returns></returns>
public TagInfo fliterByRSSI(List<TagInfo> rfidlist)
{
try
{
int maxIndex = 0;
if (rfidlist.Count < 1)
{
return null;
}
else if (rfidlist.Count == 1)
{
return rfidlist[0];
}
else
{
int first = rfidlist[0].Count;
for (int i = 1; i < rfidlist.Count; i++)
{
if (first < rfidlist[i].RSSI)
{
maxIndex = i;
first = rfidlist[i].RSSI;
}
}
}
return rfidlist[maxIndex];
}
catch (Exception ex)
{
LogHelper.RfidLog("过滤标签异常读到标签数量为" + rfidlist.Count + ex.ToString());
return null;
}
}
/// <summary>
/// 冷压四通道算法
/// 根据小标签读到的为主小天线的天线号分别是1458
/// 大天线的天线号分别是2357
/// 大天线可能会误读,小天线一般不会,所以在大天线读到的里面去掉小天线的
/// </summary>
/// <param name="rfidlist"></param>
/// <returns></returns>
public List<TagInfo> fliterByTongDao(List<TagInfo> rfidlist)
{
///先不去小天线的了,从所有天线自身读取中取最好的
List<TagInfo> message = new List<TagInfo>();
try
{
List<TagInfo> ant1list = new List<TagInfo>();
List<TagInfo> ant2list = new List<TagInfo>();
List<TagInfo> ant3list = new List<TagInfo>();
List<TagInfo> ant4list = new List<TagInfo>();
List<TagInfo> ant5list = new List<TagInfo>();
List<TagInfo> ant6list = new List<TagInfo>();
List<TagInfo> ant7list = new List<TagInfo>();
List<TagInfo> ant8list = new List<TagInfo>();
foreach (var a in rfidlist)
{
if (a.Antana == 1)
{
ant1list.Add(a);
}
else if (a.Antana == 2)
{
ant2list.Add(a);
}
else if (a.Antana == 3)
{
ant3list.Add(a);
}
else if (a.Antana == 4)
{
ant4list.Add(a);
}
else if (a.Antana == 5)
{
ant5list.Add(a);
}
else if (a.Antana == 6)
{
ant6list.Add(a);
}
else if (a.Antana == 7)
{
ant7list.Add(a);
}
else if (a.Antana == 8)
{
ant8list.Add(a);
}
}
message.Add(fliterBycount(ant1list));
message.Add(fliterBycount(ant2list));
message.Add(fliterBycount(ant3list));
message.Add(fliterBycount(ant4list));
message.Add(fliterBycount(ant5list));
message.Add(fliterBycount(ant6list));
message.Add(fliterBycount(ant7list));
message.Add(fliterBycount(ant8list));
return message;
}
catch (Exception ex)
{
LogHelper.RfidLog("过滤标签异常读到标签数量为" + rfidlist.Count + ex.ToString());
return null;
}
}
/// <summary>
/// 把每个天线最好的结果展示出来
/// </summary>
/// <param name="rfidlist"></param>
/// <returns></returns>
public string filterByAnt(List<TagInfo> rfidlist, int ant,string outEPC)
{
try
{
List<TagInfo> message = new List<TagInfo>();
foreach (var a in rfidlist)
{
string realEpc = a.EPCstring.Substring(0, a.EPCstring.Length - 2);
if (a.Antana == ant&& realEpc!= outEPC)
{
message.Add(a);
}
}
return filterByTagCount(message);
}
catch (Exception ex)
{
LogHelper.RfidLog("过滤标签异常读到标签数量为" + rfidlist.Count + ex.ToString());
return null;
}
}
/// <summary>
/// 从获取结果中获取最优的
/// 由于标签中最后两位为010203.....06
/// 其实代表的是同一卷筒所以在读取时应该取读到01-06中最多的
/// </summary>
/// <param name="rfidlist"></param>
/// <returns></returns>
public string filterByTagCount(List<TagInfo> rfidlist)
{
if (rfidlist.Count < 1)
{
return "";
}
#region 把标签相同(除了后两位),整理成一个
List<FilterRFIDInfo> LTag = new List<FilterRFIDInfo>();
foreach (var T in rfidlist)
{
//如果是第一个标签直接添加
if (LTag.Count < 1)
{
string epc = "";
if (AppConfigDto.Instance.processId == "AB")
{
epc = T.EPCstring;
}
else
{
epc = T.EPCstring.Substring(0, T.EPCstring.Length - 2);
}
LTag.Add(new FilterRFIDInfo()
{
//tagInfo = T,
TagCount = 1,
TagString = epc,
TagAllCount=T.Count,
MaxRSSI=T.RSSI
});
continue;
}
//判断是否和当前标签相同
bool newflag = true;
foreach (var newtag in LTag)
{
string epc = "";
if (AppConfigDto.Instance.processId == "AB")
{
epc = T.EPCstring;
}
else
{
epc = T.EPCstring.Substring(0, T.EPCstring.Length - 2);
}
if (epc == newtag.TagString)
{
newtag.TagCount++;
newtag.TagAllCount += T.Count;
if (newtag.MaxRSSI < T.RSSI)
{
newtag.MaxRSSI = T.RSSI;
}
newflag = false;
break;
}
}
//如果是新标签
if (newflag)
{
LTag.Add(new FilterRFIDInfo()
{
//tagInfo = T,
TagCount = 1,
TagString = T.EPCstring.Substring(0, T.EPCstring.Length - 2),
TagAllCount = T.Count,
MaxRSSI = T.RSSI
});
}
}
#endregion
return FliterByTag(LTag);
}
/// <summary>
/// 在所有标签里选择标签读取最多的
/// </summary>
/// <param name="LTag"></param>
/// <returns></returns>
private string FliterByTag(List<FilterRFIDInfo> LTag)
{
try
{
FilterRFIDInfo rfid = new FilterRFIDInfo();
//方法
foreach (var v in LTag)
{
if (rfid == null)
{
rfid.TagCount = v.TagCount;
rfid.TagString = v.TagString;
rfid.TagAllCount = v.TagAllCount;
rfid.MaxRSSI = v.MaxRSSI;
continue;
}
///选择读取标签最多的RFID比如x标签读到x01,x02,x03,y标签读到y01,y02选择x
if (rfid.TagCount < v.TagCount)
{
rfid.TagCount = v.TagCount;
rfid.TagString = v.TagString;
rfid.TagAllCount = v.TagAllCount;
rfid.MaxRSSI = v.MaxRSSI;
}
else if (rfid.TagCount == v.TagCount)
{
//如果标签数量相同,取标签读取次数最多
if (rfid.TagAllCount < v.TagAllCount)
{
rfid.TagCount = v.TagCount;
rfid.TagString = v.TagString;
rfid.TagAllCount = v.TagAllCount;
rfid.MaxRSSI = v.MaxRSSI;
}
}
}
return rfid.TagString;
}
catch(Exception ex)
{
LogHelper.RfidLog("过滤函数FliterByTag报错" + ex.ToString());
return null;
}
}
/// <summary>
/// 大天线过滤条件,先把小天线的过滤掉,然后再获取最多的
/// </summary>
/// <returns></returns>
public string BigFilter()
{
return "";
}
}
public class FilterRFIDInfo
{
//public List<TagInfo> tagInfo { get; set; }
/// <summary>
/// 读取-01-02-03标签的数量
/// </summary>
public int TagCount { get; set; }
public string TagString { get; set; }
/// <summary>
/// 所有标签的数量之和
/// </summary>
public int TagAllCount { set; get; }
/// <summary>
/// 最好信号强度
/// </summary>
public int MaxRSSI { get; set; }
}
}