-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroon.js
151 lines (141 loc) · 7.26 KB
/
roon.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var RoonApi = require("node-roon-api"),
RoonApiStatus = require("node-roon-api-status"),
RoonApiImage = require("node-roon-api-image"),
RoonApiBrowse = require("node-roon-api-browse"),
RoonApiTransport = require("node-roon-api-transport");
module.exports = function(RED) {
function roonCore(config) {
RED.nodes.createNode(this,config);
var node = this;
var theCore;
var transport;
var image;
var browse;
var roon = new RoonApi({
extension_id: 'com.jac459.nodeRedRoon',
display_name: "Roon Core",
display_version: "0.9.4",
publisher: 'jac459',
email: '[email protected]',
website: 'https://github.com/jac459/roon-node-red',
core_paired: function(core) {
let msg = {};
theCore = {'coreId':core.core_id, 'coreName':core.display_name, 'coreVersion':core.display_version};
transport = core.services.RoonApiTransport;
image = core.services.RoonApiImage;
browse = core.services.RoonApiBrowse;
let queueSubscribed = false;
let queues = [];
transport.subscribe_zones(function() {
transport.get_zones(function(cmd, data) {
theCore.zones = data.zones;
theCore.cmd = cmd;
msg.payload = theCore;
node.send([msg]);
if (!queueSubscribed) {
theCore.zones.forEach( (zone) => {
transport.subscribe_queue(zone, 256, (response, message) => {
let i = queues.findIndex((queue) => { return queue.zoneId == zone.zone_id });
if (i < 0) {
queues.push({'zoneId': zone.zone_id, "queue":message});
}
else {
queues[i] = {'zoneId': zone.zone_id, "queue":message};
}
})
});
queueSubscribed = true;
}
})
});
node.on('input', function(msg) {
if (msg.payload.state) {
transport.get_zones(function(cmd, data) {
theCore.zones = data.zones;
theCore.cmd = cmd;
msg.payload = theCore;
node.send([msg]);
})
}
else if (msg.payload.transport) {
if (msg.payload.transport.control) {
transport.control(msg.payload.transport.zone, msg.payload.transport.control, (res) => {
msg.payload = {"Transport":msg.payload.transport.control};
node.send([null, msg]);
})
};
if (msg.payload.transport.volume) {
transport.change_volume(msg.payload.transport.output, 'absolute', msg.payload.transport.volume, (res) => {
msg.payload = {"Volume":{"Output":msg.payload.transport.output, "Level":msg.payload.transport.volume}};
node.send([null, msg])
})
};
if (msg.payload.transport.seek) {
transport.seek(msg.payload.transport.zone, 'absolute', msg.payload.transport.seek, (res) => {
msg.payload = {"Seek":msg.payload.transport.seek};
node.send([null, msg])
})
};
if (msg.payload.transport.playQueue) {
transport.play_from_here(msg.payload.transport.zone, msg.payload.transport.queueId, (res) => {
msg.payload = {"PlayQueue":{"Zone":msg.payload.transport.zone, "QueueId":msg.payload.transport.queueId}};
node.send([null, msg])
})
}
}
else if (msg.payload.image_key) {
image.get_image(msg.payload.image_key, {"scale": "fill", "width":200, "height":200, "format": "image/jpeg"}, (err, content_type, content)=>{
if (!err) {
msg.payload = content;
node.send([null, null, msg])
}
else {
msg.payload = {"Error":err};
node.send([null, msg])
}
});
}
else if (msg.payload.browse) {
let browseResult = JSON.parse(msg.payload.browse);
browse.browse(browseResult,(res)=>{
msg.payload = res;
node.send([null, msg])
browse.load(browseResult, (err, content)=>{
if (!err) {
msg.payload = content;
node.send([null, null, null, msg])
}
else {
msg.payload = {"Error":err};
node.send([null, msg])
}
});
})
}
else if (msg.payload.queue) {
let queueReq = JSON.parse(msg.payload.queue);
//msg.payload = queues;
msg.payload = queues.find((queue) => { return queueReq.zoneId == queue.zoneId});
node.send([null, null, null, null, msg])
}
else {
msg.payload = {"Error":"Invalid Input"};
node.send([null, msg])
}
});
},
core_unpaired: function() {
theCore = undefined;
// node.send({'coreId':core.core_id, 'coreName':core.display_name, 'coreVersion':core.display_name});
}
});
var svc_status = new RoonApiStatus(roon);
roon.init_services({
required_services: [ RoonApiTransport, RoonApiImage, RoonApiBrowse ],
provided_services: [ svc_status ]
});
svc_status.set_status("node-red-roon extension is connected.", false);
roon.start_discovery()
}
RED.nodes.registerType("roon-core",roonCore);
}