homebridge-pjlink
Version:
Homebridge accessory for PJLink supported projectors
137 lines • 5.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TelevisionHandler = void 0;
// import {PJLink} from 'PJLink';
const PJLink = require('pjlink');
class TelevisionHandler {
constructor(log, api, accessory, device, name, enableSwitch = false) {
this.log = log;
this.api = api;
this.accessory = accessory;
this.device = device;
this.name = name;
this.enableSwitch = enableSwitch;
this.deviceActive = false;
this.tvService = this.createService();
if (this.enableSwitch) {
this.switchService = this.createSwitchService();
}
}
createService() {
// hap
const Characteristic = this.api.hap.Characteristic;
// service
const tvService = (this.accessory.getService(this.api.hap.Service.Television) ||
this.accessory.addService(this.api.hap.Service.Television, this.name));
tvService
.setCharacteristic(Characteristic.ConfiguredName, this.name)
.setCharacteristic(Characteristic.SleepDiscoveryMode, Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE);
tvService.getCharacteristic(Characteristic.Active)
.on("get" /* GET */, this.getTelevisionActive.bind(this))
.on("set" /* SET */, this.setTelevisionActive.bind(this));
tvService.getCharacteristic(this.api.hap.Characteristic.RemoteKey)
.on("set" /* SET */, this.setRemoteKey.bind(this));
return tvService;
}
createSwitchService() {
// hap
const Service = this.api.hap.Service;
const Characteristic = this.api.hap.Characteristic;
// service
const service = (this.accessory.getService(Service.Switch) ||
this.accessory.addService(Service.Switch, this.name));
service.getCharacteristic(Characteristic.On)
.on("get" /* GET */, this.getTelevisionActive.bind(this))
.on("set" /* SET */, this.setTelevisionActive.bind(this));
return service;
}
getService() {
return this.tvService;
}
getSwitchService() {
return this.switchService;
}
/**
* Get television active
* @param callback
*/
getTelevisionActive(callback) {
try {
this.device.getPowerState((error, state) => {
if (error) {
this.log.error(error);
callback(new Error(error));
}
else {
this.deviceActive = (state === PJLink.POWER.ON);
this.log.info('television active', this.deviceActive);
callback(null, this.deviceActive);
}
});
}
catch (e) {
this.log.error(e);
callback(e);
}
}
/**
* Set television active
* @param {boolean} value
* @param callback
*/
setTelevisionActive(value, callback) {
// hap
const Characteristic = this.api.hap.Characteristic;
try {
const powerState = (value === 1 || value === true) ? PJLink.POWER.ON : PJLink.POWER.OFF;
this.device.setPowerState(powerState, (error) => {
this.log.info('setPowerState', value, powerState, error);
if (error) {
callback(new Error(error));
}
else {
this.deviceActive = (powerState === PJLink.POWER.ON);
callback();
this.tvService.updateCharacteristic(Characteristic.Active, this.deviceActive);
if (this.switchService) {
this.switchService.updateCharacteristic(Characteristic.On, this.deviceActive);
}
}
});
}
catch (e) {
this.log.error(e);
callback(e);
}
}
setRemoteKey(value, callback) {
this.log.info('setRemoteKey', value);
callback();
}
update() {
try {
this.device.getPowerState((error, state) => {
if (error) {
this.log.error(error);
}
else {
const active = (state === PJLink.POWER.ON);
if (active !== this.deviceActive) {
this.deviceActive = active;
// hap
const Characteristic = this.api.hap.Characteristic;
this.tvService.updateCharacteristic(Characteristic.Active, this.deviceActive);
if (this.switchService) {
this.switchService.updateCharacteristic(Characteristic.On, this.deviceActive);
}
}
}
});
}
catch (e) {
this.log.error(e);
}
}
}
exports.TelevisionHandler = TelevisionHandler;
//# sourceMappingURL=TelevisionHandler.js.map