forked from surekap/MMM-ShairportMetadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
63 lines (52 loc) · 1.58 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-ShairportMetadata
*
* By surekap <[email protected]>
*
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const spawn = require('child_process').spawn;
module.exports = NodeHelper.create({
start: function() {
console.log("Starting module: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
this.getData();
}
},
/**
* getData
* Request data from the supplied URL and broadcast it to the MagicMirror module if it's received.
*/
getData: function() {
if (this.readableStream && this.readableStream != null){
// Do not start multiple processes
return;
}
const self = this;
self.str_payload = "";
this.readableStream = spawn(__dirname + "/shairport-metadata.sh", [this.config.metadataPipe, __dirname]);
this.readableStream.stderr.on('data', function(payload){
console.log("ERR: ", payload.toString());
});
this.readableStream.on('error', function(err){
console.log('an error occurred: ', err.toString());
});
this.readableStream.stdout.on('data', function(payload){
self.str_payload += payload.toString();
var lines = self.str_payload.split('\n')
self.str_payload = lines.pop();
for(var i = 0; i < lines.length; i++){
if (lines[i].length == 0){ continue; }
self.sendSocketNotification("DATA", JSON.parse(lines[i]));
}
});
this.readableStream.stdout.on('close', function(){
self.readableStream.kill();
self.readableStream = null;
})
}
});