homebridge-lirc-tv
Version:
Control IR Televisions using LIRC
49 lines • 1.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LIRCController = void 0;
const net_1 = __importDefault(require("net"));
const DELAY_INDENTIFIER = 'DELAY|';
class LIRCController {
constructor(host, port, remote, delay, log) {
this.host = host;
this.port = port;
this.remote = remote;
this.delay = delay;
this.log = log;
this.sendCommands = (keys) => {
return keys.reduce((collector, key) => {
return collector.then(() => new Promise((resolve, reject) => {
this.sendCommand(key, resolve, reject);
}));
}, Promise.resolve());
};
this.sendCommand = (key, resolve, reject) => {
if (key.startsWith(DELAY_INDENTIFIER)) {
// This is just a delay key, no need to send to LIRC
const delayTimeout = parseInt(key.replace(DELAY_INDENTIFIER, ''));
this.log.info(`Delaying for ${delayTimeout}ms`);
setTimeout(resolve, delayTimeout);
}
else {
const client = net_1.default.connect({
host: this.host,
port: this.port
}, () => {
const requestBody = `SEND_ONCE ${this.remote} ${key}`;
this.log.info(`Sending command to LIRC (${this.host}:${this.port}): ${requestBody}`);
client.write(`${requestBody}\r\n`);
client.end();
setTimeout(resolve, this.delay);
});
client.on('error', (error) => {
reject(error);
});
}
};
}
}
exports.LIRCController = LIRCController;
//# sourceMappingURL=lirc.js.map