3 var end_timeout = 3000; // ms from end page to start page
4 var error_timeout = 5000; // ms from error page to start page
5 var tag_rescan = 200; // ms rescan tags every 0.2s
9 window.console = new function() {
10 this.info = function(str) {};
11 this.error = function(str) {};
12 this.debug = function(str) {};
18 var pending_jsonp = 0;
21 // timeout warning dialog
22 var tick_timeout = 25; // s
23 var tick_warning = 10; // s
26 function beep( message ) {
28 $.getJSON("/beep/" + message)
29 .done( function(data) {
32 .fail( function(data) {
37 function start_timeout() {
39 tick = Math.round( tick_timeout * ( 1000 / tag_rescan ) );
42 function change_page(new_state) {
43 if ( state != new_state ) {
45 if ( new_state == 'checkin' ) {
46 new_state = 'circulation'; // page has different name
47 $('.checkout').hide();
49 circulation_type = 'checkin';
50 borrower_cardnumber = 0; // fake
51 only_reader = '/only/3M';
52 } else if ( new_state == 'checkout' ) {
53 new_state = 'circulation'; // page has different name
54 $('.checkout').show();
56 circulation_type = 'checkout';
57 only_reader = '/only/3M';
62 $('.page').each( function(i,el) {
63 if ( el.id != new_state ) {
69 console.info('change_page', state);
71 if ( state == 'start' ) {
72 circulation_type = 'checkout';
74 $('ul#books').html(''); // clear book list
75 $('#books_count').html( 0 );
76 only_reader = '/only/librfid';
80 if ( state == 'end' ) {
81 window.setTimeout(function(){
82 //change_page('start');
83 location.reload(); // force js VM to GC?
87 if ( state == 'error' || state == 'error-borrower' ) {
89 window.setTimeout(function(){
90 //change_page('start');
95 if ( state == 'circulation' || state == 'borrower_info' ) {
98 tick = 0; // timeout disabled
103 function got_visible_tags(data,textStatus) {
104 console.log('got_visible_tags', data,textStatus);
106 $('#working').hide();
108 var html = 'No tags in range';
110 html = '<ul class="tags">';
111 $.each(data.tags, function(i,tag) {
112 console.debug( i, tag );
114 if ( tag.hasOwnProperty('error') ) {
115 html += 'ERROR ' + tag.sid + ' ' + tag.error;
116 change_page('error-borrower');
119 html += '<li><tt class="' + tag.security + '">' + tag.sid;
120 var content = tag.content || tag.borrower.cardnumber;
124 if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
125 link = 'catalogue/search.pl?q=';
126 } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
127 link = 'members/member.pl?member=';
128 } else if ( tag.tag_type == 'SmartX' ) {
129 link = 'members/member.pl?member=';
131 html += '<b>UNKNOWN TAG</b> '+content;
135 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
137 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
141 console.debug( 'calling', state, content );
142 window[state]( content, tag ); // call function with barcode
153 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
155 html = '<div class=status>'
157 + ' &#' + arrows[ data.time % arrows.length ] + ';'
161 $('#tags').html( html ); // FIXME leaks memory?
166 var wait_counter = 0;
168 function scan_tags() {
169 if ( pending_jsonp ) {
171 console.debug('scan_tags', 'tick', tick, 'pending_jsonp', pending_jsonp, ' wait_conter', wait_counter);
172 if ( wait_counter > 10 ) $('#working').show(); // tag_rescan = 200ms * 10 = 2s
174 console.info('scan_tags', 'tock', tick, only_reader, 'wait_counter', wait_counter, 'tick', tick);
176 $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
177 console.error('scan error pending jsonp', pending_jsonp);
180 $('#working').hide();
185 if ( tick < tick_warning * ( 1000 / tag_rescan ) ) {
186 $('#tick').html( Math.round( tick * tag_rescan / 1000 ) );
187 $('#timeout').show();
191 $('#timeout').hide();
196 scan_timeout = window.setTimeout(function(){
198 },tag_rescan); // re-scan every 200ms
201 $(document).ready(function() {
202 $('div#tags').click( function() {
206 change_page('start');
209 function fill_in( where, value ) {
210 $('.'+where).each(function(i, el) {
216 /* Selfcheck state actions */
218 var borrower_cardnumber;
219 var circulation_type;
220 var book_barcodes = {};
222 function start( cardnumber, tag ) {
224 if ( tag.tag_type != 'SmartX' && ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) ) {
225 console.error(cardnumber, 'is not borrower card', tag);
229 borrower_cardnumber = cardnumber; // for circulation
231 fill_in( 'borrower_number', cardnumber );
234 $.getJSON('/sip2/patron_info/'+cardnumber)
235 .done( function( data ) {
236 console.info('patron', data);
237 fill_in( 'borrower_name', data['AE'] );
238 fill_in( 'borrower_email', data['BE'] );
239 fill_in( 'hold_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) * 1 );
240 //fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1 );
241 var overdue = data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1;
243 overdue = '<span style="color:red">'+overdue+'</span>';
244 beep( 'overdue: ' + overdue );
246 fill_in( 'overdue_items', overdue );
247 fill_in( 'charged_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 2 * 4 ), 4 ) * 1 );
248 fill_in( 'fine_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 3 * 4 ), 4 ) * 1 );
252 change_page('borrower_info');
253 }).fail( function(data) {
255 change_page('error');
259 function borrower_info() {
263 function circulation( barcode, tag ) {
265 && barcode.length == 10
266 && barcode.substr(0,3) == 130
267 && book_barcodes[barcode] != 1
268 && tag.reader == '3M810'
269 ) { // book, not seen yet
270 book_barcodes[ barcode ] = 1;
272 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+tag.sid , function( data ) {
273 console.info( circulation_type, data );
276 var message = 'Transakcija neuspješna. Odnesite knjige na pult!';
277 if ( data['fixed'].substr(2,1) == 1 ) {
279 message = circulation_type == 'checkout' ? 'Posuđeno' : 'Vraćeno';
281 beep( circulation_type + ': ' + data['AF'] );
285 message = data['AF'] + ' ' + message;
288 $('ul#books').append('<li>' + ( data['AJ'] || barcode ) + ' <b style="color:'+color+'">' + message + '</b></li>');
289 $('#books_count').html( $('ul#books > li').length );
290 console.debug( book_barcodes );
292 start_timeout(); // reset timeout
293 }).fail( function() {
294 change_page('error');