1
0

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

View File

@ -3,6 +3,9 @@ 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;
@ -10,6 +13,7 @@ var socketServer;
var serialPort;
var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port
var sendData = "";
var numberStringRecieved = "";
var numberRecieved = "";
// handle contains locations to browse to (vote and poll); pathnames.
@ -45,10 +49,22 @@ function initSocketIO(httpServer,debug)
socketServer.on('update', function(data) {
socket.emit('updateData',{pollOneValue:data});
});
socket.on('sendAT', function(data) {
serialPort.write('AT\r');
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
// send the incoming data to clients using the socket.
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) {
//add a new message to the board directly
if(numberRecieved){
console.log("emit");
socketServer.emit('newMessage', numberRecieved+" "+data);
//console.log("emit");
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;
}
else {
console.log("nothing");
//console.log("nothing");
}
} else {
//debugMessages trigger an alert on the clients