-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
82 lines (52 loc) · 1.74 KB
/
app.js
File metadata and controls
82 lines (52 loc) · 1.74 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Cersei - the shame client
var express = require('express');
var fs = require('fs');
var readLineSync = require('readline-sync');
var app = express();
var exec = require('child_process').exec;
var uri = "https://api.particle.io/v1/events";
//check for config file, if it doesn't exit prompt for token
if(fs.existsSync('./config.json')){
var config = require('./config.json');
var token = "Bearer " + config.token;
}
else{
var token = "Bearer " + readLineSync.question("Config file not detected. Please enter your Particle API token:");
}
//set up the SSE client
var eventSourceInitDict = {rejectUnauthorized: false, headers: {'Authorization': token}};
var EventSource = require('eventsource');
//determine which CLI player to use depending on the platform
if(process.platform == 'win32' || 'win64'){
//it is Windows
var player = __dirname + '/players/mpg123-1.22.0-x86-64/mpg123.exe';
var shame = 'http://www.myinstants.com/media/sounds/shame-1.mp3';
}
if(process.platform == 'darwin'){
//it is Mac
var player = 'afplay';
var shame = __dirname + '/sounds/shame.mp3';
}
var es = new EventSource(uri, eventSourceInitDict);
app.listen(1337);
console.log("Cersei is walking with her head down on " + process.platform);
//listens for the shame event on the Particle Cloud and plays the shame clip when it sees the event
es.addEventListener('shamedingdingding', function(event){
console.log(event);
playShame(function(err){
});
}, false);
//in case of errors...
es.onerror = function(err){
console.log(err);
}
//Shame! DING DING DING!
function playShame(callback){
console.log("Shame! Ding Ding Ding");
exec(player +' '+shame, function(error, stdout, stderr){
if(error){
return callback(error);
}
return callback(null);
})
}