AGVManager.cs
13.5 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Common;
using static DeviceLibrary.Agv_Info;
namespace DeviceLibrary
{
public enum LineState
{
None,
Busy
}
public static class AGVManager
{
static log4net.ILog log = log4net.LogManager.GetLogger("AGVManager");
/// <summary>
/// 节点信息
/// </summary>
public static List<ClientNode> nodeInfo;
/// <summary>
/// 小车信息
/// </summary>
public static List<Agv_Info> agvInfo;
public static AgvServer server;
public static Control control;
//public static WebService web;
//public static UnlockMissionManager unlockManager;
public static ChargeManager Charge;
//public static StandbyManager Standby;
public static List<JobType> jobTypes;
public static readonly string CONFIG_PATH = AppDomain.CurrentDomain.BaseDirectory + "Config\\";
/// <summary>
/// 初始化配置
/// </summary>
public static void Init()
{
string path;
string[] line;
string[] temp;
bool isuse;
string rfid = "";
Charge = new ChargeManager();
//Standby = new StandbyManager();
agvInfo = new List<Agv_Info>();
jobTypes = new List<JobType>();
jobTypes.Add(new GetEmptyShelfJobType());
jobTypes.Add(new SendFullShelfJobType());
jobTypes.Add(new ChargeJobType());
path = CONFIG_PATH + SettingString.FileName_AGV;
line = File.ReadAllLines(path, Encoding.UTF8);
for (int i = 1; i < line.Length; i++)
{
temp = line[i].Split(',');
if (temp.Length != 5) continue;
bool.TryParse(ReadIni(temp[1], SettingString.IsUse), out isuse);
rfid = ReadIni(temp[1], SettingString.RFID);
agvInfo.Add(new Agv_Info(temp[0], temp[1], temp[2], temp[3], temp[4], isuse, rfid));
}
//任务加载
MissionSys.Init();
//节点加载
nodeInfo = new List<ClientNode>();
path = CONFIG_PATH + SettingString.FileName_AgvProductionLine;
line = System.IO.File.ReadAllLines(path, Encoding.GetEncoding("gb2312"));
for (int i = 1; i < line.Length; i++)
{
temp = line[i].Split(',');
if (temp.Length != 4) continue;
Boolean.TryParse(ReadIni(temp[1], SettingString.IsUse), out bool isUse);
nodeInfo.Add(new ClientNode(temp[0], temp[1], temp[2], temp[3], isUse));
}
}
/// <summary>
/// 读取配置
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string ReadIni(string section, string key)
{
return IniFileHelper.ReadValue(section, key, CONFIG_PATH + SettingString.FileName_tempData);
}
/// <summary>
/// 写入配置
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void WriteIni(string section, string key, string value)
{
IniFileHelper.WriteValue(section, key, value, CONFIG_PATH + SettingString.FileName_tempData);
}
/// <summary>
/// 根据线体名获取节点名
/// </summary>
/// <param name="lineName"></param>
/// <param name="nodeName"></param>
/// <returns></returns>
public static bool GetNodeNameByLineName(string lineName, out string nodeName)
{
nodeName = "";
int id = nodeInfo.FindIndex(s => s.LineName.Equals(lineName));
if (id > -1)
{
nodeName = nodeInfo[id].Name;
return true;
}
else
{
return false;
}
}
/// <summary>
/// 根据节点名获取线体名
/// </summary>
/// <param name="nodeName"></param>
/// <param name="lineName"></param>
/// <returns></returns>
public static bool GetLineNameByNodeName(string nodeName, out string lineName)
{
lineName = "";
int id = nodeInfo.FindIndex(s => s.Name.Equals(nodeName));
if (id > -1)
{
lineName = nodeInfo[id].LineName;
return true;
}
else
{
return false;
}
}
/// <summary>
/// 查找节点是否存在以及是否调用
/// </summary>
/// <param name="nodeName">节点名称</param>
/// <returns>-1:不存在/未调用</returns>
public static int FindNode(string nodeName)
{
int idx = nodeInfo.FindIndex(s => s.Name.Equals(nodeName) && s.IsUse);
return idx;
}
//private static object checkLocObj = new object();
/// <summary>
/// 查看云仓需要料架的状况
/// </summary>
/// <param name="agv"></param>
/// <param name="node"></param>
public static bool CheckBoxNeedShelfState(Agv_Info agv, out string nodeName)
{
nodeName = "";
// if (Monitor.TryEnter(checkLocObj))
{
try
{
int tarIdx = nodeInfo.FindIndex(s => s.Name == SettingString.D1 && s.StateEquals(eNodeStatus.NeedEnter) && s.IsUse);
if (tarIdx > -1)
{
if (CheckAgvEmptyJob(agv, SettingString.D1))
{
nodeName = SettingString.D1;
log.Debug(string.Format("{0} {1}需要料架", agv.Name, SettingString.D1));
return true;
}
}
tarIdx = nodeInfo.FindIndex(s => s.Name == SettingString.D2 && s.StateEquals(eNodeStatus.NeedEnter) && s.IsUse);
if (tarIdx > -1)
{
if (CheckAgvEmptyJob(agv, SettingString.D2))
{
nodeName = SettingString.D2;
log.Debug(string.Format("{0} {1}需要料架", agv.Name, SettingString.D2));
return true;
}
}
tarIdx = nodeInfo.FindIndex(s => s.Name == SettingString.D3 && s.StateEquals(eNodeStatus.NeedEnter) && s.IsUse);
if (tarIdx > -1)
{
if (CheckAgvEmptyJob(agv, SettingString.D3))
{
nodeName = SettingString.D3;
log.Debug(string.Format("{0} {1}需要料架", agv.Name, SettingString.D3));
return true;
}
}
}
catch (Exception ex)
{
log.Error("CheckBoxNeedShelfState", ex);
}
finally
{
// Monitor.Exit(checkLocObj);
}
}
return false;
}
/// <summary>
/// 检查是否有AGV送空架子
/// </summary>
/// <param name="nodeName"></param>
/// <returns></returns>
public static bool CheckAgvEmptyJob(Agv_Info agv, string nodename)
{
int i = agvInfo.FindIndex(s => !s.IP.Equals(agv.IP) && s.CurJob is EmptyShelfBackJob &&
((EmptyShelfBackJob)s.CurJob).EmptyShelfTargetPlace.Equals(nodename));
if (i > -1) return false;
return true;
}
/// <summary>
/// 查看料架存放处是否可以放空料架
/// </summary>
/// <param name="agv"></param>
/// <param name="node"></param>
public static bool CheckShelfStorageInState(Agv_Info agv)
{
int tarIdx = nodeInfo.FindIndex(s => s.Name == SettingString.A2
&& (s.StateEquals(eNodeStatus.NeedEnter)) && s.IsUse);
if (tarIdx > -1)
{
log.Debug(string.Format("{0} {1}可以存放料架", agv.Name, SettingString.A2));
return true;
}
return false;
}
/// <summary>
/// 检查需要空料架的地方
/// </summary>
/// <param name="agv"></param>
/// <param name="nodeName"></param>
/// <returns></returns>
public static bool CheckNeedEmptyShelf(Agv_Info agv, out string nodeName)
{
if (!CheckBoxNeedShelfState(agv, out nodeName))
{
if (CheckShelfStorageInState(agv))
{
nodeName = SettingString.A2;
return true;
}
}
return false;
}
/// <summary>
/// 存放空料架出口是否有空料架
/// </summary>
/// <param name="agv"></param>
/// <param name="node"></param>
public static bool CheckShelfStorageOutState(Agv_Info agv)
{
int tarIdx = nodeInfo.FindIndex(s => s.Name == SettingString.A1
&& (s.StateEquals(eNodeStatus.NeedLeave)) && s.IsUse);
int idx = AGVManager.agvInfo.FindIndex(s => !s.IP.Equals(agv.IP) && s.CurJob is GoEmptyShelfLineJob &&
((GoEmptyShelfLineJob)s.CurJob).EmptyShelfPlace.Equals(SettingString.A1));
if (tarIdx > -1 && idx == -1)
{
log.Debug(string.Format("{0} {1}可以出料架", agv.Name, SettingString.A1));
return true;
}
return false;
}
//双层线工单信息
public static string doubleLine_WO = "";
public static string warnMsg = "";
/// <summary>
/// 查找出满料架任务
/// </summary>
/// <returns></returns>
//public static bool FindFullShelfTask(Agv_Info agv)
//{
// if (!CheckCanExecuteMission(agv))
// return false;
// int idx = nodeInfo.FindIndex(s => s.Name.Equals(SettingString.A6)
// && (s.StateEquals(eNodeStatus.NeedEnterLeave) || (s.StateEquals(eNodeStatus.NeedLeave))) && !s.RFID.Equals(""));
// if (idx > -1)
// {
// if (HttpManager.FindFullShelfTarget(nodeInfo[idx].RFID, out HttpManager.BoxDestInfo FullShelfDestInfo))
// {
// if (FullShelfDestInfo.location.StartsWith(SettingString.RoomC_Name_Prefix) && SettingString.RoomC_AGV_IPs.Contains(agv.IP))
// {
// int i = agvInfo.FindIndex(s => s.CurJob is GoFullShelfStationJob && !s.IP.Equals(agv.IP));
// if (i == -1)
// return true;
// }
// else if (FullShelfDestInfo.location.StartsWith(SettingString.RoomS_Name_Prefix) && !SettingString.RoomC_AGV_IPs.Contains(agv.IP))
// {
// int i = agvInfo.FindIndex(s => s.CurJob is SendFullShelfToLineJob && !s.IP.Equals(agv.IP)
// && ((SendFullShelfToLineJob)s.CurJob).FullShelfPlace.Equals(FullShelfDestInfo.location));
// if (i > -1)
// return false;
// i = agvInfo.FindIndex(s => s.CurJob is GoFullShelfStationJob && !s.IP.Equals(agv.IP));
// if (i == -1)
// return true;
// }
// }
// else
// {
// if (FullShelfDestInfo != null)
// {
// log.Error("A6的出料信息不正确,请检查:" + FullShelfDestInfo.ShowInfo("ERROR"));
// }
// }
// }
// return false;
//}
/// <summary>
/// 判断电量是否够执行任务
/// </summary>
/// <param name="agv"></param>
/// <returns></returns>
public static bool CheckCanExecuteMission(Agv_Info agv)
{
if (agv.Battery <= Charge.BatteryMin)
{
log.Debug(agv.Name + " 电量小于" + Charge.BatteryMin + "%,不执行任务");
return false;
}
return true;
}
/// <summary>
/// 判断小车是否能接任务
/// </summary>
/// <param name="agv"></param>
/// <returns></returns>
public static bool CheckAGVStatusNone(Agv_Info agv)
{
if ((agv.CurJob == null || agv.CurJob is ChargeJob) && !agv.IsExistShelf)
return true;
else
return false;
}
}
public static class Window_API
{
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll ", SetLastError = true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
public const int SW_RESTORE = 9;
}
}