UNPKG

homebridge-xfinityhome

Version:

A homebridge plugin to control your Xfinity Home security system.

80 lines 4.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const xfinityhome_1 = require("xfinityhome"); class Accessory { constructor(platform, accessory, device, service) { this.service = service; this.name = device instanceof xfinityhome_1.Panel ? 'Panel' : device.device.name || ('model' in device.device ? device.device.model : 'Unknown'); this.projectDir = path_1.default.join(platform.api.user.storagePath(), 'XfinityHome'); this.logPath = path_1.default.join(this.projectDir, this.name.replace(/\//g, '-') + '.log'); this.generalLogPath = path_1.default.join(this.projectDir, 'General.log'); if (!fs_1.default.existsSync(this.projectDir)) { fs_1.default.mkdirSync(this.projectDir); } this.log = (type, message, ...args) => { var _a; const parsedArgs = args.map(arg => JSON.stringify(arg, null, 2)); const date = new Date(); const time = `${('0' + (date.getMonth() + 1)).slice(-2)}/${('0' + date.getDate()).slice(-2)}/${date.getFullYear()}, ` + `${('0' + (date.getHours() % 12)).slice(-2)}:${('0' + (date.getMinutes())).slice(-2)}:${('0' + (date.getSeconds())).slice(-2)} ` + `${date.getHours() > 12 ? 'PM' : 'AM'}`; //if (typeof type === 'number') { if (typeof type === 'string' || type < 4) { fs_1.default.appendFileSync(this.generalLogPath, `[${time}] ${this.name}: ${message} ${parsedArgs.join(' ')}\n`); } fs_1.default.appendFileSync(this.logPath, `[${time}] ${message} ${parsedArgs.join(' ')}\n`); if (typeof type === 'string') { platform.log[type](`${this.name}: ${message} `, ...parsedArgs); } else if (type <= ((_a = platform.config.logLevel) !== null && _a !== void 0 ? _a : 3)) { platform.log.info(`${this.name}: ${message} `, ...parsedArgs); } else { platform.log.debug(`${this.name}: ${message} `, ...parsedArgs); } }; this.log(4, 'Server Started'); this.StatusError = platform.api.hap.HapStatusError; platform.api.on('shutdown', () => { this.log(4, 'Server Stopped'); accessory.context.logPath = this.logPath; accessory.context.device = device.device; accessory.context.refreshToken = platform.xhome.refreshToken; platform.api.updatePlatformAccessories([accessory]); }); const deviceInfo = device.device; // set accessory information accessory.getService(platform.Service.AccessoryInformation) .setCharacteristic(platform.Characteristic.Manufacturer, 'manufacturer' in deviceInfo ? deviceInfo.manufacturer : 'Unknown') .setCharacteristic(platform.Characteristic.SerialNumber, 'serialNumber' in deviceInfo ? deviceInfo.serialNumber : accessory.UUID) .setCharacteristic(platform.Characteristic.Model, 'model' in deviceInfo ? deviceInfo.model : 'Unknown') .setCharacteristic(platform.Characteristic.Name, this.name) .setCharacteristic(platform.Characteristic.FirmwareRevision, 'firmwareVersion' in deviceInfo ? deviceInfo.firmwareVersion : 'Unknown'); accessory.getService(platform.Service.AccessoryInformation).getCharacteristic(platform.Characteristic.Identify).on('set', () => { this.log('info', 'Identifying Device:', device.device); if (device instanceof xfinityhome_1.Light) { let mode = device.device.properties.isOn; const startMode = mode; const interval = setInterval(() => { device.set(!mode).catch(err => { this.log('error', 'Failed To Toggle Light With Error:', err); }); mode = !mode; }, 750); setTimeout(() => { clearInterval(interval); device.set(startMode).catch(err => { this.log('error', 'Failed To Toggle Light With Error:', err); }); }, 5000); } }); } } exports.default = Accessory; //# sourceMappingURL=Accessory.js.map