-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCelestronOriginSimulator.cpp
More file actions
1086 lines (881 loc) · 41.3 KB
/
CelestronOriginSimulator.cpp
File metadata and controls
1086 lines (881 loc) · 41.3 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
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
#include "CelestronOriginSimulator.h"
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDateTime>
#include <QNetworkInterface>
#include <QDir>
#include <QFile>
#include <QBuffer>
#include <QImage>
#include <QDebug>
#include <QPainter>
#include <cmath>
#include "CelestronOriginSimulator.h"
#include "TiffImageGenerator.h"
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDateTime>
#include <QNetworkInterface>
#include <QDir>
#include <QFile>
#include <QBuffer>
#include <QImage>
#include <QDebug>
#include <QPainter>
#include <cmath>
CelestronOriginSimulator::CelestronOriginSimulator(QObject *parent) : QObject(parent) {
// Initialize core components
m_telescopeState = new TelescopeState();
m_commandHandler = new CommandHandler(m_telescopeState, this);
m_statusSender = new StatusSender(m_telescopeState, this);
// Initialize the dual protocol server
m_tcpServer = new QTcpServer(this);
m_udpSocket = new QUdpSocket(this);
// Initialize headless mosaic creator
m_mosaicCreator = new EnhancedMosaicCreator(this); // Headless mode
m_mosaicInProgress = false;
// Connect mosaic completion signal
connect(m_mosaicCreator, &EnhancedMosaicCreator::mosaicComplete,
this, &CelestronOriginSimulator::onMosaicComplete);
if (false) qDebug() << "Headless Enhanced Mosaic Creator initialized";
setupHipsIntegration(); // Changed from setupRubinIntegration
if (m_tcpServer->listen(QHostAddress::Any, SERVER_PORT)) {
setupConnections();
setupTimers();
// First broadcast immediately
QTimer::singleShot(100, this, &CelestronOriginSimulator::sendBroadcast);
} else {
if (false) qDebug() << "Failed to start Origin simulator:" << m_tcpServer->errorString();
}
}
CelestronOriginSimulator::~CelestronOriginSimulator() {
if (m_tcpServer) {
m_tcpServer->close();
}
qDeleteAll(m_webSocketClients);
}
void CelestronOriginSimulator::setupHipsIntegration() {
m_hipsClient = new ProperHipsClient(this);
// Set up HiPS image directory to integrate with existing simulator structure
QString homeDir = QDir::homePath();
QString appSupportDir = QDir(homeDir).absoluteFilePath("Library/Application Support/OriginSimulator");
QString hipsDir = QDir(appSupportDir).absoluteFilePath("Images/HiPS");
// Create the HiPS directory
QDir().mkpath(hipsDir);
// Connect ProperHipsClient signals to our slots
connect(m_hipsClient, &ProperHipsClient::testingComplete,
this, &CelestronOriginSimulator::onHipsTestingComplete);
if (false) qDebug() << "ProperHips integration initialized";
if (false) qDebug() << "HiPS images will be saved to:" << hipsDir;
// Set initial coordinates
m_telescopeState->ra = m_telescopeState->baseRA;
m_telescopeState->dec = m_telescopeState->baseDec;
// Start with initial position fetch
SkyPosition initialPos = {
m_telescopeState->ra * 180.0 / M_PI,
m_telescopeState->dec * 180.0 / M_PI,
"Initial_Position",
"Telescope starting position"
};
fetchHipsImagesForPosition(initialPos);
}
void CelestronOriginSimulator::fetchHipsImagesForPosition(const SkyPosition& position) {
if (false) qDebug() << QString("Fetching HiPS images for position: RA=%1°, Dec=%2°")
.arg(position.ra_deg, 0, 'f', 6)
.arg(position.dec_deg, 0, 'f', 6);
// Use the best available survey
QString bestSurvey = getBestAvailableSurvey();
if (bestSurvey.isEmpty()) {
if (false) qDebug() << "No working surveys available";
return;
}
// Test the survey at this position
m_hipsClient->testSurveyAtPosition(bestSurvey, position);
generateCurrentSkyImage();
}
QString CelestronOriginSimulator::getBestAvailableSurvey() const {
// Get working surveys from previous tests, or use defaults
QStringList workingSurveys = m_hipsClient->getWorkingSurveys();
if (workingSurveys.isEmpty()) {
// Default to known working surveys
QStringList defaultSurveys = {"DSS2_Color", "2MASS_Color", "2MASS_J"};
return defaultSurveys.first();
}
// Prefer color surveys over single-band
QStringList preferredOrder = {"DSS2_Color", "2MASS_Color", "Mellinger_Color",
"2MASS_J", "DSS2_Red", "Gaia_DR3"};
for (const QString& preferred : preferredOrder) {
if (workingSurveys.contains(preferred)) {
return preferred;
}
}
return workingSurveys.first();
}
// Slot implementations for HiPS integration
void CelestronOriginSimulator::onHipsImageReady(const QString& filename) {
if (false) qDebug() << "HiPS Observatory image ready:" << filename;
// Extract just the filename for the telescope's image system
QFileInfo fileInfo(filename);
QString relativePath = QString("/SmartScope-1.0/dev2/Images/Astrophotography/%1").arg(fileInfo.fileName());
// Update telescope state with new image location
m_telescopeState->fileLocation = relativePath;
m_telescopeState->imageType = "HIPS_IMAGE";
m_telescopeState->sequenceNumber++;
// Notify all connected clients about the new image
m_statusSender->sendNewImageReadyToAll();
if (true) qDebug() << "Telescope updated with HiPS image:" << relativePath;
}
void CelestronOriginSimulator::onHipsTilesAvailable(const QStringList& filenames) {
if (false) qDebug() << "HiPS Observatory tiles available:" << filenames.size();
if (!filenames.isEmpty()) {
// Use the first available tile
onHipsImageReady(filenames.first());
// Log all available files
for (const QString& file : filenames) {
QFileInfo info(file);
if (false) qDebug() << " Available:" << info.fileName() << "(" << info.size() << "bytes)";
}
}
}
void CelestronOriginSimulator::onHipsFetchError(const QString& error_message) {
if (false) qDebug() << "HiPS Observatory fetch error:" << error_message;
// Send error notification to clients
QJsonObject errorNotification;
errorNotification["Command"] = "Warning";
errorNotification["Destination"] = "All";
errorNotification["Source"] = "HipsImageServer";
errorNotification["Type"] = "Notification";
errorNotification["Message"] = "HiPS Observatory data unavailable: " + error_message;
errorNotification["ExpiredAt"] = m_telescopeState->getExpiredAt();
errorNotification["SequenceID"] = m_telescopeState->getNextSequenceId();
m_statusSender->sendJsonMessageToAll(errorNotification);
}
void CelestronOriginSimulator::onHipsTestingComplete() {
if (false) qDebug() << "HiPS testing completed";
// Print summary of results
m_hipsClient->printSummary();
// Save results for debugging
m_hipsClient->saveResults("hips_test_results.csv");
}
void CelestronOriginSimulator::setupConnections() {
connect(m_tcpServer, &QTcpServer::newConnection, this, &CelestronOriginSimulator::handleNewConnection);
// Connect command handler signals
connect(m_commandHandler, &CommandHandler::slewStarted, this, [this]() {
m_slewTimer->start(500);
});
connect(m_commandHandler, &CommandHandler::imagingStarted, this, [this]() {
m_imagingTimer->start(1000);
});
// Connect imaging completion signal
connect(m_commandHandler, &CommandHandler::imagingComplete, this, [this]() {
m_imagingTimer->stop();
});
// Connect initialization signal
connect(m_commandHandler, &CommandHandler::initializationStarted, this, [this](bool fakeInit) {
if (fakeInit) {
// Complete immediately for fake initialization
QTimer::singleShot(1000, this, &CelestronOriginSimulator::completeInitialization);
} else {
// Start the initialization process
m_initTimer->start(3000); // Update every 3 seconds like real telescope
}
});
// Add these connections to your existing setupConnections method
connect(m_commandHandler, &CommandHandler::taskControllerStatusChanged, this, [this]() {
m_statusSender->sendTaskControllerStatusToAll();
});
connect(m_commandHandler, &CommandHandler::imagingComplete, this, [this]() {
m_imagingTimer->stop();
});
}
void CelestronOriginSimulator::setupTimers() {
// Create broadcast timer
m_broadcastTimer = new QTimer(this);
connect(m_broadcastTimer, &QTimer::timeout, this, &CelestronOriginSimulator::sendBroadcast);
m_broadcastTimer->start(BROADCAST_INTERVAL);
// Create update timer for regular status updates
m_updateTimer = new QTimer(this);
connect(m_updateTimer, &QTimer::timeout, this, &CelestronOriginSimulator::sendStatusUpdates);
m_updateTimer->start(1000);
// Create slew timer
m_slewTimer = new QTimer(this);
connect(m_slewTimer, &QTimer::timeout, this, &CelestronOriginSimulator::updateSlew);
// Create imaging timer
m_imagingTimer = new QTimer(this);
connect(m_imagingTimer, &QTimer::timeout, this, &CelestronOriginSimulator::updateImaging);
// Create initialization timer
m_initTimer = new QTimer(this);
m_initTimer->setSingleShot(false);
connect(m_initTimer, &QTimer::timeout, this, &CelestronOriginSimulator::updateInitialization);
}
void CelestronOriginSimulator::updateSlew() {
static int slewProgress = 0;
// Simulate slew progress
slewProgress += 20; // 20% progress per 500ms
if (slewProgress >= 100) {
if (false) qDebug() << "Before update - RA:" << m_telescopeState->ra << "Dec:" << m_telescopeState->dec;
if (false) qDebug() << "Target RA:" << m_telescopeState->targetRa << "Target Dec:" << m_telescopeState->targetDec;
// Slew complete
m_telescopeState->isGotoOver = true;
m_telescopeState->isSlewing = false;
m_telescopeState->baseRA = m_telescopeState->targetRa;
m_telescopeState->baseDec = m_telescopeState->targetDec;
m_telescopeState->ra = m_telescopeState->baseRA;
m_telescopeState->dec = m_telescopeState->baseDec;
// Stop the timer
m_slewTimer->stop();
slewProgress = 0;
if (false) qDebug() << "After update - RA:" << m_telescopeState->ra << "Dec:" << m_telescopeState->dec;
// Update mount status
m_statusSender->sendMountStatusToAll();
// Add delay to ensure coordinates are fully updated
QTimer::singleShot(100, this, [this]() {
if (m_hipsClient) {
if (false) qDebug() << "🎯 Slew complete - fetching HiPS data for new position";
// Convert telescope coordinates to SkyPosition
SkyPosition newPos = {
m_telescopeState->targetRa * 180.0 / M_PI, // Convert radians to degrees
m_telescopeState->targetDec * 180.0 / M_PI,
"Slew_Target",
"Position after telescope slew"
};
if (false) qDebug() << "🎯 Using target coordinates for HiPS fetch - RA:" << newPos.ra_deg << "Dec:" << newPos.dec_deg;
fetchHipsImagesForPosition(newPos);
}
});
if (false) qDebug() << "🎯 Slew complete";
}
}
// =============================================================================
// CORRECTED updateImaging() function for CelestronOriginSimulator
// =============================================================================
//
// This fixes the issue where NewImageReady notifications were being sent
// continuously during sample capture instead of once when complete.
//
// Replace the existing updateImaging() function (around line 2917 in sim.txt)
// with this corrected version.
// =============================================================================
void CelestronOriginSimulator::updateImaging() {
if (m_telescopeState->isImaging) {
m_telescopeState->imagingTimeLeft--;
if (m_telescopeState->imagingTimeLeft <= 0) {
m_telescopeState->isImaging = false;
m_imagingTimer->stop();
QString imagePath = m_telescopeState->getNextTIFFFile();
m_telescopeState->fileLocation = imagePath;
QString fullPath = m_absoluteTempDir + "/" + imagePath;
// No cached image data
TiffImageGenerator::generateSyntheticStarField(fullPath, 150);
qDebug() << "=== SAMPLE CAPTURE COMPLETE ===";
qDebug() << "Generated synthetic star field (no cached image)";
m_statusSender->sendNewImageReadyToAll();
m_commandHandler->completeImaging();
}
}
}
// =============================================================================
// EXPLANATION OF THE FIX
// =============================================================================
//
// BEFORE (INCORRECT):
// - Sent NewImageReady notification on EVERY timer tick during SAMPLE_CAPTURE
// - Changed image path continuously
// - INDI driver received multiple notifications for same capture
// - Confusing behavior and potential race conditions
//
// AFTER (CORRECT):
// - Only sends NewImageReady notification ONCE when imaging completes
// - Sets image path once when ready
// - Matches real telescope behavior
// - INDI driver receives exactly one notification per capture
//
// FLOW:
// 1. INDI sends RunSampleCapture with ExposureTime=1.0, ISO=800
// 2. Simulator responds immediately with success
// 3. imagingStarted signal starts m_imagingTimer (1 second ticks)
// 4. imagingTimeLeft counts down from (exposureTime + 2 seconds)
// 5. When imagingTimeLeft reaches 0:
// a. Stop timer
// b. Set fileLocation to next available image
// c. Send NewImageReady notification
// d. Complete imaging
// 6. INDI driver receives notification and downloads image via HTTP
//
// =============================================================================
void CelestronOriginSimulator::updateInitialization() {
// Simulate real telescope initialization progression
m_initUpdateCount++;
// Send status update with progress
m_statusSender->sendTaskControllerStatusToAll();
// After several updates, set focus position (matches real telescope behavior)
if (m_initUpdateCount == 5) {
m_telescopeState->initInfo.positionOfFocus = 18617; // Use value from real trace
}
// After more updates, find first alignment point
if (m_initUpdateCount == 10) {
m_telescopeState->initInfo.numPoints = 1;
m_telescopeState->initInfo.numPointsRemaining = 1;
m_telescopeState->initInfo.percentComplete = 50;
}
// Randomly decide if initialization fails (about 50% chance like real telescope)
if (m_initUpdateCount < 10 && QRandomGenerator::global()->bounded(100) < 10) {
failInitialization();
return;
}
// Complete the initialization
if (m_initUpdateCount >= 15) {
m_telescopeState->initInfo.numPoints = 2;
m_telescopeState->initInfo.numPointsRemaining = 0;
m_telescopeState->initInfo.percentComplete = 100;
completeInitialization();
}
}
void CelestronOriginSimulator::completeInitialization() {
// Stop the timer
m_initTimer->stop();
m_initUpdateCount = 0;
// Set final status
m_telescopeState->isInitializing = false;
m_telescopeState->stage = "COMPLETE";
m_telescopeState->isReady = true;
// Send completion status
m_statusSender->sendTaskControllerStatusToAll();
// Wait a moment and transition to IDLE state
QTimer::singleShot(1000, this, [this]() {
m_telescopeState->state = "IDLE";
m_statusSender->sendTaskControllerStatusToAll();
});
}
void CelestronOriginSimulator::failInitialization() {
// Stop the timer
m_initTimer->stop();
m_initUpdateCount = 0;
// Set failure status
m_telescopeState->isInitializing = false;
m_telescopeState->stage = "STOPPED";
m_telescopeState->isReady = false;
// Send error notification
QJsonObject errorNotification;
errorNotification["Command"] = "Error";
errorNotification["Destination"] = "All";
errorNotification["ErrorCode"] = -78;
errorNotification["ErrorMessage"] = "Initialization failed. Please point the scope away from any bright lights; buildings; trees and try again.";
errorNotification["ExpiredAt"] = m_telescopeState->getExpiredAt();
errorNotification["Type"] = "Notification";
m_statusSender->sendJsonMessageToAll(errorNotification);
// Send failure status
m_statusSender->sendTaskControllerStatusToAll();
}
// Rest of the existing methods remain the same...
void CelestronOriginSimulator::handleNewConnection() {
QTcpSocket *socket = m_tcpServer->nextPendingConnection();
// Store pending request data
m_pendingRequests[socket] = QByteArray();
// CRITICAL: Use QueuedConnection to prevent immediate processing conflicts
connect(socket, &QTcpSocket::readyRead, this, [this, socket]() {
handleIncomingData(socket);
}, Qt::QueuedConnection);
connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
m_pendingRequests.remove(socket);
socket->deleteLater();
});
}
// Include the rest of your existing methods here...
// (handleIncomingData, handleWebSocketUpgrade, etc.)
// They remain unchanged from your original implementation
void CelestronOriginSimulator::sendBroadcast() {
// Prepare the broadcast message
QString message = QString("Identity:Origin-") + QLatin1String(std::to_string(broadcast_id)) + QString("Z Origin IP Address = %1");
// Get our IP addresses
QList<QHostAddress> ipAddresses = QNetworkInterface::allAddresses();
for (const QHostAddress &address : ipAddresses) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress::LocalHost) {
QString broadcastMessage = message.arg(address.toString());
if (true) qDebug() << broadcastMessage;
// Send broadcast on all network interfaces
m_udpSocket->writeDatagram(
broadcastMessage.toUtf8(),
QHostAddress::Broadcast,
BROADCAST_PORT
);
}
}
}
void CelestronOriginSimulator::handleIncomingData(QTcpSocket *socket) {
QByteArray newData = socket->readAll();
m_pendingRequests[socket].append(newData);
QByteArray &requestData = m_pendingRequests[socket];
// Look for complete HTTP headers
int headerEndPos = requestData.indexOf("\r\n\r\n");
if (headerEndPos == -1) {
// Headers not complete yet, wait for more data
if (requestData.size() > 8192) {
// Too much data without finding headers, disconnect
socket->disconnectFromHost();
return;
}
return;
}
// We have complete headers, determine the protocol
QString request = QString::fromUtf8(requestData.left(headerEndPos));
QStringList lines = request.split("\r\n");
if (lines.isEmpty()) {
socket->disconnectFromHost();
return;
}
QString requestLine = lines[0];
QStringList requestParts = requestLine.split(" ");
if (requestParts.size() < 3) {
socket->disconnectFromHost();
return;
}
QString method = requestParts[0];
QString path = requestParts[1];
if (true) qDebug() << "Origin Protocol Request:" << method << path;
// Check if this is a WebSocket upgrade request
bool isWebSocketUpgrade = false;
for (const QString &line : lines) {
if (line.toLower().contains("upgrade: websocket")) {
isWebSocketUpgrade = true;
break;
}
}
if (isWebSocketUpgrade && path == "/SmartScope-1.0/mountControlEndpoint") {
// Handle WebSocket upgrade for telescope control
handleWebSocketUpgrade(socket, requestData);
} else if (method == "GET" && path.startsWith("/SmartScope-1.0/dev2/Images/Temp/")) {
// Handle HTTP image request
handleHttpImageRequest(socket, path);
} else if (method == "GET" && path.contains("/SmartScope-1.0/dev2//tmp")) {
// Handle HTTP astrophotography image request
handleHttpAstroImageRequest(socket, path);
} else {
// Unknown request
sendHttpResponse(socket, 404, "text/plain", "Not Found");
}
// Clear the pending request data
m_pendingRequests.remove(socket);
}
// CORRECTED FIX: CelestronOriginSimulator.cpp - Proper handshake sequence
// Perform handshake FIRST, then transfer socket ownership
void CelestronOriginSimulator::handleWebSocketUpgrade(QTcpSocket *socket, const QByteArray &requestData) {
// // if (false) qDebug() << "*** STARTING WEBSOCKET UPGRADE PROCESS ***";
// if (false) qDebug() << "Request data size:" << requestData.size();
// Create WebSocketConnection but DON'T disconnect protocol detector yet
WebSocketConnection *wsConn = new WebSocketConnection(socket, this, false); // false = don't take ownership yet
// FIRST: Perform the handshake using the request data
if (wsConn->performHandshake(requestData)) {
// // if (false) qDebug() << "*** HANDSHAKE SUCCESSFUL - TRANSFERRING SOCKET OWNERSHIP ***";
// CRITICAL: NOW disconnect the protocol detector since handshake worked
disconnect(socket, &QTcpSocket::readyRead, this, nullptr);
// // if (false) qDebug() << "*** PROTOCOL DETECTOR DISCONNECTED ***";
// Clear any pending data since we're switching protocols
m_pendingRequests.remove(socket);
// NOW let WebSocketConnection take full ownership
wsConn->takeSocketOwnership();
// Add to our client list
m_webSocketClients.append(wsConn);
m_statusSender->addWebSocketClient(wsConn);
// Set up all signal connections for WebSocket handling
connect(wsConn, &WebSocketConnection::textMessageReceived,
this, &CelestronOriginSimulator::processWebSocketCommand);
connect(wsConn, &WebSocketConnection::pingReceived,
this, &CelestronOriginSimulator::handleWebSocketPing);
connect(wsConn, &WebSocketConnection::pongReceived,
this, &CelestronOriginSimulator::handleWebSocketPong);
connect(wsConn, &WebSocketConnection::pingTimeout,
this, &CelestronOriginSimulator::handleWebSocketTimeout);
connect(wsConn, &WebSocketConnection::disconnected,
this, &CelestronOriginSimulator::onWebSocketDisconnected);
// if (false) qDebug() << "WebSocket connection established for telescope control";
// Send initial status updates after a brief delay
QTimer::singleShot(1000, this, [this, wsConn]() {
if (m_webSocketClients.contains(wsConn)) {
m_statusSender->sendMountStatus(wsConn);
m_statusSender->sendFocuserStatus(wsConn);
m_statusSender->sendCameraParams(wsConn);
m_statusSender->sendDiskStatus(wsConn);
m_statusSender->sendTaskControllerStatus(wsConn);
m_statusSender->sendEnvironmentStatus(wsConn);
m_statusSender->sendDewHeaterStatus(wsConn);
m_statusSender->sendOrientationStatus(wsConn);
}
});
} else {
// // if (false) qDebug() << "*** HANDSHAKE FAILED - KEEPING PROTOCOL DETECTOR ***";
// Handshake failed, so keep the original protocol detector connected
// Don't disconnect anything - let it continue as HTTP
sendHttpResponse(socket, 400, "text/plain", "Bad WebSocket Request");
// Clean up the failed WebSocket connection
delete wsConn;
}
}
void CelestronOriginSimulator::handleHttpImageRequest(QTcpSocket *socket, const QString &path) {
if (false) qDebug() << "Handling HTTP image request for path:" << path;
sendHttpResponse(socket, 200, "image/jpeg", m_imageData);
}
void CelestronOriginSimulator::handleHttpAstroImageRequest(QTcpSocket *socket, const QString &path) {
// Normalize path by replacing double slashes
QString normalizedPath = path;
normalizedPath.replace("//", "/");
// Extract just the requested filename
QString fileName = QFileInfo(normalizedPath).fileName();
// Always serve the pre-generated 16-bit TIFF instead
QString fullPath = "/tmp/temp_hips_image.tiff";
// If the TIFF doesn't exist, fall back to regular search
if (!QFile::exists(fullPath)) {
if (!m_telescopeState->fileLocation.isEmpty() && QFile::exists(m_telescopeState->fileLocation)) {
fullPath = m_telescopeState->fileLocation;
} else if (QFile::exists(m_absoluteTempDir + "/Images_1.tiff")) {
fullPath = m_absoluteTempDir + "/Images_1.tiff";
} else {
fullPath = QString("%1/%2").arg(m_absoluteTempDir, fileName);
}
}
if (!QFile::exists(fullPath)) {
qWarning() << "AstroImage request failed - file not found:" << normalizedPath << "->" << fullPath;
sendHttpResponse(socket, 404, "text/plain", "Image not found");
return;
}
QFile imageFile(fullPath);
if (!imageFile.open(QIODevice::ReadOnly)) {
sendHttpResponse(socket, 500, "text/plain", "Failed to open image");
return;
}
QByteArray imageData = imageFile.readAll();
imageFile.close();
qDebug() << "Serving image/tiff from" << fullPath << "for" << normalizedPath;
sendHttpResponse(socket, 200, "image/tiff", imageData);
}
void CelestronOriginSimulator::sendHttpResponse(QTcpSocket *socket, int statusCode,
const QString &contentType, const QByteArray &data) {
QString statusText;
switch (statusCode) {
case 200: statusText = "OK"; break;
case 404: statusText = "Not Found"; break;
case 400: statusText = "Bad Request"; break;
case 500: statusText = "Internal Server Error"; break;
default: statusText = "Unknown"; break;
}
QString response = QString("HTTP/1.1 %1 %2\r\n").arg(statusCode).arg(statusText);
response += QString("Content-Type: %1\r\n").arg(contentType);
response += QString("Content-Length: %1\r\n").arg(data.size());
response += "Cache-Control: no-cache\r\n";
response += "Access-Control-Allow-Origin: *\r\n";
response += "Connection: close\r\n";
response += "\r\n";
socket->write(response.toUtf8());
if (!data.isEmpty()) {
socket->write(data);
}
socket->disconnectFromHost();
}
void CelestronOriginSimulator::processWebSocketCommand(const QString &message) {
WebSocketConnection *wsConn = qobject_cast<WebSocketConnection*>(sender());
if (!wsConn) return;
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
if (!doc.isObject()) {
// if (false) qDebug() << "Received invalid JSON:" << message;
return;
}
QJsonObject obj = doc.object();
// Extract common fields
QString command = obj["Command"].toString();
QString destination = obj["Destination"].toString();
QString source = obj["Source"].toString();
// if (false) qDebug() << "Received WebSocket command:" << command << "to" << destination << "from" << source;
// Handle status requests directly
if (command == "GetStatus") {
int sequenceId = obj["SequenceID"].toInt();
if (destination == "System") {
m_statusSender->sendSystemVersion(wsConn, sequenceId, source);
} else if (destination == "Mount") {
m_statusSender->sendMountStatus(wsConn, sequenceId, source);
} else if (destination == "Focuser") {
m_statusSender->sendFocuserStatus(wsConn, sequenceId, source);
} else if (destination == "TaskController") {
m_statusSender->sendTaskControllerStatus(wsConn, sequenceId, source);
} else if (destination == "DewHeater") {
m_statusSender->sendDewHeaterStatus(wsConn, sequenceId, source);
} else if (destination == "Environment") {
m_statusSender->sendEnvironmentStatus(wsConn, sequenceId, source);
} else if (destination == "OrientationSensor") {
m_statusSender->sendOrientationStatus(wsConn, sequenceId, source);
} else if (destination == "Disk") {
m_statusSender->sendDiskStatus(wsConn, sequenceId, source);
} else if (destination == "FactoryCalibrationController") {
m_statusSender->sendCalibrationStatus(wsConn, sequenceId, source);
}
} else if (command == "GetVersion") {
int sequenceId = obj["SequenceID"].toInt();
m_statusSender->sendSystemVersion(wsConn, sequenceId, source);
} else if (command == "GetCaptureParameters") {
int sequenceId = obj["SequenceID"].toInt();
m_statusSender->sendCameraParams(wsConn, sequenceId, source);
} else if (command == "GetFilter") {
int sequenceId = obj["SequenceID"].toInt();
m_statusSender->sendCameraFilter(wsConn, sequenceId, source);
} else if (command == "GetModel") {
int sequenceId = obj["SequenceID"].toInt();
m_statusSender->sendSystemModel(wsConn, sequenceId, source);
} else {
// Process the command through the command handler
m_commandHandler->processCommand(obj, wsConn);
}
}
void CelestronOriginSimulator::onWebSocketDisconnected() {
WebSocketConnection *wsConn = qobject_cast<WebSocketConnection*>(sender());
if (wsConn) {
m_webSocketClients.removeAll(wsConn);
m_statusSender->removeWebSocketClient(wsConn);
wsConn->deleteLater();
}
}
// CRITICAL: Fix the status update timing to avoid blocking ping/pong
// Replace sendStatusUpdates method with this non-blocking version:
void CelestronOriginSimulator::sendStatusUpdates() {
// Update time
m_telescopeState->dateTime = QDateTime::currentDateTime();
// CRITICAL: Use QueuedConnection to avoid blocking the event loop
static int updateCounter = 0;
updateCounter++;
// Send updates in smaller batches to prevent blocking
if (updateCounter % 1 == 0) {
// Every second - critical updates only
QTimer::singleShot(0, this, [this]() {
m_statusSender->sendMountStatusToAll();
});
}
if (updateCounter % 2 == 0) {
QTimer::singleShot(5, this, [this]() {
m_statusSender->sendFocuserStatusToAll();
});
}
if (updateCounter % 3 == 0) {
QTimer::singleShot(10, this, [this]() {
if (!m_telescopeState->isImaging)
{
m_statusSender->sendCameraParamsToAll();
m_telescopeState->sequenceNumber++;
m_telescopeState->fileLocation = m_telescopeState->getNextImageFile();
m_statusSender->sendNewImageReadyToAll();
}
});
}
// Less frequent updates with longer delays
if (updateCounter % 10 == 0) {
QTimer::singleShot(15, this, [this]() {
m_statusSender->sendEnvironmentStatusToAll();
m_statusSender->sendDiskStatusToAll();
});
}
if (updateCounter % 15 == 0) {
QTimer::singleShot(20, this, [this]() {
m_statusSender->sendDewHeaterStatusToAll();
});
}
if (updateCounter % 30 == 0) {
QTimer::singleShot(25, this, [this]() {
m_statusSender->sendOrientationStatusToAll();
});
}
if (updateCounter % 5 == 0) {
QTimer::singleShot(30, this, [this]() {
m_statusSender->sendTaskControllerStatusToAll();
});
}
// Reset counter to prevent overflow
if (updateCounter > 1000) {
updateCounter = 0;
}
}
// CRITICAL: Add connection monitoring
// Add this method to monitor connection health:
void CelestronOriginSimulator::checkConnectionHealth() {
// if (false) qDebug() << "Active WebSocket connections:" << m_webSocketClients.size();
for (WebSocketConnection *wsConn : m_webSocketClients) {
// Check if connection is still alive
// We don't need to do anything here - the ping/pong mechanism handles it
}
}
// Add these new slot methods to CelestronOriginSimulator class:
void CelestronOriginSimulator::handleWebSocketPing(const QByteArray &payload) {
WebSocketConnection *wsConn = qobject_cast<WebSocketConnection*>(sender());
// if (false) qDebug() << "WebSocket ping received from client, payload size:" << payload.size();
// The WebSocketConnection automatically sends pong, we just log it here
}
void CelestronOriginSimulator::handleWebSocketPong(const QByteArray &payload) {
WebSocketConnection *wsConn = qobject_cast<WebSocketConnection*>(sender());
// if (false) qDebug() << "WebSocket pong received from client, payload size:" << payload.size();
// Client responded to our ping successfully
}
void CelestronOriginSimulator::handleWebSocketTimeout() {
WebSocketConnection *wsConn = qobject_cast<WebSocketConnection*>(sender());
// if (false) qDebug() << "WebSocket ping timeout occurred - client not responding";
if (wsConn && m_webSocketClients.contains(wsConn)) {
// Remove from active clients but don't delete yet - let disconnected signal handle cleanup
m_statusSender->removeWebSocketClient(wsConn);
}
}
void CelestronOriginSimulator::generateCurrentSkyImage() {
// Skip if already generating
if (m_mosaicInProgress) {
if (false) qDebug() << "Mosaic generation already in progress, skipping";
return;
}
// Get current telescope pointing
double ra_rad = m_telescopeState->ra;
double dec_rad = m_telescopeState->dec;
double ra_deg = ra_rad * 180.0 / M_PI;
double dec_deg = dec_rad * 180.0 / M_PI;
if (false) qDebug() << QString("Generating enhanced mosaic for RA=%1°, Dec=%2°")
.arg(ra_deg, 0, 'f', 6)
.arg(dec_deg, 0, 'f', 6);
// Create SkyPosition using the existing structure
SkyPosition currentPosition = {
ra_deg,
dec_deg,
QString("Live_Image_%1").arg(m_telescopeState->imageCounter),
QString("Real-time telescope position at %1").arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
};
// Convert coordinates to string format for the mosaic creator
double ra_hours = ra_deg / 15.0;
int ra_h = static_cast<int>(ra_hours);
int ra_m = static_cast<int>((ra_hours - ra_h) * 60);
double ra_s = ((ra_hours - ra_h) * 60 - ra_m) * 60;
bool dec_negative = dec_deg < 0;
double abs_dec = std::abs(dec_deg);
int dec_d = static_cast<int>(abs_dec);
int dec_m = static_cast<int>((abs_dec - dec_d) * 60);
double dec_s = ((abs_dec - dec_d) * 60 - dec_m) * 60;
QString raText = QString("%1h%2m%3s")
.arg(ra_h).arg(ra_m, 2, 10, QChar('0')).arg(ra_s, 0, 'f', 1);
QString decText = QString("%1%2d%3m%4s")
.arg(dec_negative ? "-" : "+")
.arg(dec_d).arg(dec_m, 2, 10, QChar('0')).arg(dec_s, 0, 'f', 1);
if (false) qDebug() << QString("Converted coordinates: %1, %2").arg(raText).arg(decText);
// Set coordinates in the headless mosaic creator
m_mosaicCreator->setCustomCoordinates(raText, decText, currentPosition.name);
// Start mosaic creation (this will use the existing 3x3 tile logic)
m_mosaicInProgress = true;
m_mosaicCreator->createCustomMosaic(currentPosition);
if (true) qDebug() << "Enhanced mosaic generation started...";
}
// Method 1: Save to QByteArray as JPEG/PNG (most common)
QByteArray saveImageToByteArray(const QImage& image, const QString& format = "JPEG", int quality = 95) {
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
// Save image to buffer in specified format
bool success = image.save(&buffer, format.toUtf8().data(), quality);
if (success) {
if (false) qDebug() << QString("Image saved to buffer: %1 bytes, format: %2")
.arg(byteArray.size()).arg(format);
} else {
if (false) qDebug() << "Failed to save image to buffer";
}
return byteArray;
}
void CelestronOriginSimulator::onMosaicComplete(const QImage& mosaic) {
if (false) qDebug() << "Enhanced mosaic complete, processing for telescope image";
if (mosaic.isNull()) {
if (false) qDebug() << "Received null mosaic image";
m_mosaicInProgress = false;
return;
}
if (true) qDebug() << QString("Received mosaic: %1x%2 pixels").arg(mosaic.width()).arg(mosaic.height());
QImage fullImage = mosaic.scaled(3056, 2048, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QImage paddedImage(3056, 2048, QImage::Format_RGB888);
paddedImage.fill(Qt::black);
QPainter tiffpainter(&paddedImage);
int x = (3056 - fullImage.width()) / 2;
int y = (2048 - fullImage.height()) / 2;
tiffpainter.drawImage(x, y, fullImage);
tiffpainter.end();
QByteArray m_fulltiff = saveImageToByteArray(paddedImage, "TIFF", 100);
QString tempPath = "/tmp/temp_hips_image.tiff";
bool success = TiffImageGenerator::generateOriginFormatTiff(tempPath, paddedImage);
qDebug() << "Saved resized image: " << tempPath;
// Resize to telescope camera resolution (800x600) - Origin camera specs
QImage telescopeImage = mosaic.scaled(800, 600, Qt::KeepAspectRatio, Qt::SmoothTransformation);
// Fill any letterbox areas with black (if aspect ratios don't match)
if (telescopeImage.width() != 800 || telescopeImage.height() != 600) {
QImage finalImage(800, 600, QImage::Format_RGB888);
finalImage.fill(Qt::black);
QPainter painter(&finalImage);
int x = (800 - telescopeImage.width()) / 2;
int y = (600 - telescopeImage.height()) / 2;
painter.drawImage(x, y, telescopeImage);
painter.end();
telescopeImage = finalImage;