homebridge-soundtouch-plugins
Version:
Homebridge SoundTouch plugins
203 lines • 11.3 kB
JavaScript
;
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 (_) 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.deviceIsOn = exports._deviceFromApi = exports.deviceFromConfig = exports.searchAllDevices = void 0;
var soundtouch_api_1 = require("@bossoq/soundtouch-api");
var errors_1 = require("./errors");
var utils_1 = require("./utils");
var types_1 = require("./types");
var searchAllDevices = function (globalConfig, accessoryConfigs, log) { return __awaiter(void 0, void 0, void 0, function () {
var apis;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, soundtouch_api_1.APIDiscovery.search()];
case 1:
apis = _a.sent();
return [2 /*return*/, Promise.all(apis.map(function (api) { return __awaiter(void 0, void 0, void 0, function () {
var info, accessoryConfig;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.getInfo()];
case 1:
info = _a.sent();
accessoryConfig = accessoryConfigs.find(function (ac) { return ac.room === info.name || ac.ip === api['host']; });
return [2 /*return*/, (0, exports._deviceFromApi)(api, info, globalConfig, accessoryConfig || {}, log)];
}
});
}); }))];
}
});
}); };
exports.searchAllDevices = searchAllDevices;
var deviceFromConfig = function (globalConfig, accessoryConfig, log) { return __awaiter(void 0, void 0, void 0, function () {
var api, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!accessoryConfig.ip) return [3 /*break*/, 1];
api = new soundtouch_api_1.API(accessoryConfig.ip, accessoryConfig.port);
return [3 /*break*/, 3];
case 1:
if (!accessoryConfig.room) return [3 /*break*/, 3];
return [4 /*yield*/, soundtouch_api_1.APIDiscovery.find(accessoryConfig.room)];
case 2:
api = _c.sent();
if (!api)
throw (0, errors_1.apiNotFoundWithName)(accessoryConfig.name);
_c.label = 3;
case 3:
_a = exports._deviceFromApi;
_b = [api];
return [4 /*yield*/, api.getInfo()];
case 4: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent(), globalConfig,
accessoryConfig,
log]))];
}
});
}); };
exports.deviceFromConfig = deviceFromConfig;
var _deviceFromApi = function (api, info, globalConfig, accessoryConfig, log) { return __awaiter(void 0, void 0, void 0, function () {
var displayName, isVerbose, pollingInterval, component, presets, sources, globalVolume, accessoryVolume, onValue;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
displayName = accessoryConfig.name || info.name;
isVerbose = (0, utils_1.isVerboseInConfigs)(globalConfig, accessoryConfig);
pollingInterval = accessoryConfig.pollingInterval || globalConfig.pollingInterval;
if (isVerbose)
log("[" + displayName + "] found device");
component = info.components.find(function (c) { return c.serialNumber.toLowerCase() === info.deviceId.toLowerCase(); });
return [4 /*yield*/, _availablePresets(api, displayName, accessoryConfig.presets, globalConfig.presets, isVerbose ? log : undefined)];
case 1:
presets = _a.sent();
return [4 /*yield*/, _availableSources(api, displayName, accessoryConfig.sources, globalConfig.sources, isVerbose ? log : undefined)];
case 2:
sources = _a.sent();
globalVolume = globalConfig.volume || {};
accessoryVolume = accessoryConfig.volume || {};
onValue = globalVolume.onValue || accessoryVolume.onValue;
return [2 /*return*/, {
api: api,
name: displayName,
id: info.deviceId,
model: info.type,
version: component ? component.softwareVersion : undefined,
verbose: isVerbose,
pollingInterval: pollingInterval,
volumeSettings: {
onValue: onValue || -1,
maxValue: globalVolume.maxValue || accessoryVolume.maxValue || 100,
unmuteValue: globalVolume.unmuteValue ||
accessoryVolume.unmuteValue ||
onValue ||
35,
mode: globalVolume.mode || accessoryVolume.mode || types_1.VolumeMode.lightbulb,
},
presets: presets,
sources: sources,
}];
}
});
}); };
exports._deviceFromApi = _deviceFromApi;
var deviceIsOn = function (device) { return __awaiter(void 0, void 0, void 0, function () {
var nowPlaying;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, device.api.getNowPlaying()];
case 1:
nowPlaying = _a.sent();
return [2 /*return*/, nowPlaying.source !== soundtouch_api_1.SourceStatus.standBy];
}
});
}); };
exports.deviceIsOn = deviceIsOn;
var _availablePresets = function (api, deviceName, accessoryPresets, globalPresets, log) { return __awaiter(void 0, void 0, void 0, function () {
var presets;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.getPresets()];
case 1:
presets = (_a.sent()) || [];
return [2 /*return*/, (0, soundtouch_api_1.compactMap)(presets, function (preset) {
var presetConfig = _findConfig(function (p) { return p.index === preset.id; }, accessoryPresets, globalPresets) || { index: preset.id };
if (log !== undefined)
log("[" + deviceName + "] found preset n\u00B0" + preset.id + " '" + preset.contentItem.itemName + "' on device");
if (presetConfig.enabled === false)
return undefined;
return {
name: presetConfig.name || preset.contentItem.itemName,
index: preset.id,
};
})];
}
});
}); };
var _availableSources = function (api, deviceName, accessorySources, globalSources, log) { return __awaiter(void 0, void 0, void 0, function () {
var sources, localSources;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.getSources()];
case 1:
sources = _a.sent();
localSources = sources.items.filter(function (s) { return s.isLocal; });
return [2 /*return*/, localSources.map(function (source) {
if (log !== undefined)
log("[" + deviceName + "] found local source '" + source.source + "' with account '" + source.sourceAccount + "' on device");
var sourceConfig = _findConfig(function (p) {
return p.source === source.source &&
(p.account !== undefined ? p.account === source.sourceAccount : true);
}, accessorySources, globalSources) || { source: source.source };
return {
name: sourceConfig.name ||
deviceName + " " + (source.name ? source.name : (0, utils_1.stringUpperCaseFirst)(sourceConfig.source)),
source: sourceConfig.source,
account: sourceConfig.account,
enabled: sourceConfig.enabled,
};
})];
}
});
}); };
var _findConfig = function (predicate, accessoryConfigs, globalConfigs) {
var config = accessoryConfigs ? accessoryConfigs.find(predicate) : undefined;
if (config !== undefined)
return config;
return globalConfigs ? globalConfigs.find(predicate) : undefined;
};
//# sourceMappingURL=sound-touch-device.js.map