|
|
using GRreader;
|
|
|
using MaterialTraceability.Common;
|
|
|
using MaterialTraceability.Entity.DAO;
|
|
|
using MaterialTraceability.Entity.DTO;
|
|
|
using MaterialTraceability.Entity.UpLoad;
|
|
|
using MaterialTraceability.Rfid;
|
|
|
using MaterialTraceability.SqlSugar;
|
|
|
using MaterialTraceability.SqlSugar.ServiceImpl;
|
|
|
using SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Linq;
|
|
|
using System.Linq.Expressions;
|
|
|
using System.Reflection;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace MaterialTraceability.Business
|
|
|
{
|
|
|
public class EquipBusiness
|
|
|
{
|
|
|
Thread thread;
|
|
|
Thread Connect;
|
|
|
static List<ProEquip> _equipList = new List<ProEquip>();
|
|
|
public List<ProEquip> IpequipList = new List<ProEquip>();
|
|
|
bool ReadContinue = true;
|
|
|
private Semaphore m_ReadSem = new Semaphore(0, 100000);
|
|
|
public delegate void FrmDisplay();
|
|
|
public int waitTime = Convert.ToInt16(ConfigHelper.GetConfig("readTime"));
|
|
|
|
|
|
public event FrmDisplay EventDisplay;
|
|
|
private BackgroundWorker m_bgwDeviceDetect = new BackgroundWorker();
|
|
|
private ManualResetEvent StartDetectEvent = new ManualResetEvent(false);
|
|
|
private ManualResetEvent ExitEvent = new ManualResetEvent(false);
|
|
|
private ManualResetEvent DetectEvent = new ManualResetEvent(false);
|
|
|
|
|
|
private AppConfigDto appConfig = AppConfigDto.Instance;
|
|
|
private SqlSugarClient _db = SqlGenerator.GetMySqlInstance();
|
|
|
|
|
|
private IBaseServices<ProEquip> equipServices = new BaseServices<ProEquip>();
|
|
|
private IBaseServices<ProReadRecord> readServices = new BaseServices<ProReadRecord>();
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取标签信息
|
|
|
/// </summary>
|
|
|
/// <param name="equipid">设备编号</param>
|
|
|
/// <param name="isFilter">是否开启过滤</param>
|
|
|
/// <param name="eq"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<TagInfo> gettaglist(string equipid,bool isFilter, out ProEquip eq)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
LogHelper.Info("进入gettaglist函数:" + equipid);
|
|
|
List<TagInfo> taglist = new List<TagInfo>();
|
|
|
List<TagInfo> result = new List<TagInfo>();
|
|
|
foreach (var equip in _equipList)
|
|
|
{
|
|
|
if (equip.equipId.ToString() == equipid)
|
|
|
{
|
|
|
taglist = equip.DeviceAdapter.Device_GetTagInfoList(DeviceType.Mesnac_GRUR445, waitTime * 1000);
|
|
|
if (taglist.Count < 1)
|
|
|
{
|
|
|
taglist = equip.DeviceAdapter.Device_GetTagInfoList(DeviceType.Mesnac_GRUR445, waitTime * 1000);
|
|
|
}
|
|
|
eq = equip;
|
|
|
LogHelper.RfidLog("gettaglist函数结果:" + JsonChange.ModeToJson(taglist));
|
|
|
if (isFilter)
|
|
|
{
|
|
|
#region 读取结果过滤,返回读取次数大于两次的数据 Add By WenJY 2023-05-11
|
|
|
result = taglist.Where(x => x.Count > 2).ToList();
|
|
|
LogHelper.Info($"gettaglist函数结果过滤,返回读取次数大于2的数据{JsonChange.ModeToJson(result)}");
|
|
|
#endregion
|
|
|
return result;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return taglist;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
eq = null;
|
|
|
LogHelper.Info($"gettaglist函数结果:{JsonChange.ModeToJson(taglist)},设备编号{equipid};未匹配到设备信息;");
|
|
|
return taglist;
|
|
|
}
|
|
|
catch(Exception ex)
|
|
|
{
|
|
|
LogHelper.Info($"gettaglist函数异常:{ex.Message}");
|
|
|
eq = null;
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 涂布、模切、冷压上料读取
|
|
|
/// </summary>
|
|
|
/// <param name="equipId"></param>
|
|
|
/// <param name="antana"></param>
|
|
|
/// <returns></returns>
|
|
|
public string ReadEPCByAntana(int equipId)
|
|
|
{
|
|
|
LogHelper.Info("进入ReadEPCByAntana函数");
|
|
|
ProEquip proEquip = _equipList.Where(x => x.equipId == equipId).FirstOrDefault();
|
|
|
List<TagInfo> info2 = this.gettaglist(proEquip.equipId.ToString(),false, out proEquip);
|
|
|
|
|
|
#region 2023/2/6关闭
|
|
|
// ----------------------------------------------------------------------2023/2/6关闭
|
|
|
// info2遍历信号强度、读取次数,上传至服务器数据库读取记录表ReadRecord,
|
|
|
// 过滤前的数据is_success字段统一设置成999,便于数据库查找观察
|
|
|
// 串读问题解决即删除方法,并减少web数据库表pro_readrecord的count和rssi字段
|
|
|
//List<ReadRecord> recordList = new List<ReadRecord>();
|
|
|
|
|
|
//foreach (TagInfo tag in info2)
|
|
|
//{
|
|
|
// ReadRecord info22 = new ReadRecord()
|
|
|
// {
|
|
|
// Count = tag.Count,
|
|
|
// RSSI = tag.RSSI,
|
|
|
// machineId = proEquip.machineId,
|
|
|
// resource = appConfig.resource,
|
|
|
// equipId = proEquip.equipId,
|
|
|
// positionId = proEquip.positionId,
|
|
|
// ant = tag.Antana,
|
|
|
// rfidStr = tag.EPCstring,
|
|
|
// is_success = 999,
|
|
|
// recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
|
// };
|
|
|
// recordList.Add(info22);
|
|
|
//}
|
|
|
//int resultCode = _db.Insertable<ReadRecord>(recordList).ExecuteCommand();
|
|
|
//if (resultCode > 0)
|
|
|
//{
|
|
|
// LogHelper.Info("标签强度上传成功");
|
|
|
//}
|
|
|
//LogHelper.Info("标签强度上传失败");
|
|
|
// -------------------------------------------------------------------
|
|
|
#endregion
|
|
|
|
|
|
if (info2.Count == 0)
|
|
|
{
|
|
|
return "";
|
|
|
}
|
|
|
CFliterRFID cFliterRFID = new CFliterRFID();
|
|
|
var result = cFliterRFID.filterByTagCount(info2);
|
|
|
|
|
|
if (result == "") return "";
|
|
|
if (StringExtension.IsBlank(result))
|
|
|
{
|
|
|
return "";
|
|
|
}
|
|
|
string info = result;
|
|
|
//info = info.Substring(0, info.Length - 2);
|
|
|
LogHelper.Info("ReadEpcByAntana函数读取结果:" + result);
|
|
|
ProReadRecord readRecord = new ProReadRecord()
|
|
|
{
|
|
|
MachineID = proEquip.machineId,
|
|
|
EquipID = proEquip.equipId,
|
|
|
PositionID = proEquip.positionId,
|
|
|
Ant = StringChange.ParseToInt(proEquip.equipAnt),
|
|
|
ReadEPC = result,
|
|
|
Result = StringExtension.IsBlank(result) ? "0" : "1",
|
|
|
ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
};
|
|
|
readServices.Add(readRecord);
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 读取EPC并过滤读取次数
|
|
|
/// </summary>
|
|
|
/// <param name="equipId"></param>
|
|
|
/// <returns></returns>
|
|
|
public string ReadEPCAndFliterCount(int equipId)
|
|
|
{
|
|
|
LogHelper.Info("进入ReadEPCByAntana函数");
|
|
|
ProEquip proEquip = _equipList.Where(x => x.equipId == equipId).FirstOrDefault();
|
|
|
List<TagInfo> info2 = this.gettaglist(proEquip.equipId.ToString(), true, out proEquip);
|
|
|
|
|
|
if (info2.Count == 0)
|
|
|
{
|
|
|
return "";
|
|
|
}
|
|
|
CFliterRFID cFliterRFID = new CFliterRFID();
|
|
|
var result = cFliterRFID.filterByTagCount(info2);
|
|
|
|
|
|
if (result == "") return "";
|
|
|
if (StringExtension.IsBlank(result))
|
|
|
{
|
|
|
return "";
|
|
|
}
|
|
|
string info = result;
|
|
|
LogHelper.Info("ReadEpcByAntana函数读取结果:" + result);
|
|
|
ProReadRecord readRecord = new ProReadRecord()
|
|
|
{
|
|
|
MachineID = proEquip.machineId,
|
|
|
EquipID = proEquip.equipId,
|
|
|
PositionID = proEquip.positionId,
|
|
|
Ant = StringChange.ParseToInt(proEquip.equipAnt),
|
|
|
ReadEPC = result,
|
|
|
Result = StringExtension.IsBlank(result) ? "0" : "1",
|
|
|
ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
};
|
|
|
readServices.Add(readRecord);
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生产过程中读取RFID信息
|
|
|
/// </summary>
|
|
|
/// <param name="equipId"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<TagInfo> ReadEpcByProduction(int equipId)
|
|
|
{
|
|
|
LogHelper.Info("ReadEpcByProduction函数");
|
|
|
ProEquip proEquip = _equipList.Where(x => x.equipId == equipId).FirstOrDefault();
|
|
|
List<TagInfo> tagInfos = this.gettaglist(proEquip.equipId.ToString(),true, out proEquip);
|
|
|
|
|
|
if (tagInfos.Count == 0)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
return tagInfos;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生产结束后过滤生产过程中读取的RFID信息
|
|
|
/// </summary>
|
|
|
/// <param name="tagInfos"></param>
|
|
|
/// <returns></returns>
|
|
|
public string FilterEpcByProductionEnd(List<TagInfo> tagInfos)
|
|
|
{
|
|
|
CFliterRFID cFliterRFID = new CFliterRFID();
|
|
|
var result = cFliterRFID.filterByTagCount(tagInfos);
|
|
|
|
|
|
if (result == "") return "";
|
|
|
if (StringExtension.IsBlank(result))
|
|
|
{
|
|
|
return "";
|
|
|
}
|
|
|
string info = result;
|
|
|
LogHelper.Info("FilterEpcByProductionEnd过滤结果:" + result);
|
|
|
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 冷压四通道,八通道读取
|
|
|
/// </summary>
|
|
|
/// <param name="positionGroup">设备位置列表,对应数据库里的</param>
|
|
|
/// <returns></returns>
|
|
|
public List<TagMessage> ReadEPCByAntanaGroup(List<int> positionGroup)
|
|
|
{
|
|
|
//必须是同一个设备的不同的天线号
|
|
|
List<TagMessage> tagList = new List<TagMessage>();
|
|
|
int position = positionGroup.FirstOrDefault();
|
|
|
ProEquip proEquip = _equipList.Where(x => x.positionId == position).FirstOrDefault();
|
|
|
//ProEquip proEquip = _equipList.Where(x => x.equipId == equipId).FirstOrDefault();//这里只获取了第一个,设备
|
|
|
var info2 = this.gettaglist(proEquip.equipId.ToString(),false, out proEquip);
|
|
|
/////做日志用,后期可以不用了
|
|
|
//foreach (var a in info2)
|
|
|
//{
|
|
|
// LogHelper.RfidLog("RFID读取日志:"+a.);
|
|
|
//}
|
|
|
//
|
|
|
//把数据都都上来
|
|
|
if (info2.Count == 0)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
string outepc = "";
|
|
|
|
|
|
#region ///先把小天线的最好结果取出来
|
|
|
int minpostion = FindMinAntPostion(positionGroup);
|
|
|
TagMessage ta = GetAntEPC(info2, minpostion, "");
|
|
|
outepc = ta.EPC;
|
|
|
tagList.Add(ta);
|
|
|
#endregion 如果不需要可以注释掉
|
|
|
|
|
|
foreach (var item in positionGroup)
|
|
|
{
|
|
|
if (minpostion == item)
|
|
|
continue;
|
|
|
tagList.Add(GetAntEPC(info2, item, outepc));
|
|
|
}
|
|
|
//做日志用
|
|
|
foreach (var a in tagList)
|
|
|
{
|
|
|
LogHelper.RfidLog("RFID读取日志:" + a.position + "位置读到标签:" + a.EPC);
|
|
|
}
|
|
|
return tagList;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取小天线的position
|
|
|
/// </summary>
|
|
|
/// <param name="positionGroup"></param>
|
|
|
/// <returns></returns>
|
|
|
private int FindMinAntPostion(List<int> positionGroup)
|
|
|
{
|
|
|
int minPostion = 0;
|
|
|
foreach (var po in positionGroup)
|
|
|
{
|
|
|
//小天线的位置是1,3
|
|
|
if (po == 1 || po == 3)
|
|
|
{
|
|
|
minPostion = po;
|
|
|
//positionGroup.Remove(po);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return minPostion;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取天线最好的结果
|
|
|
/// </summary>
|
|
|
/// <param name="taglist"></param>
|
|
|
/// <param name="pos"></param>
|
|
|
/// <param name="outEPC">排除小天线的epc,如果是小天线写""</param>
|
|
|
/// <returns></returns>
|
|
|
private TagMessage GetAntEPC(List<TagInfo> taglist, int pos, string outEPC)
|
|
|
{
|
|
|
ProEquip equip = _equipList.Where(x => x.positionId == pos).FirstOrDefault();
|
|
|
CFliterRFID cFliterRFID = new CFliterRFID();
|
|
|
var result = cFliterRFID.filterByAnt(taglist, Convert.ToInt32(equip.equipAnt), outEPC);
|
|
|
TagMessage tag = new TagMessage()
|
|
|
{
|
|
|
EPC = result,
|
|
|
position = pos
|
|
|
};
|
|
|
LogHelper.Info("ReadEpcByAntana函数读取结果:" + result);
|
|
|
readServices.Add(new ProReadRecord()
|
|
|
{
|
|
|
MachineID = equip.machineId,
|
|
|
EquipID = equip.equipId,
|
|
|
PositionID = equip.positionId,
|
|
|
Ant = StringChange.ParseToInt(equip.equipAnt),
|
|
|
ReadEPC = result,
|
|
|
Result = StringExtension.IsBlank(result) ? "0" : "1",
|
|
|
ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
});
|
|
|
return tag;
|
|
|
}
|
|
|
|
|
|
//public string TBReadEPC(int equipId)
|
|
|
//{
|
|
|
// LogHelper.Info("进入TBReadEPC函数");
|
|
|
// ProEquip proEquip = _equipList.Where(x => x.equipId == equipId).FirstOrDefault();
|
|
|
// var info2 = this.gettaglist(proEquip.equipId.ToString(), out proEquip);
|
|
|
// if (info2.Count == 0)
|
|
|
// {
|
|
|
// return "";
|
|
|
// }
|
|
|
// CFliterRFID cFliterRFID = new CFliterRFID();
|
|
|
// var result = cFliterRFID.filterByAnt(info2, Convert.ToInt32(proEquip.equipAnt));
|
|
|
// if (result == null) return "";
|
|
|
// if (StringExtension.IsBlank(result.EPCstring))
|
|
|
// {
|
|
|
// return "";
|
|
|
// }
|
|
|
// string info = System.Text.Encoding.ASCII.GetString(result.EPC);
|
|
|
// info = info.Substring(0, info.Length - 2);
|
|
|
// LogHelper.Info("TBReadEPC函数读取结果:" + info);
|
|
|
// readServices.Add(new ProReadRecord()
|
|
|
// {
|
|
|
// MachineID = proEquip.machineId,
|
|
|
// EquipID = proEquip.equipId,
|
|
|
// PositionID = proEquip.positionId,
|
|
|
// Ant = StringChange.ParseToInt(proEquip.equipAnt),
|
|
|
// ReadEPC = info,
|
|
|
// Result = StringExtension.IsBlank(info) ? "0" : "1",
|
|
|
// ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
// });
|
|
|
// return info;
|
|
|
//}
|
|
|
|
|
|
|
|
|
public string ReadEPC(string equipid)
|
|
|
{
|
|
|
//LogService.Instance.Debug(equipid + "开始查找");
|
|
|
bool find = false;
|
|
|
string result = "";
|
|
|
foreach (var equip in _equipList)
|
|
|
{
|
|
|
if (equip.equipId.ToString() == equipid)
|
|
|
{
|
|
|
find = true;
|
|
|
ushort ant = Convert.ToUInt16(equip.equipAnt);
|
|
|
byte[] buffer = new byte[24];
|
|
|
for (int i = 0; i < StringChange.ParseToInt(ConfigHelper.GetConfig("readCount")); i++)
|
|
|
{
|
|
|
int count = i + 1;
|
|
|
//LogService.Instance.Debug("读取第" + count + "次");
|
|
|
LogHelper.Info("第" + count + "次开始读取RFID条码信息");
|
|
|
byte b = equip.DeviceAdapter.Device_GetOneIdentifyData(ref buffer, Convert.ToByte(equip.equipAnt), (ushort)(StringChange.ParseToInt(ConfigHelper.GetConfig("readTime")) * 1000));
|
|
|
string epcstring = Encoding.ASCII.GetString(buffer);
|
|
|
LogHelper.Info("RFID条码信息读取完成:" + epcstring);
|
|
|
//string epcstring = equip.DeviceAdapter.Device_GetOneIdentifyData((byte)ant, (ushort)(Unity.Unity.Instance.readTime * 1000));
|
|
|
if (epcstring != null && epcstring != "")
|
|
|
{
|
|
|
epcstring = epcstring.Substring(0, epcstring.Length - 2);
|
|
|
result = epcstring;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
readServices.Add(new ProReadRecord()
|
|
|
{
|
|
|
MachineID = equip.machineId,
|
|
|
EquipID = equip.equipId,
|
|
|
PositionID = equip.positionId,
|
|
|
Ant = StringChange.ParseToInt(equip.equipAnt),
|
|
|
ReadEPC = result,
|
|
|
Result = StringExtension.IsBlank(result) ? "0" : "1",
|
|
|
ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
if (!find)
|
|
|
{
|
|
|
LogHelper.Info("未找到编码为" + equipid + "的设备");
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public bool writeUser(string equipid, string epc, string writeData)
|
|
|
{
|
|
|
//LogService.Instance.Debug(equipid + "写入开始查找");
|
|
|
bool find = false;
|
|
|
bool result = false;
|
|
|
foreach (var equip in _equipList)
|
|
|
{
|
|
|
if (equip.equipId.ToString() == equipid)
|
|
|
{
|
|
|
find = true;
|
|
|
ushort ant = Convert.ToUInt16(equip.equipAnt);
|
|
|
byte[] buffer = new byte[24];
|
|
|
|
|
|
Byte[] filter = new Byte[epc.Length];
|
|
|
filter = Encoding.ASCII.GetBytes(epc);
|
|
|
Byte[] write = new byte[writeData.Length];
|
|
|
write = Encoding.ASCII.GetBytes(writeData);
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
{
|
|
|
int count = i + 1;
|
|
|
//LogService.Instance.Debug("写入第" + count + "次");
|
|
|
int intrsult = equip.DeviceAdapter.Device_Write(G2MemBank.EPC, 0, 0, filter, G2MemBank.USER, 0, (ushort)(write.Length / 2), write, 1);
|
|
|
if (intrsult > 0)
|
|
|
{
|
|
|
result = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//LogService.Instance.Error(position + "未找到对应的设备");
|
|
|
|
|
|
if (!find)
|
|
|
{
|
|
|
//LogService.Instance.Debug("write未找到编码为" + equipid + "的设备");
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void bgwDeviceDetect_DoWork(object sender, DoWorkEventArgs e)
|
|
|
{
|
|
|
BackgroundWorker backgroundworker = sender as BackgroundWorker;
|
|
|
//StartDetectEvent.WaitOne(-1, false);
|
|
|
while (true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (ExitEvent.WaitOne(0, false))
|
|
|
{
|
|
|
e.Cancel = true;
|
|
|
ExitEvent.Reset();
|
|
|
return;
|
|
|
}
|
|
|
foreach (var equip in IpequipList)
|
|
|
{
|
|
|
if (equip.DeviceAdapter == null)
|
|
|
{
|
|
|
equip.IsConnect = false;
|
|
|
}
|
|
|
if (equip.IsConnect == false)
|
|
|
{
|
|
|
if (equip.DeviceAdapter.Device_Connect())
|
|
|
{
|
|
|
byte iResult = equip.DeviceAdapter.Device_SendHeartPack();
|
|
|
if (iResult == 1)
|
|
|
{
|
|
|
equip.IsConnect = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
equip.IsConnect = false;
|
|
|
equip.DeviceAdapter.Device_Destroy();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
byte iResult = equip.DeviceAdapter.Device_SendHeartPack();
|
|
|
if (iResult == 1)
|
|
|
{
|
|
|
equip.IsConnect = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
equip.IsConnect = false;
|
|
|
}
|
|
|
}
|
|
|
EventDisplay?.Invoke();
|
|
|
}
|
|
|
|
|
|
DetectEvent.Reset();
|
|
|
DetectEvent.WaitOne(20 * 1000, false);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
DetectEvent.Reset();
|
|
|
DetectEvent.WaitOne(10 * 1000, false);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
public bool InitEquipLsit()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
GetAllEquipList();
|
|
|
if (_equipList.Count > 0)
|
|
|
{
|
|
|
//m_bgwDeviceDetect.DoWork += new DoWorkEventHandler(bgwDeviceDetect_DoWork);
|
|
|
//m_bgwDeviceDetect.RunWorkerAsync(this);
|
|
|
Connect = new Thread(ConnectEquip);
|
|
|
Connect.Start();
|
|
|
}
|
|
|
//thread = new Thread(ThreadChickState);
|
|
|
//thread.Start();
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
//LogService.Instance.Debug("设备初始化失败,请检查类型是否正确" + e.ToString());
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取所有设备列表
|
|
|
/// </summary>
|
|
|
private async void GetAllEquipList()
|
|
|
{
|
|
|
AppConfigDto appConfig = AppConfigDto.Instance;
|
|
|
//DbHandler db = new DbHandler();
|
|
|
int machineId = appConfig.machineId;
|
|
|
Expression<Func<ProEquip, bool>> expression = s1 => true;
|
|
|
expression = expression.And(x => x.machineId == machineId);
|
|
|
_equipList = await equipServices.Query(expression);
|
|
|
|
|
|
foreach (var equip in _equipList)
|
|
|
{
|
|
|
foreach (var ipequip in IpequipList)
|
|
|
{
|
|
|
if (ipequip.equipIp == equip.equipIp)
|
|
|
{
|
|
|
equip.DeviceAdapter = ipequip.DeviceAdapter;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if (equip.DeviceAdapter == null)
|
|
|
{
|
|
|
equip.DeviceAdapter = DynamicClass(equip.equipType);
|
|
|
if (equip.DeviceAdapter == null)
|
|
|
{
|
|
|
//LogService.Instance.Debug(equip.EquipIP + "设备实体类生成失败,请检查类型是否正确" + equip.EquipType);
|
|
|
continue;
|
|
|
}
|
|
|
string url = equip.equipIp + ":" + equip.equipPort;
|
|
|
if (appConfig.machineId == 1)
|
|
|
{
|
|
|
equip.DeviceAdapter.Device_Init(CommType.RJ45, url, DeviceType.Mesnac_GRUV100);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
equip.DeviceAdapter.Device_Init(CommType.RJ45, url, DeviceType.RFly_I160);
|
|
|
}
|
|
|
IpequipList.Add(equip);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void ConnectEquip()
|
|
|
{
|
|
|
#region
|
|
|
//foreach (var equip in _equipList)
|
|
|
//{
|
|
|
// foreach (var ipequip in IpequipList)
|
|
|
// {
|
|
|
// if (ipequip.EquipIP == equip.EquipIP)
|
|
|
// {
|
|
|
// equip.DeviceAdapter = ipequip.DeviceAdapter;
|
|
|
// break;
|
|
|
// }
|
|
|
// }
|
|
|
// if (equip.DeviceAdapter == null)
|
|
|
// {
|
|
|
// equip.DeviceAdapter = DynamicClass(equip.EquipType);
|
|
|
// if (equip.DeviceAdapter == null)
|
|
|
// {
|
|
|
// LogService.Instance.Debug(equip.EquipIP+"设备实体类生成失败,请检查类型是否正确" + equip.EquipType);
|
|
|
// continue;
|
|
|
// }
|
|
|
// string url = equip.EquipIP + ":" + equip.EquipPort;
|
|
|
// equip.DeviceAdapter.Device_Init(CommType.RJ45, url, DeviceType.Mesnac_GRUV100);
|
|
|
// if (equip.DeviceAdapter.Device_Connect())
|
|
|
// {
|
|
|
// equip.IsConnect = true;
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// equip.IsConnect = false;
|
|
|
// }
|
|
|
// IpequipList.Add(equip);
|
|
|
// }
|
|
|
//}
|
|
|
#endregion
|
|
|
|
|
|
foreach (var equip in IpequipList)
|
|
|
{
|
|
|
if (equip.DeviceAdapter.Device_Connect())
|
|
|
{
|
|
|
equip.IsConnect = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
equip.IsConnect = false;
|
|
|
}
|
|
|
foreach (var e in _equipList)
|
|
|
{
|
|
|
if (equip.equipIp == e.equipIp)
|
|
|
{
|
|
|
e.IsConnect = equip.IsConnect;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
m_ReadSem.Release();
|
|
|
}
|
|
|
|
|
|
private void ThreadChickState()
|
|
|
{
|
|
|
m_ReadSem.WaitOne(10000);
|
|
|
while (ReadContinue)
|
|
|
{
|
|
|
CheckEquipState();
|
|
|
//让界面显示
|
|
|
EventDisplay?.Invoke();
|
|
|
Thread.Sleep(10000);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
public void CheckEquipState()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var info = _equipList.GroupBy(x => x.equipIp).Select(a => a.Key).ToList();
|
|
|
foreach (var item in info)
|
|
|
{
|
|
|
string timeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
ProEquip equip = _equipList.Where(x => x.equipIp == item).FirstOrDefault();
|
|
|
int iflag;
|
|
|
iflag = equip.DeviceAdapter.Device_SendHeartPack();
|
|
|
LogHelper.RfidLog(String.Format("{0}设备状态检测:{1}", equip.equipIp, iflag));
|
|
|
if (iflag == 1)
|
|
|
{
|
|
|
Console.WriteLine(timeStr + "心跳成功!!!");
|
|
|
equip.IsConnect = true;
|
|
|
}
|
|
|
else if (iflag == 2)
|
|
|
{
|
|
|
Console.WriteLine(timeStr + "心跳超时!!!");
|
|
|
LogHelper.Info("心跳超时!!!");
|
|
|
equip.IsConnect = false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine(timeStr + "心跳失败,进行重连!!!");
|
|
|
equip.IsConnect = false;
|
|
|
ResertConnect(equip);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error("设备状态检测函数CheckEquipState异常", ex);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 新加断开重连
|
|
|
/// </summary>
|
|
|
/// <param name="equip"></param>
|
|
|
private void ResertConnect(ProEquip equip)
|
|
|
{
|
|
|
///add by yinzf
|
|
|
try
|
|
|
{
|
|
|
Console.WriteLine(String.Format("{0}设备断开重连", equip.equipIp));
|
|
|
LogHelper.Info(String.Format("{0}设备断开重连", equip.equipIp));
|
|
|
equip.DeviceAdapter.Device_Destroy();
|
|
|
equip.DeviceAdapter.Device_ReConnect();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Info(String.Format("{0}设备重新连接失败{1}", equip.equipIp, ex.Message));
|
|
|
}
|
|
|
//end add
|
|
|
}
|
|
|
|
|
|
private void Reconnect(object o)
|
|
|
{
|
|
|
ProEquip equip = o as ProEquip;
|
|
|
if (equip != null)
|
|
|
{
|
|
|
equip.DeviceAdapter.Device_ReConnect();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 动态生成业务处理代码
|
|
|
/// </summary>
|
|
|
/// <param name="st"></param>
|
|
|
/// <returns></returns>
|
|
|
private IDeviceAdapter DynamicClass(string equipType)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string str = System.Environment.CurrentDirectory;
|
|
|
str = str + "\\MaterialTraceability.Rfid.dll";
|
|
|
Assembly assembly = Assembly.LoadFile(str); // 加载程序集(EXE 或 DLL)
|
|
|
dynamic obj = assembly.CreateInstance("MaterialTraceability.Rfid.Impl.RFly_I160Adapter"); // 创建类的实例
|
|
|
////根据类型,动态生成设备实体类
|
|
|
//Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集
|
|
|
//string AssemName = "Mesnac.DeviceAdapter." + equipType;
|
|
|
//var obj = assembly.CreateInstance("HighWay.Ningde.Entity.ProBaoM", true);
|
|
|
//Mesnac.DeviceAdapter.GRUV100.GRUV100Adapter a = new Mesnac.DeviceAdapter.GRUV100.GRUV100Adapter();
|
|
|
return obj as IDeviceAdapter;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
//LogService.Instance.Debug("设备实体类生成失败,请检查类型是否正确"+e.ToString());
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public List<ProEquip> Equiplist
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _equipList;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Dipose()
|
|
|
{
|
|
|
ReadContinue = false;
|
|
|
if (thread != null)
|
|
|
{
|
|
|
thread.Abort();
|
|
|
}
|
|
|
if (Connect != null)
|
|
|
{
|
|
|
Connect.Abort();
|
|
|
}
|
|
|
foreach (var equip in IpequipList)
|
|
|
{
|
|
|
equip.DeviceAdapter.Device_Destroy();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public class TagMessage
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// epc
|
|
|
/// </summary>
|
|
|
public string EPC { get; set; }
|
|
|
/// <summary>
|
|
|
/// 位置
|
|
|
/// </summary>
|
|
|
public int position { get; set; }
|
|
|
}
|
|
|
} |