var fs = require('fs'), http = require('http'), socketio = require('socket.io'), url = require("url"), serialport = require("serialport"); low = require('lowdb'), storage = require('lowdb/file-async'), db = low('db.json', { storage }); var SerialPort = serialport.SerialPort; var config = low('config.json', { storage }); var socketServer; var port = config('mainConfig').chain().find({ param: 'port' }).value()['value']; var serialPort; var portName = config('mainConfig').chain().find({ param: 'serialport' }).value()['value']; var numberStringRecieved = ""; var numberRecieved = ""; var timeRecieved = ""; var color = 0; // utility function for ucs2 decode function ucs2Parse(ucs2){ codeArray = ucs2.match(/.{1,4}/g); var returnString = ""; for(i=0;i 1) { if(numberRecieved){ //console.log("emit"); var color = db('messages').chain().takeRight(1).map('color').value(); console.log(JSON.stringify(color)); if (color == null)color = 0;//no messages yet color++; if(color > 4) color = 0; var messageRecieved = ucs2Parse(data); db('messages').push({ numberString: numberStringRecieved,number: numberRecieved, time: timeRecieved, message: messageRecieved,color: color }); //add a new message to the board directly socketServer.emit('newMessage', timeRecieved, numberRecieved, messageRecieved,color); numberStringRecieved = null; numberRecieved = null; timeRecieved = null; } else { //console.log("nothing"); } } else { //debugMessages trigger an alert on the clients //socketServer.emit('debugMessage', data); } }); var pincode = config('mainConfig').chain().find({ param: 'pincode' }).value()['value']; serialPort.write('AT+CPIN='+pincode+'\r'); console.log("Sent Pincode..."); }); } exports.start = startServer;