forked from FallingSnow/pianode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pianode.js
163 lines (147 loc) · 3.94 KB
/
pianode.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Pianode
// node.js script to run pianobar headless
var spawn = require('child_process').spawn;
var events = require('events');
var deepmerge = require('deepmerge');
var fs = require('fs');
var processIo = require('./lib/processIo.js');
var pianodeRoot = __dirname+'';
var functions = require('./lib/functions.js');
function Pianode(userOptions) {
var pianode = this;
events.EventEmitter.call(pianode);
// Check if userOptions hold all neccessary fields
if(!(userOptions.password && userOptions.email)) {
throw 'Pianode error: You have to specify pandora.com credentials.';
}
var options = {
station: 'Q',
verbose: false,
logErrors: true,
password: '',
email: ''
};
if(userOptions) {
options = deepmerge(options, userOptions);
}
var state = {};
var setState = function(newState) {
state = deepmerge(state, newState);
};
var setStateOff = function() {
setState({
running: false,
loggedIn: false,
playing: false
});
};
setStateOff();
var getState = function() {
return state;
};
pianode.getState = getState;
var status = 'not running';
var prevStatus = 'undefined';
var setStatus = function(newStatus) {
if (newStatus !== status) {
pianode.emit('statusChange', {
status: newStatus,
prevStatus: status
});
prevStatus = status;
status = newStatus;
}
};
var getStatus = function() {
return {
status: status,
prevStatus: prevStatus
};
};
pianode.getStatus = getStatus;
var log = function(message) {
if(options.verbose) console.log(message);
};
var logError = function(message) {
if(options.logErrors) console.log(message);
};
var pianobar = null;
var pianobarPath = pianodeRoot+'/pianobar/pianobar';
var pianobarConfigPrefix = pianodeRoot; // resulting in [prefix]/pianobar/config
pianode.playPause = functions.playPause;
pianode.play = functions.play;
pianode.pause = functions.pause;
pianode.next = functions.next;
pianode.love = functions.love;
pianode.ban = functions.ban;
//pianode.history = functions.history;
//pianode.upcoming = functions.upcoming;
//pianode.explain = functions.explain;
//pianode.switchStation = functions.switchStation;
pianode.start = function() {
pianobar = spawn(pianobarPath, [ ], {
stdio: 'pipe',
env: deepmerge(process.env, {
XDG_CONFIG_HOME: pianobarConfigPrefix
})
});
functions.setUp({
write: function(data) {
pianobar.stdin.write(data + '\n');
//pianobar.stdin.end();
},
setStatus: setStatus,
getStatus: function() {
return getStatus().status;
},
setState: setState
});
pianobar.stdout.on('data', function(data) {
log('' + data);
// Call routes
processIo({
data: data,
write: function(data) {
pianobar.stdin.write(data + '\n');
//pianobar.stdin.end();
},
emit: function(event, obj) {
pianode.emit(event, obj);
},
setStatus: setStatus,
log: log,
logError: logError,
options: options,
getState: getState,
setState: setState,
setStateOff: setStateOff
});
});
pianobar.stderr.on('data', function(data) {
logError('Pianobar error: ' + data);
pianode.emit('error', {
type: 'Unknown error',
text: data+''
});
setStatus('error');
setStateOff();
});
pianobar.on('close', function(code) {
logError('Pianobar exited with code ' + code + '.');
pianode.emit('close', code);
setStatus('not running');
setStateOff();
});
};
process.on('exit', function() {
if(pianobar) {
log('Pianode closing. Killing pianobar.');
pianobar.kill();
}
setStatus('not running');
setStateOff();
pianode.emit('exit');
});
}
Pianode.prototype.__proto__ = events.EventEmitter.prototype;
module.exports = Pianode;