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