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
13 * You will have to inject remote javascript in Koha intranetuserjs using:
15 // inject JavaScript RFID support
16 $.getScript('http://localhost:9000/examples/koha-rfid.js');
20 function barcode_on_screen(barcode) {
22 $('table tr td a:contains(130)').each( function(i,o) {
23 var possible = $(o).text();
24 if ( possible == barcode ) found++;
29 var rfid_refresh = 1500; // ms
30 var rfid_count_timeout = 10; // number of times to scan reader before turning off
32 function rfid_secure_json(t,val, success) {
33 if ( t.security.toUpperCase() == val.toUpperCase() ) return;
34 rfid_refresh = 0; // disable rfid pull until secure call returns
35 console.log('rfid_secure_json', t, val);
36 $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
39 function rfid_secure_check(t,val) {
40 if ( barcode_on_screen(t.content) ) {
41 rfid_secure_json(t, val);
46 var rfid_reset_field = false;
47 var rfid_current_sid = false;
48 var rfid_blank_sid = false;
50 function rfid_scan(data,textStatus) {
52 var rfid_count = $.cookie('rfid_count');
53 if ( rfid_count === undefined ) {
54 rfid_count = rfid_count_timeout;
56 console.log('rfid_count', rfid_count);
58 $.cookie('rfid', 'localhost'); // set cookie
60 console.debug( 'rfid_scan', data, textStatus, $.cookie('rfid') );
61 rfid_current_sid = false;
62 rfid_blank_sid = false;
64 var span = $('span#rfid');
66 if ( span.size() == 0 ) {
67 // insert last in language bar on bottom
68 // span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
70 // alternative pop-up version
71 span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 3em; right: 1em; background: #fff; border: 3px solid #ff0; padding: 1em; opacity: 0.7; z-index: 10;"><label for="rfid_active"><input type=checkbox id="rfid_active"> local_ip <span id="rfid">RFID reader</span></label></div>');
72 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
73 $('input#rfid_active').click(activate_scan_tags);
77 if ( span.size() == 0 ) // or before login on top
78 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
80 span = $('span#rfid');
84 if ( data.tags.length === 1 ) {
86 rfid_current_sid = t.sid;
88 // if ( span.text() != t.content ) {
89 if ( 1 ) { // force update of security
91 var script_name = document.location.pathname.split(/\//).pop();
92 // var tab_active = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
93 var tab_active = $("#header_search li[aria-selected=true]").attr('aria-controls');
94 console.debug('tab_active', tab_active);
95 var circulation = script_name == 'circulation.pl' || tab_active == 'circ_search' ;
96 var returns = script_name == 'returns.pl' || tab_active == 'checkin_search';
97 console.debug('script_name', script_name, 'circulation', circulation, 'returns', returns);
99 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
101 rfid_blank_sid = t.sid;
102 span.text( t.sid + ' blank' ).css('color', 'red' );
104 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
108 if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '⊠' }
109 if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '→' }
110 span.html( t.content + ' ' + icon ).css('color', color);
113 if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' && $('input#rfid_active').attr('checked') ) {
114 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
115 $.cookie('rfid_count', rfid_count_timeout);
116 $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
120 if ( ! barcode_on_screen( t.content ) || returns ) {
121 rfid_reset_field = 'barcode';
123 // return must be first to catch change of tab to check-in
124 var afi_secure = returns ? 'DA' : 'D7';
125 var form_selector = returns ? 'first' : 'last';
126 if ( returns || circulation ) {
128 if ( circulation && $('#circ_needsconfirmation').length > 0 ) {
129 console.log("in circulation, but needs confirmation");
131 var i = $('input[name=barcode]:'+form_selector);
132 if ( i.val() != t.content ) {
133 rfid_secure_json( t, afi_secure, function(data) {
134 console.log('secure', afi_secure, data);
135 $.cookie('rfid_count', rfid_count_timeout);
136 i.val( t.content ).closest('form').submit();
141 console.error('not in circulation or returns');
145 } else if ( t.content.substr(0,3) == '130' ) {
147 span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
150 span.html( t.content + ' ⋆' ).css('color', 'blue' );
152 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
153 console.debug('not submitting', t.contains);
155 rfid_refresh = 0; // stop rfid scan while submitting form
156 rfid_reset_field = 'findborrower';
157 $('input[name=findborrower]').val( t.content )
163 var error = data.tags.length + ' tags near reader: ';
164 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
165 span.text( error ).css( 'color', 'red' );
169 span.text( 'no tags in range' ).css('color','gray');
170 if ( rfid_reset_field ) {
171 $('input[name='+rfid_reset_field+']').val( '' );
172 rfid_reset_field = false;
177 if (rfid_count > 0) rfid_count--;
178 if (rfid_count == 0) {
179 span.text('RFID reader disabled').css('color','black');
180 $('input#rfid_active').attr('checked', false)
182 $.cookie('rfid_count', rfid_count);
185 if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
186 window.setTimeout( function() {
187 $('#rfid_popup').css('border','3px solid #ff0');
188 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
191 console.debug('rfid_refresh disabled',rfid_refresh);
194 $('#rfid_popup').css('border','3px solid #fff');
197 function scan_tags() {
198 console.info('scan_tags');
199 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
202 function set_rfid_active(active) {
203 var input_active = $('input#rfid_active').attr('checked');
204 console.info('set_rfid_active', active);
206 $.cookie('rfid_count', rfid_count_timeout);
208 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
210 if ( input_active ) $('input#rfid_active').attr('checked', false);
211 $.cookie('rfid_count', 0);
215 function activate_scan_tags() {
216 var active = $('input#rfid_active').attr('checked');
217 console.info('activate_scan_tags', active);
218 set_rfid_active(active);
221 $(document).ready( function() {
222 console.log('rfid_active', $('input#rfid_active').attr('checked') );
226 // circulation keyboard shortcuts (FFZG specific!)
227 shortcut.add('Alt+r', function() { set_rfid_active(true); } );
228 shortcut.add('Alt+z', function() { set_rfid_active(true); } );
229 shortcut.add('Alt+k', function() { set_rfid_active(false) } );
231 // intranet cataloging
232 shortcut.add('F4', function() {
233 // extract barcode from window title
234 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
236 if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
237 rfid_blank_sid = rfid_current_sid;
240 console.debug('program barcode', barcode, 'to', rfid_blank_sid);
241 $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
242 console.info('programmed', rfid_blank_sid, barcode, data);
245 console.error('no barcode in window title');