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 function barcode_on_screen(barcode) {
15 // FIXME: don't work without checkbox, probably broken
17 $('table tr td a:contains(130)').each( function(i,o) {
18 var possible = $(o).text();
19 if ( possible == barcode ) found++;
24 var rfid_refresh = 200; // ms
25 var rfid_count_timeout = 1; // number of times to scan reader before turning off
28 function rfid_secure_json(t,val, success) {
29 if ( t.security.toUpperCase() == val.toUpperCase() ) return success({ verified: val });
30 rfid_refresh = 0; // disable rfid pull until secure call returns
31 console.log('rfid_secure_json', t, val);
32 $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
37 var rfid_reset_field = false;
38 var rfid_current_sid = false;
39 var rfid_blank_sid = false;
40 var rfid_action = undefined;
42 function rfid_scan(data,textStatus) {
44 var rfid_count = $.cookie('rfid_count');
45 if ( rfid_count === undefined ) {
46 rfid_count = rfid_count_timeout;
49 console.debug( 'rfid_scan', data, 'status', textStatus, 'rfid_count', rfid_count);
50 rfid_current_sid = false;
51 rfid_blank_sid = false;
53 var span = $('span#rfid');
55 if ( span.size() == 0 ) {
56 // insert last in language bar on bottom
57 // span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
59 // alternative pop-up version
60 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>');
61 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
62 $('input#rfid_active').click(activate_scan_tags); // FIXME don't activate actions on page load
66 if ( span.size() == 0 ) // or before login on top
67 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
69 span = $('span#rfid');
70 var info = $('span#rfid-info');
74 if ( data.tags.length === 1 ) {
76 rfid_current_sid = t.sid;
78 // if ( span.text() != t.content ) {
79 if ( 1 ) { // force update of security
81 var script_name = document.location.pathname.split(/\//).pop();
82 var tab_active = $("#header_search li[aria-selected=true]").attr('aria-controls');
83 var focused_form = $('input:focus').first().name;
85 rfid_action ? rfid_action :
86 ( script_name == 'returns.pl' || tab_active == 'checkin_search') ? 'checkin' : // must be before circulation
87 ( script_name == 'circulation.pl' || tab_active == 'circ_search' ) ? 'circulation' :
89 rfid_action = undefined; // one-shot
90 console.debug('script_name', script_name, 'tab_active', tab_active, 'action', action, 'focused_form', focused_form );
93 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
95 rfid_blank_sid = t.sid;
96 span.text( t.sid + ' blank' ).css('color', 'red' );
98 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
102 if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '⊠' }
103 if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '→' }
104 span.html( t.content + ' ' + icon ).css('color', color);
107 if ( tab_active == 'catalog_search'
108 && script_name != 'moredetail.pl'
109 && script_name != 'detail.pl'
110 && $('input#rfid_active').attr('checked') ) {
112 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
113 $.cookie('rfid_count', rfid_count_timeout);
116 .css('background', '#ff0')
117 .val( 'bc:' + t.content )
118 ;//.closest('form').submit();
122 if ( action == 'returns' || action == 'checkin' || action == 'circulation' ) {
123 rfid_reset_field = 'barcode';
125 // return must be first to catch change of tab to check-in
127 action == 'checkin' ? 'DA' :
128 action == 'circulation' ? 'D7' :
132 script_name == 'returns.pl' ? 'last' : 'first';
134 var i = $('input[name=barcode]:focus');
135 if ( i.length == 1 ) {
136 i.css('background', '#ff0');
137 console.log('input barcode focus', i, i.val());
139 i = $('input[name=barcode]:'+form_selector).first();
140 i.css('background', '#ff0');
141 console.log('input barcode', form_selector, i, i.val());
144 if ( action == 'circulation' && $('#circ_needsconfirmation').length > 0 ) {
145 console.log("in circulation, but needs confirmation");
148 console.debug('val', i.val(), 'name', i.name, 'i', i);
150 if ( i.val() != t.content ) { // && i.name == 'barcode' ) {
151 i.css('background', '#0ff' );
152 rfid_secure_json( t, afi_secure, function(data) {
153 console.log('secure', afi_secure, data);
154 $.cookie('rfid_count', 0); // FIXME once? to see change rfid_count_timeout);
157 afi_secure == 'DA' ? '#f00' :
158 afi_secure == 'D7' ? '#0f0' :
162 .closest('form').submit();
165 console.error('not using element', i);
168 console.error('element not found', i);
172 console.debug(action, 'no form submit');
175 } else if ( t.content.substr(0,3) == '130' ) {
177 span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
180 span.html( t.content + ' ⋆' ).css('color', 'blue' );
182 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
183 console.debug('not submitting', t.contains);
185 rfid_refresh = 0; // stop rfid scan while submitting form
186 rfid_reset_field = 'findborrower';
187 $('input[name=findborrower]')
188 .css('background', '#00f')
190 ;//.parent().submit();
195 var error = data.tags.length + ' tags near reader: ';
196 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
197 span.text( error ).css( 'color', 'red' );
201 span.text( 'no tags in range' ).css('color','gray');
202 if ( rfid_reset_field ) {
203 $('input[name='+rfid_reset_field+']').val( '' );
204 rfid_reset_field = false;
208 if (rfid_count > 0) rfid_count--;
209 if (rfid_count == 0) {
210 //span.text('RFID reader disabled').css('color','black');
211 $('input#rfid_active').attr('checked', false)
212 console.log('RFID disabled', rfid_count);
214 $.cookie('rfid_count', rfid_count);
216 if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
217 window.setTimeout( function() {
218 if ( rfid_refresh ) {
219 $('#rfid_popup').css('border','3px solid #ff0');
220 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
222 console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
226 console.debug('rfid_refresh disabled',rfid_refresh);
229 $('#rfid_popup').css('border','3px solid #fff');
233 function scan_tags() {
234 console.info('scan_tags');
235 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
238 function set_rfid_active(active,action) {
239 rfid_action = action;
240 var input_active = $('input#rfid_active').attr('checked');
241 if ( active && input_active ) {
242 console.info('ignored set_rfid_active ', active, action);
245 console.info('set_rfid_active', active);
247 $.cookie('rfid_count', rfid_count_timeout);
249 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
251 if ( input_active ) $('input#rfid_active').attr('checked', false);
252 $.cookie('rfid_count', 0);
256 function activate_scan_tags() {
257 var active = $('input#rfid_active').attr('checked');
258 console.info('activate_scan_tags', active);
259 set_rfid_active(active);
262 $(document).ready( function() {
263 console.log('rfid_active', $('input#rfid_active').attr('checked') );
266 rfid_action = 'scan';
267 scan_tags(); // FIXME should we trigger this on page load even if rfid is not active
269 // circulation keyboard shortcuts (FFZG specific!)
270 shortcut.add('Alt+1', function() { set_rfid_active(true,'checkin' )});
271 shortcut.add('Alt+2', function() { set_rfid_active(true,'circulation')});
272 shortcut.add('Alt+3', function() { set_rfid_active(true,'search?' )});
273 shortcut.add('Alt+4', function() { set_rfid_active(true,'renew' )}); // renew
275 // send RFID tag to currently focused field on screen
276 shortcut.add('Alt+s', function() {
277 var el = $('input:focus');
278 var tag = $('span#rfid').text().split(/\s+/)[0];
279 console.log('send', el[0].name, tag, el);
280 if ( el && tag ) el.css('background', '#ff0').val( tag )
281 ;//.closest('form').submit();
284 shortcut.add('F9', function() {
286 set_rfid_active(true,'F9');
289 // intranet cataloging
290 shortcut.add('F4', function() {
291 // extract barcode from window title
292 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
294 if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
295 rfid_blank_sid = rfid_current_sid;
298 console.debug('program barcode', barcode, 'to', rfid_blank_sid);
299 $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
300 console.info('programmed', rfid_blank_sid, barcode, data);
303 console.error('no barcode in window title');