using iWareCc.Cache.Entity;
using iWareCc.Conveyor.Datagram;
using iWareCc.Conveyor.Entity;
using iWareCc.Conveyor.EnumType;
using iWareCc.Properties;
using iWareCommon.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iWareCc.Conveyor.Service
{
public class GateService
{
///
/// 需要处理的输送机端口
///
private GateEntity GateEntity;
///
/// 构造函数
///
/// 需要处理的端口
public GateService(GateEntity gateEntity)
{
this.GateEntity = gateEntity;
}
//////////////////////////////////////////////////////////////////Client///////////////////////////////////////////////////////////////////////////////////
#region Client方法
///
/// 将数据写到接收缓存,
/// 该方法在线程中执行
///
///
public void AddToDatagramReceivePool(string msg)
{
//如果字符串为空,则不判断锁
if (!string.IsNullOrEmpty(msg))
{
lock (GateEntity)
{
//string endFlag = "#";//结束标志
GateEntity.DatagramReceivePool += msg;
}
}
}
///
/// 从接收缓存中读取一个字符串,
/// 该方法在线程中执行
///
public string ReadFromDatagramReceivePool()
{
lock (GateEntity)
{
string endFlag = "#";//结束标志
int index = GateEntity.DatagramReceivePool.IndexOf(endFlag);
string msg = "";
if (index > -1)
{
msg = GateEntity.DatagramReceivePool.Substring(0, index + endFlag.Length);
GateEntity.DatagramReceivePool = GateEntity.DatagramReceivePool.Substring(index + endFlag.Length);
}
return msg;
}
}
///
/// 将数据写到发送缓存,
/// 该方法在线程中执行
///
///
public void AddToDatagramSendPool(string msg)
{
//如果字符串为空,则不判断锁
if (!string.IsNullOrEmpty(msg))
{
lock (GateEntity.Place)
{
GateEntity.DatagramSendPool += msg;
}
}
}
///
/// 从发送缓存中读取一个字符串,
/// 该方法在线程中执行
///
public string ReadFromDatagramSendPool()
{
lock (GateEntity.Place)
{
string endFlag = "#";//结束标志
int index = GateEntity.DatagramSendPool.IndexOf(endFlag);
string msg = "";
if (index > -1)
{
msg = GateEntity.DatagramSendPool.Substring(0, index + endFlag.Length);
GateEntity.DatagramSendPool = GateEntity.DatagramSendPool.Substring(index + endFlag.Length);
}
return msg;
}
}
///
/// 处理接收到的消息
///
/// 接收到的消息
public void HandleMsg(string msg)
{
if (string.IsNullOrEmpty(msg))
{
return;
}
try
{
GateDatagramProp prop = new GateDatagramAnalyze().Analyze(ASCIIEncoding.ASCII.GetBytes(msg));
}
catch (Exception ex)
{
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "HandleMsg", ex.Message);
}
}
#endregion
//////////////////////////////////////////////////////////////////Server//////////////////////////////////////////////////////////////////////////////////
#region Server端
///
/// 将数据写到缓存,
/// 该方法在线程中执行
///
///
public void AddToDatagramPoolReceive(string msg)
{
//如果字符串为空,则不判断锁
if (!string.IsNullOrEmpty(msg))
{
lock (GateEntity)
{
if (msg.Contains('?') || msg.Contains('?'))//去除无用符号
{
msg = msg.Replace("?", "").Replace("!", "");
}
if (msg.Contains("gate") && msg.Contains("#"))
{
if (GateEntity.DatagramReceivePool.Contains(msg))
{
msg = "";
}
GateEntity.DatagramReceivePool+= msg;
}
if (msg.Contains("SC@") && msg.Contains("#"))//去除重复的物料码
{
if (GateEntity.DatagramReceiveSCPool.Contains(msg))
{
msg = "";
}
GateEntity.DatagramReceiveSCPool += msg;
}
if (msg.Contains("ALARM@") && msg.Contains("#"))//去除重复的物料码
{
if (GateEntity.DatagramReceiveAlermPool.Contains(msg))
{
msg = "";
}
GateEntity.DatagramReceiveAlermPool += msg.TrimStart();
}
}
}
}
///
/// 重缓存中读取一个字符串,
/// 该方法在线程中执行
///
///
public string ReadFromDatagramPoolReceive()
{
lock (GateEntity)
{
string endFlag = "#";//结束标志
int index = GateEntity.DatagramReceivePool.IndexOf(endFlag);
string msg = "";
if (index > -1)
{
msg = GateEntity.DatagramReceivePool.Substring(0, index + endFlag.Length);
GateEntity.DatagramReceivePool = GateEntity.DatagramReceivePool.Substring(index + endFlag.Length);
}
return msg;
}
}
///
/// 重缓存中读取一个字符串,
/// 该方法在线程中执行
///
///
public string ReadFromDatagramPoolSCReceive()
{
lock (GateEntity)
{
string endFlag = "#";//结束标志
int indexSC = GateEntity.DatagramReceiveSCPool.IndexOf(endFlag);
string msg = "";
if (indexSC > -1)
{
msg = GateEntity.DatagramReceiveSCPool.Substring(0, indexSC + endFlag.Length);
GateEntity.DatagramReceiveSCPool = GateEntity.DatagramReceiveSCPool.Substring(indexSC + endFlag.Length);
}
return msg;
}
}
///
/// 重缓存中读取一个字符串,
/// 该方法在线程中执行
///
///
public string ReadFromDatagramPoolAlarmReceive()
{
lock (GateEntity)
{
string endFlag = "#";//结束标志
int indexalarm = GateEntity.DatagramReceiveAlermPool.IndexOf(endFlag);
string msg = "";
if (indexalarm > -1)
{
msg = GateEntity.DatagramReceiveAlermPool.Substring(0, indexalarm + endFlag.Length);
GateEntity.DatagramReceiveAlermPool = GateEntity.DatagramReceiveAlermPool.Substring(indexalarm + endFlag.Length);
}
return msg;
}
}
///
/// 处理发送的消息
///
/// 发送的消息
public void HandleMsgSend(string msg)
{
if (string.IsNullOrEmpty(msg))
{
return;
}
try
{
}
catch (Exception ex)
{
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "HandleMsgSend", ex.Message);
}
}
///
/// 处理接收到的消息
///
/// 接收到的消息
public void HandleMsgReceive(string msg)
{
//测试发送
//var gate = CacheEntity.Conveyors.First(x => x.Equipment.EquipName == "conveyor2").Gates.First(x => x.Place.PlaceTypeName == "gate22");
//gate.SendGateTask();
if (string.IsNullOrEmpty(msg))
{
return;
}
try
{
GateDatagramProp prop = new GateDatagramAnalyze().Analyze(ASCIIEncoding.ASCII.GetBytes(msg));
GateEntity.RIsEmpty = prop.IsEmpty == "T" ? true : false;
GateEntity.ROccupied = prop.InPlace == "T" ? true : false;
if (GateEntity.Place.PlaceTypeName == "gate22")//端口有货才发送退库任务
{
if (!GateEntity.RIsEmpty && GateEntity.ROccupied)//gate21已经发送了滚动任务并且已经到了gate22
{
if (!GateEntity.SendGateTask()) //如果发送失败,则发送失败
{
GateEntity.SendGateTask();
}
}
}
}
catch (Exception ex)
{
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "HandleMsgReceive", ex.Message);
}
}
///
/// 处理接收到的消息
///
/// 接收到的消息
public void HandleMsgSCReceive(string msg)
{
if (string.IsNullOrEmpty(msg))
{
return;
}
try
{
GateDatagramProp prop = new GateDatagramAnalyze().SCAnalyze(ASCIIEncoding.ASCII.GetBytes(msg));
if (!string.IsNullOrEmpty(prop.MaterialCode))
{
GateEntity.RCheckMaterialCode = prop.MaterialCode.Replace("\r", "").Replace("\0", "").Trim();
}
}
catch (Exception ex)
{
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "HandleMsgReceive", ex.Message);
}
}
///
/// 处理接收到的消息
///
/// 接收到的消息
public void HandleMsgAlarmReceive(string msg)
{
if (string.IsNullOrEmpty(msg))
{
return;
}
try
{
GateDatagramProp prop = new GateDatagramAnalyze().AlarmAnalyze(ASCIIEncoding.ASCII.GetBytes(msg));
if (!string.IsNullOrEmpty(prop.Alarmcode))
{
GateEntity.AlarmCode= prop.Alarmcode.Trim();
}
}
catch (Exception ex)
{
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "HandleMsgReceive", ex.Message);
}
}
///
/// 将数据写到缓存,
/// 该方法在线程中执行
///
///
public void AddToDatagramPoolSend(string msg)
{
//如果字符串为空,则不判断锁
if (!string.IsNullOrEmpty(msg))
{
lock (GateEntity)
{
GateEntity.DatagramSendPool += msg;
}
}
}
///
/// 从缓存中读取一个字符串,
/// 该方法在线程中执行
///
///
public string ReadFromDatagramPoolSend()
{
lock (GateEntity)
{
//结束标志
string endFlag = "@";
int index = GateEntity.DatagramSendPool.IndexOf(endFlag);
string msg = "";
if (index > -1)
{
msg = GateEntity.DatagramSendPool.Substring(0, index + endFlag.Length);
GateEntity.DatagramSendPool = GateEntity.DatagramSendPool.Substring(index + endFlag.Length);
}
return msg;
}
}
///
/// 发送报文
///
/// 字节码形式的报文
/// 是否发送成功
public bool Send(byte[] bytes)
{
try
{
if (GateEntity.SocketServer.Server == null)
{
return false;
}
//获取所有连接上的客户端
IntPtr[] clientIds = GateEntity.SocketServer.Server.GetAllConnectionIDs();
if (clientIds == null || clientIds.Length <= 0)
{
return false;
}
if (GateEntity.Place.PlaceTypeName == "gate22")
{
}
return GateEntity.SocketServer.Send(clientIds[0], bytes);
}
catch (Exception ex)
{
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "Send", ex.Message);
return false;
}
}
#endregion
}
}