CTUManager.cs
15.1 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using DeviceLibrary.CtuDeviceLib;
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace Mushiny
{
public class CTUManager
{
static MushinyTcpServer tcpServer;
public static List<CTU> CTUs;
public static readonly ILog LOGGER = Common.LOGGER;
public static int basketCnt = ConfigHelper.Config.Get(Common.Mushiny_ + "basketCnt", 6);
public static List<LocalLimitArea> localLimitAreas;
public static List<uint> GetLocalLimitIsOccupied(uint code,out string msg)
{
msg = "";
var areas = new List<string>();
var occupiedPoints = new List<uint>();
var finds = localLimitAreas.FindAll(s => s.PointCodes.Contains(code));
if (finds == null || finds.Count == 0) return occupiedPoints;
foreach (var item in finds)
{
occupiedPoints.AddRange(item.PointCodes);
areas.Add(item.Name);
}
msg = string.Join(",",areas);
return occupiedPoints;
}
static void loadLocalLimitArea()
{
localLimitAreas = new List<LocalLimitArea>();
var grp = ConfigAppSettings.GetValue("LocalLimitArea", "", "本地限制区");
if (string.IsNullOrEmpty(grp))
{
localLimitAreas.Add(new LocalLimitArea() { Name = "limit-1", PointCodes = new List<uint>() });
localLimitAreas.Add(new LocalLimitArea() { Name = "limit-2", PointCodes = new List<uint>() });
localLimitAreas.Add(new LocalLimitArea() { Name = "limit-3", PointCodes = new List<uint>() });
}
else
{
try
{
localLimitAreas = JsonHelper.DeserializeJsonToList<LocalLimitArea>(grp);
}
catch (Exception ex)
{
localLimitAreas = new List<LocalLimitArea>();
localLimitAreas.Add(new LocalLimitArea() { Name = "limit-1", PointCodes = new List<uint>() });
localLimitAreas.Add(new LocalLimitArea() { Name = "limit-2", PointCodes = new List<uint>() });
localLimitAreas.Add(new LocalLimitArea() { Name = "limit-3", PointCodes = new List<uint>() });
LogUtil.error("加载本地限制区异常", ex);
}
}
}
public static void SaveLocalLimitArea()
{
ConfigAppSettings.SaveValue("LocalLimitArea", JsonHelper.SerializeObject(localLimitAreas), "本地限制区");
}
#region 入料机构分组
/// <summary>
/// 入料机构组
/// </summary>
public static CtuGroup InletGroup = new CtuGroup();
/// <summary>
/// 手动线
/// </summary>
public static CtuGroup ManualGroup = new CtuGroup();
static void loadCtuGroup()
{
loadInletGroup();
loadManualGroup();
}
static void loadManualGroup()
{
var grp = ConfigAppSettings.GetValue("ManualGroup", "", "手动线CTU");
if (string.IsNullOrEmpty(grp))
{
ManualGroup.GroupName = "手动线CTU";
ManualGroup.MaxCnt = 1;
ManualGroup.CtuNames = new List<string>();
}
else
{
try
{
ManualGroup = JsonHelper.DeserializeJsonToObject<CtuGroup>(grp);
}
catch (Exception ex)
{
ManualGroup = new CtuGroup();
ManualGroup.GroupName = "入料机构CTU";
ManualGroup.MaxCnt = 1;
ManualGroup.CtuNames = new List<string>();
LogUtil.error("加载手动线分组异常", ex);
}
}
}
static void loadInletGroup()
{
var grp = ConfigAppSettings.GetValue("InletGroup", "", "入料机构CTU");
if (string.IsNullOrEmpty(grp))
{
InletGroup.GroupName = "入料机构CTU";
InletGroup.MaxCnt = 3;
InletGroup.CtuNames = new List<string>();
}
else
{
try
{
InletGroup = JsonHelper.DeserializeJsonToObject<CtuGroup>(grp);
}
catch (Exception ex)
{
InletGroup = new CtuGroup();
InletGroup.GroupName = "入料机构CTU";
InletGroup.MaxCnt = 3;
InletGroup.CtuNames = new List<string>();
LogUtil.error("加载入料分组异常", ex);
}
}
}
public static void SaveInletGroup()
{
ConfigAppSettings.SaveValue("InletGroup", JsonHelper.SerializeObject(InletGroup), "入料机构CTU");
}
public static void SaveManualGroup()
{
ConfigAppSettings.SaveValue("ManualGroup", JsonHelper.SerializeObject(ManualGroup), "手动线CTU");
}
#endregion
static CTUManager()
{
loadCtuGroup();
loadLocalLimitArea();
CTUs = new List<CTU>();
//加载agv相关信息
string linefilePath = Application.StartupPath + "\\" + ConfigHelper.Config.Get(Common.Mushiny_ + "ConfigPath_Robot", "/CtuService/Config/CSV/tbl_mushiny.csv");
if (!File.Exists(linefilePath))
{
LOGGER.Error($"未找到CTU配置路径:{linefilePath}");
return;
}
string[] lines = File.ReadAllLines(linefilePath);
//id,name,type,enable
for (int i = 1; i < lines.Length; i++)
{
string[] fields = lines[i].Split(',');
if (fields.Length < 4)
continue;
var item = new CTU(fields[1], fields[0], basketCnt)
{
Group = fields[2],
//Enabled = bool.Parse(fields[3])
};
item.ID = i;
var dis = ConfigHelper.Config.Get(Common.Mushiny_ + $"{item.Name}_DisableBaskets", new string[] { "" });
if (dis != null && dis.Length > 0)
{
foreach (var field in dis)
{
if (int.TryParse(field, out var value))
{
var find = item.GetAllBaskets().Find(s => s.Row == value);
if (find != null)
{
find.Shield = true;
}
}
}
}
CTUs.Add(item);
}
CTUs?.ForEach(s =>
{
s.GetAllBaskets()?.ForEach(basket =>
{
if(!string.IsNullOrEmpty(basket.BoxCode))
{
if (string.IsNullOrEmpty(basket.DstName))
{
if (InletGroup.IsInGroup(s.Name))
{
basket.DstName = Setting_Init.In_FeedingInlet;
}
else
{
basket.DstName = Setting_Init.Out_FeedingInlet;
}
}
}
});
s.ClearUnPuttedBox();
});
LOGGER.Info($"加载CTU路径:{linefilePath}");
}
/// <summary>
/// 编辑背篓屏蔽状态
/// </summary>
/// <param name="ctu"></param>
/// <param name="row"></param>
/// <param name="disable"></param>
public static void EditBasket(CTU ctu, byte row, bool disable)
{
if (ctu == null) return;
var find = ctu.GetAllBaskets().Find(s => s.Row == row);
if (find != null)
{
find.Shield = disable;
LOGGER.Info($"【{ctu.Name}】设置背篓【{row}】屏蔽状态为【{disable}】");
}
List<string> disables = new List<string>();
ctu.GetAllBaskets().ForEach(s =>
{
if (s.Shield)
{
disables.Add(s.Row.ToString());
}
}
);
ConfigHelper.Config.Set<string[]>(Common.Mushiny_ + $"{ctu.Name}_DisableBaskets", disables.ToArray());
}
/// <summary>
/// 编辑背篓是否有料箱
/// </summary>
/// <param name="ctu"></param>
/// <param name="row"></param>
/// <param name="exist"></param>
public static void EditBasketHasBox(CTU ctu, byte row, bool exist)
{
if (ctu == null) return;
var find = ctu.GetAllBaskets().Find(s => s.Row == row);
if (find != null)
{
find.IsPutted = exist;
LOGGER.Info($"【{ctu.Name}】设置背篓【{row}】为{(exist ? "有料箱" : "无料箱")}");
}
List<string> disables = new List<string>();
ctu.GetAllBaskets().ForEach(s =>
{
if (s.Shield)
{
disables.Add(s.Row.ToString());
}
}
);
ConfigHelper.Config.Set<string[]>(Common.Mushiny_ + $"{ctu.Name}_DisableBaskets", disables.ToArray());
}
public static void Init()
{
tcpServer = new MushinyTcpServer();
int serverPort = ConfigHelper.Config.Get(Common.Mushiny_ + "ServerPort", 9501);
try
{
tcpServer.AcceptClientEvent += TcpServer_AcceptClientEvent;
tcpServer.ReviceMsgBytesEvent += TcpServer_ReviceMsgBytesEvent;
tcpServer.Start(serverPort);
LogUtil.info($"开启牧星AGV服务,端口:{serverPort}");
}
catch (Exception ex)
{
LogUtil.error($"开启牧星AGV服务异常,端口:{serverPort}", ex);
}
}
public static List<CTU> GetCTUs()
{
return CTUs;
}
public static List<CTU> GetEnabledCTUs()
{
return CTUs.FindAll(s => s.Enabled);
}
/// <summary>
/// 获取其他已启用的CTU
/// </summary>
/// <param name="ctu"></param>
/// <returns></returns>
public static List<CTU> GetOhterEnabledCTUs(CTU ctu)
{
return CTUs.FindAll(s => s.Enabled && !s.Name.Equals(ctu.Name));
}
/// <summary>
/// 所有已启用的ctu占用的节点
/// 关闭的ctu,忽略
/// </summary>
public static List<uint> GetAllUsedPoint(CTU ctu = null)
{
List<uint> pts = new List<uint>();
CTUs.ForEach(s =>
{
if (s.Enabled)
{
if (ctu != null)
{
if (ctu.IP.Equals(s.IP))
{
}
else if (s.UsedPoint != null && s.UsedPoint.Count > 0)
{
for (int i = s.UsedPoint.Count - 1; i >= 0; i--)
{
var pp = s.UsedPoint[i];
if (!pts.Contains(pp))
{
pts.Add(pp);
}
}
}
}
else
{
if (s.UsedPoint != null && s.UsedPoint.Count > 0)
{
for (int i = s.UsedPoint.Count - 1; i >= 0; i--)
{
var pp = s.UsedPoint[i];
if (!pts.Contains(pp))
{
pts.Add(pp);
}
}
}
}
}
});
return pts;
}
private static void TcpServer_ReviceMsgBytesEvent(TcpClientBean client, byte[] Msg)
{
var find = CTUs.Find(s => s.IP.Equals(client.IP));
if (find != null)
{
find.HandleResponse(client, Msg);
}
else
{
LOGGER.Warn($"收到数据,但未找到IP为{client.IP}的CTU,请检查配置");
}
}
private static void TcpServer_AcceptClientEvent(TcpClientBean client)
{
var find = CTUs.Find(s => s.IP.Equals(client.IP));
if (find != null)
{
find.isLogin = false;
LOGGER.Info($"【{find.Name}】【{find.IP}】 上线");
}
else
{
LOGGER.Warn($"收到连接,未找到IP为{client.IP}的CTU,请检查");
}
}
}
/// <summary>
/// Ctu组
/// </summary>
public class CtuGroup
{
static List<string> allCtuNames = new List<string>();
/// <summary>
/// 组名
/// </summary>
public string GroupName { get; set; }
/// <summary>
/// 最多数量
/// </summary>
public int MaxCnt { get; set; }
/// <summary>
/// Ctu名称
/// </summary>
public List<string> CtuNames { get; set; }
/// <summary>
/// Ctu是否已在分组里
/// </summary>
/// <param name="ctuName"></param>
/// <returns></returns>
public static bool IsInGroups(string ctuName)
{
return allCtuNames.Contains(ctuName);
}
/// <summary>
/// 是否在该分组
/// </summary>
/// <param name="ctuName"></param>
/// <returns></returns>
public bool IsInGroup(string ctuName)
{
return CtuNames.Contains(ctuName);
}
public bool IsFull { get { return CtuNames.Count >= MaxCnt; } }
public void AddCtu(string ctuName)
{
if (allCtuNames.Contains(ctuName)) return;
if (CtuNames.Contains(ctuName)) return;
if (CtuNames.Count >= MaxCnt) return;
CtuNames.Add(ctuName);
CTUManager.SaveInletGroup();
LogUtil.info($"Ctu组【{GroupName}】添加CTU【{ctuName}】");
}
public void RemoveCtu(string ctuName)
{
allCtuNames.Remove(ctuName);
CtuNames.Remove(ctuName);
CTUManager.SaveInletGroup();
LogUtil.info($"Ctu组【{GroupName}】移除CTU【{ctuName}】");
}
}
}