HamBook – Blame information for rev 1
?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 | |||
26 | private readonly ConcurrentDictionary<string, Cat> _catCommands; |
||
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 | { |
||
127 | _serialPort.Open(); |
||
128 | try |
||
129 | { |
||
130 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
131 | { |
||
132 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
||
133 | if (methodInfo != null) |
||
134 | { |
||
135 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
||
136 | { |
||
137 | await (Task)methodInfo.Invoke(catCommand, param); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | finally |
||
143 | { |
||
144 | _serialPort.Close(); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | public void CatSet<T>(string command, object[] param) |
||
149 | { |
||
150 | _serialPort.Open(); |
||
151 | try |
||
152 | { |
||
153 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
154 | { |
||
155 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
||
156 | if (methodInfo != null) |
||
157 | { |
||
158 | methodInfo.Invoke(catCommand, param); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | finally |
||
163 | { |
||
164 | _serialPort.Close(); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public T CatRead<T>(string command, object[] param) |
||
169 | { |
||
170 | _serialPort.Open(); |
||
171 | try |
||
172 | { |
||
173 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
174 | { |
||
175 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
||
176 | if (methodInfo != null) |
||
177 | { |
||
178 | return (T)methodInfo.Invoke(catCommand, param); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | finally |
||
183 | { |
||
184 | _serialPort.Close(); |
||
185 | } |
||
186 | |||
187 | return default; |
||
188 | } |
||
189 | |||
190 | public async Task<T> CatRead<T>(string command, object[] param, CancellationToken cancellationToken) |
||
191 | { |
||
192 | _serialPort.Open(); |
||
193 | try |
||
194 | { |
||
195 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
196 | { |
||
197 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
||
198 | if (methodInfo != null) |
||
199 | { |
||
200 | if(methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
||
201 | { |
||
202 | return await (Task<T>)methodInfo.Invoke(catCommand, param); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | finally |
||
208 | { |
||
209 | _serialPort.Close(); |
||
210 | } |
||
211 | |||
212 | return default; |
||
213 | } |
||
214 | } |
||
215 | } |