homebridge-musiccast-bnetqc
Version:
Homebridge Yamaha MusicCast Multiroom Plugin with precise volume control
155 lines • 6.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YamahaAPI = void 0;
const http_1 = require("http");
class YamahaAPI {
constructor(log, groupId, presetInfoRegex) {
this.zone = "main";
this.log = log;
this.groupId = groupId;
this.presetInfoRegex = presetInfoRegex;
}
async httpRequest(url, postData) {
this.log.debug(url);
try {
return new Promise((resolve, reject) => {
const options = {};
if (postData) {
options.method = "POST";
options.headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
};
}
else {
options.method = "GET";
options.headers = {
'Accept': 'application/json',
};
}
const req = (0, http_1.request)(url, options);
req.on('error', (error) => {
this.log.error("httpRequest error", error);
reject(error);
});
if (postData) {
req.write(postData);
}
req.end();
req.on('response', (response) => {
response.setEncoding('utf8');
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
try {
const result = JSON.parse(data);
this.log.debug("httpRequest result", result);
resolve(result);
}
catch (e) {
this.log.error("Failed to parse JSON response:", data);
reject(e);
}
});
});
});
}
catch (error) {
this.log.error("httpRequest error", error);
}
}
async getDeviceInfo(host) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/system/getDeviceInfo';
return this.httpRequest(url).then(result => result);
}
async getFeatures(host) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/system/getFeatures';
return this.httpRequest(url).then(result => result);
}
async getPlayInfo(host) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/netusb/getPlayInfo';
return this.httpRequest(url).then(result => result);
}
async getPresetInfo(host) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/netusb/getPresetInfo';
let presetInfos = await this.httpRequest(url);
for (let i = 0; i < presetInfos.preset_info.length; i++) {
presetInfos.preset_info[i].presetId = i + 1;
presetInfos.preset_info[i].identifier = i + 200;
presetInfos.preset_info[i].displayText = presetInfos.preset_info[i].text;
if (this.presetInfoRegex !== undefined) {
presetInfos.preset_info[i].displayText = presetInfos.preset_info[i].displayText.replace(this.presetInfoRegex, '').trim();
}
}
presetInfos.preset_info = presetInfos.preset_info.filter(function (presetInfo) {
return ((presetInfo.input === 'server' || presetInfo.input === 'net_radio') && presetInfo.text !== "");
});
return presetInfos;
}
async getStatus(host) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/' + this.zone + '/getStatus';
return this.httpRequest(url).then(result => result);
}
async setInput(host, input) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/' + this.zone + '/setInput?input=' + input;
return this.httpRequest(url).then(result => result);
}
async setLinkAudioDelay(host, delay) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/' + this.zone + '/setLinkAudioDelay?delay=' + delay;
return this.httpRequest(url).then(result => result);
}
async setSoundProgram(host, program) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/' + this.zone + '/setSoundProgram?program=' + program;
return this.httpRequest(url).then(result => result);
}
async setPlayback(host, playback) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/netusb/setPlayback?playback=' + playback;
return this.httpRequest(url).then(result => result);
}
async setPower(host, power) {
let parameter = (power === true) ? "on" : "standby";
const url = 'http://' + host + '/YamahaExtendedControl/v1/' + this.zone + '/setPower?power=' + parameter;
return this.httpRequest(url).then(result => result);
}
async setVolume(host, volume) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/' + this.zone + '/setVolume?volume=' + volume;
return this.httpRequest(url).then(result => result);
}
// AJOUTÉ : Méthode pour contrôler le Mute
async setMute(host, mute) {
const url = `http://${host}/YamahaExtendedControl/v1/${this.zone}/setMute?enable=${mute}`;
return this.httpRequest(url).then(result => result);
}
async recallPreset(host, preset) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/netusb/recallPreset?zone=' + this.zone + '&num=' + preset.toString();
return this.httpRequest(url).then(result => result);
}
async startDistribution(host) {
const url = 'http://' + host + '/YamahaExtendedControl/v1/dist/startDistribution?num=0';
return this.httpRequest(url).then(result => result);
}
async setClientInfo(clientHost, serverHost) {
const url = 'http://' + clientHost + '/YamahaExtendedControl/v1/dist/setClientInfo';
const clientInfo = {
group_id: this.groupId,
zone: [this.zone],
server_ip_address: serverHost
};
return this.httpRequest(url, JSON.stringify(clientInfo)).then(result => result);
}
async setServerInfo(clientHost, serverHost, type) {
const url = 'http://' + serverHost + '/YamahaExtendedControl/v1/dist/setServerInfo';
const serverInfo = {
group_id: this.groupId,
zone: this.zone,
type: type,
client_list: [clientHost]
};
return this.httpRequest(url, JSON.stringify(serverInfo)).then(result => result);
}
}
exports.YamahaAPI = YamahaAPI;
//# sourceMappingURL=YamahaAPI.js.map