Merge pull request #165 from dwery/master

add mpd client accessory
This commit is contained in:
Nick Farina
2015-09-14 07:25:34 -07:00
3 changed files with 97 additions and 0 deletions

89
accessories/mpdclient.js Normal file
View File

@@ -0,0 +1,89 @@
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var request = require("request");
var komponist = require('komponist')
module.exports = {
accessory: MpdClient
}
function MpdClient(log, config) {
this.log = log;
this.host = config["host"] || 'localhost';
this.port = config["port"] || 6600;
}
MpdClient.prototype = {
setPowerState: function(powerOn, callback) {
var log = this.log;
komponist.createConnection(this.port, this.host, function(error, client) {
if (error) {
return callback(error);
}
if (powerOn) {
client.play(function(error) {
log("start playing");
client.destroy();
callback(error);
});
} else {
client.stop(function(error) {
log("stop playing");
client.destroy();
callback(error);
});
}
});
},
getPowerState: function(callback) {
komponist.createConnection(this.port, this.host, function(error, client) {
if (error) {
return callback(error);
}
client.status(function(error, status) {
client.destroy();
if (status['state'] == 'play') {
callback(error, 1);
} else {
callback(error, 0);
}
});
});
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "MPD")
.setCharacteristic(Characteristic.Model, "MPD Client")
.setCharacteristic(Characteristic.SerialNumber, "81536334");
var switchService = new Service.Switch();
switchService.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [informationService, switchService];
}
};

View File

@@ -189,6 +189,13 @@
"description": "Control the Hyperion TV backlight server. https://github.com/tvdzwan/hyperion",
"host": "localhost",
"port": "19444"
},
{
"accessory": "mpdclient",
"name" : "mpd",
"host" : "localhost",
"port" : 6600,
"description": "Allows some control of an MPD server"
}
]
}

View File

@@ -37,6 +37,7 @@
"wink-js": "0.0.5",
"xml2js": "0.4.x",
"xmldoc": "0.1.x",
"komponist" : "0.1.0",
"yamaha-nodejs": "0.4.x",
"debug": "^2.2.0"
}