HamBook – Blame information for rev 48

Subversion Repositories:
Rev:
Rev Author Line No. Line
39 office 1 using HamBook.Radios.Generic;
2 using HamBook.Radios;
3 using System;
4 using System.Collections.Generic;
5 using System.Threading;
6 using System.Threading.Tasks;
7 using Serilog;
8 using HamBook.Properties;
9 using System.Media;
10 using System.Reflection;
11 using RJCP.IO.Ports;
12  
13 namespace HamBook
14 {
15 public class MemoryTune
16 {
17 private CancellationTokenSource _scanningCancellationTokenSource;
18 private CancellationToken _scanningCancellationToken;
19 private Thread _scanThread;
20 private CatAssemblies _catAssemblies;
21 private SerialPortStream _serialPort;
22 private Configuration.Configuration _configuration;
48 office 23 private MemoryBanks _memoryBanks;
39 office 24  
25 private MemoryTune()
26 {
27 }
28  
29 public MemoryTune(CatAssemblies catAssemblies, SerialPortStream serialPort, Configuration.Configuration configuration) : this()
30 {
31 _catAssemblies = catAssemblies;
32 _serialPort = serialPort;
33 _configuration = configuration;
34  
48 office 35 _memoryBanks = MemoryBanks.Create(_configuration.Radio);
39 office 36 }
37  
38 public void Start()
39 {
40 if (_scanThread != null)
41 {
42 if (!_scanningCancellationToken.IsCancellationRequested)
43 {
44 _scanningCancellationTokenSource.Cancel();
45 }
46  
47 _scanThread.Join();
48 _scanThread = null;
49 }
50  
51 _scanningCancellationTokenSource = new CancellationTokenSource();
52 _scanningCancellationToken = _scanningCancellationTokenSource.Token;
53  
54 _scanThread = new Thread(new ParameterizedThreadStart(Tune));
55  
45 office 56 var memoryBankQueue = new Queue<string>();
57 foreach (var bank in _memoryBanks.GetMemoryBanks())
39 office 58 {
59 memoryBankQueue.Enqueue(bank);
60 }
61  
62 _scanThread.Start(memoryBankQueue);
63 }
64  
65 public void Stop()
66 {
67 if (_scanThread != null)
68 {
69 if (!_scanningCancellationToken.IsCancellationRequested)
70 {
71 _scanningCancellationTokenSource.Cancel();
72 }
73  
74 _scanThread.Join();
75 _scanThread = null;
76 }
77 }
78  
79 private async void Tune(object obj)
80 {
45 office 81 var memoryBankQueue = (Queue<string>)obj;
39 office 82  
83 do
84 {
85 try
86 {
87  
88 var memoryBank = memoryBankQueue.Dequeue();
89  
90 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
91 {
92 soundPlayer.Play();
93  
94 if (!await _catAssemblies.CatSetAsync<int, bool>("MC", new object[] { memoryBank }, _scanningCancellationToken))
95 {
96 throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotChangeChannel, Resources.Could_not_change_channel);
97 }
98 }
99  
100 if (!await _catAssemblies.CatSetAsync<TunerState, bool>("AC", new object[] { TunerState.TUNER_ON }, _scanningCancellationToken))
101 {
102 throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotEnableTuner, Resources.Could_not_enable_tuner);
103 }
104  
105 do
106 {
107 await Task.Delay(TimeSpan.FromSeconds(1), _scanningCancellationToken);
108  
109 try
110 {
111 var tuneState = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { }, _scanningCancellationToken);
112  
113 if (tuneState == TunerState.TUNER_ON)
114 {
115 break;
116 }
117 }
118 catch (Exception)
119 {
120 // retry
121 }
122  
123 } while (!_scanningCancellationToken.IsCancellationRequested);
124  
125 if (!await _catAssemblies.CatSetAsync<TunerState, bool>("AC", new object[] { TunerState.TUNING_START }, _scanningCancellationToken))
126 {
127 throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotStartTuning, Resources.Could_not_start_tuning);
128 }
129  
130 do
131 {
132 await Task.Delay(TimeSpan.FromSeconds(1), _scanningCancellationToken);
133  
134 try
135 {
136 var tuneState = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { }, _scanningCancellationToken);
137  
138 if (tuneState != TunerState.TUNING_START)
139 {
140 break;
141 }
142 }
143 catch (Exception)
144 {
145 // retry
146 }
147  
148 } while (!_scanningCancellationToken.IsCancellationRequested);
149  
150 }
151 catch(MemoryTuneException exception)
152 {
153 Log.Error(exception, exception.Message);
154 }
155 catch (Exception exception)
156 {
157 Log.Error(exception, Resources.Unknown_error_while_tuning);
158 }
159  
160 } while (!_scanningCancellationToken.IsCancellationRequested && memoryBankQueue.Count != 0);
161  
162 }
163 }
164 }