forked from amansx/alexa-raspberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (59 loc) · 1.78 KB
/
app.js
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
#!/usr/bin/env node
/*
* @Author: Amanpreet Singh
* @Email : [email protected]
*/
var wemore = require('wemore');
var exec = require('child_process').exec;
var path = require('path');
var deviceRefs = [], i = 0;
var loadDevices = function( devices ){
for( var device in devices ){
var dev = wemore.Emulate({ friendlyName: device, port: 9000 + i++ });
var devprop = devices[device];
dev.on( 'listening', (function(device, devprop) {
return function(){
console.log( device + " Now online, Now listening on", this.port );
}
})(device, devprop));
dev.on('on', (function(device, devprop) {
var command = devprop.oncommand;
return function(){
console.log("Device", device, "recieved on" );
if( command ){
console.log( 'Executing command "', command, '"' );
exec( command, function(error, stdout, stderr) {
//console.log( arguments );
});
}
}
})(device, devprop));
dev.on('off', (function(device, devprop) {
var command = devprop.offcommand;
return function(){
console.log("Device", device, "recieved off" );
if( command ){
console.log( 'Executing command "', command, '"');
exec( command, function(error, stdout, stderr) {
//console.log( arguments );
});
}
}
})(device, devprop));
}
};
var ProjectRoot = process.cwd();
var loadWemoEmulation = function( args ){
var configpath = ProjectRoot + "/devices.json";
if( args.length > 0 ){ configpath = path.resolve(args[0]); }
try{
console.log("Loading config file: '" + configpath + "'" );
var deviceFile = require( configpath );
loadDevices( deviceFile );
}catch(e){
console.log("Error:", e.message );
console.log("Invalid Device Configuration File! Exiting..");
}
};
var args = process.argv.slice(2);
loadWemoEmulation( args );