(no commit message)
authorChristian Weiske <cweiske@cweiske.de>
Wed, 28 May 2014 15:22:04 +0000 (17:22 +0200)
committerwww-cweiske <www-cweiske@localhost.localdomain>
Wed, 28 May 2014 15:22:04 +0000 (17:22 +0200)
rfidReader.js

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