homebridge-iammeter-accessory
Version:
An iammeter accessory plugin for homebridge written in Typescript
92 lines • 4.27 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const node_fetch_1 = __importDefault(require("node-fetch"));
let hap;
class Meter {
constructor(log, config, api) {
this.switchOn = false;
this.log = log;
this.name = config.name;
this.ip = config.ip;
this.username = config.username || 'admin'; // Default Value 'admin''
this.password = config.password || 'admin'; // Default Value 'admin'
this.pollInterval = config.pollInterval || 120; // Default Value 120s
this.onThreashold = config.onThreashold || -2000; // Default Value -2000 Watts
this.offThreashold = config.offThreashold || 0; // Default Value 0 Watts
console.log('Name: ', this.name);
console.log('IP address: ', this.ip);
console.log('Username: ', this.username);
console.log('Password: ', this.password);
console.log('Polling Interval: ', this.pollInterval);
console.log('On Threashold: ', this.onThreashold);
console.log('Off Threashold: ', this.offThreashold);
// Config error checking
if (this.onThreashold > this.offThreashold) { // Confirm on threashold is less than off threashold
console.log('ERROR - On Threashold > Off Threashold');
// Should Stop Plug-in
}
// Check for mandatory fields
this.switchService = new hap.Service.Switch(this.name);
this.switchService.getCharacteristic(hap.Characteristic.On)
.on("get" /* GET */, (callback) => {
log.info("Current state of the switch was returned: " + (this.switchOn ? "ON" : "OFF"));
callback(undefined, this.switchOn);
})
.on("set" /* SET */, (value, callback) => {
this.switchOn = value;
log.info("Switch state was set to: " + (this.switchOn ? "ON" : "OFF"));
callback();
});
this.informationService = new hap.Service.AccessoryInformation()
.setCharacteristic(hap.Characteristic.Manufacturer, "Custom Manufacturer")
.setCharacteristic(hap.Characteristic.Model, "Custom Model");
log.info("Switch finished initializing!");
setInterval(() => {
console.log('Request Energy Meter Data');
this.meterGet(this.ip, this.username, this.password).then((value) => {
console.log('');
console.log('Accessory Voltage:', value.Data[0]);
console.log('Current:', value.Data[1]);
console.log('Active Power:', value.Data[2]);
console.log('Import Power:', value.Data[3]);
console.log('Export Power:', value.Data[4]);
console.log('IP address: ', this.ip);
if (value.Data[2] < this.onThreashold) {
this.switchOn = true;
console.log('Meter reading is less than ', this.onThreashold, ' - switch on');
}
if (value.Data[2] > this.offThreashold) {
this.switchOn = false;
console.log('Meter reading is greater than ', this.offThreashold, '- switch off');
}
}, (error) => {
console.log('Error: ', error);
});
}, this.pollInterval * 1000);
} // End Class Constructor
// Now define Class methods
identify() {
this.log("Identify!");
}
getServices() {
return [
this.informationService,
this.switchService,
];
}
async meterGet(ip_address, username, password) {
const url = 'http://' + ip_address + '/monitorjson';
const auth = username + ':' + password;
// console.log('Auth string: ', auth);
const response = await node_fetch_1.default(url, { headers: { 'Authorization': 'Basic ' + btoa(auth) } });
const body = await response.json();
return body;
}
}
module.exports = (api) => {
hap = api.hap;
api.registerAccessory("Meter", Meter);
};
//# sourceMappingURL=accessory.js.map