RFID download configuration for current IP
[koha.git] / ffzg / rfid / download.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use CGI;
7 use JSON;
8 use FindBin;
9 use IO::Socket::INET;
10 use Modern::Perl;
11
12 my $query = new CGI;
13
14 use Data::Dump qw(dump);
15 my $v = $query->Vars();
16 warn "# v ", dump( $v );
17
18 my $download = $query->param('download');
19 my $remote_host = $query->remote_host;
20 my $server_name = $query->server_name;
21
22 sub get_port {
23                 my $path = "download/$remote_host";
24                 my $port = 0;
25                 if ( -e $path ) {
26                         open(my $fh, '<', $path);
27                         $port = <$fh>;
28                 } else {
29                         open(my $fh, '>', $path);
30
31                         my @nr = glob('reader/*:*');
32                         my $nr = scalar @nr;
33                         warn "# nr = $nr\n";
34                         $port = 9100 + $nr;
35                         print $fh $port;
36                         close($fh);
37                 }
38         return ( $port, $port - 100 );
39 }
40
41 my ( $serial_port, $json_port ) = get_port;
42 my $koha_url = "https://ffzg.koha-dev.rot13.org:8443/cgi-bin/koha/ffzg/rfid/reader/$server_name:$json_port/mainpage.pl";
43
44 if ( ! $download ) {
45         print $query->header, '<html><head><title>', $remote_host, '</title></head><body>';
46 }
47
48 if ( $remote_host !~ m/^10\.60\./ ) {
49         print qq{http://rfid.koha-dev.vbz.ffzg.hr/register.pl not on intranet but on };
50         die $remote_host;
51 } elsif ( $download ) {
52         print qq{Content-type: applicaton/binary\r\ncontent-disposition: attachment; filename="$download"\r\n\r\n};
53         if ( $download eq 'com2tcp.exe' ) {
54                 open(my $fh, '<', '/srv/Biblio-RFID/com2tcp-1.3.0.0-386/com2tcp.exe');
55                 binmode $fh;
56                 binmode STDOUT;
57                 local $/ = undef;
58                 print <$fh>;
59                 close($fh);
60                 exit 0;
61         } elsif ( $download eq 'rfid.bat' ) {
62                 my $bat = qq{:loop\r\ncom2tcp.exe --ignore-dsr --baud 19200 \\\\.\\com2 $server_name $serial_port\r\ngoto loop\r\n};
63                 print $bat;
64                 warn "BAT: ",dump($bat);
65                 exit 0;
66         } else {
67                 die "unknown download $download";
68         }
69
70 } else {
71
72         print qq{
73 <h1>Create rfid directory</h1>
74 <h1>Download files to the rfid directory</h1>
75 <ol>
76 <li><a href="?download=com2tcp.exe">com2tcp.exe</a> serial port redirector</li>
77 <li><a href="?download=rfid.bat">rfid.bat</a> script to start it</li>
78 </ol>
79 <h1>Create shortcut to rfid.bat on desktop<h1>
80 <h1>Run shortcut rfid.bat</h1>
81 <h1>Chceck if readedr works</h1>
82 <a href="$koha_url">$koha_url</a>
83 }
84
85 }