using HighWayIot.Log4net; using HighWayIot.Plc.PlcEntity; using HighWayIot.Plc.PlcHelper; using HighWayIot.Repository.domain; using HighWayIot.Repository.service; using HighWayIot.Rfid; using HighWayIot.Rfid.Entity; using HighWayIot.TouchSocket; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TouchSocket.Core; using Timer = System.Threading.Timer; namespace HighWayIot.Winform.Business { /// /// 工位RFID识别业务 /// public class WorkStationBusiness { /// /// 日志 /// private LogHelper _logHelper = LogHelper.Instance; /// /// PLC /// private WorkStationHelper _workStationHelper = new WorkStationHelper(); /// /// 读写器服务类 /// private ZxReaderSettingService _readerService = ZxReaderSettingService.Instance; /// /// 读写器实体类 /// private List _readerSetting; /// /// 标签服务类 /// private ZxTagSettingService _tagrService = ZxTagSettingService.Instance; /// /// 标签实体类 /// private List _tagSetting; /// /// RFID数据分析 /// private RfidDataAnalyse _rfidDataAnalyse = new RfidDataAnalyse(); /// /// TCP客户端 /// private TouchSocketTcpClient _touchSocketTcpClient = TouchSocketTcpClient.Instance; /// /// 刷新器 /// private Timer timer; /// /// 是否全部连接成功 /// private bool IsAllConnected = false; public WorkStationBusiness() { _readerSetting = _readerService.GetReaderInfos(); CreateAllRFIDClient(); _touchSocketTcpClient.GetMessageAction += ReciveRFIDSingal; timer = new Timer(new System.Threading.TimerCallback(SingalMonitor), null, 0, 2000); } /// /// 自动监视 /// /// public void SingalMonitor(object o) { bool[] singals = _workStationHelper.ReadStationSingal(); for (int i = 1; i <= singals.Length; i++) { if (singals[i - 1] == true) { SendRFIDSingal(i); } } } /// /// 创建所有RFID客户端 /// public async void CreateAllRFIDClient() { foreach (var setting in _readerSetting) { await _touchSocketTcpClient.CreateTcpClient(setting.RfidIp, "20108"); } _logHelper.Info("所有读写器均连接成功"); IsAllConnected = true; } /// /// 向指定RFID读写器发送读信号 /// public async void SendRFIDSingal(int no) { if (IsAllConnected == true) { ZxReaderSettingEntity setting = _readerSetting.FirstOrDefault(x => x.WorkstationNo == no.ToString()); if (setting == null) { return; } int i = 0; bool result = false; do { result = await _touchSocketTcpClient.Send(setting.RfidIp, _rfidDataAnalyse.Send02H(1000)); i++; } while (i < 3 && result == false); if(result == false) { _logHelper.Error($"[{no}] 工位 [{setting.RfidIp}] 读写器指令发送失败!"); } } } /// /// 接收信息的信号 /// /// public void ReciveRFIDSingal(byte[] bytes, string ip) { Receive02HEntity entity = _rfidDataAnalyse.Receive02H(bytes); //if(entity.TagCount != 1) //{ // _logHelper.Error("返回多标签!"); // return; //} //找到读取次数最大的标签EPC byte[] data = entity.Data.Where(x => x.Count == entity.Data.Max(y => y.Count)).First().EPC; //标签号byte数组转换成字符串 string result = string.Join(" ", data.Select(x => x.ToString("X2"))); //根据IP和标签EPC获取工位和对应设备号 string deviceNo = _tagrService.GetTagDeviceNoByEPC(result); string workstationNo = _readerService.GetWorkstateNoByIp(ip); if (string.IsNullOrEmpty(deviceNo)) { _logHelper.Error($"没有查询到 [{result}] 标签相关的信息!"); return; } if (string.IsNullOrEmpty(workstationNo)) { _logHelper.Error($"没有查询到 [{ip}] 读写器相关的信息!"); return; } try { ///写入对应的PLC信号 _workStationHelper.WriteStationSingal(int.Parse(workstationNo), int.Parse(deviceNo)); } catch (Exception ex) { _logHelper.Error("数值转换发生错误", ex); } } } }