hc-rcswitch/HCrcswitch.js

68 lines
1.5 KiB
JavaScript

const {
HCSwitch
} = require('homecontrol-control-base');
const os = require('os');
let rcswitch;
if(os.arch() == 'arm' || os.arch() == 'arm64') {
rcswitch = require('rcswitch4');
} else {
/**
* "Polyfill" for development purposes. Should run correctly on actual Raspberry PI hardware
*/
rcswitch = {
switchOn: function() {
console.log("switchOn", arguments);
},
switchOff: function() {
console.log("switchOff", arguments);
},
enableTransmit: function() {
console.log("enableTransmit", arguments);
},
};
}
class HCrcswitch extends HCSwitch {
constructor(config) {
super(config);
if (!("pin" in this._configuration)) {
throw new Error(`Required configuration field "address" is missing"`);
}
if (!("group" in this._configuration)) {
throw new Error(`Required configuration field "group" is missing"`);
}
if (!("switch" in this._configuration)) {
throw new Error(`Required configuration field "switch" is missing"`);
}
rcswitch.enableTransmit(this._configuration.pin);
}
turnOn() {
return new Promise((resolve) => {
rcswitch.switchOn(this._configuration.group, this._configuration.switch);
this._state.on = true;
this.emit("state change", this.state);
return resolve();
});
}
turnOff() {
return new Promise((resolve) => {
rcswitch.switchOff(this._configuration.group, this._configuration.switch);
this._state.on = false;
this.emit("state change", this.state);
return resolve();
});
}
}
module.exports = HCrcswitch;