forked from wenjy/HighWayIot
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.
190 lines
5.6 KiB
C#
190 lines
5.6 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 工位RFID识别业务
|
|
/// </summary>
|
|
public class WorkStationBusiness
|
|
{
|
|
/// <summary>
|
|
/// 日志
|
|
/// </summary>
|
|
private LogHelper _logHelper = LogHelper.Instance;
|
|
|
|
/// <summary>
|
|
/// PLC
|
|
/// </summary>
|
|
private WorkStationHelper _workStationHelper = new WorkStationHelper();
|
|
|
|
/// <summary>
|
|
/// 读写器服务类
|
|
/// </summary>
|
|
private ZxReaderSettingService _readerService = ZxReaderSettingService.Instance;
|
|
|
|
/// <summary>
|
|
/// 读写器实体类
|
|
/// </summary>
|
|
private List<ZxReaderSettingEntity> _readerSetting;
|
|
|
|
/// <summary>
|
|
/// 标签服务类
|
|
/// </summary>
|
|
private ZxTagSettingService _tagrService = ZxTagSettingService.Instance;
|
|
|
|
/// <summary>
|
|
/// 标签实体类
|
|
/// </summary>
|
|
private List<ZxTagSettingEntity> _tagSetting;
|
|
|
|
/// <summary>
|
|
/// RFID数据分析
|
|
/// </summary>
|
|
private RfidDataAnalyse _rfidDataAnalyse = new RfidDataAnalyse();
|
|
|
|
/// <summary>
|
|
/// TCP客户端
|
|
/// </summary>
|
|
private TouchSocketTcpClient _touchSocketTcpClient = TouchSocketTcpClient.Instance;
|
|
|
|
/// <summary>
|
|
/// 刷新器
|
|
/// </summary>
|
|
private Timer timer;
|
|
|
|
/// <summary>
|
|
/// 是否全部连接成功
|
|
/// </summary>
|
|
private bool IsAllConnected = false;
|
|
|
|
public WorkStationBusiness()
|
|
{
|
|
_readerSetting = _readerService.GetReaderInfos();
|
|
CreateAllRFIDClient();
|
|
_touchSocketTcpClient.GetMessageAction += ReciveRFIDSingal;
|
|
timer = new Timer(new System.Threading.TimerCallback(SingalMonitor), null, 0, 2000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自动监视
|
|
/// </summary>
|
|
/// <param name="o"></param>
|
|
public void SingalMonitor(object o)
|
|
{
|
|
bool[] singals = _workStationHelper.ReadStationSingal();
|
|
|
|
for (int i = 1; i <= singals.Length; i++)
|
|
{
|
|
if (singals[i - 1] == true)
|
|
{
|
|
SendRFIDSingal(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建所有RFID客户端
|
|
/// </summary>
|
|
public async void CreateAllRFIDClient()
|
|
{
|
|
foreach (var setting in _readerSetting)
|
|
{
|
|
await _touchSocketTcpClient.CreateTcpClient(setting.RfidIp, "20108");
|
|
}
|
|
|
|
_logHelper.Info("所有读写器均连接成功");
|
|
|
|
IsAllConnected = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 向指定RFID读写器发送读信号
|
|
/// </summary>
|
|
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}] 读写器指令发送失败!");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收信息的信号
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|