-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (57 loc) · 1.58 KB
/
app.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
'use strict';
var config = require('config');
var mpd = require('mpd');
var mqtt = require('mqtt');
var winston = require('winston');
var mqttClient = mqtt.connect(config.mqtt.url);
mqttClient.on('connect', function() {
winston.info('sucessfully connected to mqtt broker');
mqttClient.subscribe(config.mqtt.prefix+"command/+");
});
var mpdClient = mpd.connect(config.mpd);
mpdClient.on('ready', function() {
winston.info("mpd connection ready");
});
mqttClient.on('message', function(topic, message) {
var matches = topic.match(/\w+$/);
if (matches[0] === null) {
throw new Error("invalid command");
}
if (message.toString() !== "" && message.toString() !== "empty") {
var args = Array(message.toString());
winston.info("sending command "+matches[0]+" to mpd with arguments "+args);
mpdClient.sendCommand(mpd.cmd(matches[0], args), function(err, msg) {
if (err) {
winston.error(err);
}
});
} else {
winston.info("sending command "+matches[0]+" to mpd");
mpdClient.sendCommand(mpd.cmd(matches[0], []), function(err, msg) {
if (err) {
winston.error(err);
}
});
}
});
mpdClient.on('system', function() {
publishStatus();
});
function publishStatus() {
mpdClient.sendCommand(mpd.cmd("status", []), function(err, msg) {
if (err) {
throw err;
}
var re = /((\w+):\s+(.+)\S?)/g;
var m;
while ((m = re.exec(msg)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
var topic = config.mqtt.prefix + "status/"+m[2];
var value = m[3];
winston.info("publish to "+topic+ " value "+value);
mqttClient.publish(topic, value);
}
});
}