added end and error timeouts
[Biblio-RFID.git] / examples / selfcheck.js
1
2 // configure timeouts
3 var end_timeout   = 3000; // ms from end page to start page
4 var error_timeout = 5000; // ms from error page to start page
5
6 // mock console
7 if(!window.console) {
8         window.console = new function() {
9                 this.info = function(str) {};
10                 this.error = function(str) {};
11                 this.debug = function(str) {};
12         };
13 }
14
15 var state;
16 var scan_timeout;
17
18 function change_page(new_state) {
19         if ( state != new_state ) {
20
21                 if ( new_state == 'checkin' ) {
22                         new_state = 'circulation'; // page has different name
23                         $('.checkout').hide();
24                         $('.checkin').show();
25                 } else if ( new_state == 'checkout' ) {
26                         new_state = 'circulation'; // page has different name
27                         $('.checkout').show();
28                         $('.checkin').hide();
29                 }
30
31                 state = new_state;
32
33                 $('.page').each( function(i,el) {
34                         if ( el.id != new_state ) {
35                                 $(el).hide();
36                         } else {
37                                 $(el).show();
38                         }
39                 });
40                 console.info('change_page', state);
41
42                 if ( state == 'start' ) {
43                         start_scan();
44                 }
45                 if ( state == 'end' ) {
46                         window.setTimeout(function(){
47                                 change_page('start');
48                         },end_timeout);
49                 }
50
51                 if ( state == 'error' ) {
52                         window.setTimeout(function(){
53                                 change_page('start');
54                         },error_timeout);
55                 }
56         }
57 }
58
59 function got_visible_tags(data,textStatus) {
60         var html = 'No tags in range';
61         if ( data.tags ) {
62                 html = '<ul class="tags">';
63                 $.each(data.tags, function(i,tag) {
64                         console.debug( i, tag );
65                         html += '<li><tt class="' + tag.security + '">' + tag.sid;
66                         var content = tag.content
67                         if (typeof content === undefined && typeof tag.borrower !== undefined) 
68                                 content = tag.borrower.cardnumber;
69
70                         var is_book = false;
71                         var is_borrower = false;
72
73                         if ( content ) {
74                                 var link;
75                                 if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
76                                         is_book = true;
77                                         link = 'catalogue/search.pl?q=';
78                                 } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
79                                         is_borrower = true;
80                                         link = 'members/member.pl?member=';
81                                 } else {
82                                         html += '<b>UNKNOWN TAG</b> '+content;
83                                 }
84
85                                 if ( link ) {
86                                         html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
87                                                 + link + content
88                                                 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
89                                                 + '</tt>';
90                                 }
91
92                                 console.debug( 'calling', state, content );
93                                 window[state]( content ); // call function with barcode
94
95                         }
96                 });
97                 html += '</ul>';
98         }
99
100         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
101
102         html = '<div class=status>'
103                 + textStatus
104                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
105                 + '</div>'
106                 + html
107                 ;
108         $('#tags').html( html );
109         scan_timeout = window.setTimeout(function(){
110                 scan_tags();
111         },200); // re-scan every 200ms
112 };
113
114 function scan_tags() {
115         if ( $('input#pull-reader').attr('checked') ) {
116                 console.info('scan_tags');
117                 $.getJSON("/scan?callback=?", got_visible_tags);
118         }
119 }
120
121 function start_scan() {
122         $('input#pull-reader').attr('checked', true);
123         scan_tags();
124 }
125
126 function stop_scan() {
127         $('input#pull-reader').attr('checked', '');
128 }
129
130 $(document).ready(function() {
131                 $('input#pull-reader').click( function() {
132                         scan_tags();
133                 });
134
135                 $('div#tags').click( function() {
136                         $('input#pull-reader').attr('checked', false);
137                 } );
138
139                 change_page('start');
140 });
141
142 function fill_in( where, value ) {
143         $('.'+where).each(function(i, el) {
144                 $(el).html(value);
145         });
146
147 }
148
149 /* Selfcheck state actions */
150
151 var borrower_cardnumber;
152 var circulation_type;
153 var book_barcodes = {};
154
155 function start( cardnumber ) {
156
157         if ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) {
158                 console.error(cardnumber, ' is not borrower card');
159                 return;
160         }
161
162         borrower_cardnumber = cardnumber;
163         circulation_type = 'checkout';
164         book_barcodes = {};
165
166         change_page('borrower_check');
167 }
168
169 function borrower_check() {
170
171         stop_scan();
172
173         fill_in( 'borrower_number', borrower_cardnumber );
174
175         $.getJSON('/sip2/patron_info/'+borrower_cardnumber)
176         .done( function( data ) {
177                 console.info('patron', data);
178                 fill_in( 'borrower_name', data['AE'] );
179                 fill_in( 'borrower_email', data['BE'] );
180                 fill_in( 'hold_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) ) * 1;
181                 fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) ) * 1;
182                 change_page('borrower_info');
183         }).fail( function(data) {
184                 change_page('error');
185         });
186 }
187
188 function borrower_info() {
189         // nop
190 }
191
192 function circulation( barcode ) {
193         if ( barcode
194                         && barcode.length == 10
195                         && barcode.substr(0,3) == 130
196                         && typeof book_barcodes[barcode] !== undefined
197         ) { // book, not seen yet
198                 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode , function( data ) {
199                         console.info( circulation_type, data );
200                         $('ul#books').append('<li>' + data['AJ'] + ' <small>' + data['AF'] + '</small></li>');
201                         book_barcodes[ barcode ] = 1;
202                         console.debug( book_barcodes );
203                 }).fail( function() {
204                         change_page('error');
205                 });
206         }
207 }
208