soundtouch-api
Version:
SoundTouch API using TypeScript
468 lines • 20.8 kB
JavaScript
'use strict';
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.API = void 0;
var info_1 = require("./info");
var error_1 = require("./error");
var endpoints_1 = require("./endpoints");
var xml2js_1 = require("xml2js");
var utils_1 = require("./utils");
var special_types_1 = require("./special-types");
var now_playing_1 = require("./now-playing");
var volume_1 = require("./volume");
var source_1 = require("./source");
var zone_1 = require("./zone");
var bass_capabilities_1 = require("./bass-capabilities");
var bass_1 = require("./bass");
var preset_1 = require("./preset");
var group_1 = require("./group");
var util_1 = require("util");
var content_item_1 = require("./content-item");
var axios_1 = require("axios");
var parseXML = (0, util_1.promisify)(function (xml, options, cb) { return (0, xml2js_1.parseString)(xml, options, cb); });
var API = /** @class */ (function () {
function API(device) {
this.device = device;
this.axiosInstance = axios_1.default.create({
headers: {
'Content-Type': 'application/xml'
}
});
this.builder = new xml2js_1.Builder();
}
API.prototype.getInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var element, info;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.info)];
case 1:
element = _a.sent();
if (element.hasChild('info')) {
info = element.getChild('info');
if (info) {
return [2 /*return*/, (0, info_1.infoFromElement)(info)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.setVolume = function (value) {
return __awaiter(this, void 0, void 0, function () {
var element;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoints_1.Endpoints.volume, {
volume: value
})];
case 1:
element = _a.sent();
return [2 /*return*/, element ? element.getText('status') !== undefined : false];
}
});
});
};
API.prototype.getVolume = function () {
return __awaiter(this, void 0, void 0, function () {
var element, volume;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.volume)];
case 1:
element = _a.sent();
if (element.hasChild('volume')) {
volume = element.getChild('volume');
if (volume) {
return [2 /*return*/, (0, volume_1.volumeFromElement)(volume)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.speaker = function (playInfo) {
return __awaiter(this, void 0, void 0, function () {
var element;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoints_1.Endpoints.speaker, {
play_info: playInfo
})];
case 1:
element = _a.sent();
return [2 /*return*/, element ? element.getText('status') !== undefined : false];
}
});
});
};
API.prototype.pressKey = function (value) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.holdKey(value, 0)];
});
});
};
API.prototype.holdKey = function (value, duration) {
if (duration === void 0) { duration = 5000; }
return __awaiter(this, void 0, void 0, function () {
var success;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._key(special_types_1.KeyState.press, value)];
case 1:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
if (!(duration > 0)) return [3 /*break*/, 3];
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, duration); })];
case 2:
_a.sent();
_a.label = 3;
case 3: return [2 /*return*/, this._key(special_types_1.KeyState.release, value)];
}
});
});
};
API.prototype.getNowPlaying = function () {
return __awaiter(this, void 0, void 0, function () {
var element, nowPlaying;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.nowPlaying)];
case 1:
element = _a.sent();
if (element.hasChild('nowPlaying')) {
nowPlaying = element.getChild('nowPlaying');
if (nowPlaying) {
return [2 /*return*/, (0, now_playing_1.nowPlayingFromElement)(nowPlaying)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.selectSource = function (contentItem) {
return __awaiter(this, void 0, void 0, function () {
var element;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoints_1.Endpoints.select, (0, content_item_1.contentItemToElement)(contentItem).data)];
case 1:
element = _a.sent();
return [2 /*return*/, element ? element.getText('status') !== undefined : false];
}
});
});
};
API.prototype.getSources = function () {
return __awaiter(this, void 0, void 0, function () {
var element, sources;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.sources)];
case 1:
element = _a.sent();
if (element.hasChild('sources')) {
sources = element.getChild('sources');
if (sources) {
return [2 /*return*/, (0, source_1.sourcesFromElement)(sources)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.getZone = function () {
return __awaiter(this, void 0, void 0, function () {
var element, zone;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.getZone)];
case 1:
element = _a.sent();
if (element.hasChild('zone')) {
zone = element.getChild('zone');
if (zone) {
return [2 /*return*/, (0, zone_1.zoneFromElement)(zone)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.setZone = function (zone) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._updateZone(zone, endpoints_1.Endpoints.setZone)];
});
});
};
API.prototype.addZoneSlave = function (zone) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._updateZone(zone, endpoints_1.Endpoints.addZoneSlave)];
});
});
};
API.prototype.removeZoneSlave = function (zone) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._updateZone(zone, endpoints_1.Endpoints.removeZoneSlave)];
});
});
};
API.prototype.getBassCapabilities = function () {
return __awaiter(this, void 0, void 0, function () {
var element, bassCapabilities;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.bassCapabilities)];
case 1:
element = _a.sent();
if (element.hasChild('bassCapabilities')) {
bassCapabilities = element.getChild('bassCapabilities');
if (bassCapabilities) {
return [2 /*return*/, (0, bass_capabilities_1.bassCapabilitiesFromElement)(bassCapabilities)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.getBass = function () {
return __awaiter(this, void 0, void 0, function () {
var element, bass;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.bass)];
case 1:
element = _a.sent();
if (element.hasChild('bass')) {
bass = element.getChild('bass');
if (bass) {
return [2 /*return*/, (0, bass_1.bassFromElement)(bass)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.setBass = function (value) {
return __awaiter(this, void 0, void 0, function () {
var element;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoints_1.Endpoints.bass, {
bass: value
})];
case 1:
element = _a.sent();
return [2 /*return*/, element ? element.getText('status') !== undefined : false];
}
});
});
};
API.prototype.getPresets = function () {
return __awaiter(this, void 0, void 0, function () {
var element, presets;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.presets)];
case 1:
element = _a.sent();
if (element.hasChild('presets')) {
presets = element.getChild('presets');
if (presets) {
if (presets.hasChild('preset')) {
return [2 /*return*/, (0, utils_1.compactMap)(presets.getList('preset'), preset_1.presetFromElement)];
}
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.setName = function (value) {
return __awaiter(this, void 0, void 0, function () {
var element, info;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoints_1.Endpoints.name, {
name: value
})];
case 1:
element = _a.sent();
if (element.hasChild('info')) {
info = element.getChild('info');
if (info) {
return [2 /*return*/, (0, info_1.infoFromElement)(info)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
API.prototype.getGroup = function () {
return __awaiter(this, void 0, void 0, function () {
var element, group;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._get(endpoints_1.Endpoints.getGroup)];
case 1:
element = _a.sent();
if (element.hasChild('group')) {
group = element.getChild('group');
if (group) {
return [2 /*return*/, (0, group_1.groupFromElement)(group)];
}
}
return [2 /*return*/, undefined];
}
});
});
};
// PRIVATE FUNCTIONS
API._throwAPIErrors = function (root) {
if (root.hasChild('errors')) {
var errors = root.getChild('errors');
if (errors) {
throw error_1.APIErrors.fromElement(errors);
}
}
else if (root.hasChild('Error')) {
var errElement = root.getChild('Error');
if (errElement) {
var err = (0, error_1.errorFromElement)(errElement);
if (err) {
throw new error_1.APIErrors([err]);
}
}
}
};
API.prototype._req = function (method, endpoint, body) {
return __awaiter(this, void 0, void 0, function () {
var url, xml, err_1, data, root;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = "http://".concat(this.device.ip, ":").concat(this.device.port, "/").concat(endpoint);
_a.label = 1;
case 1:
_a.trys.push([1, 6, , 7]);
if (!(method === 'GET')) return [3 /*break*/, 3];
return [4 /*yield*/, this.axiosInstance.get(url)];
case 2:
xml = (_a.sent()).data;
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, this.axiosInstance.post(url, this.builder.buildObject(body))];
case 4:
xml = (_a.sent()).data;
_a.label = 5;
case 5: return [3 /*break*/, 7];
case 6:
err_1 = _a.sent();
if (!err_1.response) {
throw err_1;
}
xml = err_1.response.data;
return [3 /*break*/, 7];
case 7: return [4 /*yield*/, parseXML(xml, { trim: true })];
case 8:
data = _a.sent();
root = new utils_1.XMLElement(data);
API._throwAPIErrors(root);
return [2 /*return*/, root];
}
});
});
};
API.prototype._get = function (endpoint) {
return this._req('GET', endpoint);
};
API.prototype._post = function (endpoint, body) {
return this._req('POST', endpoint, body);
};
API.prototype._key = function (state, value) {
return __awaiter(this, void 0, void 0, function () {
var element;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoints_1.Endpoints.key, {
key: {
$: {
state: state,
sender: 'Gabbo'
},
_: value
}
})];
case 1:
element = _a.sent();
return [2 /*return*/, element ? element.getText('status') !== undefined : false];
}
});
});
};
API.prototype._updateZone = function (zone, endpoint) {
return __awaiter(this, void 0, void 0, function () {
var element;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._post(endpoint, (0, zone_1.zoneToElement)(zone).data)];
case 1:
element = _a.sent();
return [2 /*return*/, element ? element.getText('status') !== undefined : false];
}
});
});
};
return API;
}());
exports.API = API;
//# sourceMappingURL=api.js.map