display more info from sip2 partron info
[Biblio-RFID.git] / examples / selfcheck.js
index 9e72d58..9436d79 100644 (file)
@@ -1,4 +1,9 @@
 
+// configure timeouts
+var end_timeout   = 3000; // ms from end page to start page
+var error_timeout = 5000; // ms from error page to start page
+var tag_rescan    = 200;  // ms rescan tags every 0.2s
+
 // mock console
 if(!window.console) {
        window.console = new function() {
@@ -10,6 +15,7 @@ if(!window.console) {
 
 var state;
 var scan_timeout;
+var pending_jsonp = 0;
 
 function change_page(new_state) {
        if ( state != new_state ) {
@@ -18,10 +24,13 @@ function change_page(new_state) {
                        new_state = 'circulation'; // page has different name
                        $('.checkout').hide();
                        $('.checkin').show();
+                       circulation_type = 'checkin';
+                       borrower_cardnumber = 0; // fake
                } else if ( new_state == 'checkout' ) {
                        new_state = 'circulation'; // page has different name
                        $('.checkout').show();
                        $('.checkin').hide();
+                       circulation_type = 'checkout';
                }
 
                state = new_state;
@@ -36,10 +45,24 @@ function change_page(new_state) {
                console.info('change_page', state);
 
                if ( state == 'start' ) {
-                       start_scan();
+                       circulation_type = 'checkout';
+                       book_barcodes = {};
+                       $('ul#books').html(''); // clear book list
+                       $('#books_count').html( 0 );
+                       scan_tags();
+               }
+
+               if ( state == 'end' ) {
+                       window.setTimeout(function(){
+                               //change_page('start');
+                               location.reload(); // force js VM to GC?
+                       },end_timeout);
                }
+
                if ( state == 'error' ) {
-                       // FIXME: implement timeout and go back to start
+                       window.setTimeout(function(){
+                               change_page('start');
+                       },error_timeout);
                }
        }
 }
@@ -51,20 +74,13 @@ function got_visible_tags(data,textStatus) {
                $.each(data.tags, function(i,tag) {
                        console.debug( i, tag );
                        html += '<li><tt class="' + tag.security + '">' + tag.sid;
-                       var content = tag.content
-                       if (typeof content === undefined && typeof tag.borrower !== undefined) 
-                               content = tag.borrower.cardnumber;
-
-                       var is_book = false;
-                       var is_borrower = false;
+                       var content = tag.content || tag.borrower.cardnumber;
 
                        if ( content ) {
                                var link;
                                if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
-                                       is_book = true;
                                        link = 'catalogue/search.pl?q=';
                                } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
-                                       is_borrower = true;
                                        link = 'members/member.pl?member=';
                                } else {
                                        html += '<b>UNKNOWN TAG</b> '+content;
@@ -78,11 +94,12 @@ function got_visible_tags(data,textStatus) {
                                }
 
                                console.debug( 'calling', state, content );
-                               window[state]( content ); // call function with barcode
+                               window[state]( content, tag.sid ); // call function with barcode
 
                        }
                });
                html += '</ul>';
+
        }
 
        var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
@@ -93,37 +110,30 @@ function got_visible_tags(data,textStatus) {
                + '</div>'
                + html
                ;
-       $('#tags').html( html );
-       scan_timeout = window.setTimeout(function(){
-               scan_tags();
-       },200); // re-scan every 200ms
+       $('#tags').html( html ); // FIXME leaks memory?
+
+       pending_jsonp--;
 };
 
 function scan_tags() {
-       if ( $('input#pull-reader').attr('checked') ) {
+       if ( pending_jsonp ) {
+               console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting');
+       } else {
                console.info('scan_tags');
+               pending_jsonp++;
                $.getJSON("/scan?callback=?", got_visible_tags);
        }
-}
-
-function start_scan() {
-       $('input#pull-reader').attr('checked', true);
-       scan_tags();
-}
 
-function stop_scan() {
-       $('input#pull-reader').attr('checked', '');
+       scan_timeout = window.setTimeout(function(){
+               scan_tags();
+       },tag_rescan);  // re-scan every 200ms
 }
 
 $(document).ready(function() {
-               $('input#pull-reader').click( function() {
+               $('div#tags').click( function() {
                        scan_tags();
                });
 
-               $('div#tags').click( function() {
-                       $('input#pull-reader').attr('checked', false);
-               } );
-
                change_page('start');
 });
 
@@ -138,7 +148,7 @@ function fill_in( where, value ) {
 
 var borrower_cardnumber;
 var circulation_type;
-var book_barcodes;
+var book_barcodes = {};
 
 function start( cardnumber ) {
 
@@ -147,28 +157,24 @@ function start( cardnumber ) {
                return;
        }
 
-       borrower_cardnumber = cardnumber;
-       circulation_type = 'checkout';
-       book_barcodes = {};
+       borrower_cardnumber = cardnumber; // for circulation
 
-       change_page('borrower_check');
-}
-
-function borrower_check() {
-
-       stop_scan();
+       fill_in( 'borrower_number', cardnumber );
 
-       fill_in( 'borrower_number', borrower_cardnumber );
-
-       $.getJSON('/sip2/patron_info/'+borrower_cardnumber)
+       pending_jsonp++;
+       $.getJSON('/sip2/patron_info/'+cardnumber)
        .done( function( data ) {
                console.info('patron', data);
                fill_in( 'borrower_name', data['AE'] );
                fill_in( 'borrower_email', data['BE'] );
                fill_in( 'hold_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) ) * 1;
                fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) ) * 1;
+               fill_in( 'charged_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 2 * 4 ), 4 ) ) * 1;
+               fill_in( 'fine_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 3 * 4 ), 4 ) ) * 1;
+               pending_jsonp--;
                change_page('borrower_info');
        }).fail( function(data) {
+               pending_jsonp--;
                change_page('error');
        });
 }
@@ -177,20 +183,27 @@ function borrower_info() {
        // nop
 }
 
-function circulation( barcode ) {
+function circulation( barcode, sid ) {
        if ( barcode
                        && barcode.length == 10
                        && barcode.substr(0,3) == 130
-                       && ! book_barcodes[barcode]
+                       && book_barcodes[barcode] != 1
        ) { // book, not seen yet
-               $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode , function( data ) {
+               book_barcodes[ barcode ] = 1;
+               pending_jsonp++;
+               $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+sid , function( data ) {
                        console.info( circulation_type, data );
-                       $('ul#books').append('<li>' + data['AJ'] + ' <small>' + data['AF'] + '</small></li>');
-                       book_barcodes[ barcode ] = 1;
+                       $('ul#books').append('<li>' + ( data['AJ'] || barcode ) + ( data['AF'] ? ' <b>' + data['AF'] + '</b>' : '' ) + '</li>');
+                       $('#books_count').html( $('ul#books > li').length );
                        console.debug( book_barcodes );
+                       pending_jsonp--;
                }).fail( function() {
                        change_page('error');
+                       pending_jsonp--;
                });
        }
 }
 
+function end() {
+       // nop
+}