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