-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (53 loc) · 1.7 KB
/
server.js
File metadata and controls
68 lines (53 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require('mongodb');
var moment = require('moment-timezone');
var mongodbClient = mongodb.MongoClient;
var app = express();
var mongodbURI='mongodb://localhost/sensor';
var collection, requestBody;
var events = require('events');
var eventEmitter = new events.EventEmitter();
mongodbClient.connect(mongodbURI,setupCollection);
function setupCollection(err,db) {
if(err) throw err;
collection = db.collection("sensor");
eventEmitter.on('dataReceived', insertData);
}
function prepareData(data) {
for(var i in data)
{
if (!isNaN(Number(data[i]))) { // check if it is convertible
data[i] = Number(data[i]);
} else { // is geolocation
data[i] = JSON.parse(data[i]);
}
}
var tempDateTime = moment(data["timestamp"]).tz("America/Sao_Paulo");
data["timestamp"] = tempDateTime.format(); // epoch to ISODate
data["year"] = tempDateTime.year();
data["month"] = tempDateTime.month() + 1; // 0 is january
data["day"] = tempDateTime.date();
data["hour"] = tempDateTime.hours();
data["minute"] = tempDateTime.minutes();
data["second"] = tempDateTime.seconds(); // for easy aggregation
return data;
}
function insertData(topic,payload) {
requestBody = prepareData(requestBody);
//console.log(requestBody);
collection.insert(requestBody, function(err, records) {
if (err) throw err;
console.log("Record added as "+records);
});
}
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/sensor',function(req,res){
console.log(req.body);
requestBody = req.body;
eventEmitter.emit('dataReceived');
res.end("yes");
});
app.listen(40000,function(){
console.log("Started on PORT 40000");
})