-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (46 loc) · 1.27 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
'use strict';
let SerialPort = require("serialport").SerialPort;
let serialport = new SerialPort("/dev/ttyACM0");
let spawn = require('child_process').spawn;
let showDirectory = '';
let switchStatus = 'off';
let vlc = '';
let setSwitchStatus = function ( status ) {
return new Promise(function( resolve, reject ) {
if ( status === 1 ) {
switchStatus = 'on';
resolve( switchStatus );
} else if ( status === 0 ) {
switchStatus = 'off';
resolve( switchStatus );
} else {
reject( 'Status value is invalid' );
}
});
};
let handleVlc = function ( ) {
return new Promise(function( resolve, reject ) {
if ( switchStatus === 'on' && vlc === '' ) {
//Start Vlc
vlc = spawn('vlc', ['/media/alex/Movies1/Jelly Tv Shows/King of the Hill/', '--random']);
resolve( 'Started playing show...' );
} else if ( switchStatus == 'off' && vlc !== '' ) {
//Stop Vlc
vlc.kill();
vlc = '';
resolve( 'Killed show...' );
}
});
};
serialport.on('open', function( ) {
serialport.on('data', function( data ) {
setSwitchStatus( data[0] )
.then(function( resolved ) {
return handleVlc();
}).then(function(resolved) {
console.log( resolved );
}).catch(function( rejected ) {
console.log( rejected );
});
});
});