WistonAgvClientcs.cs
9.3 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Timers;
namespace DeviceLibrary
{
public class WistonAgvClient
{
Socket client;
IPEndPoint ipe;
Timer timer;
static readonly ILog LOGGER = LogManager.GetLogger("WistonAgvClient");
object conn = new object();
Dictionary<string, bool> commandConfirm;
List<(DateTime, string)> OutCommandHistroy;
List<(DateTime, string)> InCommandHistroy;
public WistonAgvClient()
{
ConfigHelper.Config.Get("wiston_disable_agv_NeedEnter", false);
commandLoad();
//端口及IP
ipe = new IPEndPoint(IPAddress.Parse(ConfigHelper.Config.Get("WistonAgvServerIP")), ConfigHelper.Config.Get<int>("WistonAgvServerPort"));
log(ipe.ToString());
timer = new Timer(10000);
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true;
connect();
timer.Start();
}
void commandLoad() {
commandConfirm = new Dictionary<string, bool>();
commandConfirm.Add("Y1", true);
commandConfirm.Add("Y2", true);
commandConfirm.Add("Y5", true);
commandConfirm.Add("Y6", true);
}
DateTime lastnoopRequest = DateTime.MinValue;
DateTime lastnoopResponse = DateTime.MaxValue;
int waitTimes = 0;
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (client != null && client.Connected)
{
return;
if (waitTimes > 5) {
try
{
log($"服务器心跳包超时,超过次数,断开连接,稍后重新连接");
client.Close();
}
catch { }
return;
}
if (lastnoopRequest != DateTime.MinValue)
{
var s = lastnoopResponse - lastnoopRequest;
if (s.TotalSeconds < -5)
{
waitTimes++;
log($"服务器心跳包超时,times:{waitTimes}");
}
else
{
waitTimes = 0;
}
}
lastnoopRequest = DateTime.Now;
AsynSend("noop");
}
else if (client != null && !client.Connected)
{
connect();
}
}
void CloseConnect() {
if (client != null)
{
if (client.Connected)
client.Close();
//client.Dispose();
//client = null;
}
}
void connect() {
lock (conn)
{
if (client != null && client.Connected)
{
return;
}
//创建套接字
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//开始连接到服务器
client.BeginConnect(ipe, asyncResult =>
{
if (!client.Connected)
{
log("连接失败,稍后后再次连接");
//Task.Delay(10000).Wait();
CloseConnect();
return;
}
client.EndConnect(asyncResult);
//向服务器发送消息
//AsynSend("noop");
log("服务器连接成功");
//接受消息
AsynRecive(client);
}, null);
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="socket"></param>
/// <param name="message"></param>
public void AsynSend(string message)
{
if (client == null || message == string.Empty) return;
//编码
byte[] data = Encoding.UTF8.GetBytes(message);
try
{
client.BeginSend(data, 0, data.Length, SocketFlags.None, asyncResult =>
{
try
{
if (!client.Connected)
{
log("连接失败,等待5秒后再次连接");
//Task.Delay(5000).Wait();
CloseConnect();
return;
}
//完成发送消息
int length = client.EndSend(asyncResult);
log(string.Format("客户端发送消息:{0}", message));
}
catch { }
}, null);
}
catch (Exception ex)
{
log(string.Format("异常信息:{0}", ex.Message));
}
}
/// <summary>
/// 接收消息
/// </summary>
/// <param name="socket"></param>
void AsynRecive(Socket socket)
{
byte[] data = new byte[1024];
try
{
//开始接收数据
socket.BeginReceive(data, 0, data.Length, SocketFlags.None,
asyncResult =>
{
try
{
if (!socket.Connected)
{
CloseConnect();
log("连接断开了,稍后再次连接,AsynRecive");
return;
}
int length = socket.EndReceive(asyncResult);
var msg = Encoding.UTF8.GetString(data, 0, length);
MsgProcess(msg);
log(string.Format("收到服务器消息:{0}", msg));
AsynRecive(socket);
}
catch (Exception ex)
{
log(string.Format("异常信息:{0}", ex.Message));
CloseConnect();
log("连接断开了,稍后再次连接,AsynRecive");
}
}, null);
}
catch (Exception ex)
{
log(string.Format("异常信息:{0}", ex.Message));
}
}
public event EventHandler EmptyShelfInReady;
public event EventHandler FullShelfOutReady;
void MsgProcess(string msg)
{
msg = msg.Trim();
if (msg.Contains("noop"))
{
lastnoopResponse = DateTime.Now;
}
else if (msg.Contains("X1"))
{
AsynSend("X1");
EmptyShelfInReady?.Invoke(this, EventArgs.Empty);
}
else if (msg.Contains("X5"))
{
AsynSend("X5");
FullShelfOutReady?.Invoke(this, EventArgs.Empty);
}
else if (msg.StartsWith("Y"))
{
var command = msg.Substring(0, 2);
if (commandConfirm.ContainsKey(command)) {
commandConfirm[command] = true;
}
}
}
public void NeedEnter() {
SendCommand("Y1");
}
public void FinishEnter()
{
SendCommand("Y2");
}
public void NeedLeave()
{
SendCommand("Y5");
}
public void FinishLeave()
{
SendCommand("Y6");
}
Task SendCommand(string cmd) {
return Task.Run(new Action(() =>
{
if (!commandConfirm[cmd])
{
log($"指令{cmd}正在执行中.");
return;
}
AsynSend(cmd);
commandConfirm[cmd] = false;
int retry = 0;
Task.Delay(5000).Wait();
while (!commandConfirm[cmd] && retry<5) {
retry++;
AsynSend(cmd);
log($"指令:{cmd}发送失败,重试:{retry}");
Task.Delay(5000);
}
if (commandConfirm[cmd]) {
log($"指令:{cmd}发送成功.");
}
else
{
commandConfirm[cmd] = true;
log($"指令:{cmd}发送失败,停止重试.");
}
}
));
}
void log(string msg) {
LOGGER.Info(msg);
Console.WriteLine(msg);
}
void AddOutCommandHistroy(string cmd) {
OutCommandHistroy.Add((DateTime.Now, cmd));
if (OutCommandHistroy.Count>10)
OutCommandHistroy.RemoveAt(0);
}
}
}