Skip to content

Commit 9a40bb8

Browse files
OATControl updates
- ensured only one command runs at a time - restored tracking state after drift alignment
1 parent 9904f04 commit 9a40bb8

File tree

1 file changed

+32
-52
lines changed
  • Software/OpenAstroTracker ASCOM/OATControl/ViewModels

1 file changed

+32
-52
lines changed

Software/OpenAstroTracker ASCOM/OATControl/ViewModels/MountVM.cs

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,30 @@ private async Task ReadHA()
157157
async private Task<string> RunCustomOATCommandAsync(string command)
158158
{
159159
string result = string.Empty;
160-
try
160+
Log("OATCustom: StartRunning [{0}], checking access", command);
161+
if (Interlocked.CompareExchange(ref _runningOATCommand, 1, 0) == 0)
161162
{
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
169176
{
170-
Log("OATCustomResult [{0}]: '{1}'", command, commandResult.Data);
171-
result = commandResult.Data;
177+
Log("OATCustom: StopRunning [{0}]", command);
178+
exclusiveAccess.Release();
172179
}
173180
}
174-
finally
181+
else
175182
{
176-
Log("OATCustom: StopRunning [{0}]", command);
177-
exclusiveAccess.Release();
178-
Interlocked.Decrement(ref _runningOATCommand);
183+
Log("OATCustom: Already running, skipping '{0}'", command);
179184
}
180185

181186
return result.TrimEnd("#".ToCharArray());
@@ -184,42 +189,11 @@ async private Task<string> RunCustomOATCommandAsync(string command)
184189
private async Task OnTimer(object s, EventArgs e)
185190
{
186191
_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)
220193
{
221-
Log("OATTimer: Running -> Skip");
194+
await UpdateStatus();
222195
}
196+
223197
_timerStatus.Start();
224198
}
225199

@@ -251,7 +225,10 @@ private async Task UpdateStatus()
251225
default: IsSlewingNorth = false; IsSlewingSouth = false; break;
252226
}
253227

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+
255232

256233
RAStepper = int.Parse(parts[2]);
257234
DECStepper = int.Parse(parts[3]);
@@ -271,7 +248,7 @@ private async Task UpdateStatus()
271248
private async Task OnHome()
272249
{
273250
await RunCustomOATCommandAsync(":hF#");
274-
251+
275252
// The next call actually blocks because Homeing is synchronous
276253
await UpdateStatus();
277254

@@ -284,7 +261,8 @@ private async Task OnPark()
284261
{
285262
await RunCustomOATCommandAsync(":hP#");
286263
ParkCommandString = "Unpark";
287-
}else
264+
}
265+
else
288266
{
289267
await RunCustomOATCommandAsync(":hU#,#");
290268
ParkCommandString = "Park";
@@ -429,13 +407,14 @@ private async void OnConnectToTelescope()
429407

430408
private async Task OnRunDriftAlignment(int driftDuration)
431409
{
410+
_timerStatus.Stop();
411+
432412
DriftAlignStatus = "Drift Alignment running...";
433413
var tracking = await RunCustomOATCommandAsync(":GIT#,#");
434414
bool wasTracking = tracking == "1";
435415
IsTracking = false;
436416
DateTime startTime = DateTime.UtcNow;
437417
TimeSpan duration = TimeSpan.FromSeconds(2 * driftDuration + 2);
438-
_timerStatus.Stop();
439418
await Task.Delay(200);
440419

441420
try
@@ -460,6 +439,7 @@ await Task.Run(() =>
460439

461440
DriftAlignStatus = "Drift Alignment";
462441
IsDriftAligning = false;
442+
IsTracking = wasTracking;
463443
RequeryCommands();
464444
_timerStatus.Start();
465445
}

0 commit comments

Comments
 (0)