6 use Data::Dump qw(dump);
13 my $socket = IO::Socket::INET->new(
20 print "$0 waiting for client connection on port 4096\n";
25 BW => 0.0, # just paper cost
26 COLOR => 3.99, # FIXME
28 COPY => 0, # no additional price other than paper
32 my $next_nop_t = time() + 5;
35 our $client_socket = $socket->accept();
38 my $text = join('', @_);
40 print $client_socket "$text\r\n";
44 #my $line = <$client_socket>;
47 my $timeout = $next_nop_t - time();
50 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
52 warn "# NOP alarm $timeout";
53 $line = <$client_socket>;
59 $line = <$client_socket>;
62 $line = <$client_socket>;
65 if ( defined $line ) {
66 $line =~ s/[\r\n]+$//;
69 warn "<< NULL ", $client_socket->connected ? '' : 'NOT ', "connected";
75 # get the host and port number of newly connected client.
76 my $peer_address = $client_socket->peerhost();
77 my $peer_port = $client_socket->peerport();
79 print "Connection from: $peer_address:$peer_port\n";
82 my $total_charged = 0.00;
86 $v = $_[0] if defined $_[0];
87 return sprintf "%1.2f kn", $v;
90 while ($client_socket->connected) {
92 my $line = client_line;
93 last if ! defined $line;
95 if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) {
96 my ($version,$serial) = ($1,$2);
98 #client_send ".SQ FAILED message";
99 } elsif ( $line =~ m/^\.CFG/ ) {
100 client_send ".CFG OK %s";
101 } elsif ( $line =~ m/\.SERVER LIST/ ) {
102 client_send ".ERROR NO-ENTERPRISE";
103 } elsif ( $line =~ m/\.CARD (\S+)/ ) {
105 client_send ".CARD OK Ime Prezime (nobody\@example.com)";
106 } elsif ( $line =~ m/\.PIN (\S+)/ ) {
108 client_send ".PIN OK Ime Pinzime (nobody\@example.com)";
109 } elsif ( $line =~ m/\.ACTION$/ ) {
110 # CMENUS0 - no printer
111 client_send ".ACTION CMENUS68"; # FIXME can be CMENUS2
113 } elsif ( $line =~ m/\.ACTION COPY/ ) {
114 client_send ".ACTION COPY"; # safeq sends this twice
115 client_send ".COPY Mozete kopirati |".credit;
117 client_send ".CREDIT ".credit;
118 } elsif ( $line =~ m/\.COPY (.+)/ ) {
121 foreach ( split(/,/,$1) ) {
122 die "can't find [$_] in prices=",dump($prices) unless exists $prices->{$_};
123 $charge += $prices->{$_};
125 warn "CHARGE: $charge\n";
127 $total_charged += $charge;
129 client_send ".CREDIT ".credit;
130 client_send ".COPY $total_pages"; # page copied
133 } elsif ( $line =~ m/\.ACTION LIST/ ) {
135 client_send "2"; # nr of items in list
136 # status: 0 - pendng/3 - printed
140 client_send "3|1|Koha online catalog|XWC7232";
141 client_send "0|1|Koha online catalog|XWC5225";
144 } elsif ( $line =~ m/\.ACTION PRINT (ALL|\d+)/ ) {
146 my $job = $1 if $1 =~ m/^\d+$/; # 0 means print all?
148 my $charge = $prices->{'A4'} || die "no A4 price";
152 if ( $nr_jobs == 0 ) {
153 client_send ".ACTION NOJOB Nema se šta tiskat";
158 warn "FIXME $line\n";
159 client_send ".ACTION PRINT"; # device locked from terminal screen?
161 # check if printer ready
162 my $printer_ready = 0;
163 if ( ! $printer_ready ) {
164 client_send ".WARN 1/1|The printer is not ready|job has been suspended ... (1x)";
168 my $send = 0; # 0 .. 100
169 my $printed = 0; # 0 .. nr pages
171 # total pages in batch
175 client_send ".PRINT 1|1/1|Microsoft Word - molba_opca";
176 client_send ".NOP S $send C 0";
178 # open 10.60.3.25:9100
181 client_send ".NOP S $send C 0";
182 client_send ".MSG Please check display of device" if $send == 100;
184 # check smtp counters to be sure page is printed
187 $total_charged += $charge;
190 client_send ".DONE $nr_jobs $total_pages ".credit($total_charged);
192 } elsif ( $line =~ m/^\.NOP/ ) {
193 # XXX it's important to sleep, before sending response or
194 # interface on terminal device will be unresponsive
195 $next_nop_t = time() + 5; # NOP every 5s?
196 } elsif ( $line =~ m/^\.END/ ) {
197 client_send ".DONE BLK WAIT";
199 my $nr_jobs = 0; # FIXME
200 client_send ".DONE $total_pages ".credit($total_charged);
201 # $client_socket->close;
202 } elsif (defined $line) {
203 warn "UNKNOWN: ",dump($line);
209 warn "NULL line ", $client_socket->connected ? '' : 'NOT ', "connected";
212 warn "# return to accept";