Form1.cs
7.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
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;
namespace AgvClientTest
{
public partial class Form1 : Form
{
private Agv.Server server;
private Agv.AgvClient client;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Agv.Common.EnDecodeSingleNode = false;
server = new Agv.Server();
server.NodeChanged += Server_NodeChanged;
server.NodeOnline += Server_NodeOnline;
comboBox1.Items.AddRange(Enum.GetNames(typeof(Agv.ClientAction)));
comboBox2.Items.AddRange(Enum.GetNames(typeof(Agv.ClientLevel)));
comboBox3.Items.AddRange(Enum.GetNames(typeof(Agv.ClientType)));
comboBox4.Items.AddRange(Enum.GetNames(typeof(Agv.ClientShelf)));
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 0;
client = new Agv.AgvClient();
client.Received += Client_Received;
client.Connected += Client_Connected;
}
private void Client_Log(string s)
{
Agv.log.info(s);
}
private void Server_NodeChanged(Agv.Node clientNode)
{
this.Invoke(new Action(() =>
{
listBox1.Items.Add(clientNode.ToText());
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//comboBox1.Items.AddRange(Enum.GetNames(typeof(Agv.ClientAction)));
//comboBox2.Items.AddRange(Enum.GetNames(typeof(Agv.ClientLevel)));
//comboBox3.Items.AddRange(Enum.GetNames(typeof(Agv.ClientType)));
//comboBox4.Items.AddRange(Enum.GetNames(typeof(Agv.ClientShelf)));
int i1 = comboBox1.SelectedIndex;
int i2 = comboBox2.SelectedIndex;
int i3 = comboBox4.SelectedIndex;
int i4 = comboBox3.SelectedIndex;
Task task = new Task(new Action(() =>
{
//client.SetStatus(textBox1.Text,textBox3.Text,textBox2.Text, (Agv.ClientAction)i1,
// (Agv.ClientLevel)i2,(Agv.ClientShelf)i3,
// (Agv.ClientType)i4);
client.SetStatus(textBox1.Text, textBox3.Text, textBox2.Text, (Agv.ClientAction)i1, (Agv.ClientLevel)i2, (Agv.ClientShelf)i3, (Agv.ClientType)i4);//, (Agv.ClientShelf)i3
//for (int i = 0; i < 100; i++)
//{
// client.SetStatus(string.Format("A{0}",i), textBox3.Text, textBox2.Text, (Agv.ClientAction)i1, (Agv.ClientLevel)i2, (Agv.ClientShelf)i3, (Agv.ClientType)i4);
//}
}));
task.Start();
}
private void button2_Click(object sender, EventArgs e)
{
server.Start(textBox4.Text, int.Parse(textBox5.Text));
this.Invoke(new Action(() =>
{
label11.BackColor = Color.Green;
}
));
}
private void Server_NodeOnline(string nodeName, bool online)
{
this.Invoke(new Action(() =>
{
label11.Text = nodeName;
if (online)
label11.BackColor = Color.Green;
else
label11.BackColor = Color.Red;
}
));
}
private void button3_Click(object sender, EventArgs e)
{
server.Stop();
this.Invoke(new Action(() =>
{
label11.BackColor = Color.Red;
}
));
}
private void button4_Click(object sender, EventArgs e)
{
client.Connect(textBox4.Text, int.Parse(textBox5.Text));
}
private void Client_Connected(bool status)
{
this.Invoke(new Action(() =>
{
if (label3 == null)
return;
if (!status)
label13.BackColor = Color.Red;
else
label13.BackColor = Color.Green;
}
));
}
private void Client_Received(Agv.Node node)
{
this.Invoke(new Action(() =>
{
listBox2.Items.Add(node.ToText());
listBox2.SelectedIndex = listBox2.Items.Count - 1;
}
));
}
private void button5_Click(object sender, EventArgs e)
{
client.Close();
}
private void button6_Click(object sender, EventArgs e)
{
int i1 = comboBox1.SelectedIndex;
server.SendToClient(textBox1.Text, textBox2.Text, (Agv.ClientAction)i1);
}
private void button7_Click(object sender, EventArgs e)
{
if (client.CancelState)
{
client.CancelState = false;
button7.BackColor = Color.Green;
}
else
{
client.CancelState = true;
button7.BackColor = Color.Red;
}
}
private void button8_Click(object sender, EventArgs e)
{
textBox6.Text = "";
try
{
int i1 = comboBox1.SelectedIndex;
int i2 = comboBox2.SelectedIndex;
int i3 = comboBox4.SelectedIndex;
int i4 = comboBox3.SelectedIndex;
Agv.Node node = new Agv.Node()
{
Name = textBox1.Text,
Mark = textBox3.Text,
RFID = textBox2.Text,
Action = (Agv.ClientAction)i1,
Level = (Agv.ClientLevel)i2,
Shelf = (Agv.ClientShelf)i3,
Type = (Agv.ClientType)i4
};
byte[] buff = Agv.Common.Encode(node);
textBox6.Text = Agv.Common.HexBuffWithoutSpace(buff);
}
catch
{
textBox6.Text = "";
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//try
//{
// if (server != null)
// {
// server.NodeChanged -= Server_NodeChanged;
// server.NodeOnline -= Server_NodeOnline;
// server.Stop();
// server = null;
// }
// if (client != null)
// {
// client.Received -= Client_Received;
// client.Connected -= Client_Connected;
// client.Close();
// client = null;
// }
//}
//catch { }
}
}
}