-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGui.cs
131 lines (117 loc) · 4.73 KB
/
Gui.cs
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
using System;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using PCR1000;
using PCR1000.Network.Server;
namespace PCRNetworkServer
{
public partial class Gui : Form
{
private PcrNetworkServer _pcrNetworkServer;
public Gui(bool security, string password, int port, string device)
{
InitializeComponent();
var ports = SerialPort.GetPortNames();
foreach (var dev in ports)
{
comboBoxSerialPort.Items.Add(dev);
}
comboBoxSerialPort.SelectedIndex = comboBoxSerialPort.Items.IndexOf(device);
textBoxNetwork.Text = port.ToString();
textBoxPassword.Text = password;
checkBoxSsl.Checked = security;
if (checkBoxSsl.Checked || !string.IsNullOrWhiteSpace(textBoxPassword.Text))
{
checkBoxUseSecurity.Checked = true;
}
}
private bool CheckSettings()
{
int port;
if (!int.TryParse(textBoxNetwork.Text, out port) || port <= 0)
{
return false;
}
return comboBoxSerialPort.SelectedIndex != -1;
}
private bool _isOn;
private void ButtonOnOffClick(object sender, EventArgs e)
{
switch (_isOn)
{
case true:
{
try
{
if (!_pcrNetworkServer.Stop())
{
throw new Exception("A fatal error occured while stopping the server. Please check debug messages for details.");
}
}
catch (Exception ex)
{
MessageBox.Show("An error occured while stopping the server:\n" + ex.Message);
}
buttonOnOff.Text = "OFF";
buttonOnOff.BackColor = Color.FromArgb(255, 128, 128);
textBoxNetwork.Enabled = true;
comboBoxSerialPort.Enabled = true;
checkBoxUseSecurity.Enabled = true;
checkBoxSsl.Enabled = checkBoxUseSecurity.Checked;
textBoxPassword.Enabled = checkBoxUseSecurity.Checked;
_isOn = false;
break;
}
case false:
{
if (!CheckSettings())
{
MessageBox.Show("Invalid Settings Provided.", "Start Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var port = int.Parse(textBoxNetwork.Text);
var comp = comboBoxSerialPort.SelectedItem.ToString();
IComm comm = new PcrSerialComm(comp);
_pcrNetworkServer = checkBoxUseSecurity.Checked ?
new PcrNetworkServer(comm, port, checkBoxSsl.Checked, textBoxPassword.Text) :
new PcrNetworkServer(comm, port);
if (!_pcrNetworkServer.Start())
{
throw new Exception("Server startup failed. Review debug messages for details.");
}
}
catch (Exception ex)
{
MessageBox.Show("An error occured while starting the server:\n" + ex.Message, "Start Server",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
buttonOnOff.Text = "ON";
buttonOnOff.BackColor = Color.FromArgb(192, 255, 192);
textBoxNetwork.Enabled = false;
comboBoxSerialPort.Enabled = false;
checkBoxUseSecurity.Enabled = false;
checkBoxSsl.Enabled = false;
textBoxPassword.Enabled = false;
_isOn = true;
break;
}
}
}
private void CheckBoxUseSecurityCheckedChanged(object sender, EventArgs e)
{
textBoxPassword.Enabled = checkBoxUseSecurity.Checked;
checkBoxSsl.Enabled = checkBoxUseSecurity.Checked;
}
private void GuiFormClosing(object sender, FormClosingEventArgs e)
{
if (buttonOnOff.Text != "ON") return;
e.Cancel = true;
MessageBox.Show("Please turn off the server before quitting.", "Quit",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}