2
1
mirror of https://github.com/s00500/nodeMessageBoard.git synced 2024-11-23 06:10:55 +00:00

Added serverside functions and routing for demomode

This commit is contained in:
Lukas Bachschwell 2016-06-28 20:14:35 +02:00
parent deca94405b
commit 191fd0a231
3 changed files with 46 additions and 6 deletions

8
app.js
View File

@ -2,7 +2,7 @@ var server = require("./server");
var router = require("./route");
var requestHandlers = require("./requestHandlers");
var debug = false;
var debug = false; // todo move this to configfile
var handle = {}
handle["/"] = requestHandlers.sendInterface;
@ -14,5 +14,11 @@ handle["/js/jquery-ui.js"] = requestHandlers.sendJqueryUI;
handle["/css/bootstrap.min.css"] = requestHandlers.sendBootstrap;
handle["/clear"] = requestHandlers.sendClear;
if(server.demoMode==1){
handle["/demo"] = requestHandlers.sendDemo;
}else{
console.log("ERROR: Demo mode not active");
}
server.start(router.route,handle,debug);

View File

@ -15,7 +15,7 @@
function initSocketIO()
{
iosocket = io.connect();
iosocket.on('onconnection', function() {
iosocket.on('connect', function() {
iosocket.on('debugMessage', function(message) {
alert(message);

View File

@ -21,6 +21,8 @@ var numberRecieved = "";
var timeRecieved = "";
var color = 0;
module.exports.demoMode = 1;
// utility function for ucs2 decode
function ucs2Parse(ucs2){
codeArray = ucs2.match(/.{1,4}/g);
@ -106,7 +108,7 @@ function initSocketIO(httpServer,debug)
});
socket.on('getLastMessages', function(number) {
console.log('retrieving messages');
console.log('retrieving messages');
var times = db('messages').chain().takeRight(number).map('time').value();
var numbers = db('messages').chain().takeRight(number).map('number').value();
var messages = db('messages').chain().takeRight(number).map('message').value();
@ -117,6 +119,26 @@ function initSocketIO(httpServer,debug)
}
});
socket.on('demoMessage', function(number,date,time,message) {
numberStringRecieved = "Date: "+date+" demo#demo#demo#demo";
numberRecieved = number;
console.log(numberRecieved);
timeRecieved = time;
var color = db('messages').chain().takeRight(1).map('color').value();
if (color == null)color = 0;//no messages yet
color++;
if(color > 4) color = 0;
console.log(message);
var messageRecieved = escapeHtml(message);
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, convert(messageRecieved),color);
numberStringRecieved = null;
numberRecieved = null;
timeRecieved = null;
data=null;
});
});
}
@ -124,12 +146,22 @@ function initSocketIO(httpServer,debug)
function serialListener(debug)
{
var receivedData = "";
serialPort = new SerialPort(portName, {
baudrate: 19200,
parser: serialport.parsers.readline("\n")
});
},false); //don't open imedeatly
serialPort.open(function (err) {
if (err) {
return console.log('Error opening port: ', err.message,'### ENABELED DEMOMODE under /demo ! ##############');
}
if (err) {
module.exports.demoMode = 1;
return console.log('Error opening port: ', err.message,'### ENABELED DEMOMODE under /demo ! ##############');
}
serialPort.on("open", function () {
console.log('opened serial communication');
serialPort.on('data', function(data) {
@ -147,7 +179,7 @@ function serialListener(debug)
if(numberRecieved){
//console.log("emit");
var color = db('messages').chain().takeRight(1).map('color').value();
console.log(JSON.stringify(color));
//console.log(JSON.stringify(color));
if (color == null)color = 0;//no messages yet
color++;
if(color > 4) color = 0;
@ -158,6 +190,7 @@ function serialListener(debug)
numberStringRecieved = null;
numberRecieved = null;
timeRecieved = null;
data=null;
}
else {
//console.log("nothing");
@ -167,6 +200,7 @@ function serialListener(debug)
//socketServer.emit('debugMessage', data);
}
});
var pincode = config('mainConfig').chain().find({ param: 'pincode' }).value()['value'];
serialPort.write('AT+CPIN='+pincode+'\r');
console.log("Sent Pincode...");