-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
311 lines (269 loc) · 9.38 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Load .env config (silently fail if no .env present)
require('dotenv').config({ silent: true });
// Require necessary libraries
var async = require('async');
var ioLib = require('socket.io');
var http = require('http');
var path = require('path');
var express = require('express');
var MbedConnectorApi = require('mbed-connector-api');
// CONFIG (change these)
//V5nz2XpPdiZlmmqh3KtXIlwjS8dmQG67NXgLZFpdVyA2DqqPDyaED1mf3dtJz2vOypkBxbIV8AhpFgNUGCNETXnEt7DXkSd80GUv
var accessKey = "39WxX2aOAIHdZKUVNwSXYXQ91SyC3tA0AKB03m6m8w2RUVBuMEG3WQ68oZBdJ0k13GlRx76L1d78b2JidJ6rbFdlJzds64mXBkL8";
var port = 3000;
// Paths to resources on the endpoints
var Temperature = '/Sensor/0/Temp';
var PressureHp = '/Sensor/0/Hp';
var Humidity = '/Sensor/0/Hum';
var GPIO = '/GPIO/0/STATE'
// Instantiate an mbed Device Connector object
var mbedConnectorApi = new MbedConnectorApi({
accessKey: accessKey
});
// Create the express app
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
// Get all of the endpoints(i.e.,Array) and necessary info to render the page
mbedConnectorApi.getEndpoints(function(error, endpoints) {
if (error) {
throw error;
} else {
/*version 1: */ /*modified by joseph*/
endpoints.map(function(endpoint) {
console.log("endpoints name>"+endpoint.name);
console.log("endpoints type>"+endpoint.type);
console.log("endpoints status>"+endpoint.status);
});
res.render('index', {
endpoints: endpoints
});
/*-------------------------------------------------------*/
/*version 2: */
/*
// Setup the function array
var functionArray = endpoints.map(function(endpoint) {
return function(ok) {
ok();
};
});
// Fetch all Resoruces in parallel, finish when all HTTP
// requests are complete (uses Async.js library)
async.parallel(functionArray, function(error) {
if (error) {
res.send(String(error));
} else {
console.log("endpoints--->"+endpoints.toString());
res.render('index', {
endpoints: endpoints
});
}
});
*/
}
});
});
// Handle unexpected server errors
app.use(function(err, req, res, next) {
console.log(err.stack);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
var sockets = [];
var server = http.Server(app);
var io = ioLib(server);
// Setup sockets for updating web UI
io.on('connection', function (socket) {
// Add new client to array of client upon connection
sockets.push(socket);
socket.on('subscribe-to-Humidity', function (data) {
// Subscribe to all changes of resource /3200/0/5501 (button presses)
mbedConnectorApi.putResourceSubscription(data.endpointName, Humidity, function(error) {
if (error) throw error;
socket.emit('subscribed-to-Humidity', {
endpointName: data.endpointName
});
});
});
socket.on('subscribe-to-Pressure', function (data) {
// Subscribe to all changes of resource /3200/0/5501 (button presses)
mbedConnectorApi.putResourceSubscription(data.endpointName, PressureHp, function(error) {
if (error) throw error;
socket.emit('subscribed-to-Pressure', {
endpointName: data.endpointName
});
});
});
socket.on('subscribe-to-tmp', function (data) {
console.log("subscribe-to-tmp");
// Subscribe to all changes of resource /3200/0/5501 (button presses)
mbedConnectorApi.putResourceSubscription(data.endpointName, Temperature, function(error) {
console.log("putResourceSubscription sub tmp in app.js");
if (error) throw error;
socket.emit('subscribed-to-tmp', {
endpointName: data.endpointName
});
});
});
socket.on('subscribe-gpio-state', function (data) {
console.log("subscribe-gpio-state");
// Subscribe to all changes of resource /3200/0/5501 (button presses)
mbedConnectorApi.putResourceSubscription(data.endpointName, GPIO, function(error) {
console.log("putResourceSubscription sub gpio in app.js");
if (error) throw error;
socket.emit('subscribed-gpio-state', {
endpointName: data.endpointName
});
});
});
socket.on('unsubscribe-to-hum', function(data) {
// Unsubscribe from the resource /3200/0/5501 (button presses)
mbedConnectorApi.deleteResourceSubscription(data.endpointName, Humidity, function(error) {
if (error) throw error;
socket.emit('unsubscribed-to-hum', {
endpointName: data.endpointName
});
});
});
socket.on('unsubscribe-to-pres', function(data) {
// Unsubscribe from the resource /3200/0/5501 (button presses)
mbedConnectorApi.deleteResourceSubscription(data.endpointName, PressureHp, function(error) {
if (error) throw error;
socket.emit('unsubscribed-to-pres', {
endpointName: data.endpointName
});
});
});
socket.on('unsubscribe-to-tmp', function(data) {
// Unsubscribe from the resource /3200/0/5501 (button presses)
mbedConnectorApi.deleteResourceSubscription(data.endpointName, Temperature, function(error) {
if (error) throw error;
socket.emit('unsubscribed-to-tmp', {
endpointName: data.endpointName
});
});
});
socket.on('unsubscribe-gpio-state', function(data) {
// Unsubscribe from the resource /3200/0/5501 (button presses)
mbedConnectorApi.deleteResourceSubscription(data.endpointName, GPIO, function(error) {
if (error) throw error;
socket.emit('unsubscribed-gpio-state', {
endpointName: data.endpointName
});
});
});
socket.on('get-humidity', function(data) {
// Read data from GET resource /3200/0/5501 (num button presses)
mbedConnectorApi.getResourceValue(data.endpointName, Humidity, function(error, value) {
if (error) throw error;
socket.emit('Hum', {
endpointName: data.endpointName,
value: value
});
});
});
socket.on('get-pressure', function(data) {
// Read data from GET resource /3200/0/5501 (num button presses)
mbedConnectorApi.getResourceValue(data.endpointName, PressureHp , function(error, value) {
if (error) throw error;
socket.emit('Pressure', {
endpointName: data.endpointName,
value: value
});
});
});
socket.on('get-tmp', function(data) {
// Read data from GET resource /3200/0/5501 (num button presses)
console.log("get-temp in app.js");
mbedConnectorApi.getResourceValue(data.endpointName, Temperature, function(error, value) {
console.log("getResourceValue : Temperature called");
if (error) throw error;
socket.emit('Temp', {
endpointName: data.endpointName,
value: value
});
});
});
socket.on('get-gpio-state', function(data) {
console.log("get-gpio in app.js");
// Read data from GET resource /3200/0/5501 (num button presses)
mbedConnectorApi.getResourceValue(data.endpointName, GPIO , function(error, value) {
if (error) throw error;
console.log("getResourceValue:get-gpio in app.js");
socket.emit('GPIO-STATE', {
endpointName: data.endpointName,
value: value
});
});
});
socket.on('update-blink-pattern', function(data) {
// Set data on PUT resource /3201/0/5853 (pattern of LED blink)
mbedConnectorApi.putResourceValue(data.endpointName, PressureHp, data.blinkPattern, function(error) {
if (error) throw error;
});
});
socket.on('blink', function(data) {
// POST to resource /3201/0/5850 (start blinking LED)
mbedConnectorApi.postResource(data.endpointName, GPIO,data.myvalue, function(error) {
if (error) throw error;
});
});
socket.on('disconnect', function() {
// Remove this socket from the array when a user closes their browser
var index = sockets.indexOf(socket);
if (index >= 0) {
sockets.splice(index, 1);
}
})
});
// When notifications are received through the notification channel, pass the
// button presses data to all connected browser windows
mbedConnectorApi.on('notification', function(notification) {
console.log("notification.path:"+notification.path);
if (notification.path === Humidity) {
sockets.forEach(function(socket) {
socket.emit('Hum', {
endpointName: notification.ep,
value: notification.payload
});
});
}
if (notification.path === PressureHp) {
sockets.forEach(function(socket) {
socket.emit('Pressure', {
endpointName: notification.ep,
value: notification.payload
});
});
}
if (notification.path === Temperature) {
sockets.forEach(function(socket) {
socket.emit('Temp', {
endpointName: notification.ep,
value: notification.payload
});
});
}
if (notification.path === GPIO) {
console.log("notification:GPIO");
sockets.forEach(function(socket) {
socket.emit('GPIO-STATE', {
endpointName: notification.ep,
value: notification.payload
});
});
}
});
// Start the app
server.listen(port, function() {
// Set up the notification channel (pull notifications)
mbedConnectorApi.startLongPolling(function(error) {
if (error) throw error;
console.log('mbed Device Connector Quickstart listening at http://localhost:%s', port);
})
});