scan only 3M reader in checkin/checkout
[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 function scan_tags() {
142         if ( pending_jsonp ) {
143                 console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting');
144         } else {
145                 console.info('scan_tags', only_reader);
146                 pending_jsonp++;
147                 $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
148                         console.error('scan error pending jsonp', pending_jsonp);
149                         pending_jsonp--;
150                 });
151         }
152
153         if ( tick > 0 ) {
154                 if ( tick < tick_warning * ( 1000 / tag_rescan ) ) {
155                         $('#tick').html( Math.round( tick * tag_rescan / 1000 ) );
156                         $('#timeout').show();
157                 }
158                 tick--;
159                 if ( tick == 0 ) {
160                         $('#timeout').hide();
161                         change_page('end');
162                 }
163         }
164
165         scan_timeout = window.setTimeout(function(){
166                 scan_tags();
167         },tag_rescan);  // re-scan every 200ms
168 }
169
170 $(document).ready(function() {
171                 $('div#tags').click( function() {
172                         scan_tags();
173                 });
174
175                 change_page('start');
176 });
177
178 function fill_in( where, value ) {
179         $('.'+where).each(function(i, el) {
180                 $(el).html(value);
181         });
182
183 }
184
185 /* Selfcheck state actions */
186
187 var borrower_cardnumber;
188 var circulation_type;
189 var book_barcodes = {};
190
191 function start( cardnumber, tag ) {
192
193         if ( tag.tag_type != 'SmartX' && ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) ) {
194                 console.error(cardnumber, 'is not borrower card', tag);
195                 return;
196         }
197
198         borrower_cardnumber = cardnumber; // for circulation
199
200         fill_in( 'borrower_number', cardnumber );
201
202         pending_jsonp++;
203         $.getJSON('/sip2/patron_info/'+cardnumber)
204         .done( function( data ) {
205                 console.info('patron', data);
206                 fill_in( 'borrower_name', data['AE'] );
207                 fill_in( 'borrower_email', data['BE'] );
208                 fill_in( 'hold_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) ) * 1;
209                 fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) ) * 1;
210                 fill_in( 'charged_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 2 * 4 ), 4 ) ) * 1;
211                 fill_in( 'fine_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 3 * 4 ), 4 ) ) * 1;
212                 pending_jsonp--;
213                 change_page('borrower_info');
214         }).fail( function(data) {
215                 pending_jsonp--;
216                 change_page('error');
217         });
218 }
219
220 function borrower_info() {
221         // nop
222 }
223
224 function circulation( barcode, tag ) {
225         if ( barcode
226                         && barcode.length == 10
227                         && barcode.substr(0,3) == 130
228                         && book_barcodes[barcode] != 1
229         ) { // book, not seen yet
230                 book_barcodes[ barcode ] = 1;
231                 pending_jsonp++;
232                 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+tag.sid , function( data ) {
233                         console.info( circulation_type, data );
234
235                         var color = 'red';
236                         var error = 'Transakcija neuspjeĆĄna. Odnesite knjige na pult!';
237                         if ( data['fixed'].substr(2,1) == 1 ) {
238                                 color='green';
239                                 error = '';
240                         }
241
242                         $('ul#books').append('<li style="color:'+color+'">' + ( data['AJ'] || barcode ) + ( data['AF'] ? ' <b>' + data['AF'] + '</b> ' + error : '' ) + '</li>');
243                         $('#books_count').html( $('ul#books > li').length );
244                         console.debug( book_barcodes );
245                         pending_jsonp--;
246                         start_timeout(); // reset timeout
247                 }).fail( function() {
248                         change_page('error');
249                         pending_jsonp--;
250                 });
251         }
252 }
253
254 function end() {
255         // nop
256 }