@@ -157,25 +157,30 @@ private async Task ReadHA()
157
157
async private Task < string > RunCustomOATCommandAsync ( string command )
158
158
{
159
159
string result = string . Empty ;
160
- try
160
+ Log ( "OATCustom: StartRunning [{0}], checking access" , command ) ;
161
+ if ( Interlocked . CompareExchange ( ref _runningOATCommand , 1 , 0 ) == 0 )
161
162
{
162
- Interlocked . Increment ( ref _runningOATCommand ) ;
163
- bool needsReturn = false ;
164
- Log ( "OATCustom: StartRunning [{0}], awaiting access" , command ) ;
165
- await exclusiveAccess . WaitAsync ( ) ;
166
- Log ( "OATCustom: Acces granted. -> Sending {0} " , command ) ;
167
- var commandResult = await _oatMount . SendCommand ( command ) ;
168
- if ( ! string . IsNullOrEmpty ( commandResult . Data ) )
163
+ try
164
+ {
165
+ await exclusiveAccess . WaitAsync ( ) ;
166
+ Log ( "OATCustom: Access granted. -> Sending {0} " , command ) ;
167
+ var commandResult = await _oatMount . SendCommand ( command ) ;
168
+ if ( ! string . IsNullOrEmpty ( commandResult . Data ) )
169
+ {
170
+ Log ( "OATCustomResult [{0}]: '{1}'" , command , commandResult . Data ) ;
171
+ result = commandResult . Data ;
172
+ }
173
+ Interlocked . Decrement ( ref _runningOATCommand ) ;
174
+ }
175
+ finally
169
176
{
170
- Log ( "OATCustomResult [{0}]: '{1}' " , command , commandResult . Data ) ;
171
- result = commandResult . Data ;
177
+ Log ( "OATCustom: StopRunning [{0}]" , command ) ;
178
+ exclusiveAccess . Release ( ) ;
172
179
}
173
180
}
174
- finally
181
+ else
175
182
{
176
- Log ( "OATCustom: StopRunning [{0}]" , command ) ;
177
- exclusiveAccess . Release ( ) ;
178
- Interlocked . Decrement ( ref _runningOATCommand ) ;
183
+ Log ( "OATCustom: Already running, skipping '{0}'" , command ) ;
179
184
}
180
185
181
186
return result . TrimEnd ( "#" . ToCharArray ( ) ) ;
@@ -184,42 +189,11 @@ async private Task<string> RunCustomOATCommandAsync(string command)
184
189
private async Task OnTimer ( object s , EventArgs e )
185
190
{
186
191
_timerStatus . Stop ( ) ;
187
- //if (_tryConnect)
188
- //{
189
- // _tryConnect = false;
190
-
191
- // MountStatus = "Connecting...";
192
-
193
- // ScopeName = Settings.Default.Scope;
194
- // if (!string.IsNullOrEmpty(ScopeName))
195
- // {
196
- // _oatMount = new OatmealTelescopeCommandHandlers(ScopeName);
197
- // try
198
- // {
199
- // OnConnectToTelescope();
200
- // }
201
- // catch (Exception)
202
- // {
203
- // }
204
-
205
- // RequeryCommands();
206
- // }
207
- //}
208
-
209
- if ( Interlocked . CompareExchange ( ref _runningOATCommand , 1 , 0 ) == 0 )
210
- {
211
- Log ( "OATTimer: Not Running" ) ;
212
- if ( MountConnected )
213
- {
214
- //UpdateCurrentCoordinates();
215
- await UpdateStatus ( ) ;
216
- }
217
- Interlocked . Decrement ( ref _runningOATCommand ) ;
218
- }
219
- else
192
+ if ( MountConnected )
220
193
{
221
- Log ( "OATTimer: Running -> Skip" ) ;
194
+ await UpdateStatus ( ) ;
222
195
}
196
+
223
197
_timerStatus . Start ( ) ;
224
198
}
225
199
@@ -251,7 +225,10 @@ private async Task UpdateStatus()
251
225
default : IsSlewingNorth = false ; IsSlewingSouth = false ; break ;
252
226
}
253
227
254
- IsTracking = parts [ 1 ] [ 2 ] == 'T' ;
228
+ // Don't use property here since it sends a command.
229
+ _isTracking = parts [ 1 ] [ 2 ] == 'T' ;
230
+ OnPropertyChanged ( "IsTracking" ) ;
231
+
255
232
256
233
RAStepper = int . Parse ( parts [ 2 ] ) ;
257
234
DECStepper = int . Parse ( parts [ 3 ] ) ;
@@ -271,7 +248,7 @@ private async Task UpdateStatus()
271
248
private async Task OnHome ( )
272
249
{
273
250
await RunCustomOATCommandAsync ( ":hF#" ) ;
274
-
251
+
275
252
// The next call actually blocks because Homeing is synchronous
276
253
await UpdateStatus ( ) ;
277
254
@@ -284,7 +261,8 @@ private async Task OnPark()
284
261
{
285
262
await RunCustomOATCommandAsync ( ":hP#" ) ;
286
263
ParkCommandString = "Unpark" ;
287
- } else
264
+ }
265
+ else
288
266
{
289
267
await RunCustomOATCommandAsync ( ":hU#,#" ) ;
290
268
ParkCommandString = "Park" ;
@@ -429,13 +407,14 @@ private async void OnConnectToTelescope()
429
407
430
408
private async Task OnRunDriftAlignment ( int driftDuration )
431
409
{
410
+ _timerStatus . Stop ( ) ;
411
+
432
412
DriftAlignStatus = "Drift Alignment running..." ;
433
413
var tracking = await RunCustomOATCommandAsync ( ":GIT#,#" ) ;
434
414
bool wasTracking = tracking == "1" ;
435
415
IsTracking = false ;
436
416
DateTime startTime = DateTime . UtcNow ;
437
417
TimeSpan duration = TimeSpan . FromSeconds ( 2 * driftDuration + 2 ) ;
438
- _timerStatus . Stop ( ) ;
439
418
await Task . Delay ( 200 ) ;
440
419
441
420
try
@@ -460,6 +439,7 @@ await Task.Run(() =>
460
439
461
440
DriftAlignStatus = "Drift Alignment" ;
462
441
IsDriftAligning = false ;
442
+ IsTracking = wasTracking ;
463
443
RequeryCommands ( ) ;
464
444
_timerStatus . Start ( ) ;
465
445
}
0 commit comments