2016-03-07 20:14:16 +00:00
|
|
|
var fs = require('fs'),
|
|
|
|
http = require('http'),
|
|
|
|
socketio = require('socket.io'),
|
|
|
|
url = require("url"),
|
|
|
|
serialport = require("serialport");
|
2016-03-08 11:07:56 +00:00
|
|
|
low = require('lowdb'),
|
|
|
|
storage = require('lowdb/file-async'),
|
|
|
|
db = low('db.json', { storage });
|
2016-03-07 20:14:16 +00:00
|
|
|
var SerialPort = serialport.SerialPort;
|
2016-03-10 14:08:22 +00:00
|
|
|
var config = low('config.json', { storage });
|
2016-03-07 20:14:16 +00:00
|
|
|
var socketServer;
|
2016-03-10 14:08:22 +00:00
|
|
|
var port = config('mainConfig').chain().find({ param: 'port' }).value()['value'];
|
2016-03-07 20:14:16 +00:00
|
|
|
var serialPort;
|
2016-03-10 14:08:22 +00:00
|
|
|
var portName = config('mainConfig').chain().find({ param: 'serialport' }).value()['value'];
|
2016-03-08 11:27:22 +00:00
|
|
|
|
2016-03-08 11:07:56 +00:00
|
|
|
var numberStringRecieved = "";
|
2016-03-08 09:06:36 +00:00
|
|
|
var numberRecieved = "";
|
2016-03-09 13:51:46 +00:00
|
|
|
var timeRecieved = "";
|
2016-03-10 08:28:41 +00:00
|
|
|
var color = 0;
|
2016-03-09 13:51:46 +00:00
|
|
|
|
|
|
|
// utility function for ucs2 decode
|
|
|
|
function ucs2Parse(ucs2){
|
|
|
|
codeArray = ucs2.match(/.{1,4}/g);
|
|
|
|
var returnString = "";
|
|
|
|
for(i=0;i<codeArray.length;i++){
|
|
|
|
returnString += String.fromCharCode(parseInt(codeArray[i], 16));
|
|
|
|
}
|
|
|
|
return returnString;
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:14:16 +00:00
|
|
|
|
|
|
|
// handle contains locations to browse to (vote and poll); pathnames.
|
|
|
|
function startServer(route,handle,debug)
|
|
|
|
{
|
2016-03-10 08:28:41 +00:00
|
|
|
|
|
|
|
db('messages').size(); //call this once to establish the db if there is nothing in the json file
|
|
|
|
if(db('messages').size() == 0)db('messages').push({ numberString: "",number: "+43 681 2033 4015", time: "now", message: "SMS MessageBoard",color: 0 });
|
|
|
|
|
2016-03-07 20:14:16 +00:00
|
|
|
// on request event
|
|
|
|
function onRequest(request, response) {
|
|
|
|
// parse the requested url into pathname. pathname will be compared
|
|
|
|
// in route.js to handle (var content), if it matches the a page will
|
|
|
|
// come up. Otherwise a 404 will be given.
|
|
|
|
var pathname = url.parse(request.url).pathname;
|
|
|
|
console.log("Request for " + pathname + " received");
|
|
|
|
var content = route(handle,pathname,response,request,debug);
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:51:46 +00:00
|
|
|
var httpServer = http.createServer(onRequest).listen(port, function(){
|
|
|
|
console.log("Listening at: http://localhost:"+port);
|
2016-03-07 20:14:16 +00:00
|
|
|
console.log("Server is up");
|
|
|
|
});
|
|
|
|
serialListener(debug);
|
|
|
|
initSocketIO(httpServer,debug);
|
|
|
|
}
|
|
|
|
|
|
|
|
function initSocketIO(httpServer,debug)
|
|
|
|
{
|
|
|
|
socketServer = socketio.listen(httpServer);
|
|
|
|
if(debug == false){
|
|
|
|
socketServer.set('log level', 1); // socket IO debug off
|
|
|
|
}
|
|
|
|
socketServer.on('connection', function (socket) {
|
|
|
|
console.log("user connected");
|
2016-03-08 11:27:22 +00:00
|
|
|
socket.emit('onconnection');
|
2016-03-08 11:07:56 +00:00
|
|
|
|
2016-03-10 14:08:22 +00:00
|
|
|
socket.on('getConfig', function(data) {
|
|
|
|
var number = config('mainConfig').chain().find({ param: 'mynumber' }).value()['value'];
|
|
|
|
socket.emit('config',number);
|
|
|
|
});
|
|
|
|
|
2016-03-07 20:48:50 +00:00
|
|
|
socket.on('sendAT', function(data) {
|
2016-03-07 20:14:16 +00:00
|
|
|
serialPort.write('AT\r');
|
2016-03-07 20:48:50 +00:00
|
|
|
console.log('sending AT...');
|
2016-03-07 20:14:16 +00:00
|
|
|
});
|
2016-03-08 11:07:56 +00:00
|
|
|
|
|
|
|
socket.on('getLastMessages', function(number) {
|
|
|
|
console.log('retrieving messages');
|
2016-03-08 11:27:22 +00:00
|
|
|
var times = db('messages').chain().takeRight(number).map('time').value();
|
2016-03-08 11:07:56 +00:00
|
|
|
var numbers = db('messages').chain().takeRight(number).map('number').value();
|
|
|
|
var messages = db('messages').chain().takeRight(number).map('message').value();
|
2016-03-10 08:28:41 +00:00
|
|
|
var colors = db('messages').chain().takeRight(number).map('color').value();
|
2016-03-08 11:07:56 +00:00
|
|
|
|
|
|
|
for(var i = 0; i < messages.length; i++){
|
2016-03-10 08:28:41 +00:00
|
|
|
socket.emit('newMessage',times[i],numbers[i],messages[i],colors[i]);
|
2016-03-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-07 20:48:50 +00:00
|
|
|
});
|
2016-03-07 20:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen to serial port
|
|
|
|
function serialListener(debug)
|
|
|
|
{
|
|
|
|
var receivedData = "";
|
|
|
|
serialPort = new SerialPort(portName, {
|
|
|
|
baudrate: 19200,
|
|
|
|
parser: serialport.parsers.readline("\n")
|
|
|
|
});
|
|
|
|
|
|
|
|
serialPort.on("open", function () {
|
|
|
|
console.log('opened serial communication');
|
|
|
|
|
|
|
|
serialPort.on('data', function(data) {
|
2016-03-08 09:06:36 +00:00
|
|
|
console.log('Incomming serial data...\r');
|
|
|
|
console.log(data);
|
|
|
|
console.log("\r"); //+data.length
|
|
|
|
// send the incoming data to clients using the socket.
|
|
|
|
if(data.startsWith("+CMT:")){ // if message ok
|
2016-03-08 11:07:56 +00:00
|
|
|
numberStringRecieved = data;
|
|
|
|
numberRecieved = data.substring(data.indexOf('+CMT: "') + 7, data.indexOf('",'));
|
2016-03-09 13:51:46 +00:00
|
|
|
numberRecieved = ucs2Parse(numberRecieved);
|
2016-03-08 11:27:22 +00:00
|
|
|
timeRecieved = data.substring(data.indexOf('","","') + 15, data.indexOf('+04'));
|
2016-03-08 09:06:36 +00:00
|
|
|
} else if (data.length > 1) {
|
2016-03-08 11:07:56 +00:00
|
|
|
|
2016-03-08 09:06:36 +00:00
|
|
|
if(numberRecieved){
|
2016-03-08 11:07:56 +00:00
|
|
|
//console.log("emit");
|
2016-03-10 08:28:41 +00:00
|
|
|
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;
|
2016-03-09 13:51:46 +00:00
|
|
|
var messageRecieved = ucs2Parse(data);
|
2016-03-10 08:28:41 +00:00
|
|
|
db('messages').push({ numberString: numberStringRecieved,number: numberRecieved, time: timeRecieved, message: messageRecieved,color: color });
|
2016-03-08 11:07:56 +00:00
|
|
|
//add a new message to the board directly
|
2016-03-10 08:28:41 +00:00
|
|
|
socketServer.emit('newMessage', timeRecieved, numberRecieved, messageRecieved,color);
|
2016-03-08 11:07:56 +00:00
|
|
|
numberStringRecieved = null;
|
2016-03-08 09:06:36 +00:00
|
|
|
numberRecieved = null;
|
2016-03-08 11:27:22 +00:00
|
|
|
timeRecieved = null;
|
2016-03-08 09:06:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-03-08 11:07:56 +00:00
|
|
|
//console.log("nothing");
|
2016-03-08 09:06:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//debugMessages trigger an alert on the clients
|
|
|
|
//socketServer.emit('debugMessage', data);
|
|
|
|
}
|
|
|
|
});
|
2016-03-10 14:08:22 +00:00
|
|
|
var pincode = config('mainConfig').chain().find({ param: 'pincode' }).value()['value'];
|
|
|
|
serialPort.write('AT+CPIN='+pincode+'\r');
|
2016-03-07 20:14:16 +00:00
|
|
|
console.log("Sent Pincode...");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports.start = startServer;
|