AgvClient.cs
11.4 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using AsaPL;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OnlineStore.LoadCSVLibrary;
using System.Web.UI.WebControls;
namespace OnlineStore.DeviceLibrary
{
public class AgvClient
{
private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp);
private AsaPL.AgvClient agvClient;
private static bool isInit = false;
public static Dictionary<string, AsaPL.ClientAction> actionMap = new Dictionary<string, AsaPL.ClientAction>();
public bool InLine_InProcess = false;
public bool OutLine_InProcess = false;
private static string InLine_Name = ConfigAppSettings.GetValue(Setting_Init.InLine_Name);
private static string OutLine_Name = ConfigAppSettings.GetValue(Setting_Init.OutLine_Name);
public string OutLine_RFID { get; set; } = "";
#region InLine Action
/// <summary>
/// 无状态
/// </summary>
public void InLine_None()
{
SetStatus(InLine_Name, "", ClientAction.None);
}
/// <summary>
/// 可以进入状态
/// </summary>
public void InLine_NeedEnter()
{
SetStatus(InLine_Name, "", ClientAction.NeedEnter);
}
#endregion
#region OutLine Action
/// <summary>
/// 无状态
/// </summary>
public void OutLine_None(string rfid="")
{
SetStatus(OutLine_Name, rfid, ClientAction.None);
}
/// <summary>
/// 可以出去状态
/// </summary>
public void OutLine_NeedLeave(string rfid)
{
if (!OutLine_RFID.Equals(rfid))
{
OutLine_RFID = rfid;
SetStatus(OutLine_Name, rfid, ClientAction.NeedLeave);
}
}
#endregion
public void Init()
{
try
{
if (!isInit)
{
isInit = true;
agvClient = new AsaPL.AgvClient(ServerIp);
agvClient.CancelState = false;
agvClient.ReadyEnter += AgvClient_ReadyEnter;
agvClient.ReadyLeave += AgvClient_ReadyLeave;
agvClient.AGVFinishEnter += AgvClient_FinishEnter;
agvClient.AGVFinishLeave += AgvClient_FinishLeave;
}
actionMap = new Dictionary<string, AsaPL.ClientAction>();
agvClient.SetStatus(InLine_Name, "", ClientAction.None);
agvClient.SetStatus(OutLine_Name, "", ClientAction.None);
agvClient.Connect();
LogUtil.info("初始化AGV服务");
}
catch (Exception ex)
{
LogUtil.error("初始化AGV服务 " + ServerIp + " 出错:", ex);
}
}
/// <summary>
/// 设置取消状态
/// </summary>
/// <param name="isCancel">true则ClientAction为None</param>
public void SetCancelState(bool isCancel)
{
agvClient.CancelState = isCancel;
}
public string Feeder_In_Msg = "";
public string Feeder_Out_Msg = "";
/// <summary>
/// 小车准备进入
/// </summary>
/// <param name="name">节点名称</param>
private void AgvClient_ReadyEnter(string name)
{
if (IOManager.IOValue(IO_Type.L1_InCheck).Equals(IO_VALUE.LOW) && !InLine_InProcess && name.Equals(InLine_Name))
{
MayEnter(InLine_Name);
Task.Factory.StartNew(delegate
{
Feeder_In_Msg = "Feeder In: 等待 L1_InCheck=HIGH";
LogUtil.info(Feeder_In_Msg);
try
{
WaitUtil.Wait(60000, delegate
{
return IOManager.IOValue(IO_Type.L1_InCheck).Equals(IO_VALUE.HIGH);
}, "等待L1_InCheck=HIGH");
}
catch (Exception ex)
{
LogUtil.error("Feeder In:" + ex.ToString());
}
//五秒后改为离开状态
Thread.Sleep(5000);
Feeder_In_Msg = "Feeder In: 调用 FinishEnter ";
LogUtil.info(Feeder_In_Msg);
FinishEnter(InLine_Name);
Feeder_In_Msg = "Feeder In: 处理结束 更新状态为None ";
LogUtil.info(Feeder_In_Msg);
InLine_None();
});
}
else
{
if (IOManager.IOValue(IO_Type.L1_InCheck).Equals(IO_VALUE.HIGH))
Feeder_In_Msg = "Feeder In: L1_InCheck 检测到有料架,AGV的料架无法进入";
LogUtil.info(Feeder_In_Msg);
InLine_None();
}
LogUtil.info("收到 AgvClient_ReadyEnter [" + name + "]");
}
/// <summary>
///料架准备离开
/// </summary>
/// <param name="name">节点名称</param>
private void AgvClient_ReadyLeave(string name)
{
//SetStatus(name, "", ClientAction.ReadyLeave);
//if (name.Equals("D22"))
//{
// D22_InProcess = true;
// D22_ReadyLeave = true;
//}
if (IOManager.IOValue(IO_Type.L2_OutCheck).Equals(IO_VALUE.HIGH) && !OutLine_InProcess && name.Equals(OutLine_Name))
{
MayLeave(OutLine_Name, OutLine_RFID);
//LineManager.feederLine.UpdateSleep(false);
LineManager.feederLine.StopIOMove(IO_Type.L2_OutStopDown, 1500);
//agvClient.MayLeave(id);
// SetStatus(id, shefId, ClientAction.MayLeave);
Feeder_Out_Msg = "Feeder Out: 允许流出料架";
LogUtil.info(Feeder_Out_Msg);
Task.Factory.StartNew(delegate
{
//十秒后改为完成状态
Thread.Sleep(10000);
Feeder_Out_Msg = "Feeder Out: 调用 FinishLeave ";
LogUtil.info(Feeder_Out_Msg);
FinishLeave(OutLine_Name, OutLine_RFID);
Thread.Sleep(5000);
Feeder_Out_Msg = "Feeder Out: 处理结束 更新状态为None ";
LogUtil.info(Feeder_Out_Msg);
OutLine_None();
});
}
else
{
if (IOManager.IOValue(IO_Type.L2_OutCheck).Equals(IO_VALUE.LOW))
Feeder_Out_Msg = "Feeder Out: L2_OutCheck 未检测到料架,无法将料架进入AGV";
LogUtil.info(Feeder_Out_Msg);
OutLine_None();
}
LogUtil.info("收到 AgvClient_ReadyLeave [" + name + "] ");
}
/// <summary>
/// 料架离开小车完成
/// </summary>
/// <param name="name"></param>
private void AgvClient_FinishLeave(string name)
{
if (name.Equals(InLine_Name))
{
InLine_InProcess = false;
}
else if (name.Equals(OutLine_Name))
{
OutLine_InProcess = false;
}
LogUtil.info("收到 AgvClient_FinishLeave [" + name + "] ");
}
/// <summary>
/// 料架进入小车完成
/// </summary>
/// <param name="name"></param>
private void AgvClient_FinishEnter(string name)
{
//if (name.Equals("D21"))
//{
// D21_InProcess = false;
//}
//else if (name.Equals("D22"))
//{
// D22_InProcess = false;
//}
LogUtil.info("收到 AgvClient_FinishEnter [" + name + "] ");
}
public void MayEnter(string name)
{
SetStatus(name, "", ClientAction.MayEnter);
if (name.Equals(InLine_Name))
{
InLine_InProcess = true;
}
LogUtil.info("MayEnter [" + name + "] ");
}
public void MayLeave(string name, string rfid)
{
SetStatus(name, rfid, ClientAction.MayLeave);
if (name.Equals(OutLine_Name))
{
OutLine_InProcess = true;
}
LogUtil.info("MayLeave [" + name + "] ");
}
/// <summary>
/// 料架进入产线完成
/// </summary>
/// <param name="name"></param>
public void FinishEnter(string name)
{
Task.Factory.StartNew(delegate
{
SetStatus(name, "", ClientAction.FinishEnter);
Thread.Sleep(15000);
if (name.Equals(InLine_Name))
{
InLine_InProcess = false;
}
else if (name.Equals(OutLine_Name))
{
OutLine_InProcess = false;
}
});
}
/// <summary>
/// 料架离开产线完成
/// </summary>
/// <param name="name"></param>
public void FinishLeave(string name, string rfid = "")
{
Task.Factory.StartNew(delegate
{
SetStatus(name, rfid, ClientAction.FinishLeave);
Thread.Sleep(15000);
if (name.Equals(InLine_Name))
{
InLine_InProcess = false;
}
else if (name.Equals(OutLine_Name))
{
OutLine_InProcess = false;
}
});
}
private bool SetStatus(string name, string shelfId = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low)
{
// if (!agvClient.IsConn)
// {
// LogUtil.error("SetStatus AGV服务未连接!");
// return false;
// }
if (actionMap.ContainsKey(name))
{
ClientAction currA = actionMap[name]; //相同状态就设置一次
if (currA.Equals(action) && shelfId == "")
{
return false;
}
// actionMap[name] = action;
// agvClient.SetStatus(name, shelfId, action, level);
LogUtil.info("设置 " + name + " [" + shelfId + "] " + action.ToString());
return true;
}
agvClient.SetStatus(name, shelfId, action, level);
//actionMap.Add(name, action);
LogUtil.info("设置 " + name + " ["+ shelfId + "] "+ action.ToString());
return true;
}
public bool ISConnected()
{
if (agvClient == null)
{
return false;
}
return agvClient.IsConn;
}
public void Dispose()
{
try
{
if (agvClient != null)
{
agvClient.Close();
LogUtil.info("关闭AGV服务");
}
}
catch (Exception ex)
{
LogUtil.error("关闭AGV服务 " + ServerIp + " 出错:", ex);
}
}
}
}