53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
const {
|
|
HCSwitch
|
|
} = require('homecontrol-control-base');
|
|
|
|
const TPLink = require('tplink-smartplug-node');
|
|
|
|
class HCTPLink extends HCSwitch {
|
|
constructor(config) {
|
|
super(config);
|
|
|
|
if (!("address" in this._configuration)) {
|
|
throw new Error(`Required configuration field "address" is missing"`);
|
|
}
|
|
}
|
|
|
|
turnOn() {
|
|
let tp = new TPLink(this._configuration.address);
|
|
|
|
return tp.turnOn().then(resp => {
|
|
if (resp.system.set_relay_state.err_code == 0) {
|
|
this._state.on = true;
|
|
this.emit("state change", this.state);
|
|
}
|
|
});
|
|
}
|
|
|
|
turnOff() {
|
|
let tp = new TPLink(this._configuration.address);
|
|
|
|
return tp.turnOff().then(resp => {
|
|
if (resp.system.set_relay_state.err_code == 0) {
|
|
this._state.on = false;
|
|
this.emit("state change", this.state);
|
|
}
|
|
});
|
|
}
|
|
|
|
pullState() {
|
|
let tp = new TPLink(this._configuration.address);
|
|
|
|
return tp.query().then(resp => {
|
|
let hashBefore = this.state.hash;
|
|
|
|
this._state.on = (resp.system.get_sysinfo.relay_state == 1);
|
|
|
|
if (this.state.hash != hashBefore) {
|
|
this.emit("state change", this.state);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = HCTPLink; |