· 8 years ago · Dec 12, 2017, 12:18 AM
1//Library Includes
2const SerialPort = require('serialport');
3var _mysql = require('mysql');
4
5//SQL handler variable
6var mysql;
7let bitsArray = [];
8
9//Top Level packet variables
10var deviceId;
11var rPass;
12let aTemp = [];
13var dTemp;
14var accel;
15var pCRC;
16var packetID;
17var pType;
18
19//Accelerometer variables
20var xforce;
21var yforce;
22var zforce;
23var xrot;
24var yrot;
25var zrot;
26
27//database connection information
28var HOST = 'localhost';
29var PORT = 3308;
30var MYSQL_USER = 'root';
31var MYSQL_PASS = '';
32var DATABASE = 'ioc';
33
34/* testing variables (these should eventially be replaced by
35data parsed from each packet or retreived from server) */
36var portName = 'COM7';
37var temppacket = '133733100a6E0686173650aFEFE0aEF100a52A48BAA5126EAABA66B45E94994';
38// |DEVICEID||n P h a s e|| aT ||DTmp||f-x|f-y|f-z|r-x|r-y|r-z|m|h|
39//console.log("Simulated Data Stream: "+temppacket);
40
41//static values
42const packetPass = "nPhase";
43
44//set up parser
45const Readline = SerialPort.parsers.Readline;
46//Set up a connection to the USB serial port
47const serialport = new SerialPort(portName, {
48 baudRate: 9600,
49 dataBits: 8,
50 parity: 'none',
51 stopBits: 1,
52 flowControl: false
53 });
54const parser = serialport.pipe(new Readline({ delimiter: '0a' }));
55
56if (mysql == undefined) {
57 //Set up and connect to MYSQL database
58 mysql = _mysql.createConnection({
59 host: HOST,
60 port: PORT,
61 user: MYSQL_USER,
62 password: MYSQL_PASS,
63 });
64}
65//Select our database
66mysql.query('use ' + DATABASE);
67
68/* try to create and setup device_index table. This will skip the commands in the 'if' statement if it already exists */
69mysql.query('show tables like \'device_index\'', function(err, tbCount) {
70 var tbCount = JSON.stringify(tbCount);
71 //if tbCount returns anything other than '[]' it means the table exists and we don't need to create it
72 if (tbCount == '[]') {
73 mysql.query('CREATE TABLE IF NOT EXISTS device_index (device_id VARCHAR(8),user_name VARCHAR(8))');
74 mysql.query('ALTER TABLE device_index ADD CONSTRAINT tb_un UNIQUE (device_id)');
75 }
76 });
77
78//open the serial port
79mysql.query('use ' + DATABASE);
80serialport.on('open', function() {
81 parser.on('data', function(data){
82
83 /***** DATA PARSING AND FORMATTING *****/
84
85 /***** split the incoming data (which is a string) into several
86 ****** different strings whenever a \n character (converted to hex)
87 ****** is detected
88 *****/
89 var bits = data;
90 bitsArray.push(data);
91 console.log(data);//debugging
92 console.log(bits);//debugging
93 console.log('there should have been data already'); //debugging
94
95 //no need to convert these as their values will just be hard coded
96 deviceId = bitsArray[0];
97 rPass = bitsArray[1];
98 packetID = bitsArray[2];
99
100 /* determine which packet has been received */
101 pType = uartConverter(bitsArray[6].slice(-2));
102
103 switch(pType) {
104 case 1:
105
106 break;
107 case 2:
108
109 break;
110 case 3:
111
112 break;
113 default:
114
115 }
116 //need to convert these to ensure they are usable for the website
117 aTemp.push(uartConverter(bitsArray[3]));
118 aTemp.push(uartConverter(bitsArray[4]));
119 dTemp = uartConverter(bitsArray[4]);
120
121 /***** SECURITY AND ERROR CHECKING *****/
122 //check if password parsed from packet matches pre-defined password
123 if (rPass != packetPass) {
124 console.log("Password is not correct!");
125 serialport.close();
126 }
127
128 /***** STORE DATA INTO DATABASE *****/
129 /*Check if a table with the name of the device exists.
130 if it doesn't, create one with columns for id, analog temperature, digital temperature, accellerometer data, and a timestamp. */
131 mysql.query('CREATE TABLE IF NOT EXISTS \'_'+deviceId+'\' (\'id\' INT AUTO_INCREMENT PRIMARY KEY,\'aTemp\' INT, \'dTemp\' INT)');
132 //add device to device-table index if it is not there yet. Assigns web name to deviceId. can be changed on webpage.
133 mysql.query('INSERT IGNORE INTO device_index(device_id,user_name) VALUES (\'_'+deviceId+'\', \'_'+deviceId+'\')');
134
135 //create a table for the accelerometer data linked to that device
136 mysql.query('CREATE TABLE IF NOT EXISTS `_'+deviceId+'_accel` (`id` INT,`x-f` INT, `y-f` INT,`z-f` INT,`x-r` INT, `y-r` INT,`z-r` INT)');
137
138 //store converted data into table corresponding to the device
139 //Analog Temperature Data
140 mysql.query('INSERT INTO _'+deviceId+' (aTemp) VALUES ("'+aTemp+'")');
141
142 //Digital Temperature Data
143 mysql.query('INSERT INTO _'+deviceId+' (dTemp) VALUES ("'+dTemp+'")');
144
145 //Accelerometer Data
146 for (j=5;j < bitsArray.length()-1; j++) {
147 //clear all previously stored data
148 xforce=0;
149 yforce=0;
150 zforce=0;
151 xrot=0;
152 yrot=0;
153 zrot=0;
154 accel = bitsArray[j].match(/.{2}/g);
155
156 //split accelerometer data up into x, y, and z components
157 for(i=0; i < 2; i++){
158 xforce.push(accel[i]);
159 yforce.push(accel[i+2]);
160 zforce.push(accel[i+4]);
161 xrot.push(accel[i+6]);
162 yrot.push(accel[i+8]);
163 zrot.push(accel[i+10]);
164 }
165 //store accelerometer data
166 mysql.query('INSERT INTO _'+deviceId+'_accel (x-f, y-f, z-f, x-r, y-r, z-r) VALUES ("'+xforce+', '+yforce+', '+zforce+', '+xrot+', '+yrot+', '+zrot+'")');
167 }
168 });
169});
170
171//converts data given from UART (hex values stored in a string) into integers
172function uartConverter(stringIn) {
173
174 for (i=0;i<stringIn.length;i=i+2){
175 newString = parseInt("0x"+stringIn.toString().substring(i,i+2));
176 }
177 return newString;
178}