-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
285 lines (236 loc) · 7.54 KB
/
index.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
/*
The Node.js API module that communicates with the devices
* Forked from Marco Schwartz [https://github.com/marcoschwartz]
* Modified and remixed by Amir Off [https://github.com/amiroff157] on Sep, 2018
*
* This library is licensed under the GNU General Public License v3.0
*/
// Importing dependencies
const router = require('express').Router();
const request = require('request');
const userCtrl = require('../controllers/user.controller');
const consoleConfig = require('../config/console-stamp.config');
require('console-stamp')(console, consoleConfig);
const rpio = require('rpio');
/* Registered devices are non-persistent and
live as long as the Node.js process is running */
const database = {
devices: [],
getDevice: function (device) {
for (let i = 0; i < this.devices.length; i++) {
if (this.devices[i].id === device || this.devices[i].name === device) {
return this.devices[i];
}
}
}
};
class Device {
constructor() {
this.hardware = "";
this.address = "";
this.name = "";
this.id = "";
}
getDevice(callback) {
request({
uri: 'http://' + this.address,
json: true,
timeout: 1000
}, callback);
};
getVariable(variable, callback) {
request({
uri: 'http://' + this.address + '/' + variable,
json: true,
timeout: 1000
}, callback);
};
callFunction(function_name, parameters, callback) {
request({
uri: 'http://' + this.address + '/' + function_name + "?params=" + parameters,
json: true,
timeout: 1000
}, callback);
};
analogRead(pin, callback) {
request({
uri: 'http://' + this.address + '/analog/' + pin,
json: true,
timeout: 1000
}, callback);
};
analogWrite(pin, value, callback) {
request({
uri: 'http://' + this.address + '/analog/' + pin + '/' + value,
json: true,
timeout: 1000
}, callback);
};
digitalRead(pin, callback) {
request({
uri: 'http://' + this.address + '/digital/' + pin,
json: true,
timeout: 1000
}, callback);
};
digitalWrite(pin, value, callback) {
request({
uri: 'http://' + this.address + '/digital/' + pin + '/' + value,
json: true,
timeout: 1000
}, callback);
};
takeSnapshot(callback) {
request({
uri: 'http://' + this.address + '/camera/snapshot/',
json: true,
timeout: 1000
}, callback);
};
gpioDigitalWrite(pin, value) {
const val = parseInt(value) === 0 ? rpio.LOW : rpio.HIGH;
rpio.open(pin, rpio.OUTPUT, rpio.LOW);
rpio.write(pin, val);
}
pinMode(pin, value, callback) {
request({
uri: 'http://' + this.address + '/mode/' + pin + '/' + value,
json: true,
timeout: 1000
}, callback);
};
}
router.post('/signin', userCtrl.signin);
router.post('/signup', userCtrl.verifyJWT, userCtrl.signup);
router.get('/user', userCtrl.verifyJWT, userCtrl.getUser);
router.post('/add', userCtrl.verifyJWT, (req, res) => {
let new_device = new Device();
new_device.hardware = req.body.hardware;
new_device.address = req.body.address;
setTimeout(() => {
new_device.getDevice((error, response, body) => {
if (error || typeof (body) === 'undefined') {
console.warn('The device is offline and cannot be added!');
res.status(404).json({'error': 'The device is offline and cannot be added!'});
} else {
new_device.id = body.id;
new_device.name = body.name;
new_device.hardware = body.hardware;
database.devices.push(new_device);
// Send response headers back to client
res.status(200).json({
'id': body.id
});
console.info("Device added with ID: " + body.id);
}
});
}, 2000);
});
// Return all devices
router.get('/devices', userCtrl.verifyJWT, function (req, res) {
res.json(database.devices);
});
// Return a specific device
router.get('/:device', userCtrl.verifyJWT, function (req, res) {
console.log('Sync request sent to device: ' + req.params.device);
let device = database.getDevice(req.params.device);
if (typeof (device) !== 'undefined') {
// Get status
device.getDevice(function (error, response, body) {
res.json(body);
});
} else {
res.json({message: 'Device not found'});
}
});
// Execute a function
router.get('/:device/execute/:command', userCtrl.verifyJWT, function (req, res) {
// Get device
let device = database.getDevice(req.params.device);
if (typeof (device) !== 'undefined') {
console.log('Function execution request sent to device: ' + req.params.device);
// Execute function
device.callFunction(req.params.command, req.query.params, (error, response, body) => {
res.json(body);
});
} else {
res.json({message: 'Device not found'});
}
});
// Get variable
router.get('/device/:device/:variable', userCtrl.verifyJWT, (req, res) => {
// Get device
let device = database.getDevice(req.params.device);
if (typeof (device) !== 'undefined') {
console.log('Variable read request sent to device: ' + req.params.device);
// Get variable
device.getVariable(req.params.variable, (error, response, body) => {
res.json(body);
});
} else {
res.json({message: 'Device not found'});
}
});
// Digital write
router.get('/:device/digital/:pin/:value', userCtrl.verifyJWT, function (req, res) {
console.log('Digital write request sent to device: ' + req.params.device);
// Get device
let device = database.getDevice(req.params.device);
if (typeof (device) !== 'undefined') {
// Send command
device.digitalWrite(req.params.pin, req.params.value, function (error, response, body) {
res.json(body);
});
} else {
res.json({message: 'Device not found'});
}
});
// Analog read
router.get('/:device/analog/:pin/', userCtrl.verifyJWT, function (req, res) {
console.log('Analog read request sent to device: ' + req.params.device);
// Get device
let device = database.getDevice(req.params.device);
// Get variable
device.analogRead(req.params.pin, function (error, response, body) {
res.json(body);
});
});
// Analog write
router.get('/:device/analog/:pin/:value', userCtrl.verifyJWT, function (req, res) {
console.log('Analog write request sent to device: ' + req.params.device);
// Get device
let device = database.getDevice(req.params.device);
// Get variable
device.analogWrite(req.params.pin, req.params.value, function (error, response, body) {
res.json(body);
});
});
// Digital read
router.get('/:device/digital/:pin/', userCtrl.verifyJWT, function (req, res) {
console.log('Digital read request sent to device: ' + req.params.device);
// Get device
let device = database.getDevice(req.params.device);
// Get variable
device.digitalRead(req.params.pin, function (error, response, body) {
res.json(body);
});
});
// GPIO Digital Write
router.get('/pi/:pin/:value', userCtrl.verifyJWT, (req, res) => {
console.info('Digital write request sent to GPIO: ' + req.params.pin);
let device = new Device();
device.gpioDigitalWrite(req.params.pin, req.params.value);
return res.json({'message': 'Digital write request sent to GPIO: ' + req.params.pin})
});
// Pin mode
router.get('/:device/mode/:pin/:value', userCtrl.verifyJWT, function (req, res) {
console.log('Pin mode request sent to device: ' + req.params.device);
// Get device
let device = database.getDevice(req.params.device);
// Get variable
device.pinMode(req.params.pin, req.params.value, function (error, response, body) {
res.json(body);
});
});
// Exporting routs to to Node.js
module.exports = router;