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