BOXManager.cs
17.2 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using log4net;
using SmartShelf.Common;
using SmartShelf.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace SmartShelf.DeviceLibrary
{
/// <summary>
/// 流水线自动料仓-流水线类
/// </summary>
public class BOXManager
{
private static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static string BoxName = "单色灯料架";
public static BOX_Config Config = null;
public static string CID = "";
private static System.Timers.Timer timersTimer;
private static System.Timers.Timer serverConTimer = new System.Timers.Timer();
private static bool isInit = false;
public static string WarnMsg = "";
public static Dictionary<string, BoxPosition> PositionMap=null;
public static int BoxCount = 1;
/// <summary>
/// 门状态列表,-1=未知,1=打开,0=关闭
/// </summary>
public static Dictionary<string, int> StatusMap = new Dictionary<string, int>();
public static bool IsRun = false;
/// <summary>
/// 灯闪烁列表,库位号 value为颜色
/// </summary>
public static Dictionary<string, string> TwinkleMap = new Dictionary<string, string>();
public static bool StartInit()
{
try
{
BoxCount = ConfigAppSettings.GetIntValue(Setting_Init.BoxCount);
string appPath = Application.StartupPath;
//加载位置
string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config);
if (BoxCount > 1)
{
string fileSub = System.IO.Path.GetExtension(positionConfigFile);
for(int i = 1; i <= BoxCount; i++)
{
string fileName = positionConfigFile.Substring(0,positionConfigFile.Length-fileSub.Length) + "_" + i+fileSub;
CSVPositionReader<BoxPosition>.AddCSVFile(fileName);
}
}
else
{
CSVPositionReader<BoxPosition>.AddCSVFile(positionConfigFile);
}
PositionMap = CSVPositionReader<BoxPosition>.allPositionMap;
if (PositionMap == null || PositionMap.Count <= 0)
{
return false ;
}
LEDManager.deviceMap = new Dictionary<string, LEDBaseModule>();
StatusMap = new Dictionary<string, int>();
TwinkleMap = new Dictionary<string, string>();
foreach (BoxPosition box in PositionMap.Values)
{
if (!LEDManager.deviceMap.ContainsKey(box.DeviceIp))
{
//LEDSingleModule led = new LEDSingleModule(box.DeviceIp);
//led.AllLightOn(Light.BlueLight(1));
LEDManager.deviceMap.Add(box.DeviceIp, LEDBaseModule.GetModule(box.DeviceIp));
}
StatusMap.Add(box.PositionNum, -1);
TwinkleMap.Add(box.PositionNum, "");
}
CID = ConfigAppSettings.GetValue(Setting_Init.Store_CID);
BoxName = (" 单色灯料架_" + CID + " ").ToUpper();
if (LEDManager.DeviceLedType.Equals(1))
{
BoxName = (" 三色灯料架_" + CID + " ").ToUpper();
}
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_ConfigPath);
Config = (BOX_Config)CSVConfigReader.LoadConfig(1, CID, StoreType.RC_PLC_SM, filePath);
if (Config == null)
{
return false;
}
LogUtil.info(BoxName + "加载完成!");
Init();
return true;
} catch (Exception ex)
{
LogUtil.error(BoxName + "加载配置出错:" + ex.ToString());
} return false;
}
/// <summary>
/// 初始化
/// </summary>
protected static void Init()
{
if (!isInit)
{
serverConTimer = new System.Timers.Timer();
serverConTimer.Enabled = false;
serverConTimer.Interval = 300;
serverConTimer.Elapsed += server_connect_timer_Tick;
serverConTimer.AutoReset = true;
timersTimer = new System.Timers.Timer();
timersTimer.Enabled = false;
timersTimer.Interval = 200;
timersTimer.Elapsed += timersTimer_Elapsed;
timersTimer.AutoReset = true;
isInit = true;
}
}
public static bool StartRun()
{
LogUtil.info(LOGGER, BoxName + "启动成功,时间:" + DateTime.Now.ToString() + "!");
timersTimer.Enabled = true;
serverConTimer.Enabled = true;
IsRun = true;
LEDManager. OpenStatusLights("green");
return true;
}
private static void RFID_RW_ReadEvent(string cardNum, string cardValue)
{
if (!String.IsNullOrEmpty(cardNum))
{
LastCardValue = cardNum + "-" + cardValue;
}
}
public static void StopRun()
{
//关闭串口
timersTimer.Enabled = false;
serverConTimer.Enabled = false;
foreach(LEDBaseModule led in LEDManager.deviceMap.Values)
{
led.AllLightOff();
}
LEDManager.CloseStatusLights();
IsRun = false;
LogUtil.info(LOGGER, BoxName + "停止运行,时间" + DateTime.Now.ToShortTimeString() + "!");
}
protected static void timersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
//判断是否需要亮黄灯
List<string> openLeds = new List<string>();
foreach (string key in StatusMap.Keys)
{
if (StatusMap[key].Equals(1))
{
openLeds.Add(key);
}
}
if (openLeds.Count > 0)
{
if (!LEDManager.CurrLedStatus.Equals(2))
{
LEDManager.OpenStatusLights("green");
}
}
else
{
if (!LEDManager.CurrLedStatus.Equals(1))
{
LEDManager.OpenStatusLights("yellow");
}
}
TwinkLight();
}
catch (Exception ex)
{
LogUtil.error("timersTimer_Elapsed出错:" + ex.ToString());
}
}
#region 与服务器通信定时器,每1秒向服务器通知一次状态,同时执行出库操作
private static bool isInProcess = false;
private static void server_connect_timer_Tick(object sender, EventArgs e)
{
if (isInProcess)
{
return;
}
isInProcess = true;
try
{
SendInOutPosId();
}catch(Exception ex)
{
LogUtil.error("server_connect_timer_Tick 出错:" + ex.ToString());
}
isInProcess = false;
}
private static string LastCardValue = "";
public static string StrMsg = "";
public static void SendInOutPosId()
{
StrMsg = "";
//构建发送给服务器的对象
Operation lineOperation = ServerManager.GetLineBoxStatus();
//获取亮灯的库位
string posId = "";
foreach(string key in StatusMap.Keys)
{
if (StatusMap[key].Equals(1))
{
posId = posId + key + "|";
}
}
if (String.IsNullOrEmpty(posId).Equals(false))
{
lineOperation.data.Add(ParamDefine.posOpened, posId);
}
foreach(string str in lineOperation.data.Keys)
{
StrMsg += "[" + str + "=" + lineOperation.data[str] + "]\r\n";
}
string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
Operation resultOperation = HttpHelper.Post(ServerManager.GetPostApi(server), lineOperation, false);
if (resultOperation == null || resultOperation.data == null)
{
//判断服务端是否返回出库操作
return;
}
// 开灯: key为open value为库位信息,如果多个用 | 分割
//客户端发送
//当前灯的状态:key为posOpened value为当前亮灯的库位,如果多个用 | 分割
//卡信息: key为card value为读取到的卡内容,卡号 - 内容
//写卡:
//key = writeCard value为要写的内容
// key = cardResult value = OK表示写成功
if (resultOperation.data.ContainsKey(ParamDefine.open))
{
ProcessOpenDoor(resultOperation.data[ParamDefine.open]);
}
if (resultOperation.data.ContainsKey(ParamDefine.close))
{
ProcessCloseDoor(resultOperation.data[ParamDefine.close]);
}
if (resultOperation.data.ContainsKey(ParamDefine.closeAll))
{
ProcessCloseAll();
}
}
public static void ProcessOpenAll(string proMsg = "Revice ", string ip = "")
{
LogUtil.info(BoxName + proMsg + " closeAll命令:");
List<string> posIdList = new List<string>(PositionMap.Keys);
foreach (LEDBaseModule led in LEDManager.deviceMap.Values)
{
if (String.IsNullOrEmpty(ip) || led.ModuleIP.Equals(ip))
{
led.AllLightOn();
}
}
foreach (string key in posIdList)
{
string pIP = "";
if (PositionMap.ContainsKey(key))
{
pIP = PositionMap[key].DeviceIp;
}
if (String.IsNullOrEmpty(ip) || pIP.Equals(ip))
{
StatusMap[key] = 1;
}
}
}
public static void ProcessCloseAll(string proMsg= "Revice ", string ip = "")
{
LogUtil.info(BoxName + proMsg+ " closeAll命令:" );
List<string> posIdList = new List<string>(PositionMap.Keys);
foreach(LEDBaseModule led in LEDManager.deviceMap.Values)
{
if (String.IsNullOrEmpty(ip) || led.ModuleIP.Equals(ip))
{
led.AllLightOff();
}
}
foreach (string key in posIdList)
{
string pIP = "";
if (PositionMap.ContainsKey(key))
{
pIP = PositionMap[key].DeviceIp;
}
if (String.IsNullOrEmpty(ip) || pIP.Equals(ip))
{
StatusMap[key] = 0;
}
}
}
public static void ProcessOpenDoor(string posids, string proMsg = "Revice ")
{
LogUtil.info(BoxName + proMsg + " open命令:" + posids);
string[] posArray = posids.Split('|');
foreach (string posStr in posArray)
{
//是否需要闪烁
bool needTwinkle = false;
string posid = posStr;
string color = "Green";
if (posStr.Contains("="))
{
string[] posInfos = posStr.Split('=');
posid = posInfos[0];
color = posInfos[1];
if (posInfos.Length > 2 && LEDManager.DeviceLedType.Equals(2))
{
LogUtil.info("库位【" + posid + "】需要开启闪烁" + color);
needTwinkle = true;
}
}
color = color.ToLower();
if (PositionMap.ContainsKey(posid))
{
BoxPosition position = PositionMap[posid];
Light[] lights = Light.GetLights(position.DmxId, color, position.GetLedList().ToArray());
LEDManager.GetLedModule(position.DeviceIp).LightOn(lights);
StatusMap[posid] = 1;
if (needTwinkle)
{
//需要闪烁
TwinkleMap[posid] = color;
}
else
{
TwinkleMap[posid] = "";
}
}
else
{
if (posid.ToLower().Equals("statuslight"))
{
LogUtil.info("状态灯指令:" + posid + "=" + color);
}
else
{
LogUtil.error(BoxName + "打开门锁失败,未找到库位号:" + posid);
}
}
}
}
private static bool TwinkleLightOpen = false;
public static void TwinkLight()
{
if (!LEDManager.DeviceLedType.Equals(1))
{
return;
}
//只有三色灯才有闪烁功能
TwinkleLightOpen = !TwinkleLightOpen;
bool hasTwinkleLight = false;
var keys = TwinkleMap.Keys.ToList();
foreach (string posid in keys)
{
string color = TwinkleMap[posid];
if (color != null && !color.Equals(""))
{
if (PositionMap.ContainsKey(posid))
{
BoxPosition position = PositionMap[posid];
if (StatusMap[posid] == 0)
{
LogUtil.info("库位【" + posid + "】灯已灭,停止闪烁" + color);
TwinkleMap[posid] = "";
LEDManager.GetLedModule(position.DeviceIp).LightOff(position.DmxId, position.GetLedList().ToArray());
}
else
{
hasTwinkleLight = true;
if (TwinkleLightOpen)
{
Light[] lights = Light.GetLights(position.DmxId, color, position.GetLedList().ToArray());
//LogUtil.info("库位【" + posid + "】闪烁开灯" + color);
LEDManager.GetLedModule(position.DeviceIp).LightOn(lights);
}
else
{
//LogUtil.info("库位【" + posid + "】闪烁灭灯" + color);
LEDManager.GetLedModule(position.DeviceIp).LightOff(position.DmxId, position.GetLedList().ToArray());
}
}
}
else
{
LogUtil.info("库位【" + posid + "】未找到,停止闪烁");
TwinkleMap[posid] = "";
}
}
}
if (hasTwinkleLight)
{
LEDManager.OpenStatusLights("red");
}
else
{
LEDManager.OpenStatusLights("red");
}
}
public static void ProcessCloseDoor(string posids, string proMsg = "Revice ")
{
LogUtil.info(BoxName + proMsg + " close命令:" + posids);
string[] posArray = posids.Split('|');
foreach (string posid in posArray)
{
string posName = posid;
if (posid.Contains("="))
{
posName = posid.Split('=')[0];
}
if (PositionMap.ContainsKey(posName))
{
BoxPosition position = PositionMap[posName];
LEDManager.GetLedModule(position.DeviceIp).LightOff(Light.GetLights(position.DmxId,position.GetLedList(),0) );
StatusMap[posName] = 0;
}
else
{
LogUtil.error(BoxName + "关闭门锁败,未找到库位号:" + posName);
}
}
}
#endregion
}
}