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