-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
1370 lines (1169 loc) · 52.4 KB
/
script.js
File metadata and controls
1370 lines (1169 loc) · 52.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
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
// WebSerial Device Configurator
class DeviceConfigurator {
constructor() {
this.port = null;
this.reader = null;
this.writer = null;
this.isConnected = false;
this.messageBuffer = '';
// Band data from WSPR.h bandDataList_
this.bandDataList = [
{ band: "2190m", freq: 136000 },
{ band: "630m", freq: 474200 },
{ band: "160m", freq: 1836600 },
{ band: "80m", freq: 3568600 },
{ band: "60m", freq: 5287200 },
{ band: "40m", freq: 7038600 },
{ band: "30m", freq: 10138700 },
{ band: "20m", freq: 14095600 },
{ band: "17m", freq: 18104600 },
{ band: "15m", freq: 21094600 },
{ band: "12m", freq: 24924600 },
{ band: "10m", freq: 28124600 },
{ band: "6m", freq: 50293000 },
{ band: "4m", freq: 70091000 },
{ band: "2m", freq: 144489000 },
{ band: "70cm", freq: 432300000 },
{ band: "23cm", freq: 1296500000 }
];
this.initializeElements();
this.populateBandSelector();
this.setupEventListeners();
this.checkWebSerialSupport();
}
initializeElements() {
this.connectBtn = document.getElementById('connectBtn');
this.disconnectBtn = document.getElementById('disconnectBtn');
this.statusIndicator = document.querySelector('.status-indicator');
this.statusText = document.querySelector('.status-text');
this.deviceInfo = {
port: document.getElementById('portInfo'),
vendorId: document.getElementById('vendorId'),
productId: document.getElementById('productId'),
serialNumber: document.getElementById('serialNumber')
};
this.messageLog = document.getElementById('messageLog');
this.pingBtn = document.getElementById('pingBtn');
this.clearBtn = document.getElementById('clearBtn');
this.hideGpsLines = document.getElementById('hideGpsLines');
this.messageType = document.getElementById('messageType');
this.sendMessageBtn = document.getElementById('sendMessageBtn');
this.jsonInput = document.getElementById('jsonInput');
this.sendJsonBtn = document.getElementById('sendJsonBtn');
// Configuration display elements
this.configElements = {
band: document.getElementById('configBand'),
channel: document.getElementById('configChannel'),
correction: document.getElementById('configCorrection'),
callsign: document.getElementById('configCallsign'),
callsignOk: document.getElementById('configCallsignOk'),
tempValue: document.getElementById('tempValue'),
tempTimestamp: document.getElementById('tempTimestamp'),
// GPS elements
gpsStatus: document.getElementById('gpsStatus'),
gpsSatellites: document.getElementById('gpsSatellites'),
gpsAntenna: document.getElementById('gpsAntenna'),
gpsJamming: document.getElementById('gpsJamming'),
gpsLatitude: document.getElementById('gpsLatitude'),
gpsLongitude: document.getElementById('gpsLongitude'),
gpsAltitude: document.getElementById('gpsAltitude'),
gpsTimestamp: document.getElementById('gpsTimestamp'),
gpsTime: document.getElementById('gpsTime'),
gpsSpeed: document.getElementById('gpsSpeed'),
gpsCourse: document.getElementById('gpsCourse'),
gpsMagVar: document.getElementById('gpsMagVar')
};
this.refreshConfigBtn = document.getElementById('refreshConfigBtn');
this.saveConfigBtn = document.getElementById('saveConfigBtn');
this.originalConfig = {}; // Store original values for change detection
}
setupEventListeners() {
this.connectBtn.addEventListener('click', () => this.connect());
this.disconnectBtn.addEventListener('click', () => this.disconnect());
this.pingBtn.addEventListener('click', () => this.sendPing());
this.clearBtn.addEventListener('click', () => this.clearLog());
this.hideGpsLines.addEventListener('change', () => this.toggleGpsLineFilter());
this.refreshConfigBtn.addEventListener('click', () => this.refreshConfig());
this.saveConfigBtn.addEventListener('click', () => this.saveConfig());
// Add change listeners for editable config fields
this.configElements.band.addEventListener('change', () => this.onConfigFieldChange());
this.configElements.channel.addEventListener('input', () => this.onConfigFieldChange());
this.configElements.callsign.addEventListener('input', () => this.onConfigFieldChange());
this.configElements.callsign.addEventListener('input', () => this.validateCallsign());
this.sendMessageBtn.addEventListener('click', () => this.sendSelectedMessage());
this.sendJsonBtn.addEventListener('click', () => this.sendCustomJson());
this.messageType.addEventListener('change', () => this.updateJsonInput());
}
checkWebSerialSupport() {
if (!('serial' in navigator)) {
this.showError('WebSerial is not supported in this browser. Please use Chrome, Edge, or Opera.');
this.connectBtn.disabled = true;
this.connectBtn.textContent = 'WebSerial Not Supported';
return false;
}
return true;
}
async connect() {
try {
this.updateStatus('connecting', 'Connecting...');
// Request a port
this.port = await navigator.serial.requestPort();
// Open the port
await this.port.open({
baudRate: 115200,
dataBits: 8,
stopBits: 1,
parity: 'none',
flowControl: 'none'
});
this.reader = this.port.readable.getReader();
this.writer = this.port.writable.getWriter();
this.isConnected = true;
this.updateStatus('connected', 'Connected');
this.updateUI(true);
this.updateDeviceInfo();
this.log('Connected to device successfully', 'info');
// Start reading from the device
this.startReading();
// Request initial configuration after connection
setTimeout(() => {
this.requestConfig();
}, 1000); // Wait 1 second after connection to ensure device is ready
} catch (error) {
this.showError('Connection failed: ' + error.message);
this.updateStatus('disconnected', 'Connection Failed');
this.updateUI(false);
}
}
async disconnect() {
try {
if (this.reader) {
await this.reader.cancel();
this.reader.releaseLock();
this.reader = null;
}
if (this.writer) {
await this.writer.close();
this.writer = null;
}
if (this.port) {
await this.port.close();
this.port = null;
}
this.isConnected = false;
this.updateStatus('disconnected', 'Disconnected');
this.updateUI(false);
this.clearDeviceInfo();
this.log('Disconnected from device', 'info');
} catch (error) {
this.showError('Disconnect failed: ' + error.message);
}
}
async startReading() {
try {
while (this.isConnected && this.reader) {
const { value, done } = await this.reader.read();
if (done) {
break;
}
// Convert Uint8Array to string
const text = new TextDecoder().decode(value);
this.messageBuffer += text;
// Process complete lines
let lines = this.messageBuffer.split('\n');
this.messageBuffer = lines.pop() || ''; // Keep the incomplete line
for (const line of lines) {
if (line.trim()) {
this.processIncomingMessage(line.trim());
}
}
}
} catch (error) {
if (this.isConnected) {
this.showError('Read error: ' + error.message);
}
}
}
processIncomingMessage(message) {
try {
// Try to parse as JSON
const jsonData = JSON.parse(message);
// Check if this is a GPS_LINE message
const isGpsLine = jsonData.type === 'GPS_LINE' ||
(typeof jsonData === 'object' &&
(message.includes('GPS_LINE') ||
message.includes('$GP') ||
message.includes('$GN') ||
message.includes('$GL')));
// Handle configuration and temperature responses
this.handleConfigResponse(jsonData);
// Handle TEMP messages specifically
if (jsonData.type === 'TEMP' || jsonData.type === 'REP_TEMP') {
this.updateTempDisplay(jsonData);
}
// Handle GPS_LINE messages specifically
if (jsonData.type === 'GPS_LINE') {
this.handleGpsLineMessage(jsonData);
}
this.log(`◀ ${JSON.stringify(jsonData, null, 2)}`, 'received', isGpsLine);
} catch (error) {
// Not JSON, check if it looks like GPS data
const isGpsLine = message.startsWith('$GP') ||
message.startsWith('$GN') ||
message.startsWith('$GL') ||
message.includes('GPS_LINE');
// Check for temperature data in plain text
this.handleTempMessage(message);
// Check for GPS data
this.handleGpsMessage(message);
this.log(`◀ ${message}`, 'received', isGpsLine);
}
}
async sendMessage(message) {
if (!this.isConnected || !this.writer) {
this.showError('Not connected to device');
return;
}
try {
const data = new TextEncoder().encode(message + '\n');
await this.writer.write(data);
// Log sent message
try {
const jsonData = JSON.parse(message);
this.log(`▶ ${JSON.stringify(jsonData, null, 2)}`, 'sent');
} catch (error) {
this.log(`▶ ${message}`, 'sent');
}
} catch (error) {
this.showError('Send failed: ' + error.message);
}
}
async sendPing() {
const pingMessage = JSON.stringify({ type: 'REQ_PING' });
await this.sendMessage(pingMessage);
}
async sendSelectedMessage() {
const messageType = this.messageType.value;
const message = JSON.stringify({ type: messageType });
await this.sendMessage(message);
}
async sendCustomJson() {
const jsonText = this.jsonInput.value.trim();
if (!jsonText) {
this.showError('Please enter JSON message');
return;
}
try {
// Validate JSON
JSON.parse(jsonText);
await this.sendMessage(jsonText);
} catch (error) {
this.showError('Invalid JSON: ' + error.message);
}
}
updateJsonInput() {
const messageType = this.messageType.value;
this.jsonInput.value = JSON.stringify({ type: messageType }, null, 2);
}
updateStatus(status, text) {
this.statusIndicator.className = `status-indicator ${status}`;
this.statusText.textContent = text;
}
updateUI(connected) {
this.connectBtn.disabled = connected;
this.disconnectBtn.disabled = !connected;
this.pingBtn.disabled = !connected;
this.sendMessageBtn.disabled = !connected;
this.jsonInput.disabled = !connected;
this.sendJsonBtn.disabled = !connected;
this.refreshConfigBtn.disabled = !connected;
this.saveConfigBtn.disabled = !connected;
// Enable/disable config inputs
this.configElements.band.disabled = !connected;
this.configElements.channel.disabled = !connected;
this.configElements.callsign.disabled = !connected;
if (!connected) {
this.clearConfigDisplay();
}
}
updateDeviceInfo() {
if (!this.port) return;
const info = this.port.getInfo();
this.deviceInfo.port.textContent = 'USB Serial Port';
this.deviceInfo.vendorId.textContent = info.usbVendorId ? `0x${info.usbVendorId.toString(16).padStart(4, '0').toUpperCase()}` : 'Unknown';
this.deviceInfo.productId.textContent = info.usbProductId ? `0x${info.usbProductId.toString(16).padStart(4, '0').toUpperCase()}` : 'Unknown';
this.deviceInfo.serialNumber.textContent = 'Connected';
}
clearDeviceInfo() {
this.deviceInfo.port.textContent = '-';
this.deviceInfo.vendorId.textContent = '-';
this.deviceInfo.productId.textContent = '-';
this.deviceInfo.serialNumber.textContent = '-';
}
log(message, type = 'info', isGpsLine = false) {
const timestamp = new Date().toLocaleTimeString();
const logEntry = document.createElement('div');
let className = `log-entry ${type}`;
if (isGpsLine) {
className += ' gps-line';
if (this.hideGpsLines.checked) {
className += ' filtered';
}
}
logEntry.className = className;
logEntry.innerHTML = `
<span class="timestamp">[${timestamp}]</span>
<pre>${message}</pre>
`;
// Remove placeholder if it exists
const placeholder = this.messageLog.querySelector('.log-placeholder');
if (placeholder) {
placeholder.remove();
}
this.messageLog.appendChild(logEntry);
this.messageLog.scrollTop = this.messageLog.scrollHeight;
}
clearLog() {
this.messageLog.innerHTML = '<p class="log-placeholder">Log cleared. Connect to device to see messages...</p>';
}
showError(message) {
this.log(`❌ ${message}`, 'error');
console.error(message);
}
toggleGpsLineFilter() {
const gpsEntries = this.messageLog.querySelectorAll('.log-entry.gps-line');
const hideGps = this.hideGpsLines.checked;
gpsEntries.forEach(entry => {
if (hideGps) {
entry.classList.add('filtered');
} else {
entry.classList.remove('filtered');
}
});
// Log the filter change
const status = hideGps ? 'enabled' : 'disabled';
this.log(`🔍 GPS line filtering ${status}`, 'info');
}
handleConfigResponse(jsonData) {
if (!jsonData || typeof jsonData !== 'object') return;
switch (jsonData.type) {
case 'REP_GET_CONFIG':
this.updateConfigDisplay(jsonData);
this.log('📡 Device configuration received', 'info');
break;
case 'TEMP':
case 'REP_TEMP':
this.updateTempDisplay(jsonData);
break;
default:
// Handle other response types if needed
break;
}
}
handleTempMessage(message) {
// Handle temperature messages that might not be JSON
// Look for patterns like "TEMP: 25.5°C", "Temperature: 25.5", or "TEMP 25.5"
const tempMatch = message.match(/(?:TEMP|Temperature)[\s:]*([0-9.-]+)(?:\s*°?C)?/i);
if (tempMatch) {
const tempValue = parseFloat(tempMatch[1]);
this.updateTempDisplay({
temp: tempValue,
temperature: tempValue,
timestamp: new Date()
});
this.log(`🌡️ Temperature updated: ${tempValue}°C`, 'info');
}
}
handleGpsMessage(message) {
// Parse NMEA GPS messages
if (message.startsWith('$GP') || message.startsWith('$GN') || message.startsWith('$GL')) {
this.parseNmeaMessage(message);
}
}
handleGpsLineMessage(jsonData) {
// Handle GPS_LINE JSON responses from the device
// These typically contain NMEA data in various formats
if (jsonData.line) {
// If GPS_LINE contains an NMEA sentence in the 'line' field
this.parseNmeaMessage(jsonData.line);
} else if (jsonData.nmea) {
// If GPS_LINE contains NMEA data in the 'nmea' field
this.parseNmeaMessage(jsonData.nmea);
} else if (jsonData.data) {
// If GPS_LINE contains NMEA data in the 'data' field
this.parseNmeaMessage(jsonData.data);
} else {
// Check if any of the JSON fields contain NMEA-like data
Object.values(jsonData).forEach(value => {
if (typeof value === 'string' &&
(value.startsWith('$GP') || value.startsWith('$GN') || value.startsWith('$GL'))) {
this.parseNmeaMessage(value);
}
});
}
}
parseNmeaMessage(nmeaMessage) {
// Clean up the NMEA message (remove any extra whitespace or line endings)
const cleanMessage = nmeaMessage.trim();
// Skip if not a valid NMEA message
if (!cleanMessage.startsWith('$') || cleanMessage.length < 6) {
return;
}
const parts = cleanMessage.split(',');
const messageType = parts[0];
try {
switch (messageType) {
case '$GPGGA':
case '$GNGGA':
this.parseGgaMessage(parts);
break;
case '$GPRMC':
case '$GNRMC':
this.parseRmcMessage(parts);
break;
case '$GPGSV':
case '$GNGSV':
case '$GLGSV':
this.parseGsvMessage(parts);
break;
case '$GPGSA':
case '$GNGSA':
this.parseGsaMessage(parts);
break;
case '$GPVTG':
case '$GNVTG':
this.parseVtgMessage(parts);
break;
case '$GPTXT':
case '$GNTXT':
this.parseGptxtMessage(parts);
break;
case '$GPZDA':
case '$GNZDA':
this.parseZdaMessage(parts);
break;
default:
// Log unknown NMEA message types for debugging
this.log(`🛰️ Unknown NMEA message: ${messageType}`, 'info');
break;
}
} catch (error) {
this.log(`⚠️ NMEA parsing error for ${messageType}: ${error.message}`, 'error');
}
}
parseGgaMessage(parts) {
// $GPGGA,time,lat,lat_dir,lon,lon_dir,quality,satellites,hdop,altitude,alt_units,geoid_height,geoid_units,dgps_time,dgps_id*checksum
if (parts.length >= 15) {
const quality = parseInt(parts[6]);
const satellites = parseInt(parts[7]);
const latitude = this.parseCoordinate(parts[2], parts[3]);
const longitude = this.parseCoordinate(parts[4], parts[5]);
const altitude = parseFloat(parts[9]);
const hdop = parseFloat(parts[8]);
const updateData = {
quality: !isNaN(quality) ? quality : undefined,
satellites: !isNaN(satellites) ? satellites : undefined,
latitude: latitude,
longitude: longitude,
altitude: !isNaN(altitude) ? altitude : undefined,
hdop: !isNaN(hdop) ? hdop : undefined,
timestamp: new Date()
};
// Only update with valid data
const validData = Object.fromEntries(
Object.entries(updateData).filter(([_, value]) => value !== undefined)
);
if (Object.keys(validData).length > 1) { // More than just timestamp
this.updateGpsDisplay(validData);
}
}
}
parseRmcMessage(parts) {
// $GPRMC,time,status,lat,lat_dir,lon,lon_dir,speed,course,date,mag_var,var_dir*checksum
// $GNRMC,time,status,lat,lat_dir,lon,lon_dir,speed,course,date,mag_var,var_dir*checksum
// Format: $GNRMC,064951.000,A,4807.038,N,01131.000,E,0.02,54.7,260523,,,A*76
// Where:
// - Field 1: Time in HHMMSS.sss format (UTC)
// - Field 2: Status (A=active, V=void)
// - Field 3: Latitude in DDMM.mmmm format
// - Field 4: N/S hemisphere
// - Field 5: Longitude in DDDMM.mmmm format
// - Field 6: E/W hemisphere
// - Field 7: Speed over ground in knots
// - Field 8: Course over ground in degrees
// - Field 9: Date in DDMMYY format
// - Field 10: Magnetic variation (degrees)
// - Field 11: E/W direction of magnetic variation
// - Field 12: Mode indicator (A=autonomous, D=differential, E=estimated, N=not valid)
if (parts.length >= 12) {
const time = parts[1];
const status = parts[2]; // A = active, V = void
const latitude = this.parseCoordinate(parts[3], parts[4]);
const longitude = this.parseCoordinate(parts[5], parts[6]);
const speed = parseFloat(parts[7]); // Speed in knots
const course = parseFloat(parts[8]); // Course in degrees
const date = parts[9]; // Date in DDMMYY format
const magVar = parseFloat(parts[10]); // Magnetic variation
const magVarDir = parts[11]; // E/W direction
const modeIndicator = parts[12] ? parts[12].split('*')[0] : undefined; // Remove checksum
const updateData = {
status: status === 'A' ? 'active' : 'void',
latitude: latitude,
longitude: longitude,
speedKnots: !isNaN(speed) ? speed : undefined,
course: !isNaN(course) ? course : undefined,
magneticVariation: !isNaN(magVar) ? magVar : undefined,
magneticVariationDirection: magVarDir,
modeIndicator: modeIndicator,
timestamp: new Date()
};
// Parse date and time if available
if (time && time.length >= 6 && date && date.length === 6) {
try {
const hours = parseInt(time.substring(0, 2));
const minutes = parseInt(time.substring(2, 4));
const seconds = parseFloat(time.substring(4));
const day = parseInt(date.substring(0, 2));
const month = parseInt(date.substring(2, 4));
const year = 2000 + parseInt(date.substring(4, 6)); // Convert YY to YYYY
if (!isNaN(hours) && !isNaN(minutes) && !isNaN(seconds) &&
!isNaN(day) && !isNaN(month) && !isNaN(year)) {
const utcDate = new Date(Date.UTC(year, month - 1, day, hours, minutes, Math.floor(seconds)));
updateData.gpsTime = utcDate.toISOString();
// Store RMC date/time information
this.rmcDateTime = {
utcDate: utcDate,
timestamp: new Date()
};
this.log(`🕐 RMC Time: ${utcDate.toISOString()} (UTC)`, 'info');
}
} catch (error) {
this.log(`⚠️ RMC date/time parsing error: ${error.message}`, 'error');
}
}
// Only update with valid data
const validData = Object.fromEntries(
Object.entries(updateData).filter(([_, value]) => value !== undefined)
);
if (Object.keys(validData).length > 1) { // More than just timestamp
this.updateGpsDisplay(validData);
}
// Log additional RMC information
const rmcInfo = [];
if (updateData.speedKnots !== undefined) {
rmcInfo.push(`Speed: ${updateData.speedKnots.toFixed(1)} knots`);
}
if (updateData.course !== undefined) {
rmcInfo.push(`Course: ${updateData.course.toFixed(1)}°`);
}
if (updateData.magneticVariation !== undefined) {
rmcInfo.push(`Mag Var: ${updateData.magneticVariation.toFixed(1)}° ${updateData.magneticVariationDirection || ''}`);
}
if (rmcInfo.length > 0) {
this.log(`🧭 RMC Data: ${rmcInfo.join(', ')}`, 'info');
}
}
}
parseGsvMessage(parts) {
// $GPGSV,total_messages,message_number,total_satellites,...
if (parts.length >= 4) {
const totalSatellites = parseInt(parts[3]);
if (!isNaN(totalSatellites)) {
this.updateGpsDisplay({
satellites: totalSatellites,
timestamp: new Date()
});
}
}
}
parseGsaMessage(parts) {
// $GPGSA,selection_mode,mode,satellite_ids...,pdop,hdop,vdop*checksum
if (parts.length >= 18) {
const mode = parseInt(parts[2]);
const hdop = parseFloat(parts[16]);
const vdop = parseFloat(parts[17]);
// Count active satellites (non-empty fields from positions 3-14)
let activeSatellites = 0;
for (let i = 3; i <= 14; i++) {
if (parts[i] && parts[i].trim() !== '') {
activeSatellites++;
}
}
this.updateGpsDisplay({
mode: mode, // 1 = no fix, 2 = 2D fix, 3 = 3D fix
activeSatellites: activeSatellites,
hdop: hdop,
vdop: vdop,
timestamp: new Date()
});
}
}
parseVtgMessage(parts) {
// $GPVTG,course,T,course,M,speed_knots,N,speed_kmh,K,mode*checksum
if (parts.length >= 10) {
const course = parseFloat(parts[1]);
const speedKnots = parseFloat(parts[5]);
const speedKmh = parseFloat(parts[7]);
this.updateGpsDisplay({
course: course,
speedKnots: speedKnots,
speedKmh: speedKmh,
timestamp: new Date()
});
}
}
parseGptxtMessage(parts) {
// $GPTXT,xx,xx,xx,text*checksum
// Format: $GPTXT,01,01,02,ANTSTATUS=OPEN*25
// Where:
// - Field 1: Total number of sentences
// - Field 2: Sentence number
// - Field 3: Text identifier (message type)
// - Field 4: Text message
if (parts.length >= 5) {
const totalSentences = parseInt(parts[1]);
const sentenceNumber = parseInt(parts[2]);
const textIdentifier = parseInt(parts[3]);
let textMessage = parts[4];
// Remove checksum if present
if (textMessage.includes('*')) {
textMessage = textMessage.split('*')[0];
}
// Parse common u-blox GPS text messages
this.parseUbloxTextMessage(textIdentifier, textMessage);
// Log the GPS text message
this.log(`📡 GPS Text (${sentenceNumber}/${totalSentences}): ${textMessage}`, 'info');
}
}
parseUbloxTextMessage(textId, message) {
// Parse specific u-blox GPS text message types
const gpsTextData = {
timestamp: new Date()
};
switch (textId) {
case 1: // Startup message
this.handleGpsStartupMessage(message);
break;
case 2: // Antenna status
this.handleGpsAntennaStatus(message);
break;
case 3: // Jamming status
this.handleGpsJammingStatus(message);
break;
case 4: // RF/AGC status
this.handleGpsRfStatus(message);
break;
case 5: // Navigation status
this.handleGpsNavStatus(message);
break;
case 6: // Time/Clock status
this.handleGpsTimeStatus(message);
break;
case 7: // Hardware status
this.handleGpsHardwareStatus(message);
break;
default:
// Generic text message handling
this.handleGpsGenericText(message);
break;
}
}
handleGpsStartupMessage(message) {
// Handle GPS startup messages
if (message.includes('STARTUP')) {
this.updateGpsDisplay({
status: 'starting',
timestamp: new Date()
});
this.log('🚀 GPS module starting up', 'info');
}
}
handleGpsAntennaStatus(message) {
// Handle antenna status messages
// Examples: "ANTSTATUS=OPEN", "ANTSTATUS=SHORT", "ANTSTATUS=OK"
if (message.includes('ANTSTATUS=')) {
const status = message.split('ANTSTATUS=')[1];
let antennaStatus = 'unknown';
let statusClass = 'invalid';
switch (status.toUpperCase()) {
case 'OK':
antennaStatus = 'OK';
statusClass = 'valid';
break;
case 'OPEN':
antennaStatus = 'Open Circuit';
statusClass = 'invalid';
break;
case 'SHORT':
antennaStatus = 'Short Circuit';
statusClass = 'invalid';
break;
default:
antennaStatus = status;
break;
}
this.updateGpsDisplay({
antennaStatus: antennaStatus,
antennaStatusClass: statusClass,
timestamp: new Date()
});
this.log(`📡 Antenna Status: ${antennaStatus}`, statusClass === 'valid' ? 'info' : 'error');
}
}
handleGpsJammingStatus(message) {
// Handle jamming/interference status
if (message.includes('JAMMING') || message.includes('INTERFERENCE')) {
const isJamming = message.includes('DETECTED') || message.includes('HIGH');
this.updateGpsDisplay({
jammingStatus: isJamming ? 'Detected' : 'Clear',
jammingStatusClass: isJamming ? 'invalid' : 'valid',
timestamp: new Date()
});
this.log(`📡 Jamming Status: ${isJamming ? 'Detected' : 'Clear'}`,
isJamming ? 'error' : 'info');
}
}
handleGpsRfStatus(message) {
// Handle RF/AGC status messages
if (message.includes('AGC') || message.includes('RF')) {
this.log(`📡 RF Status: ${message}`, 'info');
}
}
handleGpsNavStatus(message) {
// Handle navigation status messages
if (message.includes('NAV')) {
this.log(`📡 Navigation: ${message}`, 'info');
}
}
handleGpsTimeStatus(message) {
// Handle time/clock status messages
if (message.includes('TIME') || message.includes('CLOCK')) {
this.log(`📡 Time Status: ${message}`, 'info');
}
}
handleGpsHardwareStatus(message) {
// Handle hardware status messages
if (message.includes('HW') || message.includes('HARDWARE')) {
this.log(`📡 Hardware: ${message}`, 'info');
}
}
handleGpsGenericText(message) {
// Handle generic GPS text messages
this.log(`📡 GPS Info: ${message}`, 'info');
}
parseCoordinate(coordStr, direction) {
// Parse NMEA coordinate format (DDMM.mmmm or DDDMM.mmmm) to decimal degrees
if (!coordStr || coordStr === '' || !direction) {
return null;
}
try {
const coord = parseFloat(coordStr);
if (isNaN(coord)) {
return null;
}
// For latitude: DDMM.mmmm (degrees are first 2 digits)
// For longitude: DDDMM.mmmm (degrees are first 3 digits)
let degrees, minutes;
if (coordStr.length <= 9) {
// Latitude format: DDMM.mmmm
degrees = Math.floor(coord / 100);
minutes = coord - (degrees * 100);
} else {
// Longitude format: DDDMM.mmmm
degrees = Math.floor(coord / 100);
minutes = coord - (degrees * 100);
}
// Convert to decimal degrees
let decimalDegrees = degrees + (minutes / 60);
// Apply direction (negative for South and West)
if (direction === 'S' || direction === 'W') {
decimalDegrees = -decimalDegrees;
}
return parseFloat(decimalDegrees.toFixed(6));
} catch (error) {
this.log(`⚠️ Coordinate parsing error: ${error.message}`, 'error');
return null;
}
}
updateGpsDisplay(gpsData) {
if (gpsData.status !== undefined) {
const statusElement = this.configElements.gpsStatus;
statusElement.textContent = gpsData.status === 'active' ? 'Active' : 'No Fix';
statusElement.className = `status-badge ${gpsData.status === 'active' ? 'valid' : 'invalid'}`;
this.animateConfigUpdate(statusElement);
}
if (gpsData.quality !== undefined) {
const statusElement = this.configElements.gpsStatus;
const qualityText = ['No Fix', 'GPS', 'DGPS', 'PPS', 'RTK', 'Float RTK', 'Dead Reckoning', 'Manual', 'Simulation'][gpsData.quality] || 'Unknown';
statusElement.textContent = qualityText;
statusElement.className = `status-badge ${gpsData.quality > 0 ? 'valid' : 'invalid'}`;
this.animateConfigUpdate(statusElement);
}
if (gpsData.mode !== undefined) {
const statusElement = this.configElements.gpsStatus;
const modeText = ['No Fix', 'No Fix', '2D Fix', '3D Fix'][gpsData.mode] || 'Unknown';
statusElement.textContent = modeText;
statusElement.className = `status-badge ${gpsData.mode > 1 ? 'valid' : 'invalid'}`;
this.animateConfigUpdate(statusElement);
}
if (gpsData.satellites !== undefined) {
this.configElements.gpsSatellites.textContent = gpsData.satellites;
this.animateConfigUpdate(this.configElements.gpsSatellites);
}
if (gpsData.activeSatellites !== undefined) {
const current = this.configElements.gpsSatellites.textContent;
const display = current.includes('/') ?
`${gpsData.activeSatellites}/${current.split('/')[1]}` :
`${gpsData.activeSatellites}`;
this.configElements.gpsSatellites.textContent = display;
this.animateConfigUpdate(this.configElements.gpsSatellites);
}
if (gpsData.antennaStatus !== undefined) {
this.configElements.gpsAntenna.textContent = gpsData.antennaStatus;
this.configElements.gpsAntenna.className = `status-badge ${gpsData.antennaStatusClass || 'invalid'}`;
this.animateConfigUpdate(this.configElements.gpsAntenna);
}
if (gpsData.jammingStatus !== undefined) {
this.configElements.gpsJamming.textContent = gpsData.jammingStatus;
this.configElements.gpsJamming.className = `status-badge ${gpsData.jammingStatusClass || 'invalid'}`;
this.animateConfigUpdate(this.configElements.gpsJamming);
}
if (gpsData.latitude !== undefined && gpsData.latitude !== null) {
this.configElements.gpsLatitude.textContent = gpsData.latitude + '°';
this.animateConfigUpdate(this.configElements.gpsLatitude);
}
if (gpsData.longitude !== undefined && gpsData.longitude !== null) {
this.configElements.gpsLongitude.textContent = gpsData.longitude + '°';
this.animateConfigUpdate(this.configElements.gpsLongitude);
}
if (gpsData.altitude !== undefined && !isNaN(gpsData.altitude)) {
this.configElements.gpsAltitude.textContent = gpsData.altitude.toFixed(1) + 'm';
this.animateConfigUpdate(this.configElements.gpsAltitude);
}
if (gpsData.timestamp) {
const timeStr = gpsData.timestamp.toLocaleTimeString();
this.configElements.gpsTimestamp.textContent = timeStr;
this.animateConfigUpdate(this.configElements.gpsTimestamp);
}
if (gpsData.gpsTime) {
// Display GPS time from GNZDA message
const gpsTimeStr = new Date(gpsData.gpsTime).toLocaleString();
this.configElements.gpsTime.textContent = gpsTimeStr;
this.animateConfigUpdate(this.configElements.gpsTime);
}
if (gpsData.speedKnots !== undefined && !isNaN(gpsData.speedKnots)) {
this.configElements.gpsSpeed.textContent = gpsData.speedKnots.toFixed(1) + ' knots';
this.animateConfigUpdate(this.configElements.gpsSpeed);
}
if (gpsData.course !== undefined && !isNaN(gpsData.course)) {
this.configElements.gpsCourse.textContent = gpsData.course.toFixed(1) + '°';
this.animateConfigUpdate(this.configElements.gpsCourse);
}
if (gpsData.magneticVariation !== undefined && !isNaN(gpsData.magneticVariation)) {