forked from VA3HDL/ham-apps-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathham-apps-proxy.pl
executable file
·728 lines (613 loc) · 25.2 KB
/
ham-apps-proxy.pl
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
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
my $VER = '24.08.14';
my $ETIMEDOUT = 260;
my $EWOULDBLOCK = 11;
=pod
HAM-APPS-PROXY -- a HTTP => TCP/UDP/other proxy for Ham Radio applications.
Provide easy HTTP endpoints to hit various interfaces.
Author: David Westbrook K2DW
Contact: [email protected] or K2DW on POTA Slack workspace.
Support: Direct message or #proj-potaplus-browser-extension channel on POTA Slack workspace
ORIGINAL: Source & Documentation:
https://dwestbrook.net/projects/potaplus
FORK by VA3HDL:
https://github.com/VA3HDL/browsercat
=cut
=pod
cpanm PAR::Packer
cpanm Win32::OLE
cpanm IO::Socket::Timeout
cpanm HTTP::Server::Simple::CGI
pp -o ham-apps-proxy.exe -M IO::Socket::INET -M IO::Socket::Timeout -M Errno -M CGI -M HTTP::Server::Simple::CGI -M Win32::OLE ham-apps-proxy.pl
read registry to get the DXK/CMD port numbers
cmd-line options for host, port #s
Read registry to find "POTA" UDF and/or cmdline option for it.
App_DXKeeper_User_Defined_0
=cut
{
package MyWebServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
use Data::Dumper;
use IO::Socket::INET;
use IO::Socket::Timeout;
use Errno qw(ETIMEDOUT EWOULDBLOCK);
use File::Basename;
# use Win32::OLE;
use Hamlib;
print "Perl $], $Hamlib::hamlib_version\n";
#Hamlib::rig_set_debug($Hamlib::RIG_DEBUG_TRACE);
Hamlib::rig_set_debug($Hamlib::RIG_DEBUG_NONE);
my $rig = new Hamlib::Rig(2);
my $old_host = '';
my $old_port = 0;
my %dispatch = (
# omit trailing / on key names
'/version' => \&resp_version,
# LOGGING and RIG CONTROL
'/aclog' => \&resp_aclog,
# LOGGING
'/dxlab/dxkeeper' => \&resp_dxkeeper,
'/log4om' => \&resp_log4om,
'/logger32' => \&resp_logger32,
'/cqrlog' => \&resp_cqrlog,
# RIG CONTROL
'/dxlab/commander' => \&resp_commander,
'/omnirig' => \&resp_omnirig,
'/rigctl' => \&resp_rigctl,
# ROTOR CONTROL
'/pst' => \&resp_pst,
'/rotctl' => \&resp_rotctl,
);
sub stamp {
return scalar localtime() . ": ";
}
sub handle_request {
my $self = shift;
my $cgi = shift;
my $path = $cgi->path_info();
my $handler;
while ( !$handler && $path ) {
$handler = $dispatch{$path};
last unless $path =~ m#/#;
$path =~ s#/[^/]*$##;
}
if ( $cgi->request_method eq 'OPTIONS' ) {
# Handle OPTIONS request
print $cgi, 200, [
'Access-Control-Allow-Origin' =>
'*', # Replace "*" with the appropriate origin
'Access-Control-Allow-Methods' =>
'GET, POST, OPTIONS', # Include the allowed methods
'Access-Control-Allow-Headers' => 'Content-Type'
, # Include any additional headers used in the request
'Content-Length' => 0,
'Content-Type' => 'text/plain',
];
return;
}
if ( ref($handler) eq "CODE" ) {
print "HTTP/1.0 200 OK\r\n";
$handler->($cgi);
}
else {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header,
$cgi->start_html('Not found'),
$cgi->h1( 'Not found: ' . $cgi->path_info ),
$cgi->end_html;
# close $cgi->stdio_handle;
close $cgi->stdin_handle;
close $cgi->stdout_handle;
}
}
sub ADIF {
my ( $k, $v ) = @_;
return sprintf '<%s:%d>%s', $k, length($v), $v;
}
sub writeINET {
my ( $proto, $host, $port, $msg ) = @_;
my $socket = new IO::Socket::INET(
PeerHost => $host,
PeerPort => $port,
Proto => $proto,
Timeout => 2,
);
warn "writeINET [$proto:$host:$port] CANNOT CONNECT: $!" if !$socket;
return 0, "cannot connect to the server $!" unless $socket;
IO::Socket::Timeout->enable_timeouts_on($socket);
$socket->write_timeout(0.25);
$socket->read_timeout(0.25);
warn stamp, "writeINET [$proto:$host:$port] $msg";
print $socket $msg;
# $socket->shutdown(SHUT_WR);
#warn "reading...";
my $response = "";
$response .= $_ while <$socket>;
# my $response = <$socket>;
#warn "done.";
if ( !$response && ( 0 + $! == $ETIMEDOUT || 0 + $! == $EWOULDBLOCK ) )
{
#warn "READ TIMEOUT";
return 0, "timeout reading on the socket";
}
# $socket->shutdown(SHUT_RDWR);
return 1, $response;
}
sub resp_version {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
print $cgi->header(
-type => 'application/json',
-nph => 1,
-status => 200,
-Access_Control_Allow_Origin => '*',
);
print qq!{"version":"$VER"}!;
}
# http://localhost:8073/dxlab/commander/CmdSetFreqMode?xcvrfreq=14080&xcvrmode=RTTY&preservesplitanddual=N
# ORDER MATTERS for the parameters ... Commander parsing seems strict on the payload order (even though it's ADIF)
sub resp_commander {
# http://www.dxlabsuite.com/commander/Commander%20TCPIP%20Messages.pdf
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
return if !scalar @k; # no params
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 52002;
# $p{xcvrmode} = $p{xcvrfreq} >= 10000 ? 'USB' : 'LSB' if $p{xcvrfreq} && $p{xcvrmode} eq 'SSB';
my $parameters = join '', map { ADIF( $_, $p{$_} ) } @k
# sort keys %p
;
$parameters .= '<EOR>';
my $msg = join '', ADIF( 'command', $command ),
ADIF( 'parameters', $parameters );
my ( $ok, $response ) = writeINET( 'tcp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
}
# http://localhost:8073/dxlab/dxkeeper/log?CALL=P5DX&RST_SENT=599&RST_RCVD=599&FREQ=1.84027&BAND=160M&MODE=CW&QSO_DATE=20170515&TIME_ON=210700&STATION_CALLSIGN=AA6YQ&TX_PWR=1500
sub resp_dxkeeper {
# http://www.dxlabsuite.com/dxkeeper/DXKeeper%20TCPIP%20Messages%20v1.pdf
# <command:3>log<parameters:170><CALL:4>P5DX <RST_SENT:3>599 <RST_RCVD:3>599
# <FREQ:7>1.84027 <BAND:4>160M <MODE:2>CW <QSO_DATE:8>20170515 <TIME_ON:6>210700
# <STATION_CALLSIGN:5>AA6YQ <TX_PWR:4>1500 <EOR>
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
return if !scalar @k; # no params
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 52001;
my $parameters = join '', map { ADIF( $_, $p{$_} ) } @k
# sort keys %p
;
$parameters .= '<EOR>';
my $msg = join '', ADIF( 'command', $command ),
ADIF( 'parameters', $parameters );
my ( $ok, $response ) = writeINET( 'tcp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
);
print $response;
}
# http://localhost:8073/omnirig/qsy?freq=7200000&mode=LSB
sub resp_omnirig {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
my @k = grep { !/^__/ } $cgi->param;
return if !scalar @k; # no params
my $objectId =
$cgi->param('__host') || '0839E8C6-ED30-4950-8087-966F970F0CAE';
my $rigName = $cgi->param('__port') || 'Rig1';
print "OmniRig '$rigName' [$objectId]\n";
eval {
# {0839E8C6-ED30-4950-8087-966F970F0CAE}
# OmniRig.OmniRigX
my $OmniRig;
# use existing instance if OmniRig is already running
eval { $OmniRig = Win32::OLE->GetActiveObject("{$objectId}") };
die "OmniRig not installed" if $@;
$OmniRig ||= eval {
Win32::OLE->new( "{$objectId}", sub { $_[0]->Quit; } );
} or do {
warn "Oops, cannot start OmniRig";
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => 500,
-Access_Control_Allow_Origin => '*',
);
return;
};
my %modes = (
USB => 0x02000000,
LSB => 0x04000000,
'DATA-U' => 0x08000000,
'DATA-L' => 0x10000000,
AM => 0x20000000,
FM => 0x40000000,
CW => 0x00800000,
'CW-U' => 0x00800000,
'CW-L' => 0x01000000,
);
$modes{SSB} = $modes{USB};
$modes{DATA} = $modes{'DATA-U'};
my $freq = $cgi->param('freq'); # Hz
my $mode = $cgi->param('mode') || 'USB';
my $modeCode = $modes{$mode};
warn stamp, @_,
" OmniRig $rigName QSY == $freq Hz == $mode ($modeCode)\n";
# order matters -- change mode first, then freq. Because the mode might carry an offset in omnirig/radio.
$OmniRig->$rigName->{Mode} = $modeCode;
$OmniRig->$rigName->{FreqA} = $freq;
undef $OmniRig; # destroy OmniRig
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => 200,
-Access_Control_Allow_Origin => '*',
);
}
or warn Dumper( $objectId, $rigName, $cgi->path_info, { $cgi->Vars },
$@, );
}
=pod
OMNIRIG CONSTANTS
PM_UNKNOWN = $00000001;
PM_FREQ = $00000002;
PM_FREQA = $00000004;
PM_FREQB = $00000008;
PM_PITCH = $00000010;
PM_RITOFFSET = $00000020;
PM_RIT0 = $00000040;
PM_VFOAA = $00000080;
PM_VFOAB = $00000100;
PM_VFOBA = $00000200;
PM_VFOBB = $00000400;
PM_VFOA = $00000800;
PM_VFOB = $00001000;
PM_VFOEQUAL = $00002000;
PM_VFOSWAP = $00004000;
PM_SPLITON = $00008000;
PM_SPLITOFF = $00010000;
PM_RITON = $00020000;
PM_RITOFF = $00040000;
PM_XITON = $00080000;
PM_XITOFF = $00100000;
PM_RX = $00200000;
PM_TX = $00400000;
PM_CW_U = $00800000;
PM_CW_L = $01000000;
PM_SSB_U = $02000000;
PM_SSB_L = $04000000;
PM_DIG_U = $08000000;
PM_DIG_L = $10000000;
PM_AM = $20000000;
PM_FM = $40000000;
=cut
# http://localhost:8073/log4om/log?CALL=P5DX&RST_SENT=599&RST_RCVD=599&FREQ=1.84027&BAND=160M&MODE=CW&QSO_DATE=20170515&TIME_ON=210700&STATION_CALLSIGN=AA6YQ&TX_PWR=1500
sub resp_log4om {
my $cgi = shift;
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 2239;
my $parameters = join '', map { ADIF( $_, $p{$_} ) } @k;
$parameters .= '<EOR>';
my $msg = join '', ADIF( 'command', $command ),
ADIF( 'parameters', $parameters );
my ( $ok, $response ) = writeINET( 'udp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
}
sub MARKUPTAG {
my ( $k, $v ) = @_;
return sprintf '<%s>%s</%s>', $k, $v, $k;
}
sub VALUETAG {
my ( $k, $v ) = @_;
return sprintf '%s', $k;
}
# http://localhost:8073/aclog/updateandlog?CALL=WC3N&BAND=20&MODE=FT8&FREQ=14.074&RSTR=599&RSTS=488&GRID=FM19&POWER=100&DATE=2021/04/25&TIMEON=12:34&TIMEOFF=13:03
# http://localhost:8073/aclog/changefreq?value=21.446&suppressmodedefault=TRUE
# http://localhost:8073/aclog/changemode?value=RTTY
sub resp_aclog {
# http://www.n3fjp.com/help/api.html
# requires v1.7 for <UPDATEANDLOG>
# <CMD><UPDATE><CONTROL>TXTENTRYCOMMENTS</CONTROL><VALUE>" . $p{"COMMENTS"} . "</VALUE></CMD>
# <CMD><UPDATEANDLOG><CALL>WC3N</CALL><BAND>20</BAND><MODE>FT8</MODE><FREQ>14.074<RSTR>599</RSTR><RSTS>488</RSTS><GRID>FM19</GRID><POWER>100</POWER><DATE>2021/04/25</DATE><TIMEON>12:34</TIMEON><TIMEOFF>13:03</TIMEOFF></CMD>
# <CMD><CHANGEFREQ><VALUE>21.446</VALUE><SUPPRESSMODEDEFAULT>TRUE</SUPPRESSMODEDEFAULT></CMD>
# <CMD><CHANGEMODE><VALUE>RTTY</VALUE></CMD>
my $cgi = shift;
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 1100;
my $parameters = join '', map { MARKUPTAG( uc($_), $p{$_} ) } @k;
my $msg = '';
if (exists $p{'COMMENTS'}) {
#aclog needs the comments set before the update and log command
$msg .= "<CMD><UPDATE><CONTROL>TXTENTRYCOMMENTS</CONTROL><VALUE>" . $p{"COMMENTS"} . "</VALUE></CMD>\r\n"
}
$msg .=
MARKUPTAG( 'CMD', "<" . uc($command) . ">$parameters" ) . "\r\n";
my ( $ok, $response ) = writeINET( 'tcp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
}
sub resp_cqrlog {
my $cgi = shift;
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 2333;
my $msg = join '', map { ADIF( $_, $p{$_} ) } @k;
$msg .= '<EOR>';
my ( $ok, $response ) = writeINET( 'udp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
# Log ADIF to file for backup purposes
my $script_path = dirname(__FILE__); # Get the script's directory path
# my $filename = "$script_path/myfile.txt"; # Create the file path
my $filename = "$script_path/logfile.adi";
open( my $fh, '>>:encoding(UTF-8)', $filename )
or die "Could not open file '$filename'";
print $fh join '', ADIF( 'log', $msg ), "\n";
close $fh;
}
=pod
d8888b. d888888b d888b .o88b. d888888b db
88 `8D `88' 88' Y8b d8P Y8 `~~88~~' 88
88oobY' 88 88 8P 88 88
88`8b 88 88 ooo 8b 88 88
88 `88. .88. 88. ~8~ Y8b d8 88 88booo.
88 YD Y888888P Y888P `Y88P' YP Y88888P
=cut
sub resp_rigctl {
# Example requests:
# set freq http://localhost:8073/rigctl/F?14248000&__port=4531
# set mode http://localhost:8073/rigctl/M?USB%202800&__port=4531
# get freq http://localhost:8073/rigctl/f?&__port=4531
# get az&el http://localhost:8073/rotctl/p?&__port=4535
#
my $cgi = shift;
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 4532;
warn "\n", stamp, "► RIGCTL Path: ", $cgi->path_info, " ► Command: ",
$command, " ► k: ", @k, " ► p: ", %p;
if ( $old_host eq '' || $old_port eq 0 ) {
# warn stamp, "RIGCTL rig not open";
$old_host = $host;
$old_port = $port;
$rig->set_conf( "rig_pathname", "$host:$port" );
$rig->open();
}
else {
# warn stamp, "RIGCTL rig defined";
}
if ( $old_host ne $host || $old_port ne $port ) {
# warn stamp, "RIGCTL rig pathname changed $old_host:$old_port to $host:$port";
$old_host = $host;
$old_port = $port;
$rig->close();
$rig->set_conf( "rig_pathname", "$host:$port" );
$rig->open();
}
else {
# warn stamp, "RIGCTL rig pathname no change";
}
my %modes = (
SSB => $Hamlib::RIG_MODE_USB,
USB => $Hamlib::RIG_MODE_USB,
LSB => $Hamlib::RIG_MODE_LSB,
DATA => $Hamlib::RIG_MODE_PKTUSB,
'DATA-U' => $Hamlib::RIG_MODE_PKTUSB,
'DATA-L' => $Hamlib::RIG_MODE_PKTLSB,
AM => $Hamlib::RIG_MODE_AM,
FM => $Hamlib::RIG_MODE_FM,
CW => $Hamlib::RIG_MODE_CW,
'CW-U' => $Hamlib::RIG_MODE_CW,
'CW-L' => $Hamlib::RIG_MODE_CWR,
);
# $modes{SSB} = $modes{USB};
# $modes{DATA} = $modes{'DATA-U'};
my $parameters = join '', map { VALUETAG( $_, $p{$_} ) } @k;
# Define the mode map
my %mode_map = (
$Hamlib::RIG_MODE_NONE => 'NONE',
$Hamlib::RIG_MODE_AM => 'AM',
$Hamlib::RIG_MODE_CW => 'CW',
$Hamlib::RIG_MODE_CWR => 'CWR',
$Hamlib::RIG_MODE_LSB => 'LSB',
$Hamlib::RIG_MODE_USB => 'USB',
$Hamlib::RIG_MODE_FM => 'FM',
$Hamlib::RIG_MODE_WFM => 'WFM',
$Hamlib::RIG_MODE_RTTY => 'RTTY',
$Hamlib::RIG_MODE_RTTYR => 'RTTYR',
$Hamlib::RIG_MODE_PKTLSB => 'PKTLSB',
$Hamlib::RIG_MODE_PKTUSB => 'PKTUSB',
$Hamlib::RIG_MODE_PKTFM => 'PKTFM',
$Hamlib::RIG_MODE_ECSSLSB => 'ECSSLSB',
$Hamlib::RIG_MODE_ECSSUSB => 'ECSSUSB',
$Hamlib::RIG_MODE_FMN => 'FMN',
$Hamlib::RIG_MODE_DSB => 'DSB',
);
if ( $command eq "f" ) {
my $freq = $rig->get_freq($Hamlib::RIG_VFO_CURR) / 1000000;
(my $mode, my $width) = $rig->get_mode($Hamlib::RIG_VFO_CURR);
my $mode_text = $mode_map{$mode} // 'UNKNOWN';
warn stamp, "VFO Freq: $freq", " VFO Mode: $mode_text", " VFO Width: $width";
print $cgi->header(
-type => 'application/json',
-nph => 1,
-status => 200,
-Access_Control_Allow_Origin => '*',
);
print qq!{"frequency":"$freq","mode":"$mode_text","width":"$width"}!;
}
if ( $command eq "F" && $parameters gt 0 ) {
warn stamp, "RIGCTL Command: $command Value: $parameters";
$rig->set_freq( $Hamlib::RIG_VFO_CURR, $parameters );
my ( $ok, $response ) = ( 200, "Success" );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
}
if ( $command eq "M" && $parameters gt 0 ) {
my $mode = ( split ' ', $parameters )[0];
my $modeCode = $modes{$mode};
my $bw = ( split ' ', $parameters )[-1];
warn stamp, "RIGCTL Command: $command Value: $parameters Mode: $mode ModeCode: $modeCode Bandwidth: $bw";
$rig->set_mode( $modeCode, $bw, $Hamlib::RIG_VFO_CURR );
my ( $ok, $response ) = ( 200, "Success" );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
}
}
# http://localhost:8073/logger32/log?CALL=P5DX&RST_SENT=599&RST_RCVD=599&FREQ=1.84027&BAND=160M&MODE=CW&QSO_DATE=20170515&TIME_ON=210700&STATION_CALLSIGN=AA6YQ&TX_PWR=1500
sub resp_logger32 {
# https://www.logger32.net/files/Logger32_v4_User_Manual.pdf Sections 31 & 32
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 52001;
my $parameters = join '', map { ADIF( $_, $p{$_} ) } @k
# sort keys %p
;
my $msg = $parameters;
$msg .= '<APP_TAB>' if $command eq 'log';
$msg .= '<EOR>';
my ( $ok, $response ) = writeINET( 'tcp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
);
print $response;
}
# http://localhost:8073/pst?QRA=FN32bt
# http://localhost:8073/pst?AZIMUTH=180
sub resp_pst {
# PstRotatorAz -- Software for Antenna Rotators User�s Manual Rev. 2.9 -- page 12
# <PST><AZIMUTH>85</AZIMUTH></PST> - set azimuth to 85 deg
# <PST><QRA>KN34BJ</QRA></PST> - send to program the locator (4/6/8 digits) and set the azimuth
# UDP port 12000
# port + 1 for antenna position
# <PST>AZ?</PST> - in answer to this command the program will report the antenna position to UDP Port + 1
# PstRotatorAz reports the antenna position like this:
# AZ:xxx<CR>
my $cgi = shift;
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 12000;
my $parameters = join '', map { MARKUPTAG( uc($_), $p{$_} ) } @k;
my $msg = MARKUPTAG( 'PST', $parameters ) . "\r\n";
my ( $ok, $response ) = writeINET( 'udp', $host, $port, $msg );
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
}
#
#
# d8888b. .d88b. d888888b .o88b. d888888b db
# 88 `8D .8P Y8. `~~88~~' d8P Y8 `~~88~~' 88
# 88oobY' 88 88 88 8P 88 88
# 88`8b 88 88 88 8b 88 88
# 88 `88. `8b d8' 88 Y8b d8 88 88booo.
# 88 YD `Y88P' YP `Y88P' YP Y88888P
#
#
sub resp_rotctl {
my $cgi = shift;
return if !ref $cgi;
my $command = ( split '/', $cgi->path_info )[-1];
my @k = grep { !/^__/ } $cgi->param;
my %p = $cgi->Vars;
my $host = delete $p{__host} || 'localhost';
my $port = delete $p{__port} || 4535;
my $parameters = join '', map { VALUETAG( $_, $p{$_} ) } @k;
my $msg = $command . " $parameters" . "\n";
my ( $ok, $response ) = writeINET( 'tcp', $host, $port, $msg );
if ( $command eq "P" && $parameters gt 0 ) {
print $cgi->header(
-type => 'text/plain',
-nph => 1,
-status => ( $ok ? 200 : 500 ),
-Access_Control_Allow_Origin => '*',
);
print $response;
}
if ( $command eq "p" ) {
print $cgi->header(
-type => 'application/json',
-nph => 1,
-status => 200,
-Access_Control_Allow_Origin => '*',
);
my $elevation = ( split '\n', $response )[-1];
my $azimuth = ( split '\n', $response )[0];
print qq!{"azimuth":"$azimuth","elevation":"$elevation"}!;
}
}
} # package
print "ham-apps-proxy v$VER\n";
my $port = shift(@ARGV) || 8073;
# my $pid = MyWebServer->new($port)->background();
my $pid = MyWebServer->new($port)->run();