homebridge-pioneer-avr-2025
Version:
A Pioneer AVR plugin for homebridge. This plugin is designed for Pioneer models that use Telnet Commands (e.g., VSX-922). It may not be compatible with newer models (e.g., VSX-LX304).
199 lines • 10.2 kB
JavaScript
"use strict";
// src/pioneer-avr/initialize.ts
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InitializeMixin = InitializeMixin;
const onDataHandler_1 = require("./onDataHandler");
const telnetAvr_1 = require("../telnet-avr/telnetAvr");
const exitHandler_1 = require("../exitHandler");
/**
* This mixin adds initialization and connection handling capabilities to a base class.
* It includes methods to handle connection status, set up data handling, and regular state polling.
* @param Base - The base class to extend.
* @returns A new class that extends the base class with added initialization logic.
*/
function InitializeMixin(Base) {
return class extends Base {
constructor(...args) {
super(...args);
this.allIntervalCounter = 0;
// this.setupTelnetConnection();
(0, exitHandler_1.addExitHandler)(() => {
if (this.allInterval) {
clearInterval(this.allInterval);
}
}, this);
}
/**
* Sets up the Telnet connection, handling connect/disconnect events.
*/
setupTelnetConnection() {
if (!this.host || !this.port) {
this.log.error('Host or port information is missing, cannot initialize TelnetAvr.');
return;
}
this.telnetAvr = new telnetAvr_1.TelnetAvr(this);
this.onData = (0, onDataHandler_1.onDataHandler)(this);
this.isReady = false;
try {
this.telnetAvr.onData = this.onData;
this.telnetAvr.connect(() => __awaiter(this, void 0, void 0, function* () { return this.setupConnectionCallbacks(); }));
}
catch (e) {
this.log.debug('Pioneer AVR connection error', e);
}
this.log.debug('Waiting until Telnet is connected to', this.host, this.port);
}
/**
* Sets up connection and disconnection callbacks for the Telnet connection.
*/
setupConnectionCallbacks() {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!this.telnetAvr) {
this.log.error(this.device.name + '> ' + 'TelnetAvr instance is not initialized.');
return;
}
if (!this.telnetAvr.connectionReady) {
this.log.info(this.device.name + '> ' + 'Telnet is not ready :(');
return;
}
this.log.info(this.device.name + '> ' + 'Telnet connected, starting up');
this.telnetAvr.addOnDisconnectCallback(() => {
// this.log.info('Telnet Disconnected!');
});
this.telnetAvr.addOnConnectCallback(() => __awaiter(this, void 0, void 0, function* () {
this.log.debug(this.device.name + '> ' + 'Telnet connected, waited for PWR...');
}));
this.telnetAvr.displayChanged = (text) => {
try {
if (text) {
this.log.debug('[' + this.device.name + ' DISPLAY] ' + text);
}
}
catch (error) {
this.log.debug('init displayChanged()', error);
}
};
if (this.allInterval) {
clearInterval(this.allInterval);
}
this.allInterval = setInterval(() => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h;
try {
// Ensure sendKeepAliveTimeout is a number within the range [5 minutes, 2 weeks]
// If not set, default to 48 hours
let keepAliveTimeoutMinutes;
let rawTimeout = (_b = (_a = this.platform) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.sendKeepAliveTimeoutMinutes;
// If rawTimeout is a string, convert it to a number
if (typeof rawTimeout === "string") {
rawTimeout = parseInt(rawTimeout, 10);
}
// Process the timeout value, ensuring it falls within the allowed range (5 to 20160 minutes)
if (typeof rawTimeout === "number" && !isNaN(rawTimeout)) {
if (rawTimeout < 5) {
keepAliveTimeoutMinutes = 5;
}
else if (rawTimeout > 20160) {
keepAliveTimeoutMinutes = 20160;
}
else {
keepAliveTimeoutMinutes = rawTimeout;
}
}
else {
keepAliveTimeoutMinutes = 2880; // Default to 48 hours (2880 minutes) if not set
}
// Convert minutes to milliseconds
const keepAliveTimeoutMs = keepAliveTimeoutMinutes * 60 * 1000;
if (!this.state.on &&
this.lastUserInteraction &&
((Date.now() - this.lastUserInteraction) > keepAliveTimeoutMs)) {
this.allIntervalCounter = 0;
if (this.isReady &&
this.telnetAvr.connectionReady &&
((Date.now() - this.lastUserInteraction) > (keepAliveTimeoutMs + (59 * 1000)))) {
if ((_d = (_c = this.telnetAvr) === null || _c === void 0 ? void 0 : _c.connection) === null || _d === void 0 ? void 0 : _d.forcedDisconnect) {
this.telnetAvr.connection.forcedDisconnect = true;
}
(_f = (_e = this.telnetAvr.connection) === null || _e === void 0 ? void 0 : _e.disconnect) === null || _f === void 0 ? void 0 : _f.call(_e);
}
return;
}
if (this.isReady &&
this.telnetAvr.connectionReady &&
this.telnetAvr.connection.lastMessageReceived !== null &&
Date.now() - this.telnetAvr.connection.lastMessageReceived > 22000 &&
(this.state.on || this.telnetAvr.connection.forcedDisconnect !== true)) {
if (this.allIntervalCounter % 2 === 0) {
(_g = this.__updatePower) === null || _g === void 0 ? void 0 : _g.call(this, () => { });
}
else {
(_h = this.__updateVolume) === null || _h === void 0 ? void 0 : _h.call(this, () => { });
}
this.allIntervalCounter++;
}
}
catch (e) {
this.log.debug('Polling error', e);
}
}), 2031);
}
catch (error) {
console.log('setupConnectionCallbacks() error' + String(error));
}
});
}
/**
* Sends a remote key command based on the provided key.
* @param rk - The remote key to be processed.
*/
remoteKey(rk) {
this.lastUserInteraction = Date.now();
if (!this.telnetAvr ||
!this.telnetAvr.connectionReady ||
!this.state.on) {
return;
}
// Implemented key from CURSOR OPERATION
switch (rk) {
case 'UP':
this.telnetAvr.sendMessage('CUP');
break;
case 'DOWN':
this.telnetAvr.sendMessage('CDN');
break;
case 'LEFT':
this.telnetAvr.sendMessage('CLE');
break;
case 'RIGHT':
this.telnetAvr.sendMessage('CRI');
break;
case 'ENTER':
this.telnetAvr.sendMessage('CEN');
break;
case 'RETURN':
this.telnetAvr.sendMessage('CRT');
break;
case 'HOME_MENU':
this.telnetAvr.sendMessage('HM');
break;
case 'TOGGLE_PLAY_PAUSE':
this.toggleListeningMode();
// this.telnetAvr.sendMessage('CTP'); ?
break;
default:
this.log.info('Unhandled remote key: %s', rk);
}
}
};
}
//# sourceMappingURL=initialize.js.map