-
Notifications
You must be signed in to change notification settings - Fork 7
/
squeezeplayer.js
79 lines (68 loc) · 2.06 KB
/
squeezeplayer.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
var util = require('util');
var EventEmitter = require('events').EventEmitter;
function startsWith(search, s) {
return s.substr(0,search.length) == search;
}
function SqueezePlayer(telnet) {
var self = this;
this.telnet = telnet;
this.volume = 0;
// periodically check this player's parameters
setInterval(function() {
self.runTelnetCmd("mixer volume ?");
self.runTelnetCmd("signalstrength ?");
self.runTelnetCmd("power ?");
}, 30 * 1000);
}
util.inherits(SqueezePlayer, EventEmitter);
SqueezePlayer.prototype.runTelnetCmd = function(cmdstring) {
this.telnet.writeln(this.id + " " + cmdstring);
}
SqueezePlayer.prototype.handleServerData = function(strEvent, raw_buffer) {
var self = this;
if (startsWith("mixer volume", strEvent)) {
var v = strEvent.match(/^mixer volume\s(.*?)$/)[1];
// incremental change
if (startsWith("+", v) || startsWith("-", v)) {
self.volume += parseInt(v);
}
// explicit value
else {
self.volume = parseInt(v);
}
this.emit("volume", v);
} else {
this.emit("logitech_event", strEvent);
}
}
SqueezePlayer.prototype.switchOff = function() {
this.runTelnetCmd("power 0");
}
SqueezePlayer.prototype.switchOn = function() {
this.runTelnetCmd("power 1");
}
SqueezePlayer.prototype.setProperty = function(property, state) {
this[property] = state;
this.emit(property, state);
}
SqueezePlayer.prototype.getNoiseLevel = function() {
var nl = this.volume;
if (this.mode == "stop" || this.mode == "pause" || this.mode == "off" || this.power == 0) {
nl = 0;
}
return nl;
}
SqueezePlayer.prototype.inspect = function() {
// Convenience method for debugging/logging.
// Return self but without certain lengthy sub-objects.
var self = this;
var x = {};
Object.keys(self).forEach(function(k) {
if (["telnet", "_events"].indexOf(k) == -1) {
x[k] = self[k];
}
});
x.noise_level = self.getNoiseLevel();
return x;
}
module.exports = SqueezePlayer;