Skip to content

Commit

Permalink
Separate motion logic from server
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinao committed Jun 27, 2016
1 parent d30d42e commit 9e6ea9f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 54 deletions.
59 changes: 59 additions & 0 deletions motion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var config = require('./config');
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);
});
});
});
}
56 changes: 2 additions & 54 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
var express = require('express');
var request = require('request');
var config = require('./config');
var app = express();
var http = require('http').Server(app);
var request = require('request');
var fs = require('fs');
var socketIO = require('socket.io')(http);
var socketClient = null;

app.use(express.static('./public'));

app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/home.html');
});

var widgets = require('./widgets');


// Widgets needing infos from server (scraping infos mostly)
var widgets = require('./widgets');
app.get('/weather', widgets.weather);
app.get('/news', widgets.news);
app.get('/cinema', widgets.cinema);

// Web Socket IO for sending gestures to client
socketIO.of('/motion').on('connection', function(socket){ socketClient = socket; });

// Forwarding between motion server and client
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 { // Removing current photo if found
fs.unlinkSync("public/" + req.params.filepath);
} 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);
});
});
});
require('./motion')(app, http);

http.listen(config.web.port, function () {
console.log('Server listening on port %d', config.web.port);
Expand Down

0 comments on commit 9e6ea9f

Please sign in to comment.