(no commit message)
[paste/109.git] / rfidReader.js
1 rfidReader = {\r
2     webview: null,\r
3     connectionId: '',\r
4     connected: false,\r
5     infoInterval: null,\r
6     stringReceived: '',\r
7 \r
8     /**\r
9      * Initialize the rfid reader connection and register the onReceive event\r
10      *\r
11      * @return void\r
12      */\r
13     init: function() {\r
14         var self = this;\r
15 \r
16         window.addEventListener('load', function() {\r
17             self.webview = document.getElementById('guestportalWebview');\r
18 \r
19             self.webview.addEventListener("loadstop", function() {\r
20                 if (self.connected) {\r
21                     return;\r
22                 }\r
23 \r
24                 // load all devices\r
25                 chrome.serial.getDevices(self.getDevicesCallback);\r
26                 chrome.serial.onReceive.addListener(self.onReceiveCallback);\r
27             });\r
28         });\r
29     },\r
30 \r
31     /**\r
32      * Convert an array buffer into a string.\r
33      *\r
34      * @param array data\r
35      *\r
36      * @return String\r
37      */\r
38     convertArrayBufferToString: function (data) {\r
39         return String.fromCharCode.apply(null, new Uint8Array(data));\r
40     },\r
41 \r
42     /**\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
45      *\r
46      * Example:\r
47      * 39383431303030313131333036393032 converts to 9841000111306902\r
48      *\r
49      * @param String result\r
50      *\r
51      * @return String\r
52      */\r
53     convertResult: function(result) {\r
54         return result.replace(/\d(\d)/g, '$1').replace(/^\s+|\s+$/gm, '');\r
55     },\r
56 \r
57     /**\r
58      * The callback for fetching the available serial devices.\r
59      *\r
60      * @param array ports Array of XXX with all available devices\r
61      *\r
62      * @return void\r
63      */\r
64     getDevicesCallback: function(ports) {\r
65         var path = null;\r
66 \r
67         if (typeof ports === 'undefined') {\r
68             rfidReader.showError('No serial devices found.');\r
69             return;\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
75             } else {\r
76                 rfidReader.showError('No serial devices found.');\r
77                 return;\r
78             }\r
79         } else {\r
80             path = ports[0].path;\r
81         }\r
82 \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
87                 return;\r
88             }\r
89             rfidReader.connectionId = connectionInfo.connectionId;\r
90             rfidReader.connected = true;\r
91         });\r
92     },\r
93 \r
94     /**\r
95      * The callback for fetching the data sent by the serial reader.\r
96      *\r
97      * @param object info The readout from the serial port\r
98      *\r
99      * @return void\r
100      */\r
101     onReceiveCallback: function(info) {\r
102         if (info.connectionId == rfidReader.connectionId && info.data) {\r
103             var str = rfidReader.convertArrayBufferToString(info.data);\r
104 \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
108 \r
109                 if (rfidReader.stringReceived === '') {\r
110                     console.log('received an empty string');\r
111                     return;\r
112                 }\r
113 \r
114                 // send the retreived data to the webview content\r
115                 rfidReader.webview.contentWindow.postMessage({cardId: rfidReader.stringReceived}, '*');\r
116                 rfidReader.stringReceived = '';\r
117             } else {\r
118                 rfidReader.stringReceived += str;\r
119             }\r
120         }\r
121     },\r
122 \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
127     }\r
128 };\r
129 \r
130 rfidReader.init();\r