-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSharedResources.cs
More file actions
433 lines (389 loc) · 14.2 KB
/
Copy pathSharedResources.cs
File metadata and controls
433 lines (389 loc) · 14.2 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//
// ================
// Shared Resources
// ================
//
// This class is a container for all shared resources that may be needed
// by the drivers served by the Local Server.
//
// NOTES:
//
// * ALL DECLARATIONS MUST BE STATIC HERE!! INSTANCES OF THIS CLASS MUST NEVER BE CREATED!
//
// Written by: Bob Denny 29-May-2007
// Modified by Chris Rowland and Peter Simpson to hamdle multiple hardware devices March 2011
//
using System;
using System.Windows.Forms;
using System.Threading;
using ASCOM.Utilities;
namespace ASCOM.OpenAstroTracker
{
/// <summary>
/// The resources shared by all drivers and devices, in this example it's a serial port with a shared SendMessage method
/// an idea for locking the message and handling connecting is given.
/// In reality extensive changes will probably be needed.
/// Multiple drivers means that several applications connect to the same hardware device, aka a hub.
/// Multiple devices means that there are more than one instance of the hardware, such as two focusers.
/// In this case there needs to be multiple instances of the hardware connector, each with it's own connection count.
/// </summary>
public static class SharedResources
{
[Flags]
public enum LoggingFlags
{
None = 0,
Serial = 1,
Server = 2,
Scope = 4,
Focuser = 8,
Setup = 128,
}
// object used for locking to prevent multiple drivers accessing common code at the same time
private static readonly object lockObject = new object();
private static TraceLogger traceLogger;
public static string driverID = "ASCOM.OpenAstroTracker.Telescope";
private static long messageNumber = 1;
private static LoggingFlags currentLogFlags = LoggingFlags.Scope | LoggingFlags.Focuser;
private static string comPortProfileName = "COM Port";
private static string baudRateProfileName = "Baud Rate";
private static string traceStateProfileName = "Trace Level";
private static string traceFlagsProfileName = "Trace Flags";
private static string latitudeProfileName = "Latitude";
private static string longitudeProfileName = "Longitude";
private static string elevationProfileName = "Elevation";
private static string comPortDefault = "COM5";
private static string baudRateDefault = "19200";
private static string traceStateDefault = "True";
private static string traceFlagsDefault = "Scope, Focuser";
private static double latitudeDefault = 30;
private static double longitudeDefault = -97;
private static double elevationDefault = 1;
private static readonly Mutex _commandMutex;
private static ProfileData _cachedProfile;
private static readonly object _profileLock = new object();
static SharedResources()
{
_commandMutex = new Mutex(false, "CommMutex");
EnsureLogger();
}
//
// Public access to shared resources
//
public static Mutex OATCommandMutex => _commandMutex;
private static void EnsureLogger()
{
lock (lockObject)
{
if (traceLogger == null)
{
traceLogger = new TraceLogger("", "OpenAstroTracker.LocalServer");
traceLogger.Enabled = true;
}
}
}
//public static TraceLogger tl
//{
// get
// {
// if (traceLogger == null)
// {
// traceLogger = new TraceLogger("", "OpenAstroTracker.LocalServer");
// traceLogger.Enabled = true;
// }
// return traceLogger;
// }
//}
public static ProfileData ReadProfile()
{
lock (_profileLock)
{
if (_cachedProfile != null)
return _cachedProfile;
using (Profile driverProfile = new Profile())
{
driverProfile.DeviceType = "Telescope";
_cachedProfile = new ProfileData
{
TraceState = Convert.ToBoolean(driverProfile.GetValue(driverID, traceStateProfileName, String.Empty, traceStateDefault)),
TraceFlags = (LoggingFlags)Enum.Parse(typeof(LoggingFlags), driverProfile.GetValue(driverID, traceFlagsProfileName, String.Empty, traceFlagsDefault)),
ComPort = driverProfile.GetValue(driverID, comPortProfileName, string.Empty, comPortDefault),
BaudRate = long.Parse(driverProfile.GetValue(driverID, baudRateProfileName, string.Empty, baudRateDefault)),
Latitude = Convert.ToDouble(driverProfile.GetValue(driverID, latitudeProfileName, string.Empty, latitudeDefault.ToString())),
Longitude = Convert.ToDouble(driverProfile.GetValue(driverID, longitudeProfileName, string.Empty, longitudeDefault.ToString())),
Elevation = Convert.ToDouble(driverProfile.GetValue(driverID, elevationProfileName, string.Empty, elevationDefault.ToString()))
};
return _cachedProfile;
}
}
}
public static void WriteProfile(ProfileData profile)
{
using (Profile driverProfile = new Profile())
{
driverProfile.DeviceType = "Telescope";
driverProfile.WriteValue(driverID, traceStateProfileName, profile.TraceState.ToString());
driverProfile.WriteValue(driverID, traceFlagsProfileName, profile.TraceFlags.ToString());
driverProfile.WriteValue(driverID, comPortProfileName, profile.ComPort?.ToString() ?? "");
driverProfile.WriteValue(driverID, baudRateProfileName, profile.BaudRate.ToString());
driverProfile.WriteValue(driverID, latitudeProfileName, profile.Latitude.ToString());
driverProfile.WriteValue(driverID, longitudeProfileName, profile.Longitude.ToString());
driverProfile.WriteValue(driverID, elevationProfileName, profile.Elevation.ToString());
currentLogFlags = profile.TraceFlags;
}
// Update cache after successful write so subsequent ReadProfile calls skip the registry
lock (_profileLock)
{
_cachedProfile = profile;
}
}
#region single serial port connector
//
// this region shows a way that a single serial port could be connected to by multiple
// drivers.
//
// Connected is used to handle the connections to the port.
//
// SendMessage is a way that messages could be sent to the hardware without
// conflicts between different drivers.
//
// All this is for a single connection, multiple connections would need multiple ports
// and a way to handle connecting and disconnection from them - see the
// multi driver handling section for ideas.
//
/// <summary>
/// Shared serial port
/// </summary>
public static Serial SharedSerial { get; } = new ASCOM.Utilities.Serial();
/// <summary>
/// number of connections to the shared serial port
/// </summary>
public static int Connections { get; set; } = 0;
/// <summary>
/// The possible replies that a Meade command can produce
/// </summary>
enum ExpectedAnswer
{
None,
Digit,
HashTerminated,
DoubleHashTerminated,
}
/// <summary>
/// A shared SendMessage method, the lock prevents different drivers tripping over one another.
/// The message passed should end in one of these endings:
/// # - the command does not expect a reply
/// #,n - the command expect a single numerical digit (always 0 or 1)
/// #,# - the command expects a string reply that ends in a #
/// It is very important to send the right variation since otherwise the protocol will become confused.
/// </summary>
public static string SendMessage(string message)
{
string retVal = string.Empty;
long messageNr = Interlocked.Increment(ref messageNumber);
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} > ({message}) - awaiting lock");
lock (lockObject)
{
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - acquired lock");
ExpectedAnswer expect = ExpectedAnswer.None;
if (message.EndsWith("#,#"))
{
message = message.Substring(0, message.Length - 2);
expect = ExpectedAnswer.HashTerminated;
}
else if (message.EndsWith("#,##"))
{
message = message.Substring(0, message.Length - 3);
expect = ExpectedAnswer.DoubleHashTerminated;
}
else if (message.EndsWith("#,n"))
{
message = message.Substring(0, message.Length - 2);
expect = ExpectedAnswer.Digit;
}
else if (!message.EndsWith("#"))
{
message += "#";
}
if (SharedSerial.Connected && !String.IsNullOrEmpty(message))
{
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Sending command, expecting {expect} reply for '{message}'");
SharedSerial.ClearBuffers();
SharedSerial.Transmit(message);
switch (expect)
{
case ExpectedAnswer.Digit:
retVal = SharedSerial.ReceiveCounted(1);
break;
case ExpectedAnswer.HashTerminated:
retVal = SharedSerial.ReceiveTerminated("#");
retVal = retVal.TrimEnd('#');
break;
case ExpectedAnswer.DoubleHashTerminated:
retVal = SharedSerial.ReceiveTerminated("#");
retVal = retVal.TrimEnd('#');
// Protocol returns two #-terminated strings (e.g. :SC returns "1#Updating Planetary Data#").
// Return the first string (success indicator); read and discard the trailing info string.
SharedSerial.ReceiveTerminated("#");
break;
}
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Reply: " + retVal);
}
else
{
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Not connected or Empty Message: " + message);
if (!SharedSerial.Connected)
{
throw new ASCOM.NotConnectedException($"SendMessage called while serial port is disconnected. Command: {message}");
}
}
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Releasing lock");
}
LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} < Released lock");
return retVal;
}
public static string SendPassThroughCommand(string message)
{
return SendMessage(message);
}
/// <summary>
/// Example of handling connecting to and disconnection from the
/// shared serial port.
/// Needs error handling
/// the port name etc. needs to be set up first, this could be done by the driver
/// checking Connected and if it's false setting up the port before setting connected to true.
/// It could also be put here.
/// </summary>
public static bool Connected
{
set
{
LogMessage(LoggingFlags.Server, $"Connected Set({value})");
lock (lockObject)
{
LogMessage(LoggingFlags.Server, $"Connected Set - {value}");
if (value)
{
if (Connections == 0)
{
LogMessage(LoggingFlags.Server, $"Connected Set - No connections active");
try
{
SharedSerial.DTREnable = false;
SharedSerial.PortName = ReadProfile().ComPort;
SharedSerial.ReceiveTimeoutMs = 2000;
SharedSerial.Speed = (SerialSpeed)ReadProfile().BaudRate;
LogMessage(LoggingFlags.Server, $"Connected Set - Attempting to connect {SharedSerial.PortName}@{SharedSerial.Speed}");
SharedSerial.Connected = true;
Thread.Sleep(1000);
LogMessage(LoggingFlags.Server, $"Connected Set - Connection seems to have succeeded.");
SharedSerial.Transmit(":I#");
}
catch (System.IO.IOException exception)
{
MessageBox.Show("Serial port not opened for " + SharedResources.SharedSerial.PortName,
"Invalid port state", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogMessage(LoggingFlags.Server, $"Connected Set - Serial port not opened: " + exception.Message);
}
catch (System.UnauthorizedAccessException exception)
{
MessageBox.Show("Access denied to serial port " + SharedResources.SharedSerial.PortName,
"Access denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogMessage(LoggingFlags.Server, $"Connected Set - Access denied to serial port: " + exception.Message);
}
catch (ASCOM.DriverAccessCOMException exception)
{
MessageBox.Show("ASCOM driver exception: " + exception.Message,
"ASCOM driver exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogMessage(LoggingFlags.Server, $"Connected Set - Driver Access failure: " + exception.Message);
}
catch (System.Runtime.InteropServices.COMException exception)
{
MessageBox.Show(
"Serial port read timeout for port " + SharedResources.SharedSerial.PortName,
"Timeout", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogMessage(LoggingFlags.Server, $"Connected Set - Serial port COM Exception: " + exception.Message);
}
}
else
{
LogMessage(LoggingFlags.Server, $"Connected Set - Already connected with {Connections} connections active");
}
Connections++;
LogMessage(LoggingFlags.Server, $"Connected Set - Connection Count is {Connections} clients");
}
else
{
Connections--;
LogMessage(LoggingFlags.Server, $"Connected Set - Connection Count is {Connections} clients");
if (Connections <= 0)
{
Connections = 0;
LogMessage(LoggingFlags.Server, $"Connected Set - No Connections remain, sedning Qq# and disconnecting device");
SharedSerial.Transmit(":Qq#");
SharedSerial.Connected = false;
}
}
}
}
get { lock (lockObject) { return SharedSerial.Connected; } }
}
/// <summary>
/// Required Interface functions for ASCOM, apparently.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string SendMessageString(string message)
{
try
{
string answer = SharedResources.SendMessage(message);
return answer;
}
catch
{
return "";
}
}
/// <summary>
/// Required Interface functions for ASCOM, apparently.
/// </summary>
public static void SendMessageBlind(string message)
{
try
{
string answer = SharedResources.SendMessage(message);
}
catch { }
}
#endregion
/// <summary>
/// Skeleton of a hardware class, all this does is hold a count of the connections,
/// in reality extra code will be needed to handle the hardware in some way
/// </summary>
public class DeviceHardware
{
internal int count { set; get; }
internal DeviceHardware()
{
count = 0;
}
}
private static void LogMessage(LoggingFlags flag, string message)
{
LogMessageCallback(flag, message);
}
public static void SetTraceFlags(LoggingFlags flags)
{
currentLogFlags = flags;
}
public static void LogMessageCallback(LoggingFlags flag, string message)
{
if ((currentLogFlags & flag) != 0)
{
EnsureLogger();
traceLogger.LogMessage(flag.ToString(), $"[{Thread.CurrentThread.ManagedThreadId,0:000}] {message}");
}
}
}
}