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