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) {
16 $('table tr td a:contains(130)').each( function(i,o) {
17 var possible = $(o).text();
18 if ( possible == barcode ) found++;
23 var rfid_refresh = 1000; // ms
24 var rfid_count_timeout = 30; // number of times to scan reader before turning off
26 rfid_count_timeout = 0; // FIXME disable
28 function rfid_secure_json(t,val, success) {
29 if ( t.security.toUpperCase() == val.toUpperCase() ) return success();
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 );
35 function rfid_secure_check(t,val) {
36 if ( barcode_on_screen(t.content) ) {
37 rfid_secure_json(t, val);
42 var rfid_reset_field = false;
43 var rfid_current_sid = false;
44 var rfid_blank_sid = false;
46 function rfid_scan(data,textStatus) {
48 var rfid_count = $.cookie('rfid_count');
49 if ( rfid_count === undefined ) {
50 rfid_count = rfid_count_timeout;
53 console.debug( 'rfid_scan', data, 'status', textStatus, 'rfid_count', rfid_count);
54 rfid_current_sid = false;
55 rfid_blank_sid = false;
57 var span = $('span#rfid');
59 if ( span.size() == 0 ) {
60 // insert last in language bar on bottom
61 // span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
63 // alternative pop-up version
64 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>');
65 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
66 $('input#rfid_active').click(activate_scan_tags);
70 if ( span.size() == 0 ) // or before login on top
71 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
73 span = $('span#rfid');
74 var info = $('span#rfid-info');
78 if ( data.tags.length === 1 ) {
80 rfid_current_sid = t.sid;
82 // if ( span.text() != t.content ) {
83 if ( 1 ) { // force update of security
85 var script_name = document.location.pathname.split(/\//).pop();
86 // var tab_active = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
87 var tab_active = $("#header_search li[aria-selected=true]").attr('aria-controls');
88 console.debug('tab_active', tab_active);
90 ( script_name == 'circulation.pl' || tab_active == 'circ_search' ) ? 'circulation' :
91 ( script_name == 'returns.pl' || tab_active == 'checkin_search') ? 'checkin' :
93 console.debug('script_name', script_name, 'action', action);
96 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
98 rfid_blank_sid = t.sid;
99 span.text( t.sid + ' blank' ).css('color', 'red' );
101 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
105 if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '⊠' }
106 if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '→' }
107 span.html( t.content + ' ' + icon ).css('color', color);
110 if ( tab_active == 'catalog_search'
111 && script_name != 'moredetail.pl'
112 && script_name != 'detail.pl'
113 && $('input#rfid_active').attr('checked') ) {
115 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
116 $.cookie('rfid_count', rfid_count_timeout);
119 .css('background', '#ff0')
120 .val( 'bc:' + t.content )
121 ;//.closest('form').submit();
125 if ( ! barcode_on_screen( t.content ) || action == 'returns' || action == 'checkin' ) {
126 rfid_reset_field = 'barcode';
128 // return must be first to catch change of tab to check-in
130 action == 'returns' ? 'DA' :
131 action == 'checkin' ? 'DA' :
132 action == 'FIXME' ? 'D7' :
134 var form_selector = action == 'returns' ? 'first' : 'last';
135 if ( action == 'returns' || action == 'circulation' || action == 'checkin' ) {
137 if ( action == 'circulation' && $('#circ_needsconfirmation').length > 0 ) {
138 console.log("in circulation, but needs confirmation");
140 var i = $('input[name=barcode]:focus');
143 console.debug('use focus');
145 console.debug('use', form_selector);
146 i = $('input[name=barcode]:'+form_selector);
148 if ( i.val() != t.content ) {
149 i.css('background', '#0ff' );
150 rfid_secure_json( t, afi_secure, function(data) {
151 console.log('secure', afi_secure, data);
152 $.cookie('rfid_count', 0); // FIXME once? to see change rfid_count_timeout);
155 afi_secure == 'DA' ? '#f00' :
156 afi_secure == 'D7' ? '#0f0' :
160 ; //.closest('form').submit();
165 console.error('not in circulation or returns');
169 } else if ( t.content.substr(0,3) == '130' ) {
171 span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
174 span.html( t.content + ' ⋆' ).css('color', 'blue' );
176 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
177 console.debug('not submitting', t.contains);
179 rfid_refresh = 0; // stop rfid scan while submitting form
180 rfid_reset_field = 'findborrower';
181 $('input[name=findborrower]')
182 .css('background', '#ff0')
184 ;//.parent().submit();
189 var error = data.tags.length + ' tags near reader: ';
190 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
191 span.text( error ).css( 'color', 'red' );
195 span.text( 'no tags in range' ).css('color','gray');
196 if ( rfid_reset_field ) {
197 $('input[name='+rfid_reset_field+']').val( '' );
198 rfid_reset_field = false;
203 if (rfid_count > 0) rfid_count--;
204 if (rfid_count == 0) {
205 //span.text('RFID reader disabled').css('color','black');
206 $('input#rfid_active').attr('checked', false)
207 console.log('RFID disabled', rfid_count);
209 $.cookie('rfid_count', rfid_count);
212 if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
213 window.setTimeout( function() {
214 if ( rfid_refresh ) {
215 $('#rfid_popup').css('border','3px solid #ff0');
216 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
218 console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
222 console.debug('rfid_refresh disabled',rfid_refresh);
225 $('#rfid_popup').css('border','3px solid #fff');
229 function scan_tags() {
230 console.info('scan_tags');
231 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
234 function set_rfid_active(active) {
235 var input_active = $('input#rfid_active').attr('checked');
236 console.info('set_rfid_active', active);
238 $.cookie('rfid_count', rfid_count_timeout);
240 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
242 if ( input_active ) $('input#rfid_active').attr('checked', false);
243 $.cookie('rfid_count', 0);
247 function activate_scan_tags() {
248 var active = $('input#rfid_active').attr('checked');
249 console.info('activate_scan_tags', active);
250 set_rfid_active(active);
253 $(document).ready( function() {
254 console.log('rfid_active', $('input#rfid_active').attr('checked') );
256 scan_tags(); // FIXME should we trigger this on page load even if rfid is not active
258 // circulation keyboard shortcuts (FFZG specific!)
259 shortcut.add('Alt+r', function() { set_rfid_active(true); } );
260 shortcut.add('Alt+z', function() { set_rfid_active(true); } );
261 shortcut.add('Alt+k', function() { set_rfid_active(false) } );
262 shortcut.add('Alt+y', function() { set_rfid_active(true); } ); // renew
264 // send RFID tag to currently focused field on screen
265 shortcut.add('Alt+s', function() {
266 var el = $('input:focus');
267 var tag = $('span#rfid').text().split(/\s+/)[0];
268 console.log('send', el, tag);
269 if ( el && tag ) el.css('background', '#ff0').val( tag );
272 // intranet cataloging
273 shortcut.add('F4', function() {
274 // extract barcode from window title
275 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
277 if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
278 rfid_blank_sid = rfid_current_sid;
281 console.debug('program barcode', barcode, 'to', rfid_blank_sid);
282 $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
283 console.info('programmed', rfid_blank_sid, barcode, data);
286 console.error('no barcode in window title');