-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandHandler.cpp
More file actions
854 lines (726 loc) · 35.4 KB
/
CommandHandler.cpp
File metadata and controls
854 lines (726 loc) · 35.4 KB
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
#include "CommandHandler.h"
#include <QJsonDocument>
#include <QDateTime>
#include <QDebug>
#include <QJsonArray>
#include <libnova/transform.h>
#include <libnova/julian_day.h>
CommandHandler::CommandHandler(TelescopeState *state, QObject *parent)
: QObject(parent), m_telescopeState(state) {
}
void CommandHandler::processCommand(const QJsonObject &obj, WebSocketConnection *wsConn) {
QString command = obj["Command"].toString();
QString destination = obj["Destination"].toString();
int sequenceId = obj["SequenceID"].toInt();
QString source = obj["Source"].toString();
QString type = obj["Type"].toString();
if (false) qDebug() << "Processing command:" << command << "to" << destination << "from" << source;
// Handle different commands
if (command == "RunInitialize") {
handleRunInitialize(obj, wsConn, sequenceId, source, destination);
} else if (command == "StartAlignment") {
handleStartAlignment(obj, wsConn, sequenceId, source, destination);
} else if (command == "AddAlignmentPoint") {
handleAddAlignmentPoint(obj, wsConn, sequenceId, source, destination);
} else if (command == "FinishAlignment") {
handleFinishAlignment(obj, wsConn, sequenceId, source, destination);
} else if (command == "GotoRaDec") {
handleGotoRaDec(obj, wsConn, sequenceId, source, destination);
} else if (command == "AbortAxisMovement") {
handleAbortAxisMovement(obj, wsConn, sequenceId, source, destination);
} else if (command == "StartTracking") {
handleStartTracking(obj, wsConn, sequenceId, source, destination);
} else if (command == "StopTracking") {
handleStopTracking(obj, wsConn, sequenceId, source, destination);
} else if (command == "RunImaging") {
handleRunImaging(obj, wsConn, sequenceId, source, destination);
} else if (command == "RunSampleCapture") {
handleRunSampleCapture(obj, wsConn, sequenceId, source, destination);
} else if (command == "CancelImaging") {
handleCancelImaging(obj, wsConn, sequenceId, source, destination);
} else if (command == "MoveToPosition" && destination == "Focuser") {
handleMoveToPosition(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetListOfAvailableDirectories" && destination == "ImageServer") {
handleGetDirectoryList(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetDirectoryContents" && destination == "ImageServer") {
handleGetDirectoryContents(obj, wsConn, sequenceId, source, destination);
} else if (command == "SetCaptureParameters") {
handleSetCaptureParameters(obj, wsConn, sequenceId, source, destination);
} else if (command == "SetBacklash" && destination == "Focuser") {
handleSetFocuserBacklash(obj, wsConn, sequenceId, source, destination);
} else if (command == "SetMode" && destination == "DewHeater") {
handleSetDewHeaterMode(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetSerialNumber" && destination == "FactoryCalibrationController") {
handleGetSerialNumber(obj, wsConn, sequenceId, source, destination);
} else if (command == "HasUpdateAvailable" && destination == "System") {
handleHasUpdateAvailable(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetUpdateChannel" && destination == "System") {
handleGetUpdateChannel(obj, wsConn, sequenceId, source, destination);
} else if (command == "SetRegulatoryDomain" && destination == "Network") {
handleSetRegulatoryDomain(obj, wsConn, sequenceId, source, destination);
} else if (command == "HasInternetConnection" && destination == "Network") {
handleHasInternetConnection(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetForceDirectConnect" && destination == "Network") {
handleGetForceDirectConnect(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetCameraInfo" && destination == "Camera") {
handleGetCameraInfo(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetSensors" && destination == "Environment") {
handleGetSensors(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetBrightnessLevel" && destination == "LedRing") {
handleGetBrightnessLevel(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetFocuserAdvancedSettings" && destination == "Focuser") {
handleGetFocuserAdvancedSettings(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetMountConfig" && destination == "Mount") {
handleGetMountConfig(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetPositionLimits" && destination == "Focuser") {
handleGetPositionLimits(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetEnableManual" && destination == "LiveStream") {
handleGetEnableManual(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetFilter" && destination == "Camera") {
handleGetFilter(obj, wsConn, sequenceId, source, destination);
} else if (command == "GetDirectConnectPassword" && destination == "Network") {
handleGetDirectConnectPassword(obj, wsConn, sequenceId, source, destination);
} else if (command == "Slew" && destination == "Mount") {
handleSlew(obj, wsConn, sequenceId, source, destination);
} else {
// Default response for unimplemented commands
QJsonObject response;
response["Command"] = command;
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
}
// In CommandHandler.cpp
void CommandHandler::handleRunInitialize(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
// Update telescope state
m_telescopeState->dateTime = QDateTime::currentDateTime();
if (obj.contains("Date")) m_telescopeState->dateTime.setDate(QDate::fromString(obj["Date"].toString(), "dd MM yyyy"));
if (obj.contains("Time")) m_telescopeState->dateTime.setTime(QTime::fromString(obj["Time"].toString(), "hh:mm:ss"));
if (obj.contains("Latitude")) m_telescopeState->latitude = obj["Latitude"].toDouble();
if (obj.contains("Longitude")) m_telescopeState->longitude = obj["Longitude"].toDouble();
if (obj.contains("TimeZone")) m_telescopeState->timeZone = obj["TimeZone"].toString();
// Set fake initialization flag if specified
if (obj.contains("FakeInitialize")) {
m_telescopeState->isFakeInitialized = obj["FakeInitialize"].toBool();
} else {
m_telescopeState->isFakeInitialized = false;
}
// Start the initialization process
m_telescopeState->isInitializing = false;
m_telescopeState->initializationProgress = 0;
m_telescopeState->state = "INITIALIZED";
m_telescopeState->stage = "FINISHED";
m_telescopeState->isReady = true;
// Reset initialization info
m_telescopeState->initInfo.numPoints = 2;
m_telescopeState->initInfo.positionOfFocus = -1;
m_telescopeState->initInfo.numPointsRemaining = 0;
m_telescopeState->initInfo.percentComplete = 100;
m_telescopeState->isAligned = true;
// Send immediate response
QJsonObject response;
response["Command"] = "RunInitialize";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
// Emit signal to start the initialization simulation
emit initializationStarted(obj.contains("FakeInitialize") && obj["FakeInitialize"].toBool());
}
void CommandHandler::handleStartAlignment(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->isAligned = false;
m_telescopeState->numAlignRefs = 0;
QJsonObject response;
response["Command"] = "StartAlignment";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleAddAlignmentPoint(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->numAlignRefs++;
QJsonObject response;
response["Command"] = "AddAlignmentPoint";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleFinishAlignment(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
if (m_telescopeState->numAlignRefs >= 1) {
m_telescopeState->isAligned = true;
}
QJsonObject response;
response["Command"] = "FinishAlignment";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGotoRaDec(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
if (m_telescopeState->isAligned) {
m_telescopeState->isGotoOver = false;
m_telescopeState->isSlewing = true;
m_telescopeState->syncTracking();
// ADD THESE DEBUG LINES:
double received_ra = obj["Ra"].toDouble();
double received_dec = obj["Dec"].toDouble();
if (true) qDebug() << "*** GOTO COMMAND RECEIVED ***";
if (false) qDebug() << "Raw RA from JSON:" << received_ra << "radians";
if (false) qDebug() << "Raw Dec from JSON:" << received_dec << "radians";
if (true) qDebug() << "RA in hours:" << (received_ra * 12.0 / M_PI);
if (true) qDebug() << "Dec in degrees:" << (received_dec * 180.0 / M_PI);
m_telescopeState->targetRa = received_ra;
m_telescopeState->targetDec = received_dec;
if (false) qDebug() << "Stored targetRa:" << m_telescopeState->targetRa;
if (false) qDebug() << "Stored targetDec:" << m_telescopeState->targetDec;
emit slewStarted();
QJsonObject response;
response["Command"] = "GotoRaDec";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
} else {
QJsonObject response;
response["Command"] = "GotoRaDec";
response["Destination"] = source;
response["ErrorCode"] = 1;
response["ErrorMessage"] = "Telescope not aligned";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
}
void CommandHandler::handleSlew(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
if (m_telescopeState->isAligned) {
m_telescopeState->isGotoOver = false;
m_telescopeState->isSlewing = true;
m_telescopeState->syncTracking();
if (false) qDebug() << "*** SLEW COMMAND RECEIVED ***";
// Iterate with auto and structured bindings (C++17)
if (false) for (auto it = obj.begin(); it != obj.end(); ++it) {
QString key = it.key();
QJsonValue value = it.value();
// Process key and value
if (true) qDebug() << key << ": " << value;
}
long alt_rate = obj["AltRate"].toInt();
long az_rate = obj["AzmRate"].toInt();
alt_rate = alt_rate < 0 ? -1 << -alt_rate : (1 << alt_rate) - 1;
az_rate = az_rate < 0 ? -1 << -az_rate : (1 << az_rate) - 1;
if (true) qDebug() << alt_rate << " " << az_rate;
// Your RA/Dec coordinates
struct ln_equ_posn equ_pos;
equ_pos.ra = m_telescopeState->targetRa * 180.0 / M_PI;
// Right Ascension in degrees
equ_pos.dec = m_telescopeState->targetDec * 180.0 / M_PI;
// Declination in degrees
// Observer location
struct ln_lnlat_posn observer;
observer.lng = m_telescopeState->longitude; // Longitude (negative for West)
observer.lat = m_telescopeState->latitude; // Latitude (positive for North)
// Get current Julian Date
double JD = ln_get_julian_from_sys();
// Convert to Alt/Az
struct ln_hrz_posn hrz_pos;
ln_get_hrz_from_equ(&equ_pos, &observer, JD, &hrz_pos);
hrz_pos.az += alt_rate / 3600.0;
hrz_pos.alt += az_rate / 3600.0;
// Convert Alt/Az to RA/Dec
ln_get_equ_from_hrz(&hrz_pos, &observer, JD, &equ_pos);
if (true) qDebug() << "Old Incremental targetRa/Dec:"
<< m_telescopeState->targetRa << " "
<< m_telescopeState->targetDec;
m_telescopeState->targetRa = equ_pos.ra * M_PI / 180.0;
m_telescopeState->targetDec = equ_pos.dec * M_PI / 180.0;
if (true) qDebug() << "New Incremental targetRa/Dec:"
<< m_telescopeState->targetRa << " "
<< m_telescopeState->targetDec;
emit slewStarted();
QJsonObject response;
response["Command"] = "Slew";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
} else {
QJsonObject response;
response["Command"] = "Slew";
response["Destination"] = source;
response["ErrorCode"] = 1;
response["ErrorMessage"] = "Telescope not aligned";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
}
void CommandHandler::handleAbortAxisMovement(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->isGotoOver = true;
m_telescopeState->isSlewing = false;
QJsonObject response;
response["Command"] = "AbortAxisMovement";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleStartTracking(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->isTracking = true;
QJsonObject response;
response["Command"] = "StartTracking";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleStopTracking(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->isTracking = false;
QJsonObject response;
response["Command"] = "StopTracking";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleRunImaging(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->isImaging = true;
m_telescopeState->imagingTimeLeft = 30;
emit imagingStarted();
QJsonObject response;
response["Command"] = "RunImaging";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleCancelImaging(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
m_telescopeState->isImaging = false;
QJsonObject response;
response["Command"] = "CancelImaging";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleMoveToPosition(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
int targetPosition = obj["Position"].toInt();
m_telescopeState->position = targetPosition;
QJsonObject response;
response["Command"] = "MoveToPosition";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetDirectoryList(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetListOfAvailableDirectories";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = 0;
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
QJsonArray dirList;
for (const QString &dir : m_telescopeState->astrophotographyDirs) {
dirList.append(dir);
}
response["DirectoryList"] = dirList;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetDirectoryContents(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QString dir = obj["Directory"].toString();
QJsonObject response;
response["Command"] = "GetDirectoryContents";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = 0;
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
QJsonArray fileList;
fileList.append("frame_1.jpg");
fileList.append("frame_2.jpg");
fileList.append("frame_3.jpg");
fileList.append("FinalStackedMaster.tiff");
response["FileList"] = fileList;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleSetCaptureParameters(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
// Update camera parameters from the command
if (obj.contains("Exposure")) m_telescopeState->exposure = obj["Exposure"].toDouble();
if (obj.contains("ISO")) m_telescopeState->iso = obj["ISO"].toInt();
if (obj.contains("Binning")) m_telescopeState->binning = obj["Binning"].toInt();
if (obj.contains("Offset")) m_telescopeState->offset = obj["Offset"].toInt();
if (obj.contains("ColorRBalance")) m_telescopeState->colorRBalance = obj["ColorRBalance"].toDouble();
if (obj.contains("ColorGBalance")) m_telescopeState->colorGBalance = obj["ColorGBalance"].toDouble();
if (obj.contains("ColorBBalance")) m_telescopeState->colorBBalance = obj["ColorBBalance"].toDouble();
QJsonObject response;
response["Command"] = "SetCaptureParameters";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleSetFocuserBacklash(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
if (obj.contains("Backlash")) {
m_telescopeState->backlash = obj["Backlash"].toInt();
}
QJsonObject response;
response["Command"] = "SetBacklash";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleSetDewHeaterMode(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
if (obj.contains("Mode")) {
m_telescopeState->mode = obj["Mode"].toString();
}
if (obj.contains("Aggression")) {
m_telescopeState->aggression = obj["Aggression"].toInt();
}
if (obj.contains("ManualPowerLevel")) {
m_telescopeState->manualPowerLevel = obj["ManualPowerLevel"].toDouble();
}
QJsonObject response;
response["Command"] = "SetMode";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetSerialNumber(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetSerialNumber";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["SerialNumber"] = "OTU140020"; // Example serial number
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleHasUpdateAvailable(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "HasUpdateAvailable";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["Available"] = false;
response["Version"] = "";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetUpdateChannel(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetUpdateChannel";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["Channel"] = "Release";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleSetRegulatoryDomain(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QString countryCode = obj["CountryCode"].toString();
m_telescopeState->countryCode = countryCode;
QJsonObject response;
response["Command"] = "SetRegulatoryDomain";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleHasInternetConnection(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "HasInternetConnection";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["Connected"] = true;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetForceDirectConnect(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetForceDirectConnect";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["ForceDirectConnect"] = false;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetCameraInfo(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetCameraInfo";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["ModelName"] = "Origin Camera";
response["SensorWidth"] = 14.8;
response["SensorHeight"] = 11.1;
response["PixelSize"] = 4.63;
response["EffectiveFocalLength"] = 700;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetSensors(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetSensors";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
QJsonArray sensors;
sensors.append("AMBIENT_TEMPERATURE");
sensors.append("HUMIDITY");
sensors.append("DEW_POINT");
sensors.append("FRONT_CELL_TEMPERATURE");
sensors.append("CPU_TEMPERATURE");
sensors.append("CAMERA_TEMPERATURE");
response["Sensors"] = sensors;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetBrightnessLevel(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetBrightnessLevel";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["Level"] = 50;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetFocuserAdvancedSettings(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetFocuserAdvancedSettings";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["BacklashSteps"] = 255;
response["DefaultSpeed"] = 250;
response["DefaultAcceleration"] = 800;
response["DirectionToggleDelayMs"] = 500;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetMountConfig(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetMountConfig";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["MaximumSpeed"] = 3.0;
response["SlewSettleTime"] = 1.0;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetPositionLimits(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetPositionLimits";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["MaximumPosition"] = 40000;
response["MinimumPosition"] = 0;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetEnableManual(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetEnableManual";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["EnableManual"] = true;
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetFilter(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetFilter";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["Filter"] = "Clear";
sendJsonResponse(wsConn, response);
}
void CommandHandler::handleGetDirectConnectPassword(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
QJsonObject response;
response["Command"] = "GetDirectConnectPassword";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
response["Password"] = "celestron"; // Default password
sendJsonResponse(wsConn, response);
}
void CommandHandler::sendJsonResponse(WebSocketConnection *wsConn, const QJsonObject &response) {
QJsonDocument doc(response);
QString message = doc.toJson();
wsConn->sendTextMessage(message);
}
void CommandHandler::handleRunSampleCapture(const QJsonObject &obj, WebSocketConnection *wsConn, int sequenceId, const QString &source, const QString &destination) {
// Extract exposure parameters
double exposureTime = obj["ExposureTime"].toDouble();
int iso = obj["ISO"].toInt();
qDebug() << "RunSampleCapture: Exposure=" << exposureTime << "ISO=" << iso;
// Update telescope state with camera parameters
m_telescopeState->exposure = exposureTime;
m_telescopeState->iso = iso;
m_telescopeState->isImaging = true;
m_telescopeState->imagingTimeLeft = static_cast<int>(exposureTime) + 2; // Exposure + processing time
// Set TaskController state for sample capture
m_telescopeState->state = "SAMPLE_CAPTURE";
m_telescopeState->stage = "IN_PROGRESS";
m_telescopeState->isReady = true;
// Send immediate response
QJsonObject response;
response["Command"] = "RunSampleCapture";
response["Destination"] = source;
response["ErrorCode"] = 0;
response["ErrorMessage"] = "";
response["ExpiredAt"] = QDateTime::currentDateTime().toSecsSinceEpoch();
response["SequenceID"] = sequenceId;
response["Source"] = destination;
response["Type"] = "Response";
sendJsonResponse(wsConn, response);
// Emit signal for task controller status change
emit taskControllerStatusChanged();
// Emit signal to start the imaging simulation
emit imagingStarted();
}
void CommandHandler::completeSampleCapture() {
m_telescopeState->state = "IDLE";
emit taskControllerStatusChanged();
}
void CommandHandler::completeImaging() {
m_telescopeState->isImaging = false;
m_telescopeState->imagingTimeLeft = 0;
// If we were in SAMPLE_CAPTURE state, return to IDLE
if (m_telescopeState->state == "SAMPLE_CAPTURE") {
completeSampleCapture();
}
emit imagingComplete();
}