FrmMain.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
namespace AGVControl_Elevator
{
public partial class FrmMain : Form
{
private BLL.Control control;
private Label[] lineDI;
private Label[] lineShelf;
private Label[] elevatorDI;
private Button[] elevatorDO;
private readonly Color BACK_FALSE = Color.FromArgb(240, 240, 240);
private readonly Color BACK_TRUE = Color.FromArgb(128, 255, 128);
public FrmMain()
{
InitializeComponent();
control = new BLL.Control();
}
private void InitLineCall()
{
int count = Common.lineInfos.Count;
lineShelf = new Label[count];
lineDI = new Label[count];
TlpLine.RowCount = count;
TlpLine.RowStyles.Clear();
CboLineName.Items.Clear();
float percent = 100f / count;
for (int i = 0; i < count; i++)
{
lineDI[i] = new Label
{
Text = Common.lineInfos[i].Name,
Font = new Font("宋体", 12f),
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Margin = new Padding(3),
BackColor = BACK_FALSE
};
TlpLine.Controls.Add(lineDI[i]);
TlpLine.SetColumn(lineDI[i], 0);
TlpLine.SetRow(lineDI[i], i);
lineShelf[i] = new Label
{
Text = "货架",
Font = new Font("宋体", 12f),
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Margin = new Padding(3),
BackColor = BACK_FALSE
};
TlpLine.Controls.Add(lineShelf[i]);
TlpLine.SetColumn(lineShelf[i], 1);
TlpLine.SetRow(lineShelf[i], i);
CboLineName.Items.Add(lineDI[i].Text);
TlpLine.RowStyles.Add(new RowStyle(SizeType.Percent, percent));
}
CboLineName.SelectedIndex = 0;
}
private void InitElevatorIO()
{
List<IOInfo> lineDI = Common.elevatorInfos.FindAll(s => s.Type == "DI");
List<IOInfo> lineDO = Common.elevatorInfos.FindAll(s => s.Type == "DO");
elevatorDI = new Label[lineDI.Count];
elevatorDO = new Button[lineDO.Count];
int max = Math.Max(lineDI.Count, lineDO.Count);
TlpElevator.RowCount = max;
TlpElevator.RowStyles.Clear();
float percent = 100f / max;
for (int i = 0; i < max; i++)
TlpElevator.RowStyles.Add(new RowStyle(SizeType.Percent, percent));
for (int i = 0; i < elevatorDI.Length; i++)
{
elevatorDI[i] = new Label
{
Text = lineDI[i].Name,
Font = new Font("宋体", 12f),
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Margin = new Padding(3),
BackColor = BACK_FALSE
};
TlpElevator.Controls.Add(elevatorDI[i]);
TlpElevator.SetColumn(elevatorDI[i], 0);
TlpElevator.SetRow(elevatorDI[i], i);
}
for (int i = 0; i < elevatorDO.Length; i++)
{
elevatorDO[i] = new Button
{
Text = lineDO[i].Name,
Font = new Font("宋体", 12f),
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Margin = new Padding(3),
BackColor = BACK_FALSE
};
elevatorDO[i].Click += ElevatorDO_Click;
TlpElevator.Controls.Add(elevatorDO[i]);
TlpElevator.SetColumn(elevatorDO[i], 1);
TlpElevator.SetRow(elevatorDO[i], i);
}
}
private void Control_AgvChanged(int agvIndex)
{
Invoke(new Action(() =>
{
DgvName.Rows[agvIndex].SetValues(Common.agvInfos[agvIndex].ToGridRow());
tableLayoutPanel5.Controls["TxtAgvMission" + agvIndex].Text = Common.agvInfos[agvIndex].ToMissionState();
}));
}
private void Control_AgvOnline(int agvIndex)
{
Invoke(new Action(() =>
{
DgvName.Rows[agvIndex].DefaultCellStyle.ForeColor = Common.agvInfos[agvIndex].IsOnline ? Color.Black : Color.Red;
DgvName.Rows[agvIndex].SetValues(Common.agvInfos[agvIndex].ToGridRow());
tableLayoutPanel5.Controls["TxtAgvMission" + agvIndex].Text = Common.agvInfos[agvIndex].ToMissionState();
}));
}
private void LineCall_Connect_Event(bool con)
{
//LblLineIO.BackColor = con ? Color.Lime : Color.Red;
}
private void LineCall_DI_Changed()
{
for (int i = 0; i < Common.lineInfos.Count; i++)
{
lineDI[i].BackColor = Common.lineInfos[i].LineCall ? BACK_TRUE : BACK_FALSE;
lineShelf[i].BackColor = Common.lineInfos[i].ShelfExist ? BACK_TRUE : BACK_FALSE;
}
}
private void Elevator_DO_Changed(bool[] sta)
{
for (int i = 0; i < elevatorDO.Length; i++)
elevatorDO[i].BackColor = sta[i] ? BACK_TRUE : BACK_FALSE;
}
private void Elevator_DI_Changed(bool[] sta)
{
string s = "DI ";
for (int i = 0; i < elevatorDI.Length; i++)
{
s += sta[i].ToString() + " ";
elevatorDI[i].BackColor = sta[i] ? BACK_TRUE : BACK_FALSE;
}
Common.log.Info(s);
}
private void Elevator_Connect_Event(bool con)
{
LblElevatorIO.BackColor = con ? Color.Lime : Color.Red;
}
private void ElevatorDO_Click(object sender, EventArgs e)
{
if (ChkMissionDebug.Checked)
{
Button btn = sender as Button;
int index = Array.FindIndex(elevatorDO, s => s == btn);
BLL.ManageWork.elevator.WriteDO(index);
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
InitLineCall();
InitElevatorIO();
Common.log.LogBox = TxtLog;
LstMission.Items.AddRange(Common.agvMissions.Keys.ToArray());
NudShelfCurr.Value = Common.FirstFloorCurr;
NudShelfCount.Value = Common.FirstFloorCount;
ChkAutoCharge.Checked = Common.AutoCharge;
for (int i = 0; i < Common.agvInfos.Count; i++)
{
DgvName.Rows.Add(Common.agvInfos[i].ToGridRow());
DgvName.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
DgvName.Rows[i].HeaderCell.Value = (i + 1).ToString();
DgvName.Rows[i].Height = 30;
}
BLL.ManageWork.Init();
BLL.ManageWork.line.Connect_Event += LineCall_Connect_Event;
BLL.ManageWork.line.DI_Changed += LineCall_DI_Changed;
BLL.ManageWork.elevator.Connect_Event += Elevator_Connect_Event;
BLL.ManageWork.elevator.DI_Changed += Elevator_DI_Changed;
BLL.ManageWork.elevator.DO_Changed += Elevator_DO_Changed;
BLL.ManageWork.Start();
control.AgvChanged += Control_AgvChanged;
control.AgvOnline += Control_AgvOnline;
control.Start();
BLL.RunMode.Init(this);
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (BLL.RunMode.Closing(e))
{
control.Stop();
BLL.ManageWork.Stop();
}
}
#region 手动添加小车任务
private void BtnMissionAdd_Click(object sender, EventArgs e)
{
if (DgvName.SelectedCells.Count == 0) return;
int idx = DgvName.SelectedCells[0].RowIndex;
if (idx < 0) return;
AgvInfo info = Common.agvInfos[idx];
if (info.IsAuto)
{
MessageBox.Show(info.Name + " 自动状态,不允许手动发送任务");
return;
}
if (LstMission.SelectedIndex == -1) return;
if (Common.FleetSend)
Common.mir.Add_Mission_Fleet(info.FleetID, info.Authorization, LstMission.Text, out string id);
else
Common.mir.Add_Mission(info.IP, info.Authorization, LstMission.Text, out string id);
}
private void BtnMissionClear_Click(object sender, EventArgs e)
{
if (DgvName.SelectedCells.Count == 0) return;
int idx = DgvName.SelectedCells[0].RowIndex;
if (idx < 0) return;
AgvInfo info = Common.agvInfos[DgvName.SelectedRows[0].Index];
Common.mir.Del_Mission(info.IP, info.Authorization);
}
private void BtnMissionRun_Click(object sender, EventArgs e)
{
if (DgvName.SelectedCells.Count == 0) return;
int idx = DgvName.SelectedCells[0].RowIndex;
if (idx < 0) return;
AgvInfo info = Common.agvInfos[idx];
Common.mir.State_Ready(info.IP, info.Authorization);
}
private void BtnMissionPause_Click(object sender, EventArgs e)
{
if (DgvName.SelectedCells.Count == 0) return;
int idx = DgvName.SelectedCells[0].RowIndex;
if (idx < 0) return;
AgvInfo info = Common.agvInfos[idx];
Common.mir.State_Pause(info.IP, info.Authorization);
}
#endregion
private void DgvName_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1) return;
AgvInfo info = Common.agvInfos[e.RowIndex];
if (e.ColumnIndex == DgvName.Columns.Count - 2) //最后二列,自动/手动
{
info.IsAuto = !info.IsAuto;
DgvName.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = info.IsAuto.ToString();
Common.appConfig.AppSettings.Settings[info.Name].Value = info.IsAuto.ToString();
Common.appConfig.Save(System.Configuration.ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
Common.log.UI_Display(string.Format("手动修改 {0} 的自动状态为{1}", info.Name, info.IsAuto));
Common.log.Info(string.Format("手动修改 {0} 的自动状态为{1}", info.Name, info.IsAuto));
}
else if (e.ColumnIndex == DgvName.Columns.Count - 1) //最后一列,清除任务
{
string text = "确定要清除 " + info.Name + " 的任务吗?";
DialogResult dr = MessageBox.Show(text, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
BLL.ManageWork.PauseFull = false;
info.CurrentJob = null;
Common.mir.Del_Mission(info.IP, info.Authorization);
info.IsAuto = false;
info.Place = "";
DgvName.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = info.IsAuto.ToString();
Common.appConfig.AppSettings.Settings[info.Name].Value = info.IsAuto.ToString();
Common.appConfig.Save(System.Configuration.ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
Common.log.UI_Display(string.Format("手动清除 {0} 的当前任务,并且自动状态改为{1}", info.Name, info.IsAuto));
Common.log.Info(string.Format("手动清除 {0} 的当前任务,并且自动状态改为{1}", info.Name, info.IsAuto));
}
}
}
private void BtnLineCall_Click(object sender, EventArgs e)
{
LineIOInfo info = Common.lineInfos[CboLineName.SelectedIndex];
string text = string.Format("确定要取消 {0} 呼叫吗?", info.Name);
DialogResult dr = MessageBox.Show(text, "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.No) return;
info.LineCall = false;
lineDI[CboLineName.SelectedIndex].BackColor = info.LineCall ? BACK_TRUE : BACK_FALSE;
Common.log.Info(string.Format("手动取消{0}呼叫", info.Name));
BLL.ManageWork.line.Save();
}
private void BtnLineShelf_Click(object sender, EventArgs e)
{
LineIOInfo info = Common.lineInfos[CboLineName.SelectedIndex];
string text = string.Format("确定要改变 {0} 货架状态吗?", info.Name);
DialogResult dr = MessageBox.Show(text, "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.No) return;
info.ShelfExist = !info.ShelfExist;
lineShelf[CboLineName.SelectedIndex].BackColor = info.ShelfExist ? BACK_TRUE : BACK_FALSE;
Common.log.Info(string.Format("手动改变{0}货架状态{1}", info.Name, info.ShelfExist));
BLL.ManageWork.line.Save();
}
private void ChkMissionDebug_CheckedChanged(object sender, EventArgs e)
{
CboLineName.Enabled = ChkMissionDebug.Checked;
BtnLineCall.Enabled = ChkMissionDebug.Checked;
BtnLineShelf.Enabled = ChkMissionDebug.Checked;
NudShelfCurr.Enabled = ChkMissionDebug.Checked;
NudShelfCount.Enabled = ChkMissionDebug.Checked;
BtnShelfGet.Enabled = ChkMissionDebug.Checked;
BtnShelfSet.Enabled = ChkMissionDebug.Checked;
ChkAutoCharge.Enabled = ChkMissionDebug.Checked;
}
private void BtnClearLog_Click(object sender, EventArgs e)
{
Common.log.UI_Clear();
}
private void BtnShelfSet_Click(object sender, EventArgs e)
{
Common.FirstFloorCurr = Convert.ToInt32(NudShelfCurr.Value);
BLL.ManageWork.FirstFloorSave();
int n = Convert.ToInt32(NudShelfCount.Value);
if (Common.FirstFloorCount != n)
{
Common.FirstFloorCount = n;
Common.appConfig.AppSettings.Settings["FirstFloorCount"].Value = n.ToString();
Common.appConfig.Save(System.Configuration.ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
}
MessageBox.Show("设置保存成功");
}
private void ChkAutoCharge_CheckedChanged(object sender, EventArgs e)
{
Common.AutoCharge = ChkAutoCharge.Checked;
Common.appConfig.AppSettings.Settings["AutoCharge"].Value = Common.AutoCharge.ToString();
Common.appConfig.Save(System.Configuration.ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
}
private void BtnShelfGet_Click(object sender, EventArgs e)
{
NudShelfCurr.Value = Common.FirstFloorCurr;
NudShelfCount.Value = Common.FirstFloorCount;
}
private void BtnEmptyError_Click(object sender, EventArgs e)
{
//System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(BLL.ManageWork.EmptyErrorDispose));
//thread.Start();
if (DgvName.SelectedCells.Count == 0) return;
AgvInfo info = Common.agvInfos[DgvName.SelectedCells[0].RowIndex];
if (info.IsAuto)
{
MessageBox.Show(info.Name + "自动模式不允许操作");
}
else
{
BLL.ManageWork.EmptyErrorDispose();
Common.log.Info("手动异常处理,结束回收货架任务");
MessageBox.Show("结束回收货架任务处理完成", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void BtnFullError_Click(object sender, EventArgs e)
{
//System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(BLL.ManageWork.FullErrorDispose));
//thread.Start();
if (DgvName.SelectedCells.Count == 0) return;
AgvInfo info = Common.agvInfos[DgvName.SelectedCells[0].RowIndex];
if (info.IsAuto)
{
MessageBox.Show(info.Name + "自动模式不允许操作");
}
else
{
BLL.ManageWork.FullErrorDispose();
Common.log.Info("手动异常处理,结束送满料任务");
MessageBox.Show("结束送满料任务处理完成", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}