1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Check mpd really is listening on port 6600 before allowing nodes to exist.

Otherwise komponist fails badly.
This commit is contained in:
Dave C-J 2014-01-23 00:19:40 +00:00
parent 03484169e3
commit ea74eebff2

View File

@ -15,8 +15,10 @@
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); var RED = require(process.env.NODE_RED_HOME+"/red/red");
var util = require("util");
var exec = require('child_process').exec; var exec = require('child_process').exec;
var komponist = require('komponist'); var komponist = require('komponist');
var mpc = "";
exec("which mpd",function(err,stdout,stderr) { exec("which mpd",function(err,stdout,stderr) {
if (stdout.indexOf('mpd') == -1) { if (stdout.indexOf('mpd') == -1) {
util.log('[69-mpd.js] Error: Cannot find "mpd" command. Please install MPD.'); util.log('[69-mpd.js] Error: Cannot find "mpd" command. Please install MPD.');
@ -24,68 +26,74 @@ exec("which mpd",function(err,stdout,stderr) {
} }
}); });
var mpc = ""; exec("netstat -an | grep LISTEN | grep 6600",function(err,stdout,stderr) {
komponist.createConnection(6600, 'localhost', function(err, client) { if (stdout.indexOf('6600') == -1) {
if (err) node.error("MPD: Failed to connect to MPD server"); util.log('[69-mpd.js] Error: MPD daemon not listening on port 6600. Please start MPD.');
mpc = client; return;
}); }
komponist.createConnection(6600, 'localhost', function(err, client) {
function MPDOut(n) { if (err) node.error("MPD: Failed to connect to MPD server");
RED.nodes.createNode(this,n); mpc = client;
var node = this;
node.mpc = mpc;
this.on("input", function(msg) {
if (msg != null) {
console.log(msg);
try {
//node.mpc.command(msg.payload);
node.mpc.command(msg.payload, msg.param, function(err, results) {
if (err) { console.log("MPD: Error:",err); }
//else { console.log(results); }
});
} catch (err) { console.log("MPD: Error:",err); }
}
}); });
node.mpc.on('error', function(err) { function MPDOut(n) {
console.log("MPD: Error:",err); RED.nodes.createNode(this,n);
}); var node = this;
} node.mpc = mpc;
RED.nodes.registerType("mpd out",MPDOut);
function MPDIn(n) { this.on("input", function(msg) {
RED.nodes.createNode(this,n); if (msg != null) {
var node = this; console.log(msg);
node.mpc = mpc; try {
var oldMsg = ""; //node.mpc.command(msg.payload);
node.mpc.command(msg.payload, msg.param, function(err, results) {
getSong(); if (err) { console.log("MPD: Error:",err); }
//else { console.log(results); }
function getSong() { });
node.mpc.currentsong(function(err, info) { } catch (err) { console.log("MPD: Error:",err); }
if (err) console.log(err);
else {
var msg = {payload:{},topic:"music"};
msg.payload.Artist = info.Artist;
msg.payload.Album = info.Album;
msg.payload.Title = info.Title;
msg.payload.Genre = info.Genre;
msg.payload.Date = info.Date;
if (JSON.stringify(msg) != oldMsg) {
node.send(msg);
oldMsg = JSON.stringify(msg);
}
} }
}); });
node.mpc.on('error', function(err) {
console.log("MPD: Error:",err);
});
} }
RED.nodes.registerType("mpd out",MPDOut);
function MPDIn(n) {
RED.nodes.createNode(this,n);
var node = this;
node.mpc = mpc;
var oldMsg = "";
node.mpc.on('changed', function(system) {
getSong(); getSong();
});
this.on("close", function() { function getSong() {
// node.mpc.command("stop"); node.mpc.currentsong(function(err, info) {
}); if (err) console.log(err);
} else {
RED.nodes.registerType("mpd in",MPDIn); var msg = {payload:{},topic:"music"};
msg.payload.Artist = info.Artist;
msg.payload.Album = info.Album;
msg.payload.Title = info.Title;
msg.payload.Genre = info.Genre;
msg.payload.Date = info.Date;
if (JSON.stringify(msg) != oldMsg) {
node.send(msg);
oldMsg = JSON.stringify(msg);
}
}
});
}
node.mpc.on('changed', function(system) {
getSong();
});
this.on("close", function() {
// node.mpc.command("stop");
});
}
RED.nodes.registerType("mpd in",MPDIn);
});