#!/usr/bin/perl { package MyWebServer; # https://metacpan.org/pod/HTTP::Server::Simple # curl --header "Content-Type: application/json" --request POST --data '{ "type":"cardCheck", "date":"1482307795601", "lang": "EN", "machineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" }' http://localhost:2999 use lib qw (/home/pi/cashcade /home/gary/projects/cashcade); use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); # use HTTP::Server::Simple; # use base qw(HTTP::Server::Simple); use Data::Dumper; use JSON; sub handle_request { my $self = shift; my $cgi = shift; my $json_string = $cgi->param('POSTDATA'); # { "type":"cardCheck", "date":"1482307795601", "lang": "EN", "machineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" } print "json_string=$json_string\n"; $json_dict = decode_json $json_string; print "json_dict=" . Dumper($json_dict) . "\n"; my $msg = $json_dict->{param}->{POSTDATA}->[0]; # my $response = $self->{kiosk}->process($msg); # note: this says I cannot call on undefined, the problme is, how do I pass the object in # print "response=$response\n"; return $msg; } } sub sigIntHandler { exit; } END { print "MAIN: END*END*END*END*END*END*END*END*END*END*END*END*END*END\n"; $kiosk->disconnect(); } my $kiosk_port = shift // 'unknown'; die "FATAL ERROR: Need to specify a port\n" unless ($kiosk_port ne 'unknown'); my $placard = shift // 'unknown'; die "FATAL ERROR: Need to specify a placard\n" unless ($placard ne 'unknown'); my $debug_level = shift // 9; # TODO: make level higher on final print "$placard, $debug_level\n"; use Kiosk; # start the server on port __ print "INSTANTIATING SERVER port $kiosk_port ==============================\n"; my $server = MyWebServer->new($kiosk_port); print "INSTANTIATING KIOSK ==============================\n"; $kiosk = Kiosk->new($placard, $server, $debug_level); #### package Kiosk; # This implements communications to a VNE kiosk as an HTTP server # based on VNE JSON Communication Protocol Ver. 1.8 # Alessandro: add to protocol: change PIN? There is no card removed message use strict; use Device::SerialPort; #use Data::CISerializer; #use MIME::Base64; #use JSON; use Time::HiRes qw(usleep gettimeofday tv_interval time); use Data::Dumper; use Debug; use GetConfiguration; use Database; use DatabaseProcesses; $|=1; sub new { my ($class, $placard, $server, $debug_level) = @_; # config is a data structure that has serial setup information my $self = {}; $self->{me} = 'Kiosk'; $self->{class} = $class; $self->{placard} = $placard; $self->{server} = $server; $self->{debug} = Debug->new($placard, "MAIN_$placard", $debug_level); $self->{config} == GetConfiguration->new($self->{debug}, '/etc/cashcade/cashcade.conf'); $self->{debug}->prt(9, "KIOSK Config " . Dumper($self->{config}), 'KIOSK'); $self->{dbcomm} = Database->new($self->{debug}, $self->{configuration}->{config}->{local_db}); # the DB connection must go first and succeed $self->{dbcomm}->connect(); die "Database unable to connect!" if (!$self->{dbcomm}->{connected}); print "Database Connected\n"; $self->{dbproc} = DatabaseProcesses->new($self->{debug}, $self->{dbcomm}); $self->{maxDeposit} = $self->{config}->{maxDepositCents}; $self->{maxWithdraw} = $self->{config}->{maxWithdrawCents}; $self->{debug}->prt(2, "KIOSK CONFIG: kiosk_id=$self->{kiosk_id} maxDeposit=$self->{maxDeposit} maxWithdraw=$self->{maxWithdraw}", 'KIOSK INIT'); $self->{connected} = 0; $self->{verbose} = 0; $self->{kiosk_id} = $self->{dbproc}->get_kiosk_sysid_from_placard($self->{placard}); $self->{card_number} = 0; # this is the latest card number seen $self->{account_id} = 0; # this is the account id while the card is in the reader of the kiosk $self->{account_value} = 0; # value of account in cents $self->{response} = ''; $self->{server}->run(); bless($self,$class); return $self; } sub process { my ($self, $msg) = @_; # $msg is the pointer to the incoming message json hash # { "type":"cardCheck", "date":"1482307795601", "lang": "EN", "machineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" } # TODO: is message for this machine $msg->{machineName} # TODO: is date of message reasonable $msg->{date} $self->{response} = ''; if ( $msg->{type} eq 'cardCheck') { return $self->card_was_inserted($msg); } if ( $msg->{type} eq 'cardLoad') { return $self->deposit_amount_received($msg); } if ( $msg->{type} eq 'cardWithdrawal') { return $self->withdrawal_amount_requested($msg); } }