homebridge-connect-my-pool
Version:
Partially complete HomeKit integration for Astral Viron Gateway
176 lines • 8.15 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.ConnectMyPoolPlatform = void 0;
const astralGatewayAPI_1 = require("./astralGatewayAPI");
const ajaxResponse_1 = require("./ajaxResponse");
const constants_1 = require("./constants");
const util_1 = __importDefault(require("util"));
const poolSpaAccessory_1 = require("./accessory/poolSpaAccessory");
const heatingAccessory_1 = require("./accessory/heatingAccessory");
const solarAccessory_1 = require("./accessory/solarAccessory");
class ConnectMyPoolPlatform {
constructor(log, config, api) {
this.debugMode = false;
///////////////////
// EVENT FUNCTIONS
///////////////////
this.eventDebug = (response) => {
this.debug('eventDebug: %s', response);
};
this.eventError = (response) => {
var _a;
(_a = this.log) === null || _a === void 0 ? void 0 : _a.error('eventError: %s', response);
};
this.eventConnect = (status) => {
var _a;
(_a = this.log) === null || _a === void 0 ? void 0 : _a.info('eventConnect');
};
this.eventClose = () => {
var _a;
(_a = this.log) === null || _a === void 0 ? void 0 : _a.info('eventClose');
};
//When Configured Zones have been determined
this.eventSystemStatus = (status) => {
this.debug('System Status Event');
// var self = this;
// if (this.zoneAccessories.length === 0)
// self.createAccessories(status);
};
this.api = api;
this.log = log;
this.config = config;
// Capture configuration parameters.
if (config.debug) {
this.debugMode = config.debug === true;
this.debug("Debug logging on. Expect a lot of data.");
}
this.controller = new ajaxResponse_1.Controller(constants_1.GATEWAY_NAME);
this.accessories = [];
this.zoneAccessories = [];
// if (this.config.controller === undefined) {
// this.log.error('ERROR: your configuration is incorrect.');
// this.controller = null;
// }
//this.gateway = null;
this.api.on('didFinishLaunching', this.setupController.bind(this));
}
async poll(fn, interval, validate, maxAttempts) {
let attempts = 0;
const executePoll = async (resolve, reject) => {
const result = await fn();
attempts++;
if (validate && validate(result)) {
return resolve(result);
}
else if (maxAttempts && attempts === maxAttempts) {
return reject(new Error('Exceeded max attempts'));
}
else {
setTimeout(executePoll, interval, resolve, reject);
}
};
return new Promise(executePoll);
}
;
getRefreshTime(configRefresh) {
if (!configRefresh)
return 5;
else
return configRefresh;
}
// This gets called when homebridge restores cached accessories at startup. We
// intentionally avoid doing anything significant here, and save all that logic
// for device discovery.
configureAccessory(accessory) {
// Add this to the accessory array so we can track it.
this.accessories.push(accessory);
}
///////////////////
// SETUP CONTROLLERS
///////////////////
setupController() {
var _a, _b;
this.gateway = new astralGatewayAPI_1.AstralGatewayAPI(this.config, this.log);
this.gateway.on("debug" /* DEBUG */, this.eventDebug.bind(this));
this.gateway.on("error" /* ERROR */, this.eventError.bind(this));
this.gateway.on("connect" /* CONNECT */, this.eventConnect.bind(this));
this.gateway.on("close" /* CLOSE */, this.eventClose.bind(this));
this.gateway.on("SYSTEM_STATUS" /* SYSTEM_STATUS */, this.eventSystemStatus.bind(this));
if (this.gateway) {
(_a = this.log) === null || _a === void 0 ? void 0 : _a.info("Connecting to Controller");
this.gateway.connect().then((status) => {
var _a;
(_a = this.log) === null || _a === void 0 ? void 0 : _a.info("Connected to Controller");
this.status = status;
this.createAccessories(status);
}).catch((error) => {
var _a;
(_a = this.log) === null || _a === void 0 ? void 0 : _a.error("Error in connecting to Controller: %s", error);
});
}
else {
(_b = this.log) === null || _b === void 0 ? void 0 : _b.info('No Controllers configured.');
}
}
async pollStatus() {
var status = await this.gateway.getSystemStatus(this.status);
this.status = status;
return status;
}
///////////////////
// CREATE ACCESSORIES
///////////////////
createAccessories(status) {
var _a, _b;
(_a = this.log) === null || _a === void 0 ? void 0 : _a.info('Creating Accessories');
if (this.zoneAccessories.length > 0) {
return;
}
else {
if (this.gateway) {
(_b = this.log) === null || _b === void 0 ? void 0 : _b.info('Creating %s Accessories', this.gateway.ConfiguredZones.length);
// Initialize our state for this controller. We need to maintain state separately for each controller.
if (this.gateway.ConfiguredZones) {
this.gateway.ConfiguredZones.forEach(zone => {
var _a;
var zoneAccessory;
// if (zone.type === ZONES.POOL_AND_SPA)
// zoneAccessory = new PoolSpaAccessory(this, this.controller, zone);
// else if (zone.type === ZONES.LIGHTING)
// zoneAccessory = new LightingAccessory(this, this.controller, zone);
// else
if (zone.type === "solar" /* SOLAR */)
zoneAccessory = new solarAccessory_1.SolarAccessory(this, this.controller, zone);
else if (zone.type === "heating" /* HEATING */)
zoneAccessory = new heatingAccessory_1.HeatingAccessory(this, this.controller, zone);
else if (zone.type === "pool_and_spa" /* POOL_AND_SPA */)
zoneAccessory = new poolSpaAccessory_1.PoolSpaAccessory(this, this.controller, zone);
if (zoneAccessory) {
this.zoneAccessories.push(zoneAccessory);
this.accessories.push(zoneAccessory.accessory);
}
else
(_a = this.log) === null || _a === void 0 ? void 0 : _a.error('createAccessories - unrecognised zone type', zone.type);
});
this.debug('Added %s new accessories', this.accessories ? this.accessories.length : 0);
this.poll(this.pollStatus.bind(this), this.getRefreshTime(this.config.refreshTime) * 1000)
.then((status) => { this.status = status; console.log(status); })
.catch((err) => console.error(err));
}
else
this.debug('No Configured Zones for controller');
}
}
}
// Utility for debug logging.
debug(message, ...parameters) {
if (this.debugMode) {
this.log(util_1.default.format(message, ...parameters));
}
}
}
exports.ConnectMyPoolPlatform = ConnectMyPoolPlatform;
//# sourceMappingURL=connect-my-pool-platform.js.map