homebridge-gpio-on-switch
Version:
51 lines (50 loc) • 2.04 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwitchAccessory = void 0;
const settings_1 = require("./settings");
const rpio_1 = __importDefault(require("rpio"));
const { version } = require('../package.json');
class SwitchAccessory {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.log = log;
this.config = config;
this.api = api;
this.log.debug('initializing accessory with config: ', this.config);
// rpio init
rpio_1.default.init({
mapping: 'gpio'
});
this.informationService = new this.api.hap.Service.AccessoryInformation()
.setCharacteristic(this.api.hap.Characteristic.Manufacturer, settings_1.MANU_FACTURER)
.setCharacteristic(this.api.hap.Characteristic.Model, settings_1.MODEL)
.setCharacteristic(this.api.hap.Characteristic.SerialNumber, 'Version ' + version);
this.switchService = new this.api.hap.Service.Switch(this.config.name);
this.switchService.getCharacteristic(this.api.hap.Characteristic.On)
.on('get', this.getOnHandler.bind(this))
.on('set', this.setOnHandler.bind(this));
}
getServices() {
return [
this.informationService,
this.switchService
];
}
getOnHandler(callback) {
const on = !!rpio_1.default.read(this.config.pin);
this.log.info('Getting switch state: ', on);
callback(null, on);
}
setOnHandler(value, callback) {
this.log.info('Setting switch state to: ', value);
rpio_1.default.open(this.config.pin, rpio_1.default.OUTPUT, value ? rpio_1.default.HIGH : rpio_1.default.LOW);
callback(null);
}
}
exports.SwitchAccessory = SwitchAccessory;