Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Library Includes
- const SerialPort = require('serialport');
- var _mysql = require('mysql');
- //SQL handler variable
- var mysql;
- let bitsArray = [];
- //Top Level packet variables
- var deviceId;
- var rPass;
- let aTemp = [];
- var dTemp;
- var accel;
- var pCRC;
- var packetID;
- var pType;
- //Accelerometer variables
- var xforce;
- var yforce;
- var zforce;
- var xrot;
- var yrot;
- var zrot;
- //database connection information
- var HOST = 'localhost';
- var PORT = 3308;
- var MYSQL_USER = 'root';
- var MYSQL_PASS = '';
- var DATABASE = 'ioc';
- /* testing variables (these should eventially be replaced by
- data parsed from each packet or retreived from server) */
- var portName = 'COM7';
- var temppacket = '133733100a6E0686173650aFEFE0aEF100a52A48BAA5126EAABA66B45E94994';
- // |DEVICEID||n P h a s e|| aT ||DTmp||f-x|f-y|f-z|r-x|r-y|r-z|m|h|
- //console.log("Simulated Data Stream: "+temppacket);
- //static values
- const packetPass = "nPhase";
- //set up parser
- const Readline = SerialPort.parsers.Readline;
- //Set up a connection to the USB serial port
- const serialport = new SerialPort(portName, {
- baudRate: 9600,
- dataBits: 8,
- parity: 'none',
- stopBits: 1,
- flowControl: false
- });
- const parser = serialport.pipe(new Readline({ delimiter: '0a' }));
- if (mysql == undefined) {
- //Set up and connect to MYSQL database
- mysql = _mysql.createConnection({
- host: HOST,
- port: PORT,
- user: MYSQL_USER,
- password: MYSQL_PASS,
- });
- }
- //Select our database
- mysql.query('use ' + DATABASE);
- /* try to create and setup device_index table. This will skip the commands in the 'if' statement if it already exists */
- mysql.query('show tables like \'device_index\'', function(err, tbCount) {
- var tbCount = JSON.stringify(tbCount);
- //if tbCount returns anything other than '[]' it means the table exists and we don't need to create it
- if (tbCount == '[]') {
- mysql.query('CREATE TABLE IF NOT EXISTS device_index (device_id VARCHAR(8),user_name VARCHAR(8))');
- mysql.query('ALTER TABLE device_index ADD CONSTRAINT tb_un UNIQUE (device_id)');
- }
- });
- //open the serial port
- mysql.query('use ' + DATABASE);
- serialport.on('open', function() {
- parser.on('data', function(data){
- /***** DATA PARSING AND FORMATTING *****/
- /***** split the incoming data (which is a string) into several
- ****** different strings whenever a \n character (converted to hex)
- ****** is detected
- *****/
- var bits = data;
- bitsArray.push(data);
- console.log(data);//debugging
- console.log(bits);//debugging
- console.log('there should have been data already'); //debugging
- //no need to convert these as their values will just be hard coded
- deviceId = bitsArray[0];
- rPass = bitsArray[1];
- packetID = bitsArray[2];
- /* determine which packet has been received */
- pType = uartConverter(bitsArray[6].slice(-2));
- switch(pType) {
- case 1:
- break;
- case 2:
- break;
- case 3:
- break;
- default:
- }
- //need to convert these to ensure they are usable for the website
- aTemp.push(uartConverter(bitsArray[3]));
- aTemp.push(uartConverter(bitsArray[4]));
- dTemp = uartConverter(bitsArray[4]);
- /***** SECURITY AND ERROR CHECKING *****/
- //check if password parsed from packet matches pre-defined password
- if (rPass != packetPass) {
- console.log("Password is not correct!");
- serialport.close();
- }
- /***** STORE DATA INTO DATABASE *****/
- /*Check if a table with the name of the device exists.
- if it doesn't, create one with columns for id, analog temperature, digital temperature, accellerometer data, and a timestamp. */
- mysql.query('CREATE TABLE IF NOT EXISTS \'_'+deviceId+'\' (\'id\' INT AUTO_INCREMENT PRIMARY KEY,\'aTemp\' INT, \'dTemp\' INT)');
- //add device to device-table index if it is not there yet. Assigns web name to deviceId. can be changed on webpage.
- mysql.query('INSERT IGNORE INTO device_index(device_id,user_name) VALUES (\'_'+deviceId+'\', \'_'+deviceId+'\')');
- //create a table for the accelerometer data linked to that device
- 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)');
- //store converted data into table corresponding to the device
- //Analog Temperature Data
- mysql.query('INSERT INTO _'+deviceId+' (aTemp) VALUES ("'+aTemp+'")');
- //Digital Temperature Data
- mysql.query('INSERT INTO _'+deviceId+' (dTemp) VALUES ("'+dTemp+'")');
- //Accelerometer Data
- for (j=5;j < bitsArray.length()-1; j++) {
- //clear all previously stored data
- xforce=0;
- yforce=0;
- zforce=0;
- xrot=0;
- yrot=0;
- zrot=0;
- accel = bitsArray[j].match(/.{2}/g);
- //split accelerometer data up into x, y, and z components
- for(i=0; i < 2; i++){
- xforce.push(accel[i]);
- yforce.push(accel[i+2]);
- zforce.push(accel[i+4]);
- xrot.push(accel[i+6]);
- yrot.push(accel[i+8]);
- zrot.push(accel[i+10]);
- }
- //store accelerometer data
- 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+'")');
- }
- });
- });
- //converts data given from UART (hex values stored in a string) into integers
- function uartConverter(stringIn) {
- for (i=0;i<stringIn.length;i=i+2){
- newString = parseInt("0x"+stringIn.toString().substring(i,i+2));
- }
- return newString;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement