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( '///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 $.cookie('rfid', 'localhost'); // set cookie
53 console.debug( 'rfid_scan', data, textStatus, $.cookie('rfid') );
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: 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>');
65 $('input#rfid_active').attr('checked',true);
66 $('input#rfid_active').click(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');
77 if ( data.tags.length === 1 ) {
79 rfid_current_sid = t.sid;
81 // if ( span.text() != t.content ) {
82 if ( 1 ) { // force update of security
84 var script_name = document.location.pathname.split(/\//).pop();
85 // var tab_active = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
86 var tab_active = $("#header_search li[aria-selected=true]").attr('aria-controls');
87 console.debug('tab_active', tab_active);
88 var circulation = script_name == 'circulation.pl' || tab_active == 'circ_search' ;
89 var returns = script_name == 'returns.pl' || tab_active == 'checkin_search';
91 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
93 rfid_blank_sid = t.sid;
94 span.text( t.sid + ' blank' ).css('color', 'red' );
96 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
99 if ( t.security.toUpperCase() == 'DA' ) color = 'red';
100 if ( t.security.toUpperCase() == 'D7' ) color = 'green';
101 span.text( t.content ).css('color', color);
104 if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' ) {
105 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
106 $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
110 if ( ! barcode_on_screen( t.content ) || returns ) {
111 rfid_reset_field = 'barcode';
113 // return must be first to catch change of tab to check-in
114 var afi_secure = returns ? 'DA' : 'D7';
115 var form_selector = returns ? 'first' : 'last';
116 if ( returns || circulation ) {
117 var i = $('input[name=barcode]:'+form_selector);
118 if ( i.val() != t.content ) {
119 rfid_secure_json( t, afi_secure, function(data) {
120 console.log('secure', afi_secure, data);
121 i.val( t.content ).closest('form').submit();
125 console.error('not in circulation or returns');
129 } else if ( t.content.substr(0,3) == '130' ) {
131 span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
134 span.text( t.content ).css('color', 'blue' );
136 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
137 console.debug('not submitting', t.contains);
139 rfid_refresh = 0; // stop rfid scan while submitting form
140 rfid_reset_field = 'findborrower';
141 $('input[name=findborrower]').val( t.content )
147 var error = data.tags.length + ' tags near reader: ';
148 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
149 span.text( error ).css( 'color', 'red' );
153 span.text( 'no tags in range' ).css('color','gray');
154 if ( rfid_reset_field ) {
155 $('input[name='+rfid_reset_field+']').val( '' );
156 rfid_reset_field = false;
160 if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') == 'checked' ) {
161 window.setTimeout( function() {
162 $('#rfid_popup').css('border','3px solid #fff');
163 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
166 console.debug('rfid_refresh disabled',rfid_refresh);
169 $('#rfid_popup').css('border','3px solid #ff0');
172 function scan_tags() {
173 console.info('scan_tags');
174 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
177 $(document).ready( function() {
178 console.log('rfid_active', $('input#rfid_active').attr('checked') );
182 // intranet catalogingdd
183 shortcut.add('F4', function() {
184 // extract barcode from window title
185 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
187 if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
188 rfid_blank_sid = rfid_current_sid;
191 console.debug('program barcode', barcode, 'to', rfid_blank_sid);
192 $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
193 console.info('programmed', rfid_blank_sid, barcode, data);
196 console.error('no barcode in window title');