Skip to content

Commit ef265d8

Browse files
committed
seperate into different files for easier comprehension
1 parent d2077c6 commit ef265d8

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

index.js

+6-52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ var io = require('socket.io')(server);
77
var needle = require('needle');
88
var port = process.env.PORT || 3000;
99

10+
var sup = require('./lib/sup')(io);
11+
var heyThere = require('./lib/hey-there')(io);
12+
13+
console.log(heyThere);
14+
1015
app.use(express.static(path.join(__dirname, 'public')));
1116

1217
app.use('/', function(req, res){
@@ -17,57 +22,6 @@ server.listen(port, function(){
1722
console.log('server started, listening on port ' + port + '...');
1823
});
1924

20-
21-
22-
23-
// Hey There demo
24-
var notifySpark = function(status, next){
25-
var url = [
26-
'https://api.spark.io/v1/devices/',
27-
process.env.SPARK_CORE_ID,
28-
'/updateState'
29-
].join('');
30-
31-
var params = [
32-
'access_token=',
33-
process.env.SPARK_CORE_TOKEN,
34-
'&params=',
35-
status
36-
].join('');
37-
38-
needle.post(url, params, next);
39-
};
40-
41-
var busy = false;
42-
var handleStatusUpdate = function(data){
43-
if(!busy){
44-
busy = true;
45-
46-
io.sockets.emit('disable-buttons');
47-
notifySpark(data.status, function(err, res){
48-
busy = false;
49-
io.sockets.emit('enable-buttons');
50-
});
51-
}
52-
};
53-
5425
io.on('connection', function(socket){
55-
socket.on('status-update', handleStatusUpdate);
56-
});
57-
58-
59-
60-
61-
62-
// Phidgets stuff / Sup demo
63-
var phidgetsLib = require('phidgets');
64-
var phidget = new phidgetsLib();
65-
66-
phidget.connect(function(){
67-
console.log('phidget connected');
68-
});
69-
70-
phidget.on('input', function(boardId, inputId, state){
71-
if(inputId == 1 && state == 1)
72-
io.sockets.emit('sup');
26+
socket.on('status-update', heyThere.statusUpdate);
7327
});

lib/hey-there.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var busy = false;
2+
3+
var notifySpark = function(status, next){
4+
var url = [
5+
'https://api.spark.io/v1/devices/',
6+
process.env.SPARK_CORE_ID,
7+
'/updateState'
8+
].join('');
9+
10+
var params = [
11+
'access_token=',
12+
process.env.SPARK_CORE_TOKEN,
13+
'&params=',
14+
status
15+
].join('');
16+
17+
needle.post(url, params, next);
18+
};
19+
20+
21+
module.exports = function(io){
22+
return {
23+
statusUpdate : function(data){
24+
if(!busy){
25+
busy = true;
26+
27+
io.sockets.emit('disable-buttons');
28+
notifySpark(data.status, function(err, res){
29+
busy = false;
30+
io.sockets.emit('enable-buttons');
31+
});
32+
}
33+
}
34+
};
35+
};

lib/sup.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function(io){
2+
var phidgetsLib = require('phidgets');
3+
var phidget = new phidgetsLib();
4+
5+
phidget.connect(function(){
6+
console.log('phidget connected');
7+
});
8+
9+
phidget.on('input', function(boardId, inputId, state){
10+
if(inputId == 1 && state == 1)
11+
io.sockets.emit('sup');
12+
});
13+
};

0 commit comments

Comments
 (0)