--- /dev/null
+rfidReader = {
+ webview: null,
+ connectionId: '',
+ connected: false,
+ infoInterval: null,
+ stringReceived: '',
+
+ /**
+ * Initialize the rfid reader connection and register the onReceive event
+ *
+ * @return void
+ */
+ init: function() {
+ var self = this;
+
+ window.addEventListener('load', function() {
+ self.webview = document.getElementById('guestportalWebview');
+
+ self.webview.addEventListener("loadstop", function() {
+ if (self.connected) {
+ return;
+ }
+
+ // load all devices
+ chrome.serial.getDevices(self.getDevicesCallback);
+ chrome.serial.onReceive.addListener(self.onReceiveCallback);
+ });
+ });
+ },
+
+ /**
+ * Convert an array buffer into a string.
+ *
+ * @param array data
+ *
+ * @return String
+ */
+ convertArrayBufferToString: function (data) {
+ return String.fromCharCode.apply(null, new Uint8Array(data));
+ },
+
+ /**
+ * Removes not used numbers from the serial readout and trim it.
+ * This has to be done, otherwise the card id is not usable.
+ *
+ * Example:
+ * 39383431303030313131333036393032 converts to 9841000111306902
+ *
+ * @param String result
+ *
+ * @return String
+ */
+ convertResult: function(result) {
+ return result.replace(/\d(\d)/g, '$1').replace(/^\s+|\s+$/gm, '');
+ },
+
+ /**
+ * The callback for fetching the available serial devices.
+ *
+ * @param array ports Array of XXX with all available devices
+ *
+ * @return void
+ */
+ getDevicesCallback: function(ports) {
+ var path = null;
+
+ if (typeof ports === 'undefined') {
+ rfidReader.showError('No serial devices found.');
+ return;
+ } else if (ports.length === 0) {
+ if (clientInformation.platform.match(/linux/i)) {
+ //we do not get any information about serial devices
+ // on linux, so we simply assume there is one
+ path = '/dev/ttyS0';
+ } else {
+ rfidReader.showError('No serial devices found.');
+ return;
+ }
+ } else {
+ path = ports[0].path;
+ }
+
+ // connect to the first serial device
+ chrome.serial.connect(path, {bitrate: 19200}, function(connectionInfo) {
+ if (typeof connectionInfo === 'undefined') {
+ rfidReader.showError('Cannot access serial device.');
+ return;
+ }
+ rfidReader.connectionId = connectionInfo.connectionId;
+ rfidReader.connected = true;
+ });
+ },
+
+ /**
+ * The callback for fetching the data sent by the serial reader.
+ *
+ * @param object info The readout from the serial port
+ *
+ * @return void
+ */
+ onReceiveCallback: function(info) {
+ if (info.connectionId == rfidReader.connectionId && info.data) {
+ var str = rfidReader.convertArrayBufferToString(info.data);
+
+ if (str.charAt(str.length-1) === "\n") {
+ rfidReader.stringReceived += str.substring(0, str.length-1);
+ rfidReader.stringReceived = rfidReader.convertResult(rfidReader.stringReceived);
+
+ if (rfidReader.stringReceived === '') {
+ console.log('received an empty string');
+ return;
+ }
+
+ // send the retreived data to the webview content
+ rfidReader.webview.contentWindow.postMessage({cardId: rfidReader.stringReceived}, '*');
+ rfidReader.stringReceived = '';
+ } else {
+ rfidReader.stringReceived += str;
+ }
+ }
+ },
+
+ showError: function(message) {
+ console.error(message);
+ document.getElementById('error').appendChild(document.createTextNode(message));
+ document.getElementById('error').style.display = 'block';
+ }
+};
+
+rfidReader.init();