-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDriver.Starbook.cs
1444 lines (1241 loc) · 57.5 KB
/
Driver.Starbook.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define Telescope
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using ASCOM;
using ASCOM.Astrometry;
using ASCOM.Astrometry.AstroUtils;
using ASCOM.DeviceInterface;
using ASCOM.Utilities;
namespace ASCOM.Starbook
{
public partial class Telescope : ITelescopeV3
{
[ComVisible(false)]
public class Starbook
{
WebClientTimeout web;
public Starbook(IPAddress ipAddress)
{
this.IPAddress = ipAddress;
this.J2000 = true;
this.web = new WebClientTimeout()
{
Timeout = 5000
};
}
/// <summary>
/// IP address of the starbook controller (Default is 169.254.1.1)
/// </summary>
///
public IPAddress IPAddress { get; set; }
/// <summary>
/// J2000 is used by the starbook controller (Default is True)
/// </summary>
///
public bool J2000 { get; set; }
/// <summary>
/// Get firmware version of the starbook controller.
/// </summary>
/// <param name="status">Firmware version which is formatted as [MAJORVERSION].[MINORVERSION]B[BUILDNUMBER]</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetVersion(out string version)
{
version = string.Empty; Response response = this.Handshake("version", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("VERSION", out version))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get status of the mount.
/// </summary>
/// <param name="status">
/// Status which contains the following fields:
/// [*] RA: Right Ascension which is formatted as [HH]+[MM.M]
/// [*] DEC: Declination which is formatted as [S][DDD]+[MM]
/// [*] STATE: State of starbook
/// [+] INIT (Power Up)
/// [+] GUIDE (?)
/// [+] SCOPE (User Presses OK or START)
/// [+] CHART (User Presses CHART)
/// [+] USER (User Presses MENU)
/// [*] GOTO: Slewing (1) or Not Slewing (0)
/// </param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetStatus(out Status status)
{
status = new Status(); Response response = this.Handshake("getstatus", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown)
{
bool statusRA = false, statusDEC = false, statusState = false, statusGoto = false;
foreach (KeyValuePair<string, string> item in dictionary)
{
switch (item.Key)
{
case "RA":
{
if (HMS.TryParse(item.Value, out HMS ra, 1))
{
status.RA = ra; statusRA = true;
}
break;
}
case "DEC":
{
if (DMS.TryParse(item.Value, out DMS dec, 0))
{
status.Dec = dec; statusDEC = true;
}
break;
}
case "STATE":
{
State state = this.ParseState(item.Value);
if (state != State.Unknown)
{
status.State = state; statusState = true;
}
break;
}
case "GOTO":
{
if (item.Value == "1")
{
status.Goto = true; statusGoto = true;
}
else if (item.Value == "0")
{
status.Goto = false; statusGoto = true;
}
break;
}
}
}
if (statusRA && statusDEC && statusState && statusGoto)
{
response.Assign(Response.OK);
}
}
return response;
}
/// <summary>
/// Get time of the mount.
/// </summary>
/// <param name="time">Time which is formatted as [YYYY]+[MM]+[DD]+[HH]+[MM]+[SS] (Local Time)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetTime(out DateTime time)
{
time = DateTime.MinValue; Response response = this.Handshake("gettime", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("TIME", out string s) && DateTime.TryParseExact(s, "yyyy+MM+dd+HH+mm+ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Set time of the mount. [*NOTE*] This command is valid in INIT state only.
/// </summary>
/// <param name="time">Time which is formatted as [YYYY]+[MM]+[DD]+[HH]+[MM]+[SS] (Local Time)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response SetTime(DateTime time)
{
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "settime?time={0:yyyy+MM+dd+HH+mm+ss}", time));
}
/// <summary>
/// Get place of the mount.
/// </summary>
/// <param name="place">
/// Place contains the following fields:
/// [*] LATITUDE: Latitude which is formatted as [N|S][DDD]+[MM]
/// [*] LONGITUDE: Longitude which is formatted as [E|W][DDD]+[MM]
/// [*] TIMEZONE: Timezone which is ranged from [-12] to [14]
/// </param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetPlace(out Place place)
{
place = new Place(); Response response = this.Handshake("getplace", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown)
{
bool placeLatitude = false, placeLongitude = false, placeTimezone = false;
foreach (KeyValuePair<string, string> item in dictionary)
{
switch (item.Key)
{
case "LATITUDE":
{
if (DMS.TryParse(item.Value, out DMS latitude, 0))
{
place.Latitude = latitude; placeLatitude = true;
}
break;
}
case "LONGITUDE":
{
if (DMS.TryParse(item.Value, out DMS longitude, 0))
{
place.Longitude = longitude; placeLongitude = true;
}
break;
}
case "TIMEZONE":
{
if (int.TryParse(item.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out int timezone))
{
place.Timezone = timezone; placeTimezone = true;
}
break;
}
}
}
if (placeLatitude && placeLongitude && placeTimezone)
{
response.Assign(Response.OK);
}
}
return response;
}
/// <summary>
/// Set place of the mount. [*NOTE*] This command is valid in INIT state only.
/// </summary>
/// <param name="place">Place structure which is described above</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response SetPlace(Place place)
{
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "setplace?latitude={0}&longitude={1}&timezone={2:00}", place.Latitude, place.Longitude, place.Timezone));
}
/// <summary>
/// Move the mount to home position. [*NOTE*] This command is invalid in INIT state.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Home()
{
return this.Handshake("gohome?home=0");
}
/// <summary>
/// Move the mount in one of four directions continuously. [*NOTE*] This command is invalid in INIT state.
/// </summary>
/// <param name="direction">Direction to move (NORTH, SOUTH, WEST, EAST)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Move(Direction direction)
{
switch (direction)
{
case Direction.North:
return this.Handshake("move?north=1&south=0&east=0&west=0");
case Direction.South:
return this.Handshake("move?north=0&south=1&east=0&west=0");
case Direction.East:
return this.Handshake("move?north=0&south=0&east=1&west=0");
case Direction.West:
return this.Handshake("move?north=0&south=0&east=0&west=1");
default:
return Response.ErrorFormat;
}
}
/// <summary>
/// Stop moving the mount. [*NOTE*] This command is invalid in INIT state.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response NoMove()
{
return this.Handshake("move?north=0&south=0&east=0&west=0");
}
/// <summary>
/// Move the mount to specitfied celestial coordinate. [*NOTE*] This command is invalid in INIT state.
/// </summary>
/// <param name="ra">Right Ascension which is formatted as [HH]+[MM.M] ([HH]+[MM.MMMMM] if extended)</param>
/// <param name="dec">Declination which is formatted as [S][DDD]+[MM] ([S][DDD]+[MM.MMMMM] if extended)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Goto(HMS ra, DMS dec)
{
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "gotoradec?ra={0}&dec={1}", ra, dec));
}
/// <summary>
/// Stop moving the mount. This forces to stop the mount in MOVE/GOTO state, but NoMove() is also required when the mount
/// is in GOTO state. [*NOTE*] This command is invalid in INIT state.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Stop()
{
return this.Handshake("stop");
}
/// <summary>
/// Hibernate the mount. Use Start() to restart the mount.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Reset()
{
return this.Handshake("reset?reset");
}
/// <summary>
/// Start/Restart the mount to let it transit from INIT to SCOPE state.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Start()
{
return this.Handshake("start");
}
/// <summary>
/// Align/Sync the mount with the specified celesctial coordinate. [*NOTE*] This command is invalid in INIT state.
/// </summary>
/// <param name="ra">Right Ascension which is formatted as [HH]+[MM.M] ([HH]+[MM.MMM] if extended)</param>
/// <param name="dec">Declination which is formatted as [S][DDD]+[MM] ([S][DDD]+[MM.MMM] if extended)</param>
/// <returns>Response string: OK or ERROR:%</returns>
public Response Align(HMS ra, DMS dec)
{
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "align?ra={0}&dec={1}", ra, dec));
}
/// <summary>
/// Set the speed of mount. This affects the behaviour of MOVE()
/// </summary>
/// <param name="speed">Speed which is ranged from [0] to [8]</param>
/// <returns>Response string: OK or ERROR:%</returns>
public Response SetSpeed(int speed)
{
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "setspeed?speed={0}", speed));
}
/// <summary>
/// Get granularity of encoded value of the mount.
/// </summary>
/// <param name="round">Granularity of encoded value</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetRound(out int round)
{
round = 0; Response response = this.Handshake("getround", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("ROUND", out string s) && int.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture, out round))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get encoded value of X/Y coordinate of the mount.
/// </summary>
/// <param name="xy">Encoded value of X/Y coordinate</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetXY(out XY xy)
{
xy = new XY(); Response response = this.Handshake("getxy", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown)
{
bool xyX = false, xyY = false;
foreach (KeyValuePair<string, string> item in dictionary)
{
switch (item.Key)
{
case "X":
{
if (int.TryParse(item.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out int x))
{
xy.X = x; xyX = true;
}
break;
}
case "Y":
{
if (int.TryParse(item.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out int y))
{
xy.Y = y; xyY = true;
}
break;
}
}
}
if (xyX && xyY)
{
response.Assign(Response.OK);
}
}
return response;
}
/// <summary>
/// Save setting of the mount.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Save()
{
return this.Handshake("savesetting");
}
/// <summary>
/// Get screenshot of starbook controller.
/// </summary>
/// <param name="width">Width of sceenshot (Default is 160 pixels)</param>
/// <param name="height">Height of sceenshot (Default is 160 pixels)</param>
/// <param name="color">Color or monochrome (Default is monochrome)</param>
/// <returns>Screenshot as Bitmap object</returns>
///
public Bitmap Screenshot(int width = 160, int height = 160, bool color = false)
{
Bitmap screenshot = new Bitmap(width, height);
byte[] bytes = this.HandshakeBytes("getscreen.bin", false);
int byteCount = (width * height * 12 + 7) / 8;
if (bytes.Length == byteCount)
{
byte[][] screenshotBytes = new byte[height][];
int screenshotBytesStride = width * 3;
for (int y = 0; y < height; y++)
{
screenshotBytes[y] = new byte[screenshotBytesStride];
}
if (color)
{
for (int index = 0, x = 0, y = 0, z = 0; index < bytes.Length - 2; )
{
byte byte1 = bytes[index++];
byte byte2 = bytes[index++];
byte byte3 = bytes[index++];
screenshotBytes[y][z++] = (byte)((byte2 & 0x0F) << 4);
screenshotBytes[y][z++] = (byte)((byte1 & 0xF0) << 0);
screenshotBytes[y][z++] = (byte)((byte1 & 0x0F) << 4); if (++x >= width) { if (++y >= height) break; x = z = 0; }
screenshotBytes[y][z++] = (byte)((byte3 & 0xF0) << 0);
screenshotBytes[y][z++] = (byte)((byte3 & 0x0F) << 4);
screenshotBytes[y][z++] = (byte)((byte2 & 0xF0) << 0); if (++x >= width) { if (++y >= height) break; x = z = 0; }
}
}
else
{
for (int index = 0, x = 0, y = 0, z = 0; index < bytes.Length; index++)
{
byte byte1 = (byte)((bytes[index] & 0x0F) << 4);
byte byte2 = (byte)((bytes[index] & 0xF0) << 0);
screenshotBytes[y][z++] = byte1;
screenshotBytes[y][z++] = byte1;
screenshotBytes[y][z++] = byte1; if (++x >= width) { if (++y >= height) break; x = z = 0; }
screenshotBytes[y][z++] = byte2;
screenshotBytes[y][z++] = byte2;
screenshotBytes[y][z++] = byte2; if (++x >= width) { if (++y >= height) break; x = z = 0; }
}
}
BitmapData screenshotData = screenshot.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
if (screenshotData != null)
{
IntPtr screenshotDataScan = screenshotData.Scan0;
int screenshotDataStride = screenshotData.Stride;
for (int y = 0; y < height; y++)
{
Marshal.Copy(screenshotBytes[y], 0, screenshotDataScan, screenshotBytesStride);
screenshotDataScan = IntPtr.Add(screenshotDataScan, screenshotDataStride);
}
screenshot.UnlockBits(screenshotData);
screenshot.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
}
return screenshot;
}
/// <summary>
/// Issue a low-level command defined by starbook controller.
/// </summary>
/// <param name="command">Command string</param>
/// <returns>Response string</returns>
///
public string Command(string command)
{
return this.HandshakeString(command);
}
#region Extended Methods (Supported by Starbook Ten)
/// <summary>
/// Calculate the pierside of mount.
/// </summary>
/// <param name="pierSide">Pierside of mount</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response CalcSideOfPier(double ra, double dec, out PierSide pierSide)
{
pierSide = PierSide.Unknown; Response response = this.Handshake(string.Format(CultureInfo.InvariantCulture, "calc_sideofpier?ra={0}&dec={1}", ra, dec), out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("PIERSIDE", out string s) && TryParsePierSide(s, out pierSide))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get the altitude and azimuth of mount.
/// </summary>
/// <param name="altitude">Altitude of mount (DMS)</param>
/// <param name="azimuth">Azimuth of mount (DMS)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetAltAz(out double altitude, out double azimuth)
{
altitude = azimuth = 0; Response response = this.Handshake("getaltaz", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("ALTITUDE", out string s) && double.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture, out altitude) &&
dictionary.TryGetValue("AZIMUTH", out s) && double.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture, out azimuth))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get the pulse guide statuses of mount.
/// </summary>
/// <param name="rap">Pulse guide status of RA+</param>
/// <param name="ran">Pulse guide status of RA-</param>
/// <param name="decp">Pulse guide status of DEC+</param>
/// <param name="decn">Pulse guide status of DEC-</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetGuideStatus(out bool rap, out bool ran, out bool decp, out bool decn)
{
rap = ran = decp = decn = false; Response response = this.Handshake("getguidestatus", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("RA+", out string s) && TryParseState(s, out rap) &&
dictionary.TryGetValue("RA-", out s) && TryParseState(s, out ran) &&
dictionary.TryGetValue("DEC+", out s) && TryParseState(s, out decp) &&
dictionary.TryGetValue("DEC-", out s) && TryParseState(s, out decn))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get the pierside of mount.
/// </summary>
/// <param name="pierSide">Pierside of mount</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetPierSide(out PierSide pierSide)
{
pierSide = PierSide.Unknown; Response response = this.Handshake("get_pierside", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("PIERSIDE", out string s) && TryParsePierSide(s, out pierSide))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get the code of mount.
/// </summary>
/// <param name="type">Code of mount</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetMountCode(out string code)
{
code = string.Empty; Response response = this.Handshake("getmountcode", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("CODE"/*TBD*/, out code))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get the RA/DEC type of mount.
/// </summary>
/// <param name="type">RA/DEC type of mount</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetRADecType(out RADecType type)
{
type = RADecType.Unknown; Response response = this.Handshake("getradectype", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("TYPE", out string s) && TryParseRADecType(s, out type))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Get the status of mount (v2)
/// </summary>
/// <param name="status">
/// Status which contains the following fields:
/// [*] RA: Right Ascension which is formatted as [HH.HHHHHH]
/// [*] DEC: Declination which is formatted as [S][DDD.DDDDDD]
/// [*] STATE: State of starbook
/// [+] INIT (Power Up)
/// [+] GUIDE (?)
/// [+] SCOPE (User Presses OK or START)
/// [+] CHART (User Presses CHART)
/// [+] USER (User Presses MENU)
/// [*] GOTO: Slewing (1) or Not Slewing (0)
/// </param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetStatus2(out Status status)
{
status = new Status(); Response response = this.Handshake("getstatus2", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown)
{
bool statusRA = false, statusDEC = false, statusState = false, statusGoto = false;
foreach (KeyValuePair<string, string> item in dictionary)
{
switch (item.Key)
{
case "RA":
{
if (double.TryParse(item.Value, out double value) && HMS.FromValue(value, out HMS ra, 5))
{
status.RA = ra; statusRA = true;
}
break;
}
case "DEC":
{
if (double.TryParse(item.Value, out double value) && DMS.FromValue(value, out DMS dec, 5))
{
status.Dec = dec; statusDEC = true;
}
break;
}
case "STATE":
{
State state = this.ParseState(item.Value);
if (state != State.Unknown)
{
status.State = state; statusState = true;
}
break;
}
case "GOTO":
{
if (item.Value == "1")
{
status.Goto = true; statusGoto = true;
}
else if (item.Value == "0")
{
status.Goto = false; statusGoto = true;
}
break;
}
}
}
if (statusRA && statusDEC && statusState && statusGoto)
{
response.Assign(Response.OK);
}
}
return response;
}
/// <summary>
/// Get the track status of mount.
/// </summary>
/// <param name="track">Track status of mount</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GetTrackStatus(out bool track)
{
track = false; Response response = this.Handshake("gettrackstatus", out Dictionary<string, string> dictionary);
if (response == Response.ErrorUnknown && dictionary.TryGetValue("TRACK", out string s) && TryParseState(s, out track))
{
response.Assign(Response.OK);
}
return response;
}
/// <summary>
/// Goto the set parking position and park the mount.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response GotoPark()
{
return this.Handshake("goto_park");
}
/// <summary>
/// Move the mount along one of two axes in the specified rate.
/// </summary>
/// <param name="axis">Axis to move</param>
/// <param name="rate">Rate to move (DMS/sec)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response MoveAxis(Axis axis, double rate)
{
switch (axis)
{
case Axis.Primary:
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "move_axis?axis=0&rate={0}", rate));
case Axis.Secondary:
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "move_axis?axis=1&rate={0}", rate));
default:
return Response.ErrorFormat;
}
}
/// <summary>
/// Move pulse the mount in one of four directions for the specified duration.
/// </summary>
/// <param name="direction">Direction to move (NORTH, SOUTH, WEST, EAST)</param>
/// <param name="duration">Duration to move (milliseconds)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response MovePulse(Direction direction, double duration)
{
switch (direction)
{
case Direction.North:
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "movepulse?direct=0&duration={0}", duration));
case Direction.South:
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "movepulse?direct=1&duration={0}", duration));
case Direction.East:
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "movepulse?direct=2&duration={0}", duration));
case Direction.West:
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "movepulse?direct=3&duration={0}", duration));
default:
return Response.ErrorFormat;
}
}
/// <summary>
/// Set the current altitude and azimuth as parking position.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response SetPark()
{
return this.Handshake("set_park");
}
/// <summary>
/// Set the speed of mount. This affects the behaviour of MOVEPULSE()
/// </summary>
/// <param name="ra">Speed of RA (DMS/hr)</param>
/// <param name="dec">Speed of DEC (DMS/hr)</param>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response SetPulseSpeed(double ra, double dec)
{
return this.Handshake(string.Format(CultureInfo.InvariantCulture, "setpulsespeed?ra={0}&dec={1}", ra, dec));
}
/// <summary>
/// Unpark the mount.
/// </summary>
/// <returns>Response string: OK or ERROR:%</returns>
///
public Response Unpark()
{
return this.Handshake("unpark");
}
/////
private bool TryParseState(string s, out bool state)
{
switch (s)
{
case "0":
state = false; return true;
case "1":
case "2": /* ??? */
state = true; return true;
default:
state = false; return false;
}
}
private bool TryParsePierSide(string s, out PierSide pierSide)
{
switch (s)
{
case "0":
pierSide = PierSide.West; return true;
case "1":
pierSide = PierSide.East; return true;
default:
pierSide = PierSide.Unknown; return false;
}
}
private bool TryParseRADecType(string s, out RADecType type)
{
switch (s)
{
case "J2000":
type = RADecType.J2000; return true;
case "NOW":
type = RADecType.Now; return true;
default:
type = RADecType.Unknown; return false;
}
}
[ComVisible(false)]
public enum Axis
{
Unknown, Primary, Secondary
}
[ComVisible(false)]
public enum PierSide
{
Unknown, West, East
}
[ComVisible(false)]
public enum RADecType
{
Unknown, J2000, Now
}
#endregion
#region Private Methods
private Response Handshake(string command)
{
string reply = this.HandshakeString(command);
Response response = ParseResponse(reply);
response.Command = command;
response.Reply = reply;
return response;
}
private Response Handshake(string command, out Dictionary<string, string> dictionary)
{
dictionary = new Dictionary<string, string>(); Response response = this.Handshake(command);
if (response == Response.ErrorUnknown)
{
MatchCollection matches = Regex.Matches(response.Reply, @"(?<Name>[^&=]+)=(?<Value>[^&]+)");
foreach (Match match in matches)
{
string name = match.Groups["Name"].Value.ToUpper();
string value = match.Groups["Value"].Value;
dictionary[name] = value;
}
}
return response;
}
private string HandshakeString(string command)
{
string strinq;
try
{
lock (this.web)
{
strinq = this.web.DownloadString(string.Format(CultureInfo.InvariantCulture, "http://{0}/{1}", this.IPAddress, command));
}
}
catch
{
strinq = null;
}
if (string.IsNullOrEmpty(strinq))
{
return "ERROR:NETWORK";
}
Match match = Regex.Match(strinq, @"<!--(?<String>.*)-->");
if (match.Success)
{
strinq = match.Groups["String"].Value;
}
strinq = Regex.Replace(strinq, @"^<[^>]+>", string.Empty);
strinq = Regex.Replace(strinq, @"</[^>]+>$", string.Empty);
return strinq;
}
private byte[] HandshakeBytes(string command, bool web = true)
{
byte[] bytes;
try
{
if (web)
{
lock (this.web)
{
bytes = this.web.DownloadData(string.Format(CultureInfo.InvariantCulture, "http://{0}/{1}", this.IPAddress, command));
}
}
else
{
using (TcpClient tcpClient = new TcpClient())
{
tcpClient.Connect(new IPEndPoint(this.IPAddress, 80));
using (NetworkStream networkStream = tcpClient.GetStream())
{
byte[] request = Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "GET /{0} HTTP/1.1\r\n\r\n", command));
networkStream.Write(request, 0, request.Length);
networkStream.Flush();
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] packet = new byte[8192];
while (true)
{
int read = networkStream.Read(packet, 0, packet.Length);
if (read <= 0)
{
break;
}
memoryStream.Write(packet, 0, read);
}
bytes = memoryStream.ToArray();
}
}
}
}
}
catch