-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedResources.cs
158 lines (142 loc) · 5.82 KB
/
SharedResources.cs
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
//
// ================
// 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!
using ASCOM.Utilities;
namespace ASCOM.LocalServer
{
/// <summary>
/// Add and manage resources that are shared by all drivers served by this local server here.
/// 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 drivers 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>
[HardwareClass]
public static class SharedResources
{
// Object used for locking to prevent multiple drivers accessing common code at the same time
private static readonly object lockObject = new object();
// Shared serial port. This will allow multiple drivers to use one single serial port.
private static Serial sharedSerial = new Serial(); // Shared serial port
private static int serialConnectionCount = 0; // counter for the number of connections to the serial port
// Public access to shared resources
#region Dispose method to clean up resources before close
/// <summary>
/// Deterministically release both managed and unmanaged resources that are used by this class.
/// </summary>
/// <remarks>
/// TODO: Release any managed or unmanaged resources that are used in this class.
///
/// Do not call this method from the CameraHardware.Dispose() method in your hardware class.
///
/// This is because this shared resources class is decorated with the <see cref="HardwareClassAttribute"/> attribute and this Dispose() method will be called
/// automatically by the local server executable when it is irretrievably shutting down. This gives you the opportunity to release managed and unmanaged resources
/// in a timely fashion and avoid any time delay between local server close down and garbage collection by the .NET runtime.
///
/// </remarks>
public static void Dispose()
{
try
{
if (sharedSerial != null)
{
sharedSerial.Dispose();
}
}
catch
{
}
}
#endregion
#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
{
return sharedSerial;
}
}
/// <summary>
/// Number of connections to the shared serial port
/// </summary>
public static int Connections
{
get
{
return serialConnectionCount;
}
set
{
serialConnectionCount = value;
}
}
/// <summary>
/// Example of a shared SendMessage method
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
/// <remarks>
/// The lock prevents different drivers tripping over one another. It needs error handling and assumes that the message will be sent unchanged and that the reply will always be terminated by a "#" character.
/// </remarks>
public static string SendMessage(string message)
{
// TODO update this with your requirements
lock (lockObject)
{
SharedSerial.Transmit(message);
return SharedSerial.ReceiveTerminated("#");
}
}
/// <summary>
/// Example of handling connecting to and disconnection from the shared serial port.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public static bool Connected
{
set
{
lock (lockObject)
{
if (value)
{
if (serialConnectionCount == 0)
{
SharedSerial.Connected = true;
}
serialConnectionCount++;
}
else
{
serialConnectionCount--;
if (serialConnectionCount <= 0)
{
SharedSerial.Connected = false;
}
}
}
}
get { return SharedSerial.Connected; }
}
#endregion
}
}