3f6610d51d37826f78205f57583b0a1afad447b6
[safeq] / terminal-server.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 use Data::Dump qw(dump);
7
8 use IO::Socket::INET;
9 use Time::HiRes;
10
11 $| = 1;
12
13 my $socket = IO::Socket::INET->new(
14         LocalPort => 4096,
15         Proto => 'tcp',
16         Listen => 5,
17         Reuse => 1
18 ) or die "ERROR: $!";
19
20 print "$0 waiting for client connection on port 4096\n";
21
22 my $prices = {
23         A3 => 0.3, # FIXME
24         A4 => 0.2,
25         BW => 0.0, # just paper cost
26         COLOR => 3.99, # FIXME
27         DUPLEX => -0.05,
28 };
29
30
31 my $next_nop_t = time() + 5;
32
33 while(1) {
34         our $client_socket = $socket->accept();
35
36         sub client_send {
37                 my $text = join('', @_);
38                 warn ">> $text\n";
39                 print $client_socket "$text\r\n";
40         }
41
42         sub client_line {
43                 #my $line = <$client_socket>;
44
45                 my $line;
46                 my $timeout = $next_nop_t - time();
47                 if ( $timeout > 0 ) {
48                         eval {
49                                 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
50                                 alarm $timeout;
51                                 warn "# NOP alarm $timeout";
52                                 $line = <$client_socket>;
53                                 alarm 0;
54                         };
55                         if ($@) {
56                                 # timed out
57                                 client_send ".NOP";
58                                 $line = <$client_socket>;
59                         }
60                 } else {
61                         $line = <$client_socket>;
62                 }
63
64                 if ( defined $line ) {
65                         $line =~ s/[\r\n]+$//;
66                         warn "<< $line\n";
67                 } else {
68                         warn "<< NULL ", $client_socket->connected ? '' : 'NOT ', "connected";
69                 }
70
71                 return $line;
72         }
73
74         # get the host and port number of newly connected client.
75         my $peer_address = $client_socket->peerhost();
76         my $peer_port = $client_socket->peerport();
77
78         print "Connection from: $peer_address:$peer_port\n";
79
80         my $credit = 3.30;
81         my $total_charged = 0.00;
82         my $total_pages   = 0;
83         sub credit {
84                 my $v = $credit;
85                 $v = $_[0] if defined $_[0];
86                 return sprintf "%1.2f kn", $v;
87         }
88
89         while ($client_socket->connected) {
90
91                 my $line = client_line;
92                 last if ! defined $line;
93
94                 if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) {
95                         my ($version,$serial) = ($1,$2);
96                         client_send  ".SQ OK";
97                         #client_send  ".SQ FAILED message";
98                 } elsif ( $line =~ m/^\.CFG/ ) {
99                         client_send  ".CFG OK %s";
100                 } elsif ( $line =~ m/\.SERVER LIST/ ) {
101                         client_send  ".ERROR NO-ENTERPRISE";
102                 } elsif ( $line =~ m/\.CARD (\S+)/ ) {
103                         my ($rfid_sid) = $1;
104                         client_send  ".CARD OK Ime Prezime (nobody\@example.com)";
105                 } elsif ( $line =~ m/\.PIN (\S+)/ ) {
106                         my ($pin) = $1;
107                         client_send  ".PIN OK Ime Pinzime (nobody\@example.com)";
108                 } elsif ( $line =~ m/\.ACTION$/ ) {
109                         # CMENUS0 - no printer
110                         client_send  ".ACTION CMENUS68"; # FIXME can be CMENUS2
111
112                 } elsif ( $line =~ m/\.ACTION COPY/ ) {
113                         client_send  ".ACTION COPY";    # safeq sends this twice
114                         client_send  ".COPY Mozete kopirati |".credit;
115                         client_send  ".NOP";
116                         client_send  ".CREDIT ".credit;
117                 } elsif ( $line =~ m/\.COPY (.+)/ ) {
118                         # FIXME
119                         my $charge = 0;
120                         foreach ( split(/,/,$1) ) {
121                                 die "can't find [$_] in prices=",dump($prices) unless exists $prices->{$_};
122                                 $charge += $prices->{$_};
123                         }
124                         warn "CHARGE: $charge\n";
125                         $credit        -= $charge;
126                         $total_charged += $charge;
127                         $total_pages++;
128                         client_send ".CREDIT ".credit;
129                         client_send ".COPY 1"; # I verified that you are allowed to copy 1 page?
130                         client_send ".NOP";
131
132                 } elsif ( $line =~ m/\.ACTION LIST/ ) {
133
134                         client_send "2"; # nr of items in list
135                         #            status: 0 - pendng/3 - printed
136                         #            | pages
137                         #            | | title
138                         #            | | |                   queue
139                         client_send "3|1|Koha online catalog|XWC7232";
140                         client_send "0|1|Koha online catalog|XWC5225";
141                         # FIXME
142
143                 } elsif ( $line =~ m/\.ACTION PRINT (ALL|\d+)/ ) {
144                         my $what = $1;
145                         my $job = $1 if $1 =~ m/^\d+$/; # 0 means print all?
146
147                         my $charge = $prices->{'A4'} || die "no A4 price";
148
149                         my $nr_jobs = 2;
150
151                         if ( $nr_jobs == 0 ) {
152                                 client_send ".ACTION NOJOB Nema se ┼íta tiskat";
153                                 next;
154                         }
155
156                         # FIXME
157                         warn "FIXME $line\n";
158                         client_send ".ACTION PRINT"; # device locked from terminal screen?
159
160                         # check if printer ready
161                         my $printer_ready = 0;
162                         if ( ! $printer_ready ) {
163                                 client_send ".WARN 1/1|The printer is not ready|job has been suspended ... (1x)";
164                                 next;
165                         }
166
167                         my $send = 0; # 0 .. 100
168                         my $printed = 0; # 0 .. nr pages
169
170                         #                   total pages in batch
171                         #                   | page/batch
172                         #                   | |   title
173                         #                   | |   |
174                         client_send ".PRINT 1|1/1|Microsoft Word - molba_opca";
175                         client_send ".NOP S $send C 0";
176
177                         # open 10.60.3.25:9100
178                         $send = 100;
179
180                         client_send ".NOP S $send C 0";
181                         client_send ".MSG Please check display of device" if $send == 100;
182
183                         # check smtp counters to be sure page is printed
184
185                         $credit        -= $charge;
186                         $total_charged += $charge;
187                         $total_pages++;
188
189                         client_send ".DONE $nr_jobs $total_pages ".credit($total_charged);
190
191                 } elsif ( $line =~ m/^\.NOP/ ) {
192                         # XXX it's important to sleep, before sending response or
193                         # interface on terminal device will be unresponsive
194                         $next_nop_t = time() + 5; # NOP every 5s?
195                 } elsif ( $line =~ m/^\.END/ ) {
196                         client_send ".DONE BLK WAIT";
197                         client_send ".NOP";
198                         my $nr_jobs = 0; # FIXME
199                         client_send ".DONE $nr_jobs $total_pages ".credit($total_charged);
200 #                       $client_socket->close;
201                 } elsif (defined $line) {
202                         warn "UNKNOWN: ",dump($line);
203                         print "Response>";
204                         my $r = <STDIN>;
205                         chomp $r;
206                         client_send $r;
207                 } else {
208                         warn "NULL line ", $client_socket->connected ? '' : 'NOT ', "connected";
209                 }
210         }
211         warn "# return to accept";
212 }
213
214 $socket->close();
215
216