forked from Shinao/SmartMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.js
More file actions
61 lines (52 loc) · 1.68 KB
/
motion.js
File metadata and controls
61 lines (52 loc) · 1.68 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
var config = require('./config');
var fs = require('fs');
var request = require('request');
var socketClient = null;
var socketIO = null;
module.exports = function (app, http) {
socketIO = require('socket.io')(http);
// Web Socket IO for sending gestures to client
socketIO.of('/motion').on('connection', function(socket){
socketClient = socket;
});
app.get('/motion/gesture', function (req, res) {
try {
socketClient.emit('gesture', req.query);
} catch(e) {}
res.sendStatus(200);
});
app.get('/motion/takePhoto/:filepath', function (req, res) {
try {
fs.unlinkSync("public/" + req.params.filepath); // Removing current photo if found
}
catch(e) {}
request(config.web.motionUrl + req.params.filepath, function(err) {
res.sendStatus(err ? 404 : 200);
});
});
app.get('/doesFileExist/:filepath', function (req, res) {
fs.access("public/" + req.params.filepath, fs.R_OK | fs.W_OK, (err) => {
res.sendStatus(err ? 404 : 200);
})
});
app.get('/motion/uploadPhoto/:filepath', function (req, res) {
fs.readFile("public/" + req.params.filepath, function read(err, data) {
if (err) {
console.log("Could not read photo: ", err);
res.sendStatus(404);
}
content = data;
request.put('https://api-content.dropbox.com/1/files_put/auto/' + req.params.filepath, {
headers: { Authorization: 'Bearer ' + config.widget.photo.dropboxKey, 'Content-Type': 'text/plain'},
body:content},
function optionalCallback (err, httpResponse, bodymsg) {
if (err) {
console.log("Could not upload to dropbox: ", err);
res.sendStatus(400);
}
console.log("Uploaded photo to dropbox: ", bodymsg);
res.sendStatus(200);
});
});
});
}