-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnvConfigForm.cs
More file actions
176 lines (150 loc) · 5.94 KB
/
Copy pathEnvConfigForm.cs
File metadata and controls
176 lines (150 loc) · 5.94 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
using bpac;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace janog_reception_ui
{
public partial class EnvConfigForm : Form
{
bpac.Document labelDocument;
internal Config config;
public EnvConfigForm()
{
InitializeComponent();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void EnvConfigForm_Load(object sender, EventArgs e)
{
labelDocument = new bpac.Document();
var printers = labelDocument.Printer.GetInstalledPrinters();
printerComboBox.Items.Clear();
// プリンタ一覧を取得してコンボボックスへ設定
foreach (string printer in PrinterSettings.InstalledPrinters)
{
if (labelDocument.Printer.IsPrinterSupported(printer))
{
printerComboBox.Items.Add(printer);
if (printer == config.Printer)
{
printerComboBox.SelectedItem = printer;
}
}
}
// シリアルポート一覧を取得してコンボボックスへ設定
var portList = BuildPortList();
readerComboBox.Items.Clear();
readerComboBox.DataSource = portList;
readerComboBox.DisplayMember = "Caption"; // 表示名
readerComboBox.ValueMember = "Port"; // COMx
// いったん非選択状態にする
readerComboBox.SelectedIndex = -1;
if (!string.IsNullOrWhiteSpace(config.Reader.Port))
{
// ValueMember (= Port) と一致するものがあれば選択される
readerComboBox.SelectedValue = config.Reader.Port;
}
}
private void EnvConfigForm_Shown(object sender, EventArgs e)
{
if (config.Environment.Environment != EnvironmentKind.Production)
{
radioEnvDevelop.Checked = true;
radioEnvProduction.Checked = false;
}
else
{
radioEnvDevelop.Checked = false;
radioEnvProduction.Checked = true;
}
textBoxGate.Text = config.Gate;
printerComboBox.SelectedItem = config.Printer;
textBoxDevBaseUrl.Text = config.Environment.Develop.BaseUrl;
textBoxDevUsername.Text = config.Environment.Develop.Username;
textBoxDevPassword.Text = config.Environment.Develop.Password;
textBoxProdBaseUrl.Text = config.Environment.Production.BaseUrl;
textBoxProdUsername.Text = config.Environment.Production.Username;
textBoxProdPassword.Text = config.Environment.Production.Password;
}
private void EnvConfigForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (radioEnvProduction.Checked)
{
config.Environment.Environment = EnvironmentKind.Production;
}
else
{
config.Environment.Environment = EnvironmentKind.Develop;
}
config.Gate = textBoxGate.Text;
config.Printer = printerComboBox.SelectedItem.ToString();
config.Reader = new Reader
{
Port = readerComboBox.SelectedValue.ToString(),
Serial = "",
};
config.Environment.Develop.BaseUrl = textBoxDevBaseUrl.Text;
config.Environment.Develop.Username = textBoxDevUsername.Text;
config.Environment.Develop.Password = textBoxDevPassword.Text;
config.Environment.Production.BaseUrl = textBoxProdBaseUrl.Text;
config.Environment.Production.Username = textBoxProdUsername.Text;
config.Environment.Production.Password = textBoxProdPassword.Text;
}
private void label9_Click(object sender, EventArgs e)
{
}
static Dictionary<string, string> GetComCaptionMapByPnP()
{
var map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var regex = new Regex(@"\(COM\d+\)", RegexOptions.IgnoreCase);
using var searcher = new ManagementObjectSearcher(
"SELECT Name FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'"
);
foreach (ManagementObject mo in searcher.Get())
{
var name = mo["Name"]?.ToString();
if (string.IsNullOrWhiteSpace(name)) continue;
var m = regex.Match(name);
if (!m.Success) continue;
// "(COM3)" -> "COM3"
var com = m.Value.Trim('(', ')');
if (!map.ContainsKey(com))
map[com] = name; // "USB Serial Device (COM3)" 等
}
return map;
}
static SerialPortItem[] BuildPortList()
{
var ports = SerialPort.GetPortNames()
.OrderBy(x => x, StringComparer.OrdinalIgnoreCase)
.ToArray();
var captionMap = GetComCaptionMapByPnP();
return ports.Select(com =>
new SerialPortItem
{
Port = com,
Caption = captionMap.TryGetValue(com, out var cap) ? cap : com
})
.ToArray();
}
}
class SerialPortItem
{
public string Caption { get; set; }
public string Port { get; set; }
}
}