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