Permalink
Browse files

Separate motion logic from server

  • Loading branch information...
1 parent d30d42e commit 9e6ea9feae823aec59c6760c7eddeca21882a8f7 @Shinao committed Jun 27, 2016
Showing with 61 additions and 54 deletions.
  1. +59 −0 motion.js
  2. +2 −54 server.js
View
@@ -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);
+ });
+ });
+ });
+}
View
@@ -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);

0 comments on commit 9e6ea9f

Please sign in to comment.