-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (115 loc) · 3.73 KB
/
server.js
File metadata and controls
155 lines (115 loc) · 3.73 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var express = require("express"),
http = require('http'),
md5 = require('md5');
var app = express();
var router = express.Router();
var port = 8080;
/*
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
router.route('/data')
.get(function(req, res) {
console.log('Data post !');
console.log(req.ip);
res.json({ message: 'Data post !' });
})
.post(function(req, res) {
console.log('Data post !');
res.json({ message: 'Data post!' });
});
app.use('/',express.static(__dirname + '/public/'));
app.use('/api', router);
*/
var appName = 'HACKATON2016',
pass = 'hackATHon20!6CS',
appUser = 'reto1',
sensorId = 'SENSOR_TEMPERATURA',
variableName = 'temperatura';
/*
.addHeader("GG-Requester-Application", credentials.getApp())
.addHeader("GG-Request-Timestamp", currentTimestamp.toString())
.addHeader("GG-Request-Signature", md5String)
.addHeader("IOT-Authorized-User", credentials.getUser());
String preToken = currentTimestamp + credentials.getSecret() + credentials.getApp();
byte[] md5 = md5Generator.digest(preToken.getBytes());
*/
app.use('/',express.static(__dirname + '/public/'));
app.get('/data', function (req, res) {
var currentTimestamp = new Date().getTime();
var preToken = `${currentTimestamp}${pass}${appName}`;
var hash = md5(preToken);
console.log(currentTimestamp);
console.log(preToken);
console.log(hash);
var options = {
host: 'api.iotsens.com',
path: `/v1/sensors/${sensorId}/variables/${variableName}/measures`,
headers: {
'GG-Requester-Application': appName,
'GG-Request-Timestamp': currentTimestamp,
'GG-Request-Signature': hash,
'IOT-Authorized-User': appUser
}
};
console.log(options.path);
return http.get(options, function(response, callback ) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);
//callback(parsed);
//console.log(parsed);
res.send(parsed);
//res.send(body);
});
response.on('error', function(e){
console.log(`error $e`);
});
});
//res.send('Hello World!');
});
app.get('/data/actual', function (req, res) {
var currentTimestamp = new Date().getTime();
var preToken = `${currentTimestamp}${pass}${appName}`;
var hash = md5(preToken);
console.log(currentTimestamp);
console.log(preToken);
console.log(hash);
var options = {
host: 'api.iotsens.com',
path: `/v1/sensors/${sensorId}/variables/${variableName}/measures`,
headers: {
'GG-Requester-Application': appName,
'GG-Request-Timestamp': currentTimestamp,
'GG-Request-Signature': hash,
'IOT-Authorized-User': appUser
}
};
console.log(options.path);
return http.get(options, function(response, callback ) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);
//callback(parsed);
//console.log(parsed);
res.send(parsed.data[0].value);
//res.send(body);
});
response.on('error', function(e){
console.log(`error $e`);
});
});
//res.send('Hello World!');
});
var server = app.listen(port, function () {
console.log('Server listening at http://localhost:'+ port);
});