using IWareCC.CacheInfo;
using IWareCC.Properties;
using IWareCommon.Enum.Srm;
using IWareCommon.Help;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace IWareCC.SRM.Entity
{
public class SrmEntity
{
///
/// 堆垛机名称
///
public string SrmName { get; set; }
///
/// 设备编号
///
public int DeviceId { get; set; }
///
/// 根据心跳判断堆垛机是否在线
///
public bool IsOnline { get; set; }
///
/// 下线次数
///
public int OffLineTimes { get; set; }
///
/// 上一次心跳数
///
public int LastHandShake { get; set; }
///
/// 连接PLC读的对象
///
public PlcS7 RS71500 { get; set; }
///
/// 连接PLC写的对象
///
public PlcS7 WS71500 { get; set; }
#region 写入的堆垛机信息
///
/// 心跳
///
public int WHandShake { get; set; }
///
/// 堆垛机编号
///
public int WSrmNo { get; set; }
///
/// 任务类型
///
public int WCommand { get; set; }
///
/// 急停
///
public int WEmergencyStop { get; set; }
///
/// 起始列
///
public int WSourcePosX { get; set; }
///
/// 起始层
///
public int WSourcePosY { get; set; }
///
/// 起始排
///
public int WSourcePosZ { get; set; }
///
/// 目标列
///
public int WDestinationPosX { get; set; }
///
/// 目标层
///
public int WDestinationPosY { get; set; }
///
/// 目标排
///
public int WDestinationPosZ { get; set; }
///
///任务号
///
public int WTaskNo { get; set; }
///
/// 任务完成确认
///
public bool WTaskFinishConfirm { get; set; }
///
/// 解除报警
///
public bool WCancleAlarm { get; set; }
///
/// 确认任务已发送的信号
///
public bool WStb { get; set; }
///
/// 托盘类型
///
public int WPalletType { get; set; }
///
/// 货物高度
///
public int WHigh { get; set; }
#endregion
#region 读取到的堆垛机信息
///
/// 读取心跳
///
public int RHandShake { get; set; }
///
/// 设备编号
///
public int RSrmNo { get; set; }
///
/// 是否有报警
///
public bool RAlarm { get; set; }
///
/// 载货台是否有货
///
public bool RLiftFull { get; set; }
///
/// 设备模式
///
public int RMode { get; set; }
///
/// 当前列
///
public int RPosX { get; set; }
///
/// 当前层
///
public int RPosY { get; set; }
///
/// 当前排
///
public int RPosZ { get; set; }
///
/// 当前列坐标
///
public int RPosXmm { get; set; }
///
/// 当前层坐标
///
public int RPosYmm { get; set; }
///
/// 当前排坐标
///
public int RPosZmm { get; set; }
///
/// 取货完成
///
public bool RPickFinish { get; set; }
///
/// 放货完成
///
public bool RDeliveryFinish { get; set; }
///
/// 任务完成
///
public bool RTaskFinish { get; set; }
///
/// 当前巷道
///
public int RActualLane { get; set; }
///
/// 任务状态
///
public int RState { get; set; }
///
/// 报警代码
///
public int RAlarmCode { get; set; }
///
/// 任务号
///
public int RTaskNo { get; set; }
///
/// 确认收到下发的任务信号
///
public bool RAck { get; set; }
#endregion
public OpcReadItem OpcReadItems { get; set; }
public OpcWriteItem OpcWriteItems { get; set; }
public SrmEntity() { }
public SrmEntity(string srmName, int deviceId, PlcS7 plc)
{
this.SrmName = srmName;
this.DeviceId = deviceId;
this.WS71500 = plc;
OpcWriteItems = new OpcWriteItem
{
HandShake = "DB540.DBW0",
SrmNo = "DB540.DBW2",
TaskNo = "DB540.DBW34",
TaskFinishConfirm = "DB540.DBW24",
EmergencyStop = "DB540.DBW20",
CancleAlarm = "DB540.DBW22",
Command = "DB540.DBW18",
SourcePosX = "DB540.DBW6",
SourcePosY = "DB540.DBW8",
SourcePosZ = "DB540.DBW10",
DestinationPosX = "DB540.DBW12",
DestinationPosY = "DB540.DBW14",
DestinationPosZ = "DB540.DBW16",
Stb = "DB540.DBW30",
PalletType = "DB540.DBW4",
High = "DB540.DBW26",
};
OpcReadItems = new OpcReadItem
{
HandShake = "DB541.DBW0",
PickFinish = "DB541.DBW28",
LiftFull = "DB541.DBW8",
Mode = "DB541.DBW4",
Alarm = "DB541.DBW6",
TaskFinish = "DB541.DBW32",
DeliveryFinish = "DB541.DBW30",
ActualLane = "DB541.DBW38",
SrmNo = "DB541.DBW2",
AlarmCode = "DB541.DBW34",
State = "DB541.DBW36",
PosZ = "DB541.DBW10",
PosX = "DB541.DBW12",
PosY = "DB541.DBW14",
PosXmm = "DB541.DBD16",
PosYmm = "DB541.DBD20",
PosZmm = "DB541.DBD24",
TaskNo = "DB541.DBW40",
Ack = "DB540.DBW32"
};
}
public bool SendTask(int deviceId, int taskId, string sourcePlace, string toPlace, int type, int palltype,int heighttype,out string msg)
{
try
{
msg = "";
if (!CanSendTask(deviceId, out msg))
{
return false;
}
var sourceSplit = sourcePlace.Split('-');
var toSplit = toPlace.Split('-');
bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
bool b3 = this.WriteValue(this.OpcWriteItems.SourcePosY, int.Parse(sourceSplit[2]));
bool b4 = this.WriteValue(this.OpcWriteItems.SourcePosX, int.Parse(sourceSplit[1]));
bool b5 = this.WriteValue(this.OpcWriteItems.SourcePosZ, int.Parse(sourceSplit[0]));
bool b6 = this.WriteValue(this.OpcWriteItems.DestinationPosY, int.Parse(toSplit[2]));
bool b7 = this.WriteValue(this.OpcWriteItems.DestinationPosX, int.Parse(toSplit[1]));
bool b8 = this.WriteValue(this.OpcWriteItems.DestinationPosZ, int.Parse(toSplit[0]));
bool b10 = this.WriteValue(this.OpcWriteItems.Command, 1);
bool b11 = this.WriteValue(this.OpcWriteItems.PalletType, palltype);//2代表大托盘1小托盘
bool b12 = this.WriteValue(this.OpcWriteItems.High, heighttype);//2代表大托盘1小托盘
if (!(b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8 && b10&&b11&&b12))
{
msg = string.Format("向堆垛机{0}发送任务{1}失败", SrmName, taskId);
return false;
}
bool b9 = this.WriteValue(this.OpcWriteItems.Stb, 1);
if (b9)
{
return true;
}else
{
return false;
}
}
catch (Exception ex)
{
msg = ex.Message;
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendTask", msg);
return false;
}
}
///
/// 移动任务
///
///
///
///
///
///
public bool SendMove(int deviceId, int taskId, string sourcePlace, string toPlace, out string msg)
{
try
{
msg = "";
if (!CanDoMove(deviceId, out msg))
{
return false;
}
var toSplit = toPlace.Split('-');
var sourSplit = sourcePlace.Split('-');
bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
bool b3 = this.WriteValue(this.OpcWriteItems.SourcePosY, int.Parse(sourSplit[2]));
bool b4 = this.WriteValue(this.OpcWriteItems.SourcePosX, int.Parse(sourSplit[1]));
bool b5 = this.WriteValue(this.OpcWriteItems.SourcePosZ, int.Parse(sourSplit[0]));
bool b6 = this.WriteValue(this.OpcWriteItems.DestinationPosY, int.Parse(toSplit[2]));
bool b7 = this.WriteValue(this.OpcWriteItems.DestinationPosX, int.Parse(toSplit[1]));
bool b8 = this.WriteValue(this.OpcWriteItems.DestinationPosZ, int.Parse(toSplit[0]));
bool b9 = this.WriteValue(this.OpcWriteItems.Stb, 1);
bool b10 = this.WriteValue(this.OpcWriteItems.Command, 1);
bool b11 = this.WriteValue(this.OpcWriteItems.PalletType, 2);//2代表大托盘1小托盘
bool b12 = this.WriteValue(this.OpcWriteItems.High, 1);//2代表大托盘1小托盘
if (!(b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8 && b9 && b10))
{
msg = string.Format("向堆垛机{0}发送移动指令{1}失败", SrmName, taskId);
return false;
}
return true;
}
catch (Exception ex)
{
msg = ex.Message;
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendMove", msg);
return false;
}
}
///
/// 任务召回
///
///
///
///
///
public bool SendCallBackTask(int deviceId, int taskId, out string msg)
{
try
{
msg = "";
if (!IsOnline)
{
msg = string.Format("堆垛机{0}不在线", this.SrmName);
return false;
}
if (RMode != (int)ESrmMode.自动模式)
{
msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
return false;
}
if (RAlarm)
{
msg = string.Format("堆垛机{0}报警中", this.SrmName);
return false;
}
if (RLiftFull)
{
msg = string.Format("堆垛机{0}叉齿有货", this.SrmName);
return false;
}
if (RState != (int)ESrmState.空闲)
{
msg = string.Format("堆垛机{0}非空闲", this.SrmName);
return false;
}
bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
bool b3 = this.WriteValue(this.OpcWriteItems.Command, (int)ESrmCmd.召回);
if (!(b1 && b2 && b3))
{
msg = string.Format("向堆垛机{0}发送召回任务{1}失败", SrmName, taskId);
return false;
}
return true;
}
catch (Exception ex)
{
msg = ex.Message;
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendCallBackTask", msg);
return false;
}
}
///
/// 任务删除
///
///
///
///
///
public bool SendDelTask(int deviceId, int taskId, out string msg)
{
try
{
msg = "";
//if (!IsOnline)
//{
// msg = string.Format("堆垛机{0}不在线", this.SrmName);
// return false;
//}
//if (RMode != (int)ESrmMode.自动模式)
//{
// msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
// return false;
//}
//if (RState != (int)ESrmState.空闲)
//{
// msg = string.Format("堆垛机{0}非空闲", this.SrmName);
// return false;
//}
bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
bool b3 = this.WriteValue(this.OpcWriteItems.Command, (int)ESrmCmd.任务删除);
if (!(b1 && b2 && b3))
{
msg = string.Format("向堆垛机{0}发送召回任务{1}失败", SrmName, taskId);
return false;
}
return true;
}
catch (Exception ex)
{
msg = ex.Message;
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendCallBackTask", msg);
return false;
}
}
///
/// 是否可以发送出入库任务
///
///
///
///
public bool CanSendTask(int deviceId, out string msg)
{
msg = "";
if (!IsOnline)
{
msg = string.Format("堆垛机{0}不在线", this.SrmName);
return false;
}
if (RMode != (int)ESrmMode.自动模式)
{
msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
return false;
}
if (RAlarm)
{
msg = string.Format("堆垛机{0}报警中", this.SrmName);
return false;
}
if (RLiftFull)
{
msg = string.Format("堆垛机{0}叉齿有货", this.SrmName);
return false;
}
if (RState != (int)ESrmState.空闲)
{
msg = string.Format("堆垛机{0}非空闲", this.SrmName);
return false;
}
return true;
}
///
/// 是否可以发送移库任务
///
///
///
///
public bool CanDoMove(int deviceId, out string msg)
{
msg = "";
if (!IsOnline)
{
msg = string.Format("堆垛机{0}不在线", this.SrmName);
return false;
}
if (RMode != (int)ESrmMode.自动模式)
{
msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
return false;
}
if (RAlarm)
{
msg = string.Format("堆垛机{0}报警中", this.SrmName);
return false;
}
if (RLiftFull)
{
msg = string.Format("堆垛机{0}叉齿有货", this.SrmName);
return false;
}
if (RState != (int)ESrmState.空闲)
{
msg = string.Format("堆垛机{0}非空闲", this.SrmName);
return false;
}
return true;
}
///
/// 向设备写入指令
///
///
///
///
public bool WriteValue(string addr, object value)
{
string msg = string.Empty;
if (!string.IsNullOrEmpty(addr))
{
try
{
if (!this.WS71500.IsConnected)
{
this.WS71500.Close();
Thread.Sleep(100);
this.WS71500.Open();
}
msg = this.WS71500.Write(addr, value).ToString();
if (msg != "NoError")
{
string msg1 = this.WS71500.Write(addr, value).ToString();
if (msg1 != "NoError")
{
this.WS71500.Close();
Thread.Sleep(100);
this.WS71500.Open();
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), this.SrmName + "WriteValue" + addr, msg);
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
catch
{
msg = addr + "写入失败 ";
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), this.SrmName + "WriteValue", msg);
return false;
}
}
else
{
msg = addr + "写入失败 ";
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), this.SrmName+"WriteValue", msg);
return false;
}
}
}
}