9 * Initialize the rfid reader connection and register the onReceive event
\r
16 window.addEventListener('load', function() {
\r
17 self.webview = document.getElementById('guestportalWebview');
\r
19 self.webview.addEventListener("loadstop", function() {
\r
20 if (self.connected) {
\r
25 chrome.serial.getDevices(self.getDevicesCallback);
\r
26 chrome.serial.onReceive.addListener(self.onReceiveCallback);
\r
32 * Convert an array buffer into a string.
\r
38 convertArrayBufferToString: function (data) {
\r
39 return String.fromCharCode.apply(null, new Uint8Array(data));
\r
43 * Removes not used numbers from the serial readout and trim it.
\r
44 * This has to be done, otherwise the card id is not usable.
\r
47 * 39383431303030313131333036393032 converts to 9841000111306902
\r
49 * @param String result
\r
53 convertResult: function(result) {
\r
54 return result.replace(/\d(\d)/g, '$1').replace(/^\s+|\s+$/gm, '');
\r
58 * The callback for fetching the available serial devices.
\r
60 * @param array ports Array of XXX with all available devices
\r
64 getDevicesCallback: function(ports) {
\r
67 if (typeof ports === 'undefined') {
\r
68 rfidReader.showError('No serial devices found.');
\r
70 } else if (ports.length === 0) {
\r
71 if (clientInformation.platform.match(/linux/i)) {
\r
72 //we do not get any information about serial devices
\r
73 // on linux, so we simply assume there is one
\r
74 path = '/dev/ttyS0';
\r
76 rfidReader.showError('No serial devices found.');
\r
80 path = ports[0].path;
\r
83 // connect to the first serial device
\r
84 chrome.serial.connect(path, {bitrate: 19200}, function(connectionInfo) {
\r
85 if (typeof connectionInfo === 'undefined') {
\r
86 rfidReader.showError('Cannot access serial device.');
\r
89 rfidReader.connectionId = connectionInfo.connectionId;
\r
90 rfidReader.connected = true;
\r
95 * The callback for fetching the data sent by the serial reader.
\r
97 * @param object info The readout from the serial port
\r
101 onReceiveCallback: function(info) {
\r
102 if (info.connectionId == rfidReader.connectionId && info.data) {
\r
103 var str = rfidReader.convertArrayBufferToString(info.data);
\r
105 if (str.charAt(str.length-1) === "\n") {
\r
106 rfidReader.stringReceived += str.substring(0, str.length-1);
\r
107 rfidReader.stringReceived = rfidReader.convertResult(rfidReader.stringReceived);
\r
109 if (rfidReader.stringReceived === '') {
\r
110 console.log('received an empty string');
\r
114 // send the retreived data to the webview content
\r
115 rfidReader.webview.contentWindow.postMessage({cardId: rfidReader.stringReceived}, '*');
\r
116 rfidReader.stringReceived = '';
\r
118 rfidReader.stringReceived += str;
\r
123 showError: function(message) {
\r
124 console.error(message);
\r
125 document.getElementById('error').appendChild(document.createTextNode(message));
\r
126 document.getElementById('error').style.display = 'block';
\r