HamBook – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using HamBook.Properties; |
2 | using Org.BouncyCastle.Utilities; |
||
3 | using Serilog; |
||
4 | using System; |
||
5 | using System.Collections.Concurrent; |
||
6 | using System.Collections.Generic; |
||
7 | using System.IO.Ports; |
||
8 | using System.Linq; |
||
9 | using System.Reflection; |
||
10 | using System.Runtime.CompilerServices; |
||
11 | using System.Text; |
||
12 | using System.Threading; |
||
13 | using System.Threading.Tasks; |
||
14 | using System.Windows.Input; |
||
15 | |||
16 | namespace HamBook.Radios |
||
17 | { |
||
18 | public class CatAssemblies : IDisposable |
||
19 | { |
||
20 | public int Count => _count; |
||
21 | |||
22 | private int _count; |
||
23 | private SerialPort _serialPort; |
||
24 | private string _radio; |
||
25 | |||
3 | office | 26 | private ConcurrentDictionary<string, Cat> _catCommands; |
1 | office | 27 | |
28 | private CatAssemblies() |
||
29 | { |
||
30 | _catCommands = new ConcurrentDictionary<string, Cat>(); |
||
31 | } |
||
32 | |||
33 | public CatAssemblies(SerialPort serialPort, string radio) : this() |
||
34 | { |
||
35 | _serialPort = serialPort; |
||
36 | _radio = radio; |
||
37 | |||
38 | Load(_radio); |
||
39 | } |
||
40 | public void Dispose() |
||
41 | { |
||
42 | Unload(); |
||
43 | } |
||
44 | |||
45 | private void Load(string radio) |
||
46 | { |
||
47 | Interlocked.Exchange(ref _count, 0); |
||
48 | |||
49 | foreach (var type in Assembly.GetExecutingAssembly() |
||
50 | .GetTypes() |
||
51 | .Where(type => type.IsSubclassOf(typeof(Cat))) |
||
52 | .Where(type => type.GetCustomAttribute<RadioAttribute>() !=null && type.GetCustomAttribute<RadioAttribute>().Radio == radio)) |
||
53 | { |
||
54 | try |
||
55 | { |
||
56 | var catCommand = (Cat)type |
||
57 | .GetConstructor(new[] { typeof(SerialPort) }) |
||
58 | .Invoke(new [] { _serialPort }); |
||
59 | |||
60 | if (catCommand == null) |
||
61 | { |
||
62 | throw new ArgumentException(Resources.Could_not_create_assembly); |
||
63 | } |
||
64 | |||
65 | if (!_catCommands.TryAdd(catCommand.Name, catCommand)) |
||
66 | { |
||
67 | Log.Warning(Resources.Unable_to_register_assembly, catCommand.Name); |
||
68 | |||
69 | catCommand.Dispose(); |
||
70 | |||
71 | continue; |
||
72 | } |
||
73 | |||
74 | //BindEventHandlers(command); |
||
75 | |||
76 | Interlocked.Increment(ref _count); |
||
77 | } |
||
78 | catch (Exception exception) |
||
79 | { |
||
80 | Log.Error(exception, Resources.Could_not_instantiate_assembly, type); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | Log.Information(Resources.Registering_commands, Count); |
||
85 | } |
||
86 | |||
87 | private void Unload() |
||
88 | { |
||
89 | // Dispose all command assemblies. |
||
90 | var list = new List<Cat>(_catCommands.Values); |
||
91 | |||
92 | for (var i = 0; i < list.Count; ++i) |
||
93 | { |
||
94 | try |
||
95 | { |
||
96 | //UnbindEventHandlers(list[i]); |
||
97 | |||
98 | if (list[i] != null) |
||
99 | { |
||
100 | Log.Debug(Resources.Disposing_assembly, list[i].GetType().Name); |
||
101 | |||
102 | list[i] |
||
103 | .Dispose(); |
||
104 | |||
105 | list[i] = null; |
||
106 | } |
||
107 | } |
||
108 | catch (NullReferenceException) |
||
109 | { |
||
110 | // Already removed. |
||
111 | } |
||
112 | catch (ObjectDisposedException) |
||
113 | { |
||
114 | list[i] = null; |
||
115 | } |
||
116 | catch (Exception exception) |
||
117 | { |
||
118 | Log.Error(exception, Resources.Error_encountered_while_disposing_object); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | _catCommands.Clear(); |
||
123 | } |
||
124 | |||
125 | public async Task CatSet<T>(string command, object[] param, CancellationToken cancellationToken) |
||
126 | { |
||
3 | office | 127 | if (!_serialPort.IsOpen) |
128 | { |
||
129 | _serialPort.Open(); |
||
130 | } |
||
131 | |||
1 | office | 132 | try |
133 | { |
||
134 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
135 | { |
||
136 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
||
137 | if (methodInfo != null) |
||
138 | { |
||
139 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
||
140 | { |
||
141 | await (Task)methodInfo.Invoke(catCommand, param); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | finally |
||
147 | { |
||
3 | office | 148 | if (_serialPort.IsOpen) |
149 | { |
||
150 | _serialPort.Close(); |
||
151 | } |
||
1 | office | 152 | } |
153 | } |
||
154 | |||
155 | public void CatSet<T>(string command, object[] param) |
||
156 | { |
||
3 | office | 157 | if (!_serialPort.IsOpen) |
158 | { |
||
159 | _serialPort.Open(); |
||
160 | } |
||
161 | |||
1 | office | 162 | try |
163 | { |
||
164 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
165 | { |
||
166 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
||
167 | if (methodInfo != null) |
||
168 | { |
||
169 | methodInfo.Invoke(catCommand, param); |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | finally |
||
174 | { |
||
3 | office | 175 | if (_serialPort.IsOpen) |
176 | { |
||
177 | _serialPort.Close(); |
||
178 | } |
||
1 | office | 179 | } |
180 | } |
||
181 | |||
3 | office | 182 | public T CatGetDefault<T>(string command, object[] param) |
183 | { |
||
184 | if (!_serialPort.IsOpen) |
||
185 | { |
||
186 | _serialPort.Open(); |
||
187 | } |
||
188 | |||
189 | try |
||
190 | { |
||
191 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
192 | { |
||
193 | var methodInfo = catCommand.GetType().GetMethod("GetDefault"); |
||
194 | if (methodInfo != null) |
||
195 | { |
||
196 | return (T)methodInfo.Invoke(catCommand, param); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | finally |
||
201 | { |
||
202 | if (_serialPort.IsOpen) |
||
203 | { |
||
204 | _serialPort.Close(); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return default; |
||
209 | } |
||
210 | |||
211 | public T CatParse<T>(string command, object[] param) |
||
212 | { |
||
213 | if (!_serialPort.IsOpen) |
||
214 | { |
||
215 | _serialPort.Open(); |
||
216 | } |
||
217 | |||
218 | try |
||
219 | { |
||
220 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
221 | { |
||
222 | var methodInfo = catCommand.GetType().GetMethod("Parse"); |
||
223 | if (methodInfo != null) |
||
224 | { |
||
225 | return (T)methodInfo.Invoke(catCommand, param); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | finally |
||
230 | { |
||
231 | if (_serialPort.IsOpen) |
||
232 | { |
||
233 | _serialPort.Close(); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | return default; |
||
238 | } |
||
239 | |||
1 | office | 240 | public T CatRead<T>(string command, object[] param) |
241 | { |
||
3 | office | 242 | if (!_serialPort.IsOpen) |
243 | { |
||
244 | _serialPort.Open(); |
||
245 | } |
||
246 | |||
1 | office | 247 | try |
248 | { |
||
249 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
250 | { |
||
251 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
||
252 | if (methodInfo != null) |
||
253 | { |
||
254 | return (T)methodInfo.Invoke(catCommand, param); |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | finally |
||
259 | { |
||
3 | office | 260 | if (_serialPort.IsOpen) |
261 | { |
||
262 | _serialPort.Close(); |
||
263 | } |
||
1 | office | 264 | } |
265 | |||
266 | return default; |
||
267 | } |
||
268 | |||
269 | public async Task<T> CatRead<T>(string command, object[] param, CancellationToken cancellationToken) |
||
270 | { |
||
3 | office | 271 | if (!_serialPort.IsOpen) |
272 | { |
||
273 | _serialPort.Open(); |
||
274 | } |
||
275 | |||
1 | office | 276 | try |
277 | { |
||
278 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
279 | { |
||
280 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
||
281 | if (methodInfo != null) |
||
282 | { |
||
283 | if(methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
||
284 | { |
||
285 | return await (Task<T>)methodInfo.Invoke(catCommand, param); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | finally |
||
291 | { |
||
3 | office | 292 | if (_serialPort.IsOpen) |
293 | { |
||
294 | _serialPort.Close(); |
||
295 | } |
||
1 | office | 296 | } |
297 | |||
298 | return default; |
||
299 | } |
||
300 | } |
||
301 | } |