homebridge-green-mountain-grills
Version:
A dynamic platform plugin for homebridge to provide access to your GMG WiFi smoker.
127 lines • 5.33 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.GMGPlatform = void 0;
const settings_1 = require("./settings");
const platform_accessory_1 = require("./platform-accessory");
const crypto_1 = __importDefault(require("crypto"));
const gmg_service_1 = require("./gmg-service");
class GMGPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.cachedAccessories = [];
this.smokers = new Map();
this.alreadyPolling = false;
try {
this.smokerService = new gmg_service_1.SmokerService(log, config.ipAddress);
if (!config.pollSeconds || Number.isNaN(config.pollSeconds) || Number(config.pollSeconds) < 1) {
this.config.pollSeconds = 30;
}
log.info(settings_1.PLATFORM_NAME + ' finished initializing!');
/*
* When this event is fired, homebridge restored all cached accessories from disk and did call their respective
* `configureAccessory` method for all of them. Dynamic Platform plugins should only register new accessories
* after this event was fired, in order to ensure they weren't added to homebridge already.
* This event can also be used to start discovery of new accessories.
*/
api.on("didFinishLaunching" /* APIEvent.DID_FINISH_LAUNCHING */, () => {
log.info(settings_1.PLATFORM_NAME + ' finished launching!');
this.discover()
.then(() => this.log.info('Discovery action completed'));
});
}
catch (e) {
this.log.error(e);
}
}
async discover() {
try {
this.log.info('Discovering Grills');
// Get smokers from API
const smokers = [await this.smokerService.getSmoker()];
// Remove all cached smokers
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, this.cachedAccessories);
// Register smokers
smokers.forEach(this.register.bind(this));
// We don't want to set another interval for the poller if we're already running it.
if (!this.alreadyPolling) {
this.alreadyPolling = true;
// poll again after configured time.
setInterval(this.pollForNewData.bind(this), Number(this.config.pollSeconds) * 1000);
}
}
catch (e) {
this.log.error(e);
}
}
async register(smoker) {
try {
this.log.info(`Discovered GMG Smoker: ${smoker.deviceId}.`);
const uuid = this.generate(smoker.deviceId);
// create a new accessory
const accessory = new this.api.platformAccessory('Grill', uuid);
// Add context to accessory
const context = accessory.context;
context.smoker = smoker;
// Initialize the controller
this.smokers.set(context.smoker.deviceId, new platform_accessory_1.GMGPlatformAccessory(this, accessory));
// link the accessory to your platform
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
this.log.info(`Device ${smoker.deviceId} has been registered!`);
}
catch (e) {
this.log.error(e);
}
}
pollForNewData() {
try {
this.log.info('Updating smoker status.');
// Update device attributes
for (const key of this.smokers.keys()) {
const smoker = this.smokers.get(key);
smoker.pollForNewData();
}
}
catch (e) {
this.log.error(e);
}
}
/*
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to set up event handlers for characteristics and update respective values.
*/
configureAccessory(accessory) {
try {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache, so we can track if it has already been registered
this.cachedAccessories.push(accessory);
}
catch (e) {
this.log.error(e);
}
}
generate(deviceSerialNumber) {
const sha1sum = crypto_1.default.createHash('sha1');
sha1sum.update(deviceSerialNumber);
const s = sha1sum.digest('hex');
let i = -1;
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
i += 1;
switch (c) {
case 'y':
return ((parseInt('0x' + s[i], 16) & 0x3) | 0x8).toString(16);
case 'x':
default:
return s[i];
}
});
}
}
exports.GMGPlatform = GMGPlatform;
//# sourceMappingURL=dynamic-platform.js.map