-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioStationReaderSerial.cs
414 lines (332 loc) · 16.6 KB
/
RadioStationReaderSerial.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
//using System.Windows.Forms;
namespace shvFT991A
{
class RadioStationReaderSerial
{
private List<RadioStationItem> _radiostation_serial;
private List<string> _radiostation_not_found;
public MainWindow mainWindow = (MainWindow)App.Current.MainWindow;
private string file_radio_station;
private string file_not_found;
public List<RadioStationItem> Read_SoumuGoJpSerial(List<string> requested_call_signs, /*BindingSource bindingSource,*/ int parseNumber)
{
file_radio_station = mainWindow.file_radio_station;
file_not_found = mainWindow.file_not_found;
//------------------------------------------------------------------------------------------
_radiostation_serial = Read_Prev_Data();
_radiostation_not_found = Read_NotFound_Data();
//------------------------------------------------------------------------------------------
int loop_counter = 0;
int loop_end = parseNumber ;
foreach (string call1 in requested_call_signs)
{
//-------------------------------------------------------------------------------------Delete Portable Info
string call;
if (call1.Contains("/"))
{
call = call1.Substring(0, call1.IndexOf("/"));
string m = "Read_SoumuGoJpSerial: remove /portable = " + call1 + " " + call;
Console.WriteLine(m);
mainWindow.LabelStatusBarSoumu.Content = m;
}
else
{
call = call1;
}
//-------------------------------------------------------------------------------------Already ?
int indexcall = _radiostation_serial.FindIndex(m => m.call_sign == call);
if (-1 < indexcall)
{
string m = "Read_SoumuGoJpSerial: call ALREADY in index = " + call + " file = " + file_radio_station + " index = " + indexcall;
//Console.WriteLine(m);
mainWindow.LabelStatusBarSoumu.Content = m;
continue;
}
int indexcall2 = _radiostation_not_found.FindIndex(m => m == call);
if (-1 < indexcall2)
{
string m = "Read_SoumuGoJpSerial: call ALREADY in _radiostation_not_found index = " + call + " index = " + indexcall2;
Console.WriteLine(m);
mainWindow.LabelStatusBarSoumu.Content = m;
continue;
}
//-------------------------------------------------------------------------------------Download URL
string uricall = @"https://www.tele.soumu.go.jp/musen/SearchServlet?SC=1&pageID=3&SelectID=1&CONFIRM=0&SelectOW=01&IT=&HC=&HV=&FF=&TF=&HZ=3&NA=&DFY=&DFM=&DFD=&DTY=&DTM=&DTD=&SK=2&DC=100&MA=" + call;
//Console.WriteLine("Read_SoumuGoJpSerial: uricall = " + uricall);
WebClient wc = new WebClient();
wc.Encoding = Encoding.Default;
string source = wc.DownloadString(uricall);
wc.Dispose();
//-------------------------------------------------------------------------------------Parse NEXT Page
CompleteDownloadProcSerial(source, call/*, bindingSource*/);
/*
try
{
wc.DownloadStringCompleted += CompleteDownloadProc;
wc.DownloadStringAsync(new Uri(uricall), call);
}
catch (WebException exc)
{
//textBox1.Text += exc.Message;
Console.WriteLine("Read_SoumuGoJp: exc.Message = " + exc.Message + " " + call);
}
*/
//-------------------------------------------------------------------------------------Check Finish
loop_counter++;
if (loop_end < loop_counter)
{
break;
}
//-------------------------------------------------------------------------------------Next Call Sign
System.Threading.Thread.Sleep(3000);
DateTime dt = DateTime.Now;
Console.WriteLine("Now------------------------- " + dt + " --- " + loop_counter.ToString() + " / " + loop_end.ToString());
}
Console.WriteLine("Read_SoumuGoJpSerial: _radiostation_serial.Count() " + _radiostation_serial.Count() );
Console.WriteLine("Read_SoumuGoJpSerial: _radiostation_not_found.Count() " + _radiostation_not_found.Count());
return _radiostation_serial;
}
public void CompleteDownloadProcSerial(string result, string call/*, BindingSource bindingSource*/)
{
//Console.WriteLine("CompleteDownloadProcSerial: e.Result = " + result);
MatchCollection zeromatch = Regex.Matches(result, "検索結果が0件です。");
if (0 < zeromatch.Count)
{
//-------------------------------------------------------------------------------------検索結果が0件です。
string m = "CompleteDownloadProcSerial: zeromatch.Count = " + zeromatch.Count + " " + call;
mainWindow.LabelStatusBarSoumu.Content = m;
//-----------------------------------------------------------item data .csv write
DateTime dt_now = DateTime.Now;
string[] aStrings = { call, dt_now.ToString("yyyy-MM-dd") };
string strSeparator = ",";
string strLine = string.Join(strSeparator, aStrings);
using (StreamWriter sw = new StreamWriter(file_not_found, true))
{
sw.WriteLine(strLine);
sw.Close();
}
}
else
{
MatchCollection matches = Regex.Matches(result, ".*[都道府県].*<br>");
foreach (Match m in matches)
{
//Console.WriteLine(m.Value);
string address = m.Value.Replace("<br>", "").Trim();
Console.WriteLine("CompleteDownloadProcSerial: address = " + address + " " + call);
}
//-----------------------------------------------------------
//<a href="./SearchServlet?pageID=4&IT=A&DFCD=0009844&DD=1&styleNumber=50" target="_blank">XXX(XXXXXX)</a>
//-----------------------------------------------------------
string HRefPattern = "href\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))";
MatchCollection mat2 = Regex.Matches(result, HRefPattern);
foreach (Match m in mat2)
{
if (m.Value.Contains("pageID=4"))
{
//-------------------------------------------------------------------------------------parse subpage
string href = m.Value;
Console.WriteLine("CompleteDownloadProcSerial: address = " + href + " " + call);
string url = href.Remove(0, 7);
string url2 = "https://www.tele.soumu.go.jp/musen" + url.Remove(url.Length - 1, 1);
System.Net.WebClient wc = new System.Net.WebClient();
wc.Encoding = System.Text.Encoding.Default;
string source = wc.DownloadString(url2);
wc.Dispose();
//Console.WriteLine(source);
//----
string[] deli = { "\n" };
string[] lines = source.Split(deli, StringSplitOptions.None);
//----------------------------------------------------------識別信号
int index0 = Array.FindIndex(lines, ContainsCallsign);
Console.WriteLine("CompleteDownloadProcSerial: lines[index + 4] = " + lines[index0 + 4].Trim() + " " + call);
string callsign = lines[index0 + 4].Replace("<BR>", "").Trim();
//----------------------------------------------------------氏名又は名称
int index1 = Array.FindIndex(lines, ContainsKeyword);
Console.WriteLine("CompleteDownloadProcSerial: lines[index + 4] = " + lines[index1 + 4].Trim() + " " + call);
string op_name = lines[index1 + 4].Trim();
//----------------------------------------------------------移動範囲
string type_station = "";
if (source.Contains("陸上、海上及び上空"))
{
type_station = "陸上、海上及び上空";
}
else if (source.Contains("移動しない"))
{
type_station = "移動しない";
}
//----------------------------------------------------------無線設備の設置場所/常置場所
string sta_place = "";
int index2 = Array.FindIndex(lines, ContainsPlace);
for (int i = 0; i < 40; i++)
{
//Console.WriteLine("CompleteDownloadProc: lines[index + 4] = " + lines[index2 + i].Trim() + " " + call);
if (lines[index2 + i].Contains("<br>"))
{
sta_place = lines[index2 + i].Trim().Replace("<br>", "");
Console.WriteLine("CompleteDownloadProcSerial: Contains <br> = " + sta_place + " " + call);
}
}
//----------------------------------------------------------14175 MHz
int index3 = Array.FindIndex(lines, Contains14175);
string power = "";
string bool14mhz = "NO";
if (0 < index3)
{
power = lines[index3 + 22].Replace(" ", "").Replace(@"<td><pre class=""defpre"">", "").Replace(@"</pre></td>", "").Trim();
Console.WriteLine("CompleteDownloadProcSerial: power = 14175MHz " + power.Trim() + " " + call);
bool14mhz = "YES14MHz";
}
else
{
bool14mhz = "NO";
}
//-----------------------------------------------------------item data Add
DateTime dt_now = DateTime.Now;
string date_today = dt_now.ToString("yyyy-MM-dd") ;
RadioStationItem newItem = new RadioStationItem()
{
call_sign = callsign,
station_type = type_station,
station_address = sta_place,
bool_14mhz = bool14mhz,
max_power_watt = power,
checked_date = date_today
};
_radiostation_serial.Add(newItem);
mainWindow.DataGridSoumuGoJp.ItemsSource = _radiostation_serial;
//bindingSource.DataSource = _radiostation_serial;
Console.WriteLine("CompleteDownloadProcSerial: _radiostation_serial.Count() " + _radiostation_serial.Count() + " " + call);
//-----------------------------------------------------------item data .csv write
string[] aStrings = { callsign, type_station, sta_place, bool14mhz, power, date_today };
string strSeparator = ",";
string strLine = string.Join(strSeparator, aStrings);
using (StreamWriter sw = new StreamWriter(file_radio_station, true))
{
sw.WriteLine(strLine);
sw.Close();
}
}
}
}
}
private static bool ContainsCallsign(String s)
{
if (s.Contains("識別信号"))
{
return true;
}
else
{
return false;
}
}
private static bool ContainsKeyword(String s)
{
if (s.Contains("氏名又は名称"))
{
return true;
}
else
{
return false;
}
}
private static bool ContainsPlace(String s)
{
if (s.Contains("無線設備の設置場所/常置場所"))
{
return true;
}
else
{
return false;
}
}
private static bool Contains14175(String s)
{
if (s.Contains("14175"))
{
return true;
}
else
{
return false;
}
}
public List<RadioStationItem> Read_Prev_Data()
{
List<RadioStationItem> _radiostation_prev = new List<RadioStationItem>();
string fileName = file_radio_station;
if (System.IO.File.Exists(fileName))
{
using (StreamReader sr = new StreamReader(file_radio_station))
{
Console.WriteLine(@"read_prev_data file_radio_station = " + file_radio_station);
string line;
int no_data = 0;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
string[] fields = line.Split(',');
no_data++;
RadioStationItem logItem = new RadioStationItem()
{
no = no_data,
call_sign = fields[0],
station_type = fields[1],
station_address = fields[2],
bool_14mhz = fields[3],
max_power_watt = fields[4],
checked_date = fields[5]
};
_radiostation_prev.Add(logItem);
//bindingSource.DataSource = _radiostation_prev;
}
}
//Console.WriteLine(@"read_prev_data _radiostation_prev.Count() = " + _radiostation_prev.Count());
mainWindow.LabelNumberOfSoumu.Content = _radiostation_prev.Count();
}
else
{
MessageBox.Show("File not found in Read_Prev_Data:" + fileName );
}
return _radiostation_prev;
}
public List<string> Read_NotFound_Data()
{
List<string> _radiostation_not_found = new List<string>();
if (File.Exists(file_not_found))
{
Console.WriteLine("File Exists: " + file_not_found);
}
else
{
MessageBox.Show("File not found in Read_NotFound_Data: " + file_not_found);
return _radiostation_not_found;
}
using (StreamReader sr = new StreamReader(file_not_found))
{
Console.WriteLine(@"read_notfound_data file_not_found = " + file_not_found);
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
string[] fields = line.Split(',');
_radiostation_not_found.Add(fields[0]);
}
}
Console.WriteLine(@"read_notfound_data _radiostation_not_found.Count() = " + _radiostation_not_found.Count());
return _radiostation_not_found;
}
}
}