2 * RFID support for Koha
4 * Writtern by Dobrica Pavlinusic <dpavlin@rot13.org> under GPL v2 or later
6 * This provides example how to intergrate JSONP interface from
8 * scripts/RFID-JSONP-server.pl
10 * to provide overlay for tags in range and emulate form fill for Koha Library System
11 * which allows check-in and checkout-operations without touching html interface
14 var rainbow_colors = [ '#9400D3', '#4B0082', '#0000FF', '#00FF00', '#FFFF00', '#FF7F00', '#FF0000' ];
16 function barcode_on_screen(barcode) {
17 // FIXME: don't work without checkbox, probably broken
19 $('table tr td a:contains(130)').each( function(i,o) {
20 var possible = $(o).text();
21 if ( possible == barcode ) found++;
23 var lastchecked = $('div.lastchecked').text();
25 console.info('found lastchecked div', lastchecked);
26 var checked_out_barcode = lastchecked.split(/\(/)[1].split(/\)/)[0];
27 if ( checked_out_barcode == barcode ) found++;
29 // Not checked out message in returns.pl
30 var alert_dialog = $('div.alert p a:contains(130)').text();
32 console.info('found alert dialog', alert_dialog);
33 var alert_barcode = alert_dialog.split(/:/)[0];
34 if ( alert_barcode == barcode ) found++;
36 console.debug('barcode_on_screen', barcode, found);
40 var rfid_refresh = 200; // ms
41 var rfid_count_timeout = 50; // number of times to scan reader before turning off
44 function rfid_secure_json(t,val, success) {
45 if ( t.security.toUpperCase() == val.toUpperCase() ) return success({ verified: val });
46 rfid_refresh = 0; // disable rfid pull until secure call returns
47 console.log('rfid_secure_json', t, val);
48 $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
53 var rfid_reset_field = false;
54 var rfid_current_sid = false;
55 var rfid_blank_sid = false;
56 var rfid_action = undefined;
57 var rfid_scan_busy = false;
59 function rfid_scan(data,textStatus) {
61 rfid_scan_busy = false;
63 var rfid_count = $.cookie('rfid_count');
64 if ( rfid_count === undefined ) {
65 rfid_count = rfid_count_timeout;
68 console.debug( 'rfid_scan', data, 'status', textStatus, 'rfid_count', rfid_count);
69 rfid_current_sid = false;
70 rfid_blank_sid = false;
72 var span = $('span#rfid');
74 if ( span.size() == 0 ) {
75 // insert last in language bar on bottom
76 // span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
78 // alternative pop-up version
79 span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 0; right: 0; background: #fff; border: 0.25em solid #ff0; padding: 0.25em; opacity: 0.9; z-index: 1040; font-size: 200%"><label for="rfid_active"><input type=checkbox id="rfid_active"><!-- local_ip --> <span id="rfid">RFID reader</span> <span id="rfid-info"></span></label></div>');
80 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
81 $('input#rfid_active').click(activate_scan_tags); // FIXME don't activate actions on page load
85 if ( span.size() == 0 ) // or before login on top
86 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
88 span = $('span#rfid');
89 var info = $('span#rfid-info');
93 if ( data.tags.length === 1 ) {
95 rfid_current_sid = t.sid;
96 var rfid_last_tag = $.cookie('rfid_last_tag');
98 // if ( span.text() != t.content ) {
99 if ( 1 ) { // force update of security
101 var script_name = document.location.pathname.split(/\//).pop();
102 var referrer_name = document.referrer.split(/\//).pop();
103 var tab_active = $("#header_search li[aria-selected=true]").attr('aria-controls');
104 var focused_form = $('input:focus').first().name;
106 rfid_action ? rfid_action :
107 ( script_name == 'returns.pl' || tab_active == 'checkin_search') ? 'checkin' : // must be before circulation
108 ( script_name == 'circulation.pl' || tab_active == 'circ_search' ) ? 'circulation' :
110 rfid_action = undefined; // one-shot
111 console.debug('script_name', script_name, 'referrer_name', referrer_name, 'tab_active', tab_active, 'action', action, 'focused_form', focused_form, 'rfid_last_tag' , rfid_last_tag );
114 // keep refreshing rfid reader
115 if ( referrer_name == 'circulation.pl' ) {
116 rfid_count = rfid_count_timeout;
120 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
122 rfid_blank_sid = t.sid;
123 span.text( t.sid + ' blank' ).css('color', 'red' );
125 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
129 if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '⊠' }
130 if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '→' }
131 span.html( t.content + ' ' + icon ).css('color', color);
134 if ( tab_active == 'catalog_search'
135 && script_name != 'moredetail.pl'
136 && script_name != 'detail.pl'
137 && $('input#rfid_active').attr('checked') ) {
139 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
140 $.cookie('rfid_count', rfid_count_timeout);
143 .css('background', '#ff0')
144 .val( 'bc:' + t.content )
145 .closest('form').submit();
150 ( action == 'returns' || action == 'checkin' || action == 'circulation' )
151 && ! barcode_on_screen( t.content )
152 //&& t.content != rfid_last_tag
154 rfid_reset_field = 'barcode';
156 // return must be first to catch change of tab to check-in
158 action == 'checkin' ? 'DA' :
159 action == 'circulation' ? 'D7' :
163 script_name == 'returns.pl' ? 'last' : 'first';
165 var i = $('input[name=barcode]:focus');
166 if ( i.length == 1 ) {
167 i.css('background', '#ff0');
168 console.log('input barcode focus', i, i.val());
170 i = $('input[name=barcode]:'+form_selector).first();
171 i.css('background', '#ff0');
172 console.log('input barcode', form_selector, i, i.val());
175 if ( action == 'circulation' && $('#circ_needsconfirmation').length > 0 ) {
176 console.log("in circulation, but needs confirmation");
179 console.debug('val', i.val(), 'name', i.name, 'i', i);
181 if ( i.val() != t.content ) { // && i.name == 'barcode' ) {
182 i.css('background', '#0ff' );
184 rfid_secure_json( t, afi_secure, function(data) {
185 console.log('secure', afi_secure, data);
186 $.cookie('rfid_count', 0); // FIXME once? to see change rfid_count_timeout);
188 afi_secure == 'DA' ? '#f00' :
189 afi_secure == 'D7' ? '#0f0' :
193 .closest('form').submit();
196 console.error('not using element', i);
199 console.error('element not found', i);
203 console.debug(action, 'no form submit');
206 } else if ( t.content.substr(0,3) == '130' ) {
208 span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
211 span.html( t.content + ' ⋆' ).css('color', 'blue' );
213 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
214 console.debug('not submitting', t.contains);
216 rfid_refresh = 0; // stop rfid scan while submitting form
217 rfid_reset_field = 'findborrower';
218 $('input[name=findborrower]')
219 .css('background', '#00f')
226 var error = data.tags.length + ' tags near reader: ';
227 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
228 span.text( error ).css( 'color', 'red' );
232 span.text( 'no tags in range' ).css('color','gray');
233 if ( rfid_reset_field ) {
234 $('input[name='+rfid_reset_field+']').val( '' );
235 rfid_reset_field = false;
239 if (rfid_count > 0) rfid_count--;
240 if (rfid_count == 0) {
241 //span.text('RFID reader disabled').css('color','black');
242 $('input#rfid_active').attr('checked', false)
243 console.log('RFID disabled', rfid_count);
245 $.cookie('rfid_count', rfid_count);
247 if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
248 window.setTimeout( function() {
249 if ( rfid_refresh ) {
250 var color = rainbow_colors[ rfid_count % rainbow_colors.length ];
251 console.debug('color', color);
252 $('#rfid_popup').css('border','3px solid '+color);
255 console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
259 console.debug('rfid_refresh disabled',rfid_refresh);
260 $('#rfid_popup').css('border','3px solid #fff');
263 $.cookie('rfid_last_tag', t ? t.content : '--none--');
267 function scan_tags() {
268 if ( rfid_scan_busy ) {
269 console.error('rfid_scan_busy');
272 rfid_scan_busy = true;
273 console.info('scan_tags');
274 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
277 function set_rfid_active(active,action) {
278 rfid_action = action;
279 var input_active = $('input#rfid_active').attr('checked');
280 if ( active && input_active ) {
281 $.cookie('rfid_count', rfid_count_timeout);
282 console.info('ignored set_rfid_active ', active, action);
286 console.info('set_rfid_active', active);
288 $.cookie('rfid_count', rfid_count_timeout);
290 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
292 if ( input_active ) $('input#rfid_active').attr('checked', false);
293 $.cookie('rfid_count', 0);
297 function activate_scan_tags() {
298 var active = $('input#rfid_active').attr('checked');
299 console.info('activate_scan_tags', active);
300 set_rfid_active(active);
303 $(document).ready( function() {
304 console.log('rfid_active', $('input#rfid_active').attr('checked') );
307 rfid_action = 'scan';
308 scan_tags(); // FIXME should we trigger this on page load even if rfid is not active
310 // circulation keyboard shortcuts (FFZG specific!)
311 shortcut.add('Alt+r', function() { set_rfid_active(true,'checkin' )});
312 shortcut.add('Alt+z', function() { set_rfid_active(true,'circulation')});
313 shortcut.add('Alt+y', function() { set_rfid_active(true,'circulation')});
315 shortcut.add('Alt+3', function() { set_rfid_active(true,'search?' )});
316 shortcut.add('Alt+4', function() { set_rfid_active(true,'renew' )}); // renew
319 // send RFID tag to currently focused field on screen
320 shortcut.add('Alt+s', function() {
321 var el = $('input:focus');
322 var tag = $('span#rfid').text().split(/\s+/)[0];
323 console.log('send', el[0].name, tag, el);
324 if ( el && tag ) el.css('background', '#ff0').val( tag )
325 ;//.closest('form').submit();
328 shortcut.add('F9', function() {
330 //set_rfid_active(true,'F9');
334 // intranet cataloging
335 shortcut.add('F4', function() {
336 // extract barcode from window title
337 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
339 if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
340 rfid_blank_sid = rfid_current_sid;
343 console.debug('program barcode', barcode, 'to', rfid_blank_sid);
344 $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
345 console.info('programmed', rfid_blank_sid, barcode, data);
348 console.error('no barcode in window title');