Skip to content

Commit 96342e5

Browse files
V1.6.2x - Updates
1 parent 6c2c432 commit 96342e5

File tree

5 files changed

+118
-12
lines changed

5 files changed

+118
-12
lines changed

Software/OpenAstroTracker ASCOM/OATControl/App.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MahApps.Metro;
2+
using OATControl.ViewModels;
23
using System;
34
using System.Collections.Generic;
45
using System.Configuration;
@@ -28,8 +29,9 @@ protected override void OnStartup(StartupEventArgs e)
2829
// now set the Green accent and dark theme
2930
ThemeManager.ChangeAppStyle(Application.Current,
3031
ThemeManager.GetAccent("RedAccent"),
31-
ThemeManager.GetAppTheme("RedTheme"));
32+
ThemeManager.GetAppTheme("RedTheme"));
3233

34+
Log.Init("OatControl");
3335
base.OnStartup(e);
3436
}
3537
}

Software/OpenAstroTracker ASCOM/OATControl/MainWindow.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10">
112112
<Grid.RowDefinitions>
113113
<RowDefinition Height="Auto"/>
114-
<RowDefinition Height="30"/>
114+
<RowDefinition Height="Auto"/>
115115
<RowDefinition Height="Auto"/>
116116
<RowDefinition Height="Auto"/>
117117
<RowDefinition Height="Auto"/>
@@ -175,8 +175,9 @@
175175
<TextBlock Grid.Row="7" Grid.Column="0" Text="Preset" Margin="0,20,0,0" Style="{StaticResource TextBlockHeading}" />
176176
<ComboBox Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="7" Text="Preset" Margin="0,20,0,0" ItemsSource="{Binding AvailablePointsOfInterest}" DisplayMemberPath="Name" SelectedIndex="{Binding SelectedPointOfInterest}"/>
177177

178-
<Button Grid.Row="1" Grid.Column="7" Grid.RowSpan="5" Margin="8,0" Padding="30,20" Content="Slew!" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding SlewToTargetCommand}" />
179-
<Button Grid.Row="6" Grid.Column="7" Grid.RowSpan="1" Margin="8,8,8,0" Padding="0,0" Content="Sync" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding SyncToTargetCommand}" />
178+
<Button Grid.Row="1" Grid.Column="7" Grid.RowSpan="1" Margin="8,0,8,4" Padding="0,4,0,8" Content="&lt;- Sync" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding SyncToCurrentCommand}" />
179+
<Button Grid.Row="2" Grid.Column="7" Grid.RowSpan="4" Margin="8,0" Padding="30,20" Content="Slew!" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding SlewToTargetCommand}" />
180+
<Button Grid.Row="6" Grid.Column="7" Grid.RowSpan="1" Margin="8,4,8,0" Padding="0,4,0,8" Content="Sync -&gt;" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding SyncToTargetCommand}" />
180181

181182

182183
<TextBlock Grid.Row="0" Grid.Column="8" Grid.ColumnSpan="7" Text="Current" Style="{StaticResource TextBlockTitle}" HorizontalAlignment="Center"/>

Software/OpenAstroTracker ASCOM/OATControl/OATControl.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
</Compile>
117117
<Compile Include="Converters\StretchModeConverter.cs" />
118118
<Compile Include="ViewModels\DelegateCommand.cs" />
119+
<Compile Include="ViewModels\Log.cs" />
119120
<Compile Include="ViewModels\MountVM.cs" />
120121
<Compile Include="ViewModels\PointOfInterest.cs" />
121122
<Compile Include="ViewModels\ViewModelBase.cs" />
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace OATControl.ViewModels
10+
{
11+
public class Log
12+
{
13+
private static DateTime appStartTime = DateTime.UtcNow;
14+
private static object oLock = new object();
15+
private static string sPath = string.Format("{0}\\oat_{1}-{2}.log", Environment.GetFolderPath(Environment.SpecialFolder.Personal), DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"), Environment.UserName);
16+
17+
private static List<string> lstBuffer = new List<string>();
18+
private static DateTime dtLastUpdate = DateTime.Now.AddSeconds(5.0);
19+
private static int maxBuffered = 0;
20+
21+
public static string Filename
22+
{
23+
get
24+
{
25+
return Log.sPath;
26+
}
27+
}
28+
29+
public static void Init(string sTitle)
30+
{
31+
Log.WriteLine("*********************************");
32+
Log.WriteLine(string.Format("* {0} *", sTitle.PadRight(28)));
33+
Log.WriteLine("*********************************");
34+
Log.WriteLine("* Started : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " *");
35+
Log.WriteLine(string.Format("* User : {0} *", Environment.UserName.PadRight(19)));
36+
Log.WriteLine("*********************************");
37+
}
38+
39+
private static string FormatMessage(string message, object[] args)
40+
{
41+
var sb = new StringBuilder(message.Length + 64);
42+
43+
TimeSpan elapsed = DateTime.UtcNow - Log.appStartTime;
44+
sb.AppendFormat("[{0}] [{1}]: ", elapsed.ToString("hh\\:mm\\:ss\\.ffff"), Thread.CurrentThread.ManagedThreadId);
45+
46+
if (args != null && args.Length > 0)
47+
{
48+
sb.AppendFormat(message, args);
49+
}
50+
else
51+
{
52+
sb.Append(message);
53+
}
54+
55+
return sb.ToString();
56+
}
57+
58+
public static void WriteLine(string message, params object[] args)
59+
{
60+
if ((DateTime.UtcNow - Log.dtLastUpdate).TotalMilliseconds > 1000.0)
61+
{
62+
lock (Log.oLock)
63+
{
64+
File.AppendAllText(Log.sPath, string.Join("\r\n", Log.lstBuffer.ToArray()) + "\r\n");
65+
Log.lstBuffer.Clear();
66+
}
67+
Log.dtLastUpdate = DateTime.UtcNow;
68+
return;
69+
}
70+
71+
string sLine = FormatMessage(message, args);
72+
73+
lock (Log.oLock)
74+
{
75+
Log.lstBuffer.Add(sLine);
76+
if (Log.lstBuffer.Count > Log.maxBuffered)
77+
{
78+
Log.maxBuffered = Log.lstBuffer.Count;
79+
}
80+
}
81+
}
82+
83+
public static void Quit()
84+
{
85+
}
86+
}
87+
}
88+
89+

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class MountVM : ViewModelBase
5151
bool _isSlewingEast = false;
5252
bool _tryConnect = false;
5353
bool _driftAlignRunning = false;
54-
bool _runningOATCommand = false;
54+
int _runningOATCommand = 0;
5555
SemaphoreSlim exclusiveAccess = new SemaphoreSlim(1, 1);
5656
string _driftAlignStatus = "Drift Alignment";
5757
float _driftPhase = 0;
@@ -69,6 +69,7 @@ public class MountVM : ViewModelBase
6969
DelegateCommand _connectScopeCommand;
7070
DelegateCommand _slewToTargetCommand;
7171
DelegateCommand _syncToTargetCommand;
72+
DelegateCommand _syncToCurrentCommand;
7273
DelegateCommand _startSlewingCommand;
7374
DelegateCommand _stopSlewingCommand;
7475
DelegateCommand _homeCommand;
@@ -104,6 +105,7 @@ public MountVM()
104105
_connectScopeCommand = new DelegateCommand(() => OnConnectToTelescope(), () => _oatMount != null);
105106
_slewToTargetCommand = new DelegateCommand(async () => await OnSlewToTarget(), () => MountConnected);
106107
_syncToTargetCommand = new DelegateCommand(async () => await OnSyncToTarget(), () => MountConnected);
108+
_syncToCurrentCommand = new DelegateCommand(() => OnSyncToCurrent(), () => MountConnected);
107109
_startSlewingCommand = new DelegateCommand(async s => await OnStartSlewing(s.ToString()), () => MountConnected);
108110
_stopSlewingCommand = new DelegateCommand(async () => await OnStopSlewing('a'), () => MountConnected);
109111
_homeCommand = new DelegateCommand(async () => await OnHome(), () => MountConnected);
@@ -130,7 +132,7 @@ private void Log(string format, params object[] p)
130132
{
131133
var now = DateTime.UtcNow;
132134
var elapsed = now - _startTime;
133-
var timestamp = string.Format("[{0:00}:{1:00}:{2:00}.{3:00}] ", elapsed.Hours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds / 10);
135+
var timestamp = string.Format("[{0:00}:{1:00}:{2:00}.{3:00}] <{4}>:", elapsed.Hours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds / 10, Thread.CurrentThread.ManagedThreadId);
134136
Console.WriteLine(String.Format(timestamp + format, p));
135137
}
136138

@@ -157,11 +159,11 @@ async private Task<string> RunCustomOATCommandAsync(string command)
157159
string result = string.Empty;
158160
try
159161
{
160-
_runningOATCommand = true;
162+
Interlocked.Increment(ref _runningOATCommand);
161163
bool needsReturn = false;
162-
Log("OATCustom: StartRunning [{0}]", command);
164+
Log("OATCustom: StartRunning [{0}], awaiting access", command);
163165
await exclusiveAccess.WaitAsync();
164-
Log("OATCustom: -> {0} ", command);
166+
Log("OATCustom: Acces granted. -> Sending {0} ", command);
165167
var commandResult = await _oatMount.SendCommand(command);
166168
if (!string.IsNullOrEmpty(commandResult.Data))
167169
{
@@ -172,8 +174,8 @@ async private Task<string> RunCustomOATCommandAsync(string command)
172174
finally
173175
{
174176
Log("OATCustom: StopRunning [{0}]", command);
175-
_runningOATCommand = false;
176177
exclusiveAccess.Release();
178+
Interlocked.Decrement(ref _runningOATCommand);
177179
}
178180

179181
return result.TrimEnd("#".ToCharArray());
@@ -204,14 +206,15 @@ private async Task OnTimer(object s, EventArgs e)
204206
// }
205207
//}
206208

207-
if (!_runningOATCommand)
209+
if (Interlocked.CompareExchange(ref _runningOATCommand,1,0)==0)
208210
{
209211
Log("OATTimer: Not Running");
210212
if (MountConnected)
211213
{
212214
//UpdateCurrentCoordinates();
213215
await UpdateStatus();
214216
}
217+
Interlocked.Decrement(ref _runningOATCommand);
215218
}
216219
else
217220
{
@@ -334,6 +337,13 @@ private async Task OnSyncToTarget()
334337
await _oatMount.Sync(new TelescopePosition(1.0 * TargetRASecond / 3600.0 + 1.0 * TargetRAMinute / 60.0 + TargetRAHour, 1.0 * TargetDECSecond / 3600.0 + 1.0 * TargetDECMinute / 60.0 + TargetDECDegree, Epoch.JNOW));
335338
}
336339

340+
private void OnSyncToCurrent()
341+
{
342+
TargetRAHour = CurrentRAHour;
343+
TargetRAMinute = CurrentRAMinute;
344+
TargetRASecond = CurrentRASecond;
345+
}
346+
337347
private void FloatToHMS(double val, out int h, out int m, out int s)
338348
{
339349
h = (int)Math.Floor(val);
@@ -370,7 +380,8 @@ private async void OnConnectToTelescope()
370380
//await +.WaitAsync();
371381

372382
var result = await _oatMount.SendCommand("GVP#,#");
373-
ScopeName = result.Data;
383+
var resultNr = await _oatMount.SendCommand("GVN#,#");
384+
ScopeName = $"{result.Data} {resultNr.Data}";
374385

375386
_transform.SiteElevation = 100;// _oatMount.SiteElevation;
376387
_transform.SiteLatitude = 47.74; //_oatMount.SiteLatitude;
@@ -458,6 +469,7 @@ private void RequeryCommands()
458469
_connectScopeCommand.Requery();
459470
_slewToTargetCommand.Requery();
460471
_syncToTargetCommand.Requery();
472+
_syncToCurrentCommand.Requery();
461473
_startSlewingCommand.Requery();
462474
_stopSlewingCommand.Requery();
463475
_homeCommand.Requery();
@@ -542,6 +554,7 @@ private void OnAdjustTarget(string command)
542554
public ICommand ConnectScopeCommand { get { return _connectScopeCommand; } }
543555
public ICommand SlewToTargetCommand { get { return _slewToTargetCommand; } }
544556
public ICommand SyncToTargetCommand { get { return _syncToTargetCommand; } }
557+
public ICommand SyncToCurrentCommand { get { return _syncToCurrentCommand; } }
545558
public ICommand StartSlewingCommand { get { return _startSlewingCommand; } }
546559
public ICommand StopSlewingCommand { get { return _stopSlewingCommand; } }
547560
public ICommand HomeCommand { get { return _homeCommand; } }

0 commit comments

Comments
 (0)