Form1.cs
5.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
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)
{
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 Server_NodeChanged(Agv.Node clientNode)
{
this.Invoke(new Action(() =>
{
label8.Text = "从客户端接收:" + clientNode.ToText();
}
));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs 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 { }
}
private void button1_Click(object sender, EventArgs e)
{
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);
}));
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 (!status)
label13.BackColor = Color.Red;
else
label13.BackColor = Color.Green;
}
));
}
private void Client_Received(Agv.Node node)
{
this.Invoke(new Action(() =>
{
label12.Text = "从服务端端接收:" + node.ToText();
}
));
}
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;
}
}
}
}