-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (93 loc) · 3 KB
/
app.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Module dependencies.
*/
var fs = require('fs');
var request = require('request');
// file is included here:
eval(fs.readFileSync('lib/numjs.js')+'');
var express = require('express');
var sys = require('sys');
var logging = require('node-logging');
logging.setLevel('error');
var app = express.createServer();
console.log("ENV.PORT=" + process.env.PORT);
var MemJS = require("memjs").Client
memjs = MemJS.create();
app.listen(process.env.PORT || 3001);
app.use(logging.requestLogger);
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
var io = require('socket.io').listen(app, { log: false });
var eda_cache = {};
var NUM_SAMPLES_TO_STORE = 8*60;
var DEFAULT_ENDPOINT = "https://physio-glass.appspot.com/physio";
app.use(express.static(__dirname + '/public'));
app.get("/stream/:id", function(req, res) {
console.log(req.params.id);
res.sendfile(__dirname + '/public/' + "index.html");
});
app.post("/register/:sensorid", function(req,res) {
console.log("Got a registration request!");
console.log(req.params);
console.log(req.body);
console.log(req.body.userid);
var error = memjs.set(req.params.sensorid+"", req.body.userid+"");
if(error) console.log(error);
res.send("{'status':'registered'}");
});
app.post("/unregister/:sensorid", function(req,res) {
console.log(req.params);
memjs.set(req.params.sensorid, null);
res.send("{'status':'unregistered'}");
});
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
console.log("Socket connected! ");
socket.on('packet', function (data) {
try {
console.log("Recieved Packet: " + data);
var packet = JSON.parse(data);
if (!eda_cache.hasOwnProperty(packet.id)) {
eda_cache[packet.id] = new Array();
}
if (eda_cache[packet.id].length >= NUM_SAMPLES_TO_STORE) {
memjs.get(packet.id+"", function(userid) {
console.log("Stored data for " + packet.id + " is " + userid);
var endpoint = DEFAULT_ENDPOINT;
request({
uri: endpoint,
method: "POST",
form: {
'EDA': eda_cache[packet.id].join(","),
'userid': userid+''
}
}, function(error, response, body) {
console.log(error);
console.log(body);
console.log(response);
});
eda_cache[packet.id] = [];
});
}
var eda = parseFloat(packet.payload.split(",")[packet.payload.split(",").length-1])
console.log("EDA value is: " + eda +" | Sample Number: " + eda_cache[packet.id].length);
eda_cache[packet.id].push(eda);
console.log("ID: " +packet.id + " | Payload: " + packet.payload);
io.sockets.emit(packet.id,packet.payload);
}
catch(err) {
console.log(err);
}
});
socket.on('disconnect', function () {
console.log("Socket disconnected!");
});
});
console.log("Express server listening on port " + process.env.PORT);