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
31 function rfid_secure_json(t,val, success) {
32 if ( t.security.toUpperCase() == val.toUpperCase() ) return;
33 rfid_refresh = 0; // disable rfid pull until secure call returns
34 console.log('rfid_secure_json', t, val);
35 $.getJSON( 'http://localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
38 function rfid_secure_check(t,val) {
39 if ( barcode_on_screen(t.content) ) {
40 rfid_secure_json(t, val);
45 var rfid_reset_field = false;
46 var rfid_current_sid = false;
47 var rfid_blank_sid = false;
49 function rfid_scan(data,textStatus) {
51 console.debug( 'rfid_scan', data, textStatus );
52 rfid_current_sid = false;
53 rfid_blank_sid = false;
55 var span = $('span#rfid');
57 if ( span.size() == 0 ) // insert last in language bar on bottom
58 // span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
59 span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 0; right: 0; background: #fff; border: 3px solid #ff0; padding: 1em; opacity: 0.7; z-index: 10;"><span id="rfid">RFID reader</span></div>');
61 if ( span.size() == 0 ) // or before login on top
62 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
64 span = $('span#rfid');
68 if ( data.tags.length === 1 ) {
70 rfid_current_sid = t.sid;
72 // if ( span.text() != t.content ) {
73 if ( 1 ) { // force update of security
75 var script_name = document.location.pathname.split(/\//).pop();
76 var tab_active = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
77 console.debug('tab_active', tab_active);
78 var circulation = script_name == 'circulation.pl';
79 var returns = script_name == 'returns.pl' || tab_active == 'checkin_search';
81 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
83 rfid_blank_sid = t.sid;
84 span.text( t.sid + ' blank' ).css('color', 'red' );
86 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
89 if ( t.security.toUpperCase() == 'DA' ) color = 'red';
90 if ( t.security.toUpperCase() == 'D7' ) color = 'green';
91 span.text( t.content ).css('color', color);
94 if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' ) {
95 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
96 $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
100 if ( ! barcode_on_screen( t.content ) || returns ) {
101 rfid_reset_field = 'barcode';
103 // return must be first to catch change of tab to check-in
104 var afi_secure = returns ? 'DA' : 'D7';
105 var form_selector = returns ? 'first' : 'last';
106 if ( returns || circulation ) {
107 var i = $('input[name=barcode]:'+form_selector);
108 if ( i.val() != t.content ) {
109 rfid_secure_json( t, afi_secure, function(data) {
110 console.log('secure', afi_secure, data);
111 i.val( t.content ).closest('form').submit();
115 console.error('not in circulation or returns');
119 } else if ( t.content.substr(0,3) == '130' ) {
121 span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
124 span.text( t.content ).css('color', 'blue' );
126 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
127 console.debug('not submitting', t.contains);
129 rfid_refresh = 0; // stop rfid scan while submitting form
130 rfid_reset_field = 'findborrower';
131 $('input[name=findborrower]').val( t.content )
137 var error = data.tags.length + ' tags near reader: ';
138 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
139 span.text( error ).css( 'color', 'red' );
143 span.text( 'no tags in range' ).css('color','gray');
144 if ( rfid_reset_field ) {
145 $('input[name='+rfid_reset_field+']').val( '' );
146 rfid_reset_field = false;
150 if (rfid_refresh > 1) {
151 window.setTimeout( function() {
152 $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
155 console.debug('rfid_refresh disabled',rfid_refresh);
159 $(document).ready( function() {
160 $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
162 shortcut.add('F4', function() {
163 // extract barcode from window title
164 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
166 if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
167 rfid_blank_sid = rfid_current_sid;
170 console.debug('program barcode', barcode, 'to', rfid_blank_sid);
171 $.getJSON( 'http://localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
172 console.info('programmed', rfid_blank_sid, barcode, data);
175 console.error('no barcode in window title');