I already blogged about RFID readers but now I really need to replace antique 3M software with something better. So, basically, I started with specification:
User should put RFID tag on reader and get output from KohaNice and simple. However, readers are connected to Windows machine. And we need some way for reader to push data about new tag to browser. Hmmm... push? Sound like Comet, doesn't it?
Let's see what we need:
- Comet server to display page in web browser and push updates
- RFID reader program installable on Windows to communicate with serial port and push messages to Comet server
- User with a browser
- Meteor server provides perl comet server which required one additional file to provide integration with Koha.
I needed comet server to deliver Koha data because of single origin security in browsers which doesn't allow me to make AJAX requests to Koha directly. On the other hand, this gave me excuse to try out HTML::Query to extract part of page which I needed on server.
I liked this split between Koha and Comet server (even on different boxes and/or network segments), but it came with a price: every page generated in Koha took more than 2 seconds, which was just too slow for nice interactive demo. So, I implemented on-disk caching which is also really nice for debugging. In production version, requests for Koha data might be implemented as forkers (since browser uses AJAX request for it, it makes sense) or I will issue queries directly over Koha's database. - Next step was to add push from my perl RFID reader to Meteor.
This also turned out to be simple: one socket connection to Meteor, and few revisions later I had more or less following protocol which reader could push to comet server as pipe | encoded string:meteor( 'info', "Found reader hardware $hw_ver" ); meteor( 'info-none-in-range' ); meteor( 'info-in-range', join(' ',@tags)); meteor( 'in-range', $tag ); meteor( 'read', $tag ); meteor( 'removed', $tag );
- I also needed web interface in browser.
When I need to do something quickly I often turn to jQuery as long time readers of my blog already know. One additional javascript file in Meteor's public_html directory and we have a working interface.
At first, I created JavaScript object (like hash in perl :-) to keep track of visible tags on screen. But, since each tag is div which has id set to SID of tag, it was easier (and shorter) to just use jQuery to ask DOM if there is element on page. KISS wins again...
So how does it look, you might ask? (You might want to turn volume down, because AudioSwap feature of YouTube should be really considered harmful :-)
This might help solve question is perl dead. Only if it means dead easy. it's 4k of perl code, 4k of JavaScript and 4k of CSS.