2
1
mirror of https://github.com/s00500/nodeMessageBoard.git synced 2024-11-21 18:20:53 +00:00

Database load working

This commit is contained in:
Lukas Bachschwell 2016-03-08 12:07:56 +01:00
parent 95ea248d25
commit 5c3d9216c6
3 changed files with 42 additions and 66 deletions

40
app.js
View File

@ -9,43 +9,3 @@ handle["/"] = requestHandlers.sendInterface;
handle["/interface"] = requestHandlers.sendInterface; handle["/interface"] = requestHandlers.sendInterface;
server.start(router.route,handle,debug); server.start(router.route,handle,debug);
// old app.js
/*
console.log("hello world\r");
var port = 3000;
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World')
});
app.listen(port, function () {
console.log('Example app listening on port '+port+'!');
});
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("/dev/tty.usbmodemFD121", {
baudrate: 19200,
parser: serialport.parsers.readline("\n")
});
serialPort.on("open", function () {
console.log('open');
serialPort.on("data", function (data) {
console.log("here: "+data);
app.get('/', function (req, res) {
res.send(data)
})
});
serialPort.write("AT+CPIN?\r");
});
*/

View File

@ -49,23 +49,20 @@
{ {
iosocket = io.connect(); iosocket = io.connect();
iosocket.on('onconnection', function(value) { iosocket.on('onconnection', function(value) {
pollOneH = value.pollOneValue/2; // recieve start poll value from server
initButton(); initButton();
// recieve changed values by other client from server iosocket.on('debugMessage', function(message) {
iosocket.on('updateData', function(recievedData) { alert(message);
pollOneH = recievedData.pollOneValue/2; // recieve start poll value from server
}); });
iosocket.on('newMessage', function(message) { iosocket.on('newMessage', function(number,message) {
var date = new Date();
var mainTable = document.getElementById("mainTable"); var mainTable = document.getElementById("mainTable");
var newRow = mainTable.insertRow(mainTable.rows.length); var newRow = mainTable.insertRow(mainTable.rows.length);
var dateCell = newRow.insertCell(0); var dateCell = newRow.insertCell(0);
var textCell = newRow.insertCell(1); var numberCell = newRow.insertCell(1);
dateCell.innerHTML = date.toLocaleString(); var textCell = newRow.insertCell(2);
dateCell.innerHTML = "Insert serverdate";
numberCell.innerHTML = number;
textCell.innerHTML = message; textCell.innerHTML = message;
}); });
}); });
@ -74,14 +71,17 @@
function initButton() function initButton()
{ {
$( "#check" ).button(); $("#check").button();
} }
window.onload = function() { window.onload = function() {
initSocketIO(); initSocketIO();
iosocket.emit('getLastMessages',10);
}; };
$(document).ready(function() { $(document).ready(function() {
//request last messages from server
$('#check').click(function() { $('#check').click(function() {
toggleVal += 1; toggleVal += 1;
toggleVal %= 2; toggleVal %= 2;
@ -91,23 +91,19 @@
</script> </script>
</head> </head>
<body> <body>
<div id="rData"> <div style="text-align:center;">
<h2>Data from Sim900</h2> <h2>Data from Sim900</h2>
<table id="mainTable" border="1"> <table id="mainTable" border="1" align="center">
<tr> <tr>
<td>Uhrzeit</td><td>Nachricht</td> <td>Uhrzeit</td><td>Nummer</td><td>Nachricht</td>
</tr> </tr>
</table> </table>
<br>
</div> <br>
<div id="sData">
<h2>Debug functions</h2> <h2>Debug functions</h2>
<div id="btnHolder"> <div id="btnHolder">
<input type="checkbox" id="check" value="toggle"/><label for="check">Toggle LED</label> <input type="checkbox" id="check" value="toggle"/><label for="check">Send AT</label>
</div>
<p id="sliderTxt">LED Dimmer</p>
<div id="slider"></div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -3,6 +3,9 @@ http = require('http'),
socketio = require('socket.io'), socketio = require('socket.io'),
url = require("url"), url = require("url"),
serialport = require("serialport"); serialport = require("serialport");
low = require('lowdb'),
storage = require('lowdb/file-async'),
db = low('db.json', { storage });
var SerialPort = serialport.SerialPort; var SerialPort = serialport.SerialPort;
@ -10,6 +13,7 @@ var socketServer;
var serialPort; var serialPort;
var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port
var sendData = ""; var sendData = "";
var numberStringRecieved = "";
var numberRecieved = ""; var numberRecieved = "";
// handle contains locations to browse to (vote and poll); pathnames. // handle contains locations to browse to (vote and poll); pathnames.
@ -45,10 +49,22 @@ function initSocketIO(httpServer,debug)
socketServer.on('update', function(data) { socketServer.on('update', function(data) {
socket.emit('updateData',{pollOneValue:data}); socket.emit('updateData',{pollOneValue:data});
}); });
socket.on('sendAT', function(data) { socket.on('sendAT', function(data) {
serialPort.write('AT\r'); serialPort.write('AT\r');
console.log('sending AT...'); console.log('sending AT...');
}); });
socket.on('getLastMessages', function(number) {
console.log('retrieving messages');
var numbers = db('messages').chain().takeRight(number).map('number').value();
var messages = db('messages').chain().takeRight(number).map('message').value();
for(var i = 0; i < messages.length; i++){
socket.emit('newMessage',numbers[i],messages[i]);
}
});
}); });
} }
@ -70,16 +86,20 @@ function serialListener(debug)
console.log("\r"); //+data.length console.log("\r"); //+data.length
// send the incoming data to clients using the socket. // send the incoming data to clients using the socket.
if(data.startsWith("+CMT:")){ // if message ok if(data.startsWith("+CMT:")){ // if message ok
numberRecieved = data; numberStringRecieved = data;
numberRecieved = data.substring(data.indexOf('+CMT: "') + 7, data.indexOf('",'));
} else if (data.length > 1) { } else if (data.length > 1) {
//add a new message to the board directly
if(numberRecieved){ if(numberRecieved){
console.log("emit"); //console.log("emit");
socketServer.emit('newMessage', numberRecieved+" "+data); db('messages').push({ numberString: numberStringRecieved,number: numberRecieved, message: data });
//add a new message to the board directly
socketServer.emit('newMessage', numberRecieved,data);
numberStringRecieved = null;
numberRecieved = null; numberRecieved = null;
} }
else { else {
console.log("nothing"); //console.log("nothing");
} }
} else { } else {
//debugMessages trigger an alert on the clients //debugMessages trigger an alert on the clients