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;
47 function rfid_scan(data,textStatus) {
49 console.debug( 'rfid_scan', data, textStatus );
51 var span = $('span#rfid');
53 if ( span.size() == 0 ) // insert last in language bar on bottom
54 span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
56 if ( span.size() == 0 ) // or before login on top
57 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
59 span = $('span#rfid');
63 if ( data.tags.length === 1 ) {
65 // if ( span.text() != t.content ) {
66 if ( 1 ) { // force update of security
68 var script_name = document.location.pathname.split(/\//).pop();
69 var tab_active = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
70 console.debug('tab_active', tab_active);
71 var circulation = script_name == 'circulation.pl';
72 var returns = script_name == 'returns.pl' || tab_active == 'checkin_search';
74 if ( t.content.length == 0 ) { // empty tag
76 span.text( t.sid + ' empty' ).css('color', 'red' );
78 } else if ( t.content.substr(0,3) == '130' ) { // books
81 if ( t.security.toUpperCase() == 'DA' ) color = 'red';
82 if ( t.security.toUpperCase() == 'D7' ) color = 'green';
83 span.text( t.content ).css('color', color);
86 if ( tab_active == 'catalog_search' ) {
87 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
88 $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
92 if ( ! barcode_on_screen( t.content ) || returns ) {
93 rfid_reset_field = 'barcode';
95 // return must be first to catch change of tab to check-in
96 var afi_secure = returns ? 'DA' : 'D7';
97 var form_selector = returns ? 'first' : 'last';
98 if ( returns || circulation ) {
99 var i = $('input[name=barcode]:'+form_selector);
100 if ( i.val() != t.content ) {
101 rfid_secure_json( t, afi_secure, function(data) {
102 console.log('secure', afi_secure, data);
103 i.val( t.content ).closest('form').submit();
107 console.error('not in circulation or returns');
112 span.text( t.content ).css('color', 'blue' );
114 if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
115 console.debug('not submitting', t.contains);
117 rfid_refresh = 0; // stop rfid scan while submitting form
118 rfid_reset_field = 'findborrower';
119 $('input[name=findborrower]').val( t.content )
125 var error = data.tags.length + ' tags near reader: ';
126 $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
127 span.text( error ).css( 'color', 'red' );
131 span.text( 'no tags in range' ).css('color','gray');
132 if ( rfid_reset_field ) {
133 $('input[name='+rfid_reset_field+']').val( '' );
134 rfid_reset_field = false;
138 if (rfid_refresh > 1) {
139 window.setTimeout( function() {
140 $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
143 console.debug('rfid_refresh disabled',rfid_refresh);
147 $(document).ready( function() {
148 $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);