8b7b029766379940d082160f78c80cf3918dbc54
[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         var html = 'No tags in range';
105         if ( data.tags ) {
106                 html = '<ul class="tags">';
107                 $.each(data.tags, function(i,tag) {
108                         console.debug( i, tag );
109
110                   if ( tag.hasOwnProperty('error') ) {
111                                 html += 'ERROR ' + tag.sid + ' ' + tag.error;
112                                 change_page('error-borrower');
113                   } else {
114
115                         html += '<li><tt class="' + tag.security + '">' + tag.sid;
116                         var content = tag.content || tag.borrower.cardnumber;
117
118                         if ( content ) {
119                                 var link;
120                                 if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
121                                         link = 'catalogue/search.pl?q=';
122                                 } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
123                                         link = 'members/member.pl?member=';
124                                 } else if ( tag.tag_type == 'SmartX' ) {
125                                         link = 'members/member.pl?member=';
126                                 } else {
127                                         html += '<b>UNKNOWN TAG</b> '+content;
128                                 }
129
130                                 if ( link ) {
131                                         html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
132                                                 + link + content
133                                                 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
134                                                 + '</tt>';
135                                 }
136
137                                 console.debug( 'calling', state, content );
138                                 window[state]( content, tag ); // call function with barcode
139
140                         }
141
142                   } // not error
143
144                 });
145                 html += '</ul>';
146
147         }
148
149         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
150
151         html = '<div class=status>'
152                 + textStatus
153                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
154                 + '</div>'
155                 + html
156                 ;
157         $('#tags').html( html ); // FIXME leaks memory?
158
159         pending_jsonp--;
160 };
161
162 var wait_counter = 0;
163
164 function scan_tags() {
165         if ( pending_jsonp ) {
166                 wait_counter++;
167                 console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting counter', wait_counter);
168                 if ( wait_counter > 3 ) $('#working').show();
169         } else {
170                 console.info('scan_tags', only_reader);
171                 pending_jsonp++;
172                 $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
173                         console.error('scan error pending jsonp', pending_jsonp);
174                         pending_jsonp--;
175                 });
176                 wait_counter = 0;
177                 $('#working').hide();
178         }
179
180         if ( tick > 0 ) {
181                 if ( tick < tick_warning * ( 1000 / tag_rescan ) ) {
182                         $('#tick').html( Math.round( tick * tag_rescan / 1000 ) );
183                         $('#timeout').show();
184                 }
185                 tick--;
186                 if ( tick == 0 ) {
187                         $('#timeout').hide();
188                         change_page('end');
189                 }
190         }
191
192         scan_timeout = window.setTimeout(function(){
193                 scan_tags();
194         },tag_rescan);  // re-scan every 200ms
195 }
196
197 $(document).ready(function() {
198                 $('div#tags').click( function() {
199                         scan_tags();
200                 });
201
202                 change_page('start');
203 });
204
205 function fill_in( where, value ) {
206         $('.'+where).each(function(i, el) {
207                 $(el).html(value);
208         });
209
210 }
211
212 /* Selfcheck state actions */
213
214 var borrower_cardnumber;
215 var circulation_type;
216 var book_barcodes = {};
217
218 function start( cardnumber, tag ) {
219
220         if ( tag.tag_type != 'SmartX' && ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) ) {
221                 console.error(cardnumber, 'is not borrower card', tag);
222                 return;
223         }
224
225         borrower_cardnumber = cardnumber; // for circulation
226
227         fill_in( 'borrower_number', cardnumber );
228
229         pending_jsonp++;
230         $.getJSON('/sip2/patron_info/'+cardnumber)
231         .done( function( data ) {
232                 console.info('patron', data);
233                 fill_in( 'borrower_name', data['AE'] );
234                 fill_in( 'borrower_email', data['BE'] );
235                 fill_in( 'hold_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) * 1 );
236                 //fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1 );
237                 var overdue = data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1;
238                 if ( overdue > 0 ) {
239                         overdue = '<span style="color:red">'+overdue+'</span>';
240                         beep( 'overdue: ' + overdue );
241                 }
242                 fill_in( 'overdue_items', overdue );
243                 fill_in( 'charged_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 2 * 4 ), 4 ) * 1 );
244                 fill_in( 'fine_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 3 * 4 ), 4 ) * 1 );
245
246
247                 pending_jsonp--;
248                 change_page('borrower_info');
249         }).fail( function(data) {
250                 pending_jsonp--;
251                 change_page('error');
252         });
253 }
254
255 function borrower_info() {
256         // nop
257 }
258
259 function circulation( barcode, tag ) {
260         if ( barcode
261                         && barcode.length == 10
262                         && barcode.substr(0,3) == 130
263                         && book_barcodes[barcode] != 1
264                         && tag.reader == '3M810'
265         ) { // book, not seen yet
266                 book_barcodes[ barcode ] = 1;
267                 pending_jsonp++;
268                 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+tag.sid , function( data ) {
269                         console.info( circulation_type, data );
270
271                         var color = 'red';
272                         var message = 'Transakcija neuspješna. Odnesite knjige na pult!';
273                         if ( data['fixed'].substr(2,1) == 1 ) {
274                                 color='green';
275                                 message = circulation_type == 'checkout' ? 'Posuđeno' : 'Vraćeno';
276                         } else {
277                                 beep( circulation_type + ': ' + data['AF'] );
278                         }
279
280                         if ( data['AF'] ) {
281                                 message = data['AF'] + ' ' + message;
282                         }
283
284                         $('ul#books').append('<li>' + ( data['AJ'] || barcode ) + ' <b style="color:'+color+'">' + message + '</b></li>');
285                         $('#books_count').html( $('ul#books > li').length );
286                         console.debug( book_barcodes );
287                         pending_jsonp--;
288                         start_timeout(); // reset timeout
289                 }).fail( function() {
290                         change_page('error');
291                         pending_jsonp--;
292                 });
293         }
294 }
295
296 function end() {
297         // nop
298 }