HamBook – Blame information for rev 15

Subversion Repositories:
Rev:
Rev Author Line No. Line
15 office 1 using Configuration;
2 using HamBook.Properties;
3 using HamBook.Radios;
4 using HamBook.Radios.Generic;
5 using Serilog;
6 using System;
7 using System.Collections;
8 using System.Collections.Concurrent;
9 using System.Collections.Generic;
10 using System.ComponentModel;
11 using System.Data;
12 using System.Diagnostics;
13 using System.Drawing;
14 using System.IO;
15 using System.Linq;
16 using System.Reflection;
17 using System.Text;
18 using System.Text.RegularExpressions;
19 using System.Threading;
20 using System.Threading.Tasks;
21 using System.Windows.Forms;
22  
23 namespace HamBook
24 {
25 public partial class MemoryOrganizerForm : Form
26 {
27 private Configuration.Configuration Configuration { get; set; }
28  
29 private CatAssemblies _catAssemblies;
30 private CancellationToken _cancellationToken;
31  
32 public MemoryOrganizerForm()
33 {
34 InitializeComponent();
35 }
36  
37 public MemoryOrganizerForm(Configuration.Configuration configuration, CatAssemblies catAssemblies, CancellationToken cancellationToken) : this()
38 {
39 Configuration = configuration;
40 _catAssemblies = catAssemblies;
41 _cancellationToken = cancellationToken;
42 }
43  
44 /// <summary>
45 /// Clean up any resources being used.
46 /// </summary>
47 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
48 protected override void Dispose(bool disposing)
49 {
50 if (disposing && (components != null))
51 {
52 components.Dispose();
53 }
54 base.Dispose(disposing);
55 }
56  
57 private async void button1_Click(object sender, EventArgs e)
58 {
59 var rows = GetSelectedDataGridViewRows(dataGridView1);
60 await ReadMemoryBanks(rows);
61 }
62  
63 private async void button2_Click(object sender, EventArgs e)
64 {
65 var rows = GetSelectedDataGridViewRows(dataGridView1);
66 await WriteMemoryBanks(rows);
67 }
68  
69 private void MemoryOrganizerForm_Load(object sender, EventArgs e)
70 {
71 toolStripProgressBar1.Minimum = 0;
72 toolStripProgressBar1.Maximum = 98;
73  
74 var memoryBankQueue = new ConcurrentQueue<int>();
75 var snapshotsQueuedTaskCompletionSource = new TaskCompletionSource<object>();
76  
77 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
78 {
79 await snapshotsQueuedTaskCompletionSource.Task;
80  
81 try
82 {
83 if (!memoryBankQueue.TryDequeue(out var memoryBank))
84 {
85 Application.Idle -= IdleHandler;
86  
87 dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending);
88 toolStripStatusLabel1.Text = "Done.";
89  
90 return;
91 }
92  
93 var index = dataGridView1.Rows.Add();
94  
95 dataGridView1.Rows[index].Cells["LocationColumn"].Value = $"{memoryBank:000}";
96 dataGridView1.Rows[index].Cells["FrequencyColumn"].Value = default;
97 dataGridView1.Rows[index].Cells["ClarifierDirectionColumn"].Value = default;
98 dataGridView1.Rows[index].Cells["ClarifierOffsetColumn"].Value = default;
99 dataGridView1.Rows[index].Cells["ClarColumn"].Value = default;
100 dataGridView1.Rows[index].Cells["ModeColumn"].Value = default;
101 dataGridView1.Rows[index].Cells["CtcssColumn"].Value = default;
102 dataGridView1.Rows[index].Cells["PhaseColumn"].Value = default;
103 dataGridView1.Rows[index].Cells["TagColumn"].Value = default;
104 dataGridView1.Rows[index].Cells["TextColumn"].Value = default;
105  
106 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {memoryBank}";
107  
108 toolStripProgressBar1.Increment(1);
109  
110 statusStrip1.Update();
111 }
112 catch (Exception exception)
113 {
114 Log.Error(exception, Resources.Could_not_update_data_grid_view);
115 }
116 }
117  
118 Application.Idle += IdleHandler;
119 try
120 {
121 foreach (var memoryBank in Enumerable.Range(1, 99))
122 {
123 memoryBankQueue.Enqueue(memoryBank);
124 }
125  
126 snapshotsQueuedTaskCompletionSource.TrySetResult(new { });
127 }
128 catch (Exception exception)
129 {
130 Application.Idle -= IdleHandler;
131  
132 Log.Error(exception, Resources.Unable_to_create_memory_banks);
133 }
134 }
135  
136 private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
137 {
138 var dataGridView = (DataGridView)sender;
139  
140 if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
141 {
142 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
143 }
144 }
145  
146 private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
147 {
148 var dataGridView = (DataGridView)sender;
149  
150 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
151 }
152  
153 private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
154 {
155 var dataGridView = (DataGridView)sender;
156  
157 if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
158 {
159 if (dataGridView.CurrentCell.IsInEditMode)
160 {
161 if (dataGridView.IsCurrentCellDirty)
162 {
163 dataGridView.EndEdit();
164 }
165 }
166 }
167 }
168  
169 private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
170 {
171 var dataGridView = (DataGridView)sender;
172  
173 if (e.RowIndex == -1 || e.ColumnIndex == -1)
174 {
175 return;
176 }
177  
178 switch (dataGridView.Columns[e.ColumnIndex].Name)
179 {
180 case "EnableColumn":
181 //ProcessEnable(dataGridView.Rows[e.RowIndex]);
182 break;
183 }
184 }
185  
186 private void DataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
187 {
188 var dataGridView = (DataGridView)sender;
189  
190 if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
191 !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
192 {
193 return;
194 }
195  
196 dataGridView.EndEdit();
197 }
198  
199 private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
200 {
201 var dataGridView = (DataGridView)sender;
202  
203 if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
204 !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
205 {
206 return;
207 }
208  
209 dataGridView.EndEdit();
210 }
211  
212 private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
213 {
214 // @(-.-)@ -(o.o)- @(o_o)@
215 }
216  
217 private static List<DataGridViewRow> GetSelectedDataGridViewRows(DataGridView dataGridView)
218 {
219 return dataGridView.SelectedRows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
220 }
221  
222 private async Task WriteMemoryBanks(List<DataGridViewRow> rows)
223 {
224 var count = rows.Count;
225  
226 toolStripProgressBar1.Minimum = 0;
227 toolStripProgressBar1.Maximum = count;
228  
229 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
230 {
231 switch (rowProgress)
232 {
233 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
234 var success = rowProgressSuccess.Data;
235  
236 if (!success)
237 {
238 Log.Error($"{Resources.Could_not_write_memory_bank}");
239  
240 toolStripStatusLabel1.Text =
241 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
242 toolStripProgressBar1.Value = rowProgress.Index + 1;
243 statusStrip1.Update();
244  
245 return;
246 }
247  
248 toolStripStatusLabel1.Text =
249 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
250 toolStripProgressBar1.Value = rowProgress.Index + 1;
251 statusStrip1.Update();
252 break;
253 case DataGridViewRowProgressFailure rowProgressFailure:
254 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
255  
256 toolStripStatusLabel1.Text =
257 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
258 toolStripProgressBar1.Value = rowProgress.Index + 1;
259 statusStrip1.Update();
260 break;
261 }
262 });
263  
264 await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
265  
266 if (!_cancellationToken.IsCancellationRequested)
267 {
268 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
269 toolStripStatusLabel1.Text = "Done.";
270 }
271 }
272  
273 private async Task ReadMemoryBanks(IReadOnlyList<DataGridViewRow> rows)
274 {
275 var count = rows.Count;
276  
277 toolStripProgressBar1.Minimum = 0;
278 toolStripProgressBar1.Maximum = count;
279  
280 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
281 {
282 switch(rowProgress)
283 {
284 case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess:
285 var result = rowProgressSuccess.Data;
286  
287 //rowProgress.Row.Cells["LocationColumn"].Value = $"{rowProgressSuccess.Index + 1:000}";
288 rowProgress.Row.Cells["FrequencyColumn"].Value = result.Frequency;
289 rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)result.ClarifierDirection;
290 rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = result.ClarifierOffset;
291 rowProgress.Row.Cells["ClarColumn"].Value = result.Clar;
292 rowProgress.Row.Cells["ModeColumn"].Value = (string)result.MemoryRadioMode;
293 rowProgress.Row.Cells["CtcssColumn"].Value = (string)result.CtcssMode;
294 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase;
295 rowProgress.Row.Cells["TagColumn"].Value = result.Tag;
296 rowProgress.Row.Cells["TextColumn"].Value = result.Text.Trim();
297  
298 break;
299 case DataGridViewRowProgressFailure rowProgressFailure:
300 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
301  
302 break;
303 }
304  
305 toolStripStatusLabel1.Text =
306 $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
307 toolStripProgressBar1.Value = rowProgress.Index + 1;
308  
309 statusStrip1.Update();
310 });
311  
312 await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
313  
314 if (!_cancellationToken.IsCancellationRequested)
315 {
316 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
317 toolStripStatusLabel1.Text = "Done.";
318 }
319 }
320  
321 private async Task ReadMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress,
322 CancellationToken cancellationToken)
323 {
324 var count = rows.Count;
325  
326 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
327 {
328 try
329 {
330 var location = int.Parse($"{rows[i].Cells["LocationColumn"].Value}");
331  
332 var memoryBank = $"{location:000}";
333  
334 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { memoryBank }, _cancellationToken);
335  
336 progress.Report(new DataGridViewRowProgressSuccess<MemoryChannel>(rows[i], i, result));
337 }
338 catch (UnexpectedRadioResponseException exception)
339 {
340 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
341 }
342 catch (Exception exception)
343 {
344 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
345 }
346 }
347 }
348  
349 private async Task WriteMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress,
350 CancellationToken cancellationToken)
351 {
352 var count = rows.Count;
353  
354 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
355 {
356 try
357 {
358 var location = int.Parse($"{rows[i].Cells["LocationColumn"].Value}");
359  
360 var currentLocation = $"{location:000}";
361 var frequency = int.Parse($"{rows[i].Cells["FrequencyColumn"].Value}");
362 var clarifierDirection = char.Parse($"{rows[i].Cells["ClarifierDirectionColumn"].Value}");
363 var clarifierOffset = int.Parse($"{rows[i].Cells["ClarifierOffsetColumn"].Value}");
364 var clar = bool.Parse($"{rows[i].Cells["ClarColumn"].Value}");
365 var radioMode = new MemoryRadioMode($"{rows[i].Cells["ModeColumn"].Value}");
366 var ctcssMode = new CtcssMode($"{rows[i].Cells["CtcssColumn"].Value}");
367 var phase = new RadioPhase($"{rows[i].Cells["PhaseColumn"].Value}");
368 var tag = bool.Parse($"{rows[i].Cells["TagColumn"].Value}");
369 var text = $"{rows[i].Cells["TextColumn"].Value}";
370  
371 var memoryChannel = new MemoryChannel
372 {
373 CurrentLocation = currentLocation,
374 Frequency = frequency,
375 ClarifierDirection = clarifierDirection,
376 ClarifierOffset = clarifierOffset,
377 Clar = clar,
378 MemoryRadioMode = radioMode,
379 CtcssMode = ctcssMode,
380 Phase = phase,
381 Tag = tag,
382 Text = text
383 };
384  
385 var result = await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { memoryChannel }, _cancellationToken);
386  
387 progress.Report(new DataGridViewRowProgressSuccess<bool>(rows[i], i, result));
388 }
389 catch (UnexpectedRadioResponseException exception)
390 {
391 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
392 }
393 catch (Exception exception)
394 {
395 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
396 }
397 }
398 }
399 }
400 }