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; /// /// RFID数据分析 /// private RfidDataAnalyse _RfidDataAnalyse = new RfidDataAnalyse(); /// /// TCP客户端 /// private TouchSocketTcpClient _touchSocketTcpClient = TouchSocketTcpClient.Instance; /// /// TCP客户端工厂 /// private TCPClientFactory tcpClientFactory = new TCPClientFactory(); /// /// 刷新器 /// private Timer timer; /// /// 是否全部连接成功 /// private bool IsAllConnected = false; public WorkStationBusiness() { _readerSetting = _readerService.GetReaderInfos(); this.CreateAllRFIDClient(); _touchSocketTcpClient.GetMessageAction += tcpClientFactory.ReciveDataRoute; timer = new Timer(new System.Threading.TimerCallback(SingalMonitor), null, 0, 1000); } /// /// 创建所有RFID客户端 /// public void CreateAllRFIDClient() { foreach (var setting in _readerSetting) { Task.Run(() => { _touchSocketTcpClient.CreateTcpClient(setting.RfidIp, "20108"); _touchSocketTcpClient.Send(setting.RfidIp, _RfidDataAnalyse.SendBFH(5)).GetAwaiter().GetResult(); }); } IsAllConnected = true; } /// /// 自动监视 获取PLC信号 /// /// public void SingalMonitor(object o) { bool[] singals = _workStationHelper.ReadStationSingal(); if(singals == null) { _logHelper.Error("工位监视PLC读取信号失败"); return; } for (int i = 1; i <= singals.Length; i++) { if (singals[i - 1] == true) { SendRFIDSingal(i); } } } /// /// 向指定RFID读写器发送读信号 /// public async void SendRFIDSingal(int no) { if (IsAllConnected == true) { ZxReaderSettingEntity setting = null; try { setting = _readerSetting.FirstOrDefault(x => x.WorkstationNo == no.ToString()); } catch (Exception ex) { LogHelper.Instance.Error($"[{no}] 工位读写器信息获取失败!", ex); } if (setting == null) { LogHelper.Instance.Error($"找不到第[{no}]个读写器"); 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}] 读写器指令发送失败!"); } } } } }