added working indicator
[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 start_timeout() {
27         $('#timeout').hide();
28         tick = Math.round( tick_timeout * ( 1000 / tag_rescan ) );
29 }
30
31 function change_page(new_state) {
32         if ( state != new_state ) {
33
34                 if ( new_state == 'checkin' ) {
35                         new_state = 'circulation'; // page has different name
36                         $('.checkout').hide();
37                         $('.checkin').show();
38                         circulation_type = 'checkin';
39                         borrower_cardnumber = 0; // fake
40                         only_reader = '/only/3M';
41                 } else if ( new_state == 'checkout' ) {
42                         new_state = 'circulation'; // page has different name
43                         $('.checkout').show();
44                         $('.checkin').hide();
45                         circulation_type = 'checkout';
46                         only_reader = '/only/3M';
47                 }
48
49                 state = new_state;
50
51                 $('.page').each( function(i,el) {
52                         if ( el.id != new_state ) {
53                                 $(el).hide();
54                         } else {
55                                 $(el).show();
56                         }
57                 });
58                 console.info('change_page', state);
59
60                 if ( state == 'start' ) {
61                         circulation_type = 'checkout';
62                         book_barcodes = {};
63                         $('ul#books').html(''); // clear book list
64                         $('#books_count').html( 0 );
65                         only_reader = '';
66                         scan_tags();
67                 }
68
69                 if ( state == 'end' ) {
70                         window.setTimeout(function(){
71                                 //change_page('start');
72                                 location.reload(); // force js VM to GC?
73                         },end_timeout);
74                 }
75
76                 if ( state == 'error' ) {
77                         window.setTimeout(function(){
78                                 //change_page('start');
79                                 location.reload();
80                         },error_timeout);
81                 }
82
83                 if ( state == 'circulation' || state == 'borrower_info' ) {
84                         start_timeout();
85                 } else {
86                         tick = 0; // timeout disabled
87                 }
88         }
89 }
90
91 function got_visible_tags(data,textStatus) {
92         var html = 'No tags in range';
93         if ( data.tags ) {
94                 html = '<ul class="tags">';
95                 $.each(data.tags, function(i,tag) {
96                         console.debug( i, tag );
97                         html += '<li><tt class="' + tag.security + '">' + tag.sid;
98                         var content = tag.content || tag.borrower.cardnumber;
99
100                         if ( content ) {
101                                 var link;
102                                 if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
103                                         link = 'catalogue/search.pl?q=';
104                                 } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
105                                         link = 'members/member.pl?member=';
106                                 } else if ( tag.tag_type == 'SmartX' ) {
107                                         link = 'members/member.pl?member=';
108                                 } else {
109                                         html += '<b>UNKNOWN TAG</b> '+content;
110                                 }
111
112                                 if ( link ) {
113                                         html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
114                                                 + link + content
115                                                 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
116                                                 + '</tt>';
117                                 }
118
119                                 console.debug( 'calling', state, content );
120                                 window[state]( content, tag ); // call function with barcode
121
122                         }
123                 });
124                 html += '</ul>';
125
126         }
127
128         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
129
130         html = '<div class=status>'
131                 + textStatus
132                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
133                 + '</div>'
134                 + html
135                 ;
136         $('#tags').html( html ); // FIXME leaks memory?
137
138         pending_jsonp--;
139 };
140
141 var wait_counter = 0;
142
143 function scan_tags() {
144         if ( pending_jsonp ) {
145                 wait_counter++;
146                 console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting counter', wait_counter);
147                 if ( wait_counter > 3 ) $('#working').show();
148         } else {
149                 console.info('scan_tags', only_reader);
150                 pending_jsonp++;
151                 $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
152                         console.error('scan error pending jsonp', pending_jsonp);
153                         pending_jsonp--;
154                 });
155                 wait_counter = 0;
156                 $('#working').hide();
157         }
158
159         if ( tick > 0 ) {
160                 if ( tick < tick_warning * ( 1000 / tag_rescan ) ) {
161                         $('#tick').html( Math.round( tick * tag_rescan / 1000 ) );
162                         $('#timeout').show();
163                 }
164                 tick--;
165                 if ( tick == 0 ) {
166                         $('#timeout').hide();
167                         change_page('end');
168                 }
169         }
170
171         scan_timeout = window.setTimeout(function(){
172                 scan_tags();
173         },tag_rescan);  // re-scan every 200ms
174 }
175
176 $(document).ready(function() {
177                 $('div#tags').click( function() {
178                         scan_tags();
179                 });
180
181                 change_page('start');
182 });
183
184 function fill_in( where, value ) {
185         $('.'+where).each(function(i, el) {
186                 $(el).html(value);
187         });
188
189 }
190
191 /* Selfcheck state actions */
192
193 var borrower_cardnumber;
194 var circulation_type;
195 var book_barcodes = {};
196
197 function start( cardnumber, tag ) {
198
199         if ( tag.tag_type != 'SmartX' && ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) ) {
200                 console.error(cardnumber, 'is not borrower card', tag);
201                 return;
202         }
203
204         borrower_cardnumber = cardnumber; // for circulation
205
206         fill_in( 'borrower_number', cardnumber );
207
208         pending_jsonp++;
209         $.getJSON('/sip2/patron_info/'+cardnumber)
210         .done( function( data ) {
211                 console.info('patron', data);
212                 fill_in( 'borrower_name', data['AE'] );
213                 fill_in( 'borrower_email', data['BE'] );
214                 fill_in( 'hold_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) ) * 1;
215                 fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) ) * 1;
216                 fill_in( 'charged_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 2 * 4 ), 4 ) ) * 1;
217                 fill_in( 'fine_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 3 * 4 ), 4 ) ) * 1;
218                 pending_jsonp--;
219                 change_page('borrower_info');
220         }).fail( function(data) {
221                 pending_jsonp--;
222                 change_page('error');
223         });
224 }
225
226 function borrower_info() {
227         // nop
228 }
229
230 function circulation( barcode, tag ) {
231         if ( barcode
232                         && barcode.length == 10
233                         && barcode.substr(0,3) == 130
234                         && book_barcodes[barcode] != 1
235         ) { // book, not seen yet
236                 book_barcodes[ barcode ] = 1;
237                 pending_jsonp++;
238                 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+tag.sid , function( data ) {
239                         console.info( circulation_type, data );
240
241                         var color = 'red';
242                         var error = 'Transakcija neuspjeĆĄna. Odnesite knjige na pult!';
243                         if ( data['fixed'].substr(2,1) == 1 ) {
244                                 color='green';
245                                 error = '';
246                         }
247
248                         $('ul#books').append('<li style="color:'+color+'">' + ( data['AJ'] || barcode ) + ( data['AF'] ? ' <b>' + data['AF'] + '</b> ' + error : '' ) + '</li>');
249                         $('#books_count').html( $('ul#books > li').length );
250                         console.debug( book_barcodes );
251                         pending_jsonp--;
252                         start_timeout(); // reset timeout
253                 }).fail( function() {
254                         change_page('error');
255                         pending_jsonp--;
256                 });
257         }
258 }
259
260 function end() {
261         // nop
262 }