forked from bertouttier/homebridge-mpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (96 loc) · 2.46 KB
/
index.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
var mpd = require('mpd');
var cmd = mpd.cmd;
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-mpd", "mpd", MpdAccessory);
}
//
// MPD Accessory
//
function MpdAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"] || 'MPD';
this.host = config["host"] || 'localhost';
this.port = config["port"] || 6600;
this.client = mpd.connect({
port: this.port,
host: this.host,
});
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.service
.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
}
MpdAccessory.prototype.getServices = function() {
return [this.service];
}
MpdAccessory.prototype.getOn = function(callback) {
var accessory = this;
this.client.sendCommand(cmd("status", []), function(err, msg) {
if (err) {
callback(err);
}
else {
var response = mpd.parseKeyValueMessage(msg);
var on = response.state == "play";
accessory.log("player state: %s", response.state);
callback(null, on);
}
});
}
MpdAccessory.prototype.setOn = function(on, callback) {
this.log("Setting power to " + on);
if (on) {
this.client.sendCommand(cmd("play", []), function(err, msg) {
if (err) {
callback(err);
}
else {
callback(null);
}
});
}
else {
this.client.sendCommand(cmd("stop", []), function(err, msg) {
if (err) {
callback(err);
}
else {
callback(null);
}
});
}
}
MpdAccessory.prototype.getVolume = function(callback) {
var accessory = this;
this.client.sendCommand(cmd("status", []), function(err, msg) {
if (err) {
callback(err);
}
else {
var response = mpd.parseKeyValueMessage(msg);
var volume = response.volume;
accessory.log("volume is %s", volume);
callback(null, Number(volume));
}
});
}
MpdAccessory.prototype.setVolume = function(volume, callback) {
this.log("Setting volume to %s", volume);
this.client.sendCommand(cmd("setvol", [volume]), function(err, msg) {
if (err) {
callback(err);
}
else {
callback(null);
}
});
}