SmartX turn off warnings
[Biblio-RFID.git] / examples / selfcheck.js
index bb1589a..a03d297 100644 (file)
@@ -23,6 +23,17 @@ var tick_timeout = 25; // s
 var tick_warning = 10; // s
 var tick = 0;
 
+function beep( message ) {
+       pending_jsonp++;
+       $.getJSON("/beep/" + message)
+       .done( function(data) {
+               pending_jsonp--;
+       })
+       .fail( function(data) {
+               pending_jsonp--;
+       });
+}
+
 function start_timeout() {
        $('#timeout').hide();
        tick = Math.round( tick_timeout * ( 1000 / tag_rescan ) );
@@ -62,7 +73,7 @@ function change_page(new_state) {
                        book_barcodes = {};
                        $('ul#books').html(''); // clear book list
                        $('#books_count').html( 0 );
-                       only_reader = 'librfid';
+                       only_reader = '/only/librfid';
                        scan_tags();
                }
 
@@ -73,7 +84,8 @@ function change_page(new_state) {
                        },end_timeout);
                }
 
-               if ( state == 'error' ) {
+               if ( state == 'error' || state == 'error-borrower' ) {
+                       beep( 'error page' );
                        window.setTimeout(function(){
                                //change_page('start');
                                location.reload();
@@ -89,11 +101,21 @@ function change_page(new_state) {
 }
 
 function got_visible_tags(data,textStatus) {
+       console.log('got_visible_tags', data,textStatus);
+       wait_counter = 0;
+       $('#working').hide();
+
        var html = 'No tags in range';
        if ( data.tags ) {
                html = '<ul class="tags">';
                $.each(data.tags, function(i,tag) {
                        console.debug( i, tag );
+
+                 if ( tag.hasOwnProperty('error') ) {
+                               html += 'ERROR ' + tag.sid + ' ' + tag.error;
+                               change_page('error-borrower');
+                 } else {
+
                        html += '<li><tt class="' + tag.security + '">' + tag.sid;
                        var content = tag.content || tag.borrower.cardnumber;
 
@@ -120,6 +142,9 @@ function got_visible_tags(data,textStatus) {
                                window[state]( content, tag ); // call function with barcode
 
                        }
+
+                 } // not error
+
                });
                html += '</ul>';
 
@@ -143,17 +168,17 @@ var wait_counter = 0;
 function scan_tags() {
        if ( pending_jsonp ) {
                wait_counter++;
-               console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting counter', wait_counter);
-               if ( wait_counter > 3 ) $('#working').show();
+               console.debug('scan_tags', 'tick', tick, 'pending_jsonp', pending_jsonp, ' wait_conter', wait_counter);
+               if ( wait_counter > 10 ) $('#working').show(); // tag_rescan = 200ms * 10 = 2s
        } else {
-               console.info('scan_tags', only_reader);
+               console.info('scan_tags', 'tock', tick, only_reader, 'wait_counter', wait_counter, 'tick', tick);
                pending_jsonp++;
                $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
                        console.error('scan error pending jsonp', pending_jsonp);
                        pending_jsonp--;
+                       wait_counter = 0;
+                       $('#working').hide();
                });
-               wait_counter = 0;
-               $('#working').hide();
        }
 
        if ( tick > 0 ) {
@@ -211,10 +236,18 @@ function start( cardnumber, tag ) {
                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;
+               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 );
+               var overdue = data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1;
+               if ( overdue > 0 ) {
+                       overdue = '<span style="color:red">'+overdue+'</span>';
+                       beep( 'overdue: ' + overdue );
+               }
+               fill_in( 'overdue_items', overdue );
+               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) {
@@ -232,6 +265,7 @@ function circulation( barcode, tag ) {
                        && barcode.length == 10
                        && barcode.substr(0,3) == 130
                        && book_barcodes[barcode] != 1
+                       && tag.reader == '3M810'
        ) { // book, not seen yet
                book_barcodes[ barcode ] = 1;
                pending_jsonp++;
@@ -239,13 +273,19 @@ function circulation( barcode, tag ) {
                        console.info( circulation_type, data );
 
                        var color = 'red';
-                       var error = 'Transakcija neuspješna. Odnesite knjige na pult!';
+                       var message = 'Transakcija neuspješna. Odnesite knjige na pult!';
                        if ( data['fixed'].substr(2,1) == 1 ) {
                                color='green';
-                               error = '';
+                               message = circulation_type == 'checkout' ? 'Posuđeno' : 'Vraćeno';
+                       } else {
+                               beep( circulation_type + ': ' + data['AF'] );
+                       }
+
+                       if ( data['AF'] ) {
+                               message = data['AF'] + ' ' + message;
                        }
 
-                       $('ul#books').append('<li style="color:'+color+'">' + ( data['AJ'] || barcode ) + ( data['AF'] ? ' <b>' + data['AF'] + '</b> ' + error : '' ) + '</li>');
+                       $('ul#books').append('<li>' + ( data['AJ'] || barcode ) + ' <b style="color:'+color+'">' + message + '</b></li>');
                        $('#books_count').html( $('ul#books > li').length );
                        console.debug( book_barcodes );
                        pending_jsonp--;