1
1
using System ;
2
2
using System . Linq ;
3
+ using System . Runtime . CompilerServices ;
3
4
using System . Threading . Tasks ;
5
+ using Microsoft . Win32 . SafeHandles ;
4
6
using OATCommunications . CommunicationHandlers ;
5
7
using OATCommunications . Model ;
6
8
using OATCommunications . TelescopeCommandHandlers ;
@@ -22,9 +24,9 @@ public OatmealTelescopeCommandHandlers(ICommunicationHandler commHandler) {
22
24
public bool Connected { get { return _commHandler . Connected ; } }
23
25
24
26
public async Task < bool > RefreshMountState ( ) {
25
- var _slewingStates = new [ ] { "SlewToTarget" , "FreeSlew" , "ManualSlew" } ;
27
+ var _slewingStates = new [ ] { "SlewToTarget" , "FreeSlew" , "ManualSlew" } ;
26
28
27
- var status = await SendCommand ( ":GX#" ) ;
29
+ var status = await SendCommand ( ":GX#,# " ) ;
28
30
if ( ! status . Success ) {
29
31
return false ;
30
32
}
@@ -56,11 +58,11 @@ private double GetCompactRA(string part) {
56
58
}
57
59
58
60
public async Task < TelescopePosition > GetPosition ( ) {
59
- var ra = await SendCommand ( ":GR#" ) ;
60
- var dec = await SendCommand ( ":GD#" ) ;
61
+ var ra = await SendCommand ( ":GR#,# " ) ;
62
+ var dec = await SendCommand ( ":GD#,# " ) ;
61
63
62
- if ( ra . Success && dec . Success &&
63
- TryParseRA ( ra . Data , out double dRa ) &&
64
+ if ( ra . Success && dec . Success &&
65
+ TryParseRA ( ra . Data , out double dRa ) &&
64
66
TryParseDec ( dec . Data , out double dDec ) ) {
65
67
66
68
MountState . RightAscension = dRa ;
@@ -71,7 +73,15 @@ public async Task<TelescopePosition> GetPosition() {
71
73
MountState . RightAscension = 0 ;
72
74
MountState . Declination = 0 ;
73
75
return TelescopePosition . Invalid ;
74
-
76
+ }
77
+
78
+ private void FloatToHMS ( double val , out int h , out int m , out int s )
79
+ {
80
+ h = ( int ) Math . Floor ( val ) ;
81
+ val = ( val - h ) * 60 ;
82
+ m = ( int ) Math . Floor ( val ) ;
83
+ val = ( val - m ) * 60 ;
84
+ s = ( int ) Math . Round ( val ) ;
75
85
}
76
86
77
87
private bool TryParseRA ( string ra , out double dRa ) {
@@ -104,8 +114,18 @@ public async Task<bool> StopMoving(string dir) {
104
114
return status . Success ;
105
115
}
106
116
107
- public Task < bool > Slew ( TelescopePosition position ) {
108
- throw new NotImplementedException ( ) ;
117
+ public async Task < bool > Slew ( TelescopePosition position ) {
118
+ int deg , hour , min , sec ;
119
+
120
+ FloatToHMS ( Math . Abs ( position . Declination ) , out deg , out min , out sec ) ;
121
+ string sign = position . Declination < 0 ? "-" : "+" ;
122
+ var result = await SendCommand ( string . Format ( ":Sd{0}{1:00}*{2:00}:{3:00}#,#" , sign , deg , min , sec ) ) ;
123
+ if ( ! result . Success || result . Data != "1" ) return false ;
124
+ FloatToHMS ( Math . Abs ( position . RightAscension ) , out hour , out min , out sec ) ;
125
+ await SendCommand ( string . Format ( ":Sr{0:00}:{1:00}:{2:00}#,#" , hour , min , sec ) ) ;
126
+ if ( ! result . Success || result . Data != "1" ) return false ;
127
+ await SendCommand ( $ ":MS#") ;
128
+ return result . Success ;
109
129
}
110
130
111
131
public Task < bool > Sync ( TelescopePosition position ) {
@@ -125,7 +145,7 @@ public async Task<bool> SetHome()
125
145
126
146
public async Task < bool > SetTracking ( bool enabled ) {
127
147
var b = enabled ? 1 : 0 ;
128
- var status = await SendCommand ( $ ":MT{ b } #") ;
148
+ var status = await SendCommand ( $ ":MT{ b } #,# ") ;
129
149
if ( status . Success ) {
130
150
MountState . IsTracking = enabled ;
131
151
}
@@ -141,7 +161,7 @@ public async Task<bool> SetLocation(double lat, double lon, double altitudeInMet
141
161
}
142
162
int lonFront = ( int ) lon ;
143
163
int lonBack = ( int ) ( ( lon - lonFront ) * 100.0 ) ;
144
- var lonCmd = $ ":Sg{ lonFront : 000} *{ lonBack : 00} #";
164
+ var lonCmd = $ ":Sg{ lonFront : 000} *{ lonBack : 00} #,# ";
145
165
var status = await SendCommand ( lonCmd ) ;
146
166
if ( ! status . Success ) return false ;
147
167
@@ -151,36 +171,39 @@ public async Task<bool> SetLocation(double lat, double lon, double altitudeInMet
151
171
var absLat = Math . Abs ( lat ) ;
152
172
int latFront = ( int ) absLat ;
153
173
int latBack = ( int ) ( ( absLat - latFront ) * 100.0 ) ;
154
- var latCmd = $ ":St{ latSign } { latFront : 00} *{ latBack : 00} #";
174
+ var latCmd = $ ":St{ latSign } { latFront : 00} *{ latBack : 00} #,# ";
155
175
status = await SendCommand ( latCmd ) ;
156
176
if ( ! status . Success ) return false ;
157
177
158
178
159
179
// GMT Offset
160
180
var offsetSign = DateTimeOffset . Now . Offset . TotalHours > 0 ? "+" : "-" ;
161
181
var offset = Math . Abs ( DateTimeOffset . Now . Offset . TotalHours ) ;
162
- status = await SendCommand ( $ ":SG{ offsetSign } { offset : 00} #") ;
182
+ status = await SendCommand ( $ ":SG{ offsetSign } { offset : 00} #,# ") ;
163
183
if ( ! status . Success ) return false ;
164
184
165
185
166
186
// Local Time and Date
167
187
var n = DateTime . Now ;
168
- status = await SendCommand ( $ ":SL{ n : HH:mm:ss} #") ;
188
+ status = await SendCommand ( $ ":SL{ n : HH:mm:ss} #,# ") ;
169
189
if ( ! status . Success ) return false ;
170
- status = await SendCommand ( $ ":SC{ n : MM/dd/yy} #") ;
190
+ status = await SendCommand ( $ ":SC{ n : MM/dd/yy} #,# ") ;
171
191
return status . Success ;
172
192
}
173
193
174
194
public async Task < CommandResponse > SendCommand ( string cmd ) {
175
195
if ( ! cmd . StartsWith ( ":" ) ) {
176
196
cmd = $ ":{ cmd } ";
177
197
}
178
-
179
- if ( ! cmd . EndsWith ( "#" ) ) {
198
+
199
+ if ( ! cmd . EndsWith ( "#" ) ) {
180
200
cmd += "#" ;
181
201
}
182
-
183
- return await _commHandler . SendCommand ( cmd ) ;
202
+ if ( cmd . EndsWith ( "#,#" ) )
203
+ {
204
+ return await _commHandler . SendCommand ( cmd . Substring ( 0 , cmd . Length - 2 ) ) ;
205
+ }
206
+ return await _commHandler . SendBlind ( cmd ) ;
184
207
}
185
208
}
186
209
}
0 commit comments