MsgRecevie.cs
6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using XingYe_ACS.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace XingYe_ACS.Business
{
public class MsgRecevie
{
/// <summary>
/// 指令解析
/// </summary>
/// <param name="socketOpt">客户端对象</param>
public static void TcpTranslate(SocketOpt socketOpt)
{
try
{
ClientMsg clientMsg = null;
byte[] dataRecv = socketOpt.recData;
clientMsg = AgvHeartRecevie(dataRecv);//无错误时msgClientErr=0;
#region 注释功能码
//switch (dataRecv[1])
//{
// case 0:
// clientMsg = AgvHeartRecevie(dataRecv);//无错误时msgClientErr=0;
// break;
// default:
// App.ExFile.MessageLog("MsgException", string.Format("Tcp指令功能码无定义:FuncCode[{0}]", dataRecv[1].ToString()));
// socketOpt.sendData = GetSendData.GetRepeatData();
// break;
//}
#endregion
if (clientMsg.clientErr != 0)
{
if (clientMsg.clientErr != EnumMsg.ErrMsg.不作处理)
{
App.ExFile.MessageLog("MsgException", "客户端信息出错:" + clientMsg.clientErr.ToString()
+ "=名称=" + clientMsg.strAddress + "=任务号=" + clientMsg.intTaskNo + "=动作状态=" + clientMsg.actState);
}
return;
}
else if (App.SingleControlList.Exists(a => a.agvNo == clientMsg.strAddress))
{
List<ClientMotion> listMotion = new List<ClientMotion>();
SingleControl singleControl = App.SingleControlList.Find(a => a.agvNo == clientMsg.strAddress);
socketOpt.sendData = GetSendData.GetNewActionData(singleControl.agvAction, null);
if (singleControl.agvAction == 200)
{
if (clientMsg.intState < (int)EnumMsg.State.手动无码) return;
else
{
singleControl.agvAction = 0;
}
}
else if (singleControl.agvAction == 209 && clientMsg.intState < (int)EnumMsg.State.离线)
{
App.SingleControlList.RemoveAll(a => a.agvNo == clientMsg.strAddress);
}
}
else
{
Config_ClientControl clientControl = App.ClientWork.Find(a => a.strClientIp == ((IPEndPoint)socketOpt.clientSocket.RemoteEndPoint).Address.ToString());
if (clientControl.strAgvNo == "" || clientControl.strAgvNo == null)
{
clientControl.strAgvNo = clientMsg.strAddress;
//将小车IP地址更新到agv表中
UpdateManage.UpdateAgvIP(clientControl);
}
MsgManage.TcpManage(clientMsg, socketOpt);
}
}
catch (Exception Ex)
{
App.ExFile.MessageError("TcpTranslate", Ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 获取Agv心跳信息
/// </summary>
/// <param name="dataRecv">心跳数据报文</param>
/// <returns>Agv心跳信息</returns>
public static ClientMsg AgvHeartRecevie(byte[] dataRecv)
{
try
{
//客户端发送数据对象
ClientMsg clientMsg = new ClientMsg();
clientMsg.intDataLength = dataRecv[0];
clientMsg.strAddress = Encoding.ASCII.GetString(dataRecv, 1, 10).Replace("\0", "");
clientMsg.msgAgv = App.AgvList.FirstOrDefault(a => a.strAgvNo == clientMsg.strAddress);
if (clientMsg.msgAgv == null)
{
clientMsg.clientErr = EnumMsg.ErrMsg.小车号不存在;
return clientMsg;
}
if (!clientMsg.msgAgv.isEnable)
{
clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
return clientMsg;
}
clientMsg.msgAgv.agvHeart = DateTime.Now;
clientMsg.intCheckCode = ComnMethod.checkCode(dataRecv, clientMsg.intDataLength + 12); //校验
if (clientMsg.intCheckCode != dataRecv[30])
{
clientMsg.clientErr = EnumMsg.ErrMsg.校验位错误;
clientMsg.msgAgv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, clientMsg.clientErr.ToString());
return clientMsg;
}
clientMsg.msgAgv.strStopReason = "";
clientMsg.strBarcode = BitConverter.ToUInt32(dataRecv, 11).ToString();
clientMsg.intVoltage = dataRecv[15];
#region 记录AGV电量显示
if (ComnMethod.DiffTime(DateTime.Parse(clientMsg.msgAgv.logTime.ToString())) > 10)
{
clientMsg.msgAgv.logTime = DateTime.Now;
App.ExFile.AgvVoltageLog("agvVoltage" + clientMsg.msgAgv.strAgvNo, clientMsg.intVoltage.ToString());
}
#endregion
clientMsg.intState = dataRecv[16];
clientMsg.agvDirection = (EnumMsg.OriType)dataRecv[17];
clientMsg.dialDirection = (EnumMsg.OriType)dataRecv[18];
clientMsg.intHeightState = dataRecv[19];
clientMsg.ErrorList = new int[] { dataRecv[20], dataRecv[21], dataRecv[22] };
clientMsg.intRunDistance = BitConverter.ToUInt16(dataRecv, 23);
string strTaskNo = BitConverter.ToUInt32(dataRecv, 25).ToString();
if (string.IsNullOrEmpty(strTaskNo))
clientMsg.intTaskNo = 0;
else
clientMsg.intTaskNo = int.Parse(strTaskNo);
clientMsg.actState = (EnumMsg.ActState)dataRecv[29];
// clientMsg.isCarry = dataRecv[31];
return clientMsg;
}
catch (Exception Ex)
{
throw new Exception("AgvHeartRecevie:" + Ex.Message.Replace(Environment.NewLine, ""));
}
}
}
}