Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·105 lines (89 loc) · 3.76 KB
/
Copy pathserver.js
File metadata and controls
executable file
·105 lines (89 loc) · 3.76 KB
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
const soap = require("soap");
const { updateAudioBookshelfProgress } = require("./utils");
const express = require("express");
const config = require("./config");
/* CONFIG */
const EXPRESS_APP = express();
const SONOS_SOAP_SERVICE = require("./sonos-service");
console.log("🚀 ~ file: server.js:9 ~ SONOS_SOAP_SERVICE:", SONOS_SOAP_SERVICE);
const HTTP_PORT = config.HTTP_PORT;
console.log("🚀 ~ file: server.js:10 ~ HTTP_PORT:", HTTP_PORT);
const SOAP_URI = config.SOAP_URI;
console.log("🚀 ~ file: server.js:12 ~ SOAP_URI:", SOAP_URI);
const SOAP_ENDPOINT = config.SOAP_ENDPOINT;
console.log("🚀 ~ file: server.js:14 ~ SOAP_ENDPOINT:", SOAP_ENDPOINT);
const SONOS_WSDL_FILE = config.SONOS_WSDL_FILE;
/**********/
EXPRESS_APP.use(express.json()); // express.json allows for native body parsing
EXPRESS_APP.listen(HTTP_PORT, function () {
/*
SOAP server
The Sonos Music API (SMAPI) uses SOAP rather than REST. All requests go to the /wsdl endpoint, where SOAP does SOAP magic
and handles the different routes via the service (sonosService) defined in sonos-service.js
SMAPI documentation: https://developer.sonos.com/reference/sonos-music-api/
*/
var soaper = soap.listen(
EXPRESS_APP,
SOAP_ENDPOINT,
SONOS_SOAP_SERVICE,
SONOS_WSDL_FILE,
function () {
console.log("[soapServer] server initialized");
}
);
soaper.log = function (type, data) {
// uncomment to log SOAP requests coming in
// console.log(data);
};
/*
Sonos Cloud Queue Routes
"Cloud Queue" is a misnomer here. These routes are only used for handling "reporting" (audiobook progress updates). These endpoints exist outside
of the SMAPI / SOAP implementation above.
POST /timePlayed documentation: https://developer.sonos.com/reference/cloud-queue-api/post-timeplayed/
Reportig documentation: https://developer.sonos.com/build/content-service-add-features/add-reporting/
*/
EXPRESS_APP.get("/manifest", (req, res) => {
console.log("[soapServer] /manifest called");
res.send({
schemaVersion: "1.0",
endpoints: [
{
type: "reporting",
uri: `${SOAP_URI}/playback/v2.1/report`,
},
],
});
});
// TODO: Remove this probably? I think I saw it called once.. but it's never returned something, so it's probably safe to remove.
EXPRESS_APP.post("/playback/v2.1/report", (req, res) => {
console.log("[soapServer] /playback/v2.1/report called");
});
EXPRESS_APP.post("/playback/v2.1/report/timePlayed", (req, res) => {
console.log("[soapServer] /playback/v2.1/report/timePlayed called");
let sonosProgressUpdate = req.body.items[0];
let progressUpdateForABS = {
libraryItemId: sonosProgressUpdate.containerId,
libraryItemIdAndFileName: sonosProgressUpdate.objectId,
positionMillis: sonosProgressUpdate.positionMillis,
};
updateAudioBookshelfProgress(progressUpdateForABS);
/*
1. Only send back a 200 if we are returning data
2. By default (I think per session?), sonos will only send up progress reports every 60 seconds. This response to each subsequent
progress report will ensure it reports every 10 seconds.
3. According to their documentation, this entire object needs to be returned, but everything except reports is ignored, so we send
empty for everything besides reports, which is all we care about anyway.
4. 10 seconds is the lowest update interval it supports.
Documentation: https://developer.sonos.com/reference/cloud-queue-api/post-timeplayed/
*/
res.status(200).send({
contextVersion: "",
queueVersion: "",
container: {},
playbackPolicies: {},
reports: {
periodicIntervalMillis: 10000, // 10 seconds is the lowest it supports
},
});
});
});