BUGFIX: hide #working as soon as request returns
[Biblio-RFID.git] / examples / selfcheck.js
1
2 // configure timeouts
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
6
7 // mock console
8 if(!window.console) {
9         window.console = new function() {
10                 this.info = function(str) {};
11                 this.error = function(str) {};
12                 this.debug = function(str) {};
13         };
14 }
15
16 var state;
17 var scan_timeout;
18 var pending_jsonp = 0;
19 var only_reader = '';
20
21 // timeout warning dialog
22 var tick_timeout = 25; // s
23 var tick_warning = 10; // s
24 var tick = 0;
25
26 function beep( message ) {
27         pending_jsonp++;
28         $.getJSON("/beep/" + message)
29         .done( function(data) {
30                 pending_jsonp--;
31         })
32         .fail( function(data) {
33                 pending_jsonp--;
34         });
35 }
36
37 function start_timeout() {
38         $('#timeout').hide();
39         tick = Math.round( tick_timeout * ( 1000 / tag_rescan ) );
40 }
41
42 function change_page(new_state) {
43         if ( state != new_state ) {
44
45                 if ( new_state == 'checkin' ) {
46                         new_state = 'circulation'; // page has different name
47                         $('.checkout').hide();
48                         $('.checkin').show();
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();
55                         $('.checkin').hide();
56                         circulation_type = 'checkout';
57                         only_reader = '/only/3M';
58                 }
59
60                 state = new_state;
61
62                 $('.page').each( function(i,el) {
63                         if ( el.id != new_state ) {
64                                 $(el).hide();
65                         } else {
66                                 $(el).show();
67                         }
68                 });
69                 console.info('change_page', state);
70
71                 if ( state == 'start' ) {
72                         circulation_type = 'checkout';
73                         book_barcodes = {};
74                         $('ul#books').html(''); // clear book list
75                         $('#books_count').html( 0 );
76                         only_reader = '/only/librfid';
77                         scan_tags();
78                 }
79
80                 if ( state == 'end' ) {
81                         window.setTimeout(function(){
82                                 //change_page('start');
83                                 location.reload(); // force js VM to GC?
84                         },end_timeout);
85                 }
86
87                 if ( state == 'error' || state == 'error-borrower' ) {
88                         beep( 'error page' );
89                         window.setTimeout(function(){
90                                 //change_page('start');
91                                 location.reload();
92                         },error_timeout);
93                 }
94
95                 if ( state == 'circulation' || state == 'borrower_info' ) {
96                         start_timeout();
97                 } else {
98                         tick = 0; // timeout disabled
99                 }
100         }
101 }
102
103 function got_visible_tags(data,textStatus) {
104         console.log('got_visible_tags', data,textStatus);
105         wait_counter = 0;
106         $('#working').hide();
107
108         var html = 'No tags in range';
109         if ( data.tags ) {
110                 html = '<ul class="tags">';
111                 $.each(data.tags, function(i,tag) {
112                         console.debug( i, tag );
113
114                   if ( tag.hasOwnProperty('error') ) {
115                                 html += 'ERROR ' + tag.sid + ' ' + tag.error;
116                                 change_page('error-borrower');
117                   } else {
118
119                         html += '<li><tt class="' + tag.security + '">' + tag.sid;
120                         var content = tag.content || tag.borrower.cardnumber;
121
122                         if ( content ) {
123                                 var link;
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=';
130                                 } else {
131                                         html += '<b>UNKNOWN TAG</b> '+content;
132                                 }
133
134                                 if ( link ) {
135                                         html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
136                                                 + link + content
137                                                 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
138                                                 + '</tt>';
139                                 }
140
141                                 console.debug( 'calling', state, content );
142                                 window[state]( content, tag ); // call function with barcode
143
144                         }
145
146                   } // not error
147
148                 });
149                 html += '</ul>';
150
151         }
152
153         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
154
155         html = '<div class=status>'
156                 + textStatus
157                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
158                 + '</div>'
159                 + html
160                 ;
161         $('#tags').html( html ); // FIXME leaks memory?
162
163         pending_jsonp--;
164 };
165
166 var wait_counter = 0;
167
168 function scan_tags() {
169         if ( pending_jsonp ) {
170                 wait_counter++;
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
173         } else {
174                 console.info('scan_tags', 'tock', tick, only_reader, 'wait_counter', wait_counter, 'tick', tick);
175                 pending_jsonp++;
176                 $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
177                         console.error('scan error pending jsonp', pending_jsonp);
178                         pending_jsonp--;
179                         wait_counter = 0;
180                         $('#working').hide();
181                 });
182         }
183
184         if ( tick > 0 ) {
185                 if ( tick < tick_warning * ( 1000 / tag_rescan ) ) {
186                         $('#tick').html( Math.round( tick * tag_rescan / 1000 ) );
187                         $('#timeout').show();
188                 }
189                 tick--;
190                 if ( tick == 0 ) {
191                         $('#timeout').hide();
192                         change_page('end');
193                 }
194         }
195
196         scan_timeout = window.setTimeout(function(){
197                 scan_tags();
198         },tag_rescan);  // re-scan every 200ms
199 }
200
201 $(document).ready(function() {
202                 $('div#tags').click( function() {
203                         scan_tags();
204                 });
205
206                 change_page('start');
207 });
208
209 function fill_in( where, value ) {
210         $('.'+where).each(function(i, el) {
211                 $(el).html(value);
212         });
213
214 }
215
216 /* Selfcheck state actions */
217
218 var borrower_cardnumber;
219 var circulation_type;
220 var book_barcodes = {};
221
222 function start( cardnumber, tag ) {
223
224         if ( tag.tag_type != 'SmartX' && ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) ) {
225                 console.error(cardnumber, 'is not borrower card', tag);
226                 return;
227         }
228
229         borrower_cardnumber = cardnumber; // for circulation
230
231         fill_in( 'borrower_number', cardnumber );
232
233         pending_jsonp++;
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;
242                 if ( overdue > 0 ) {
243                         overdue = '<span style="color:red">'+overdue+'</span>';
244                         beep( 'overdue: ' + overdue );
245                 }
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 );
249
250
251                 pending_jsonp--;
252                 change_page('borrower_info');
253         }).fail( function(data) {
254                 pending_jsonp--;
255                 change_page('error');
256         });
257 }
258
259 function borrower_info() {
260         // nop
261 }
262
263 function circulation( barcode, tag ) {
264         if ( barcode
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;
271                 pending_jsonp++;
272                 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+tag.sid , function( data ) {
273                         console.info( circulation_type, data );
274
275                         var color = 'red';
276                         var message = 'Transakcija neuspješna. Odnesite knjige na pult!';
277                         if ( data['fixed'].substr(2,1) == 1 ) {
278                                 color='green';
279                                 message = circulation_type == 'checkout' ? 'Posuđeno' : 'Vraćeno';
280                         } else {
281                                 beep( circulation_type + ': ' + data['AF'] );
282                         }
283
284                         if ( data['AF'] ) {
285                                 message = data['AF'] + ' ' + message;
286                         }
287
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 );
291                         pending_jsonp--;
292                         start_timeout(); // reset timeout
293                 }).fail( function() {
294                         change_page('error');
295                         pending_jsonp--;
296                 });
297         }
298 }
299
300 function end() {
301         // nop
302 }