UNPKG

homebridge-config-ui-x

Version:

A web based management, configuration and control platform for Homebridge

144 lines • 6.16 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs-extra"); const path = require("path"); const common_1 = require("@nestjs/common"); const logger_service_1 = require("../../core/logger/logger.service"); const config_service_1 = require("../../core/config/config.service"); let ConfigEditorService = class ConfigEditorService { constructor(configService, logger) { this.configService = configService; this.logger = logger; } getConfigFile() { return __awaiter(this, void 0, void 0, function* () { return yield fs.readJson(this.configService.configPath); }); } updateConfigFile(config) { return __awaiter(this, void 0, void 0, function* () { const now = new Date(); if (!config) { config = {}; } if (!config.bridge) { config.bridge = {}; } if (!config.bridge.name) { config.bridge.name = 'Homebridge'; } if (!config.bridge.port) { config.bridge.port = 51826; } if (!config.bridge.username) { config.bridge.username = this.generateUsername(); } if (!config.bridge.pin) { config.bridge.pin = this.generatePin(); } if (!config.accessories) { config.accessories = []; } if (!config.platforms) { config.platforms = []; } if (config.plugins && Array.isArray(config.plugins)) { if (!config.plugins.length) { delete config.plugins; } } else if (config.plugins) { delete config.plugins; } yield fs.rename(this.configService.configPath, `${this.configService.configPath}.${now.getTime()}`); fs.writeJsonSync(this.configService.configPath, config, { spaces: 4 }); this.logger.log('Changes to config.json saved.'); return config; }); } listConfigBackups() { return __awaiter(this, void 0, void 0, function* () { const dirContents = yield fs.readdir(this.configService.storagePath); const backups = dirContents .filter(x => x.indexOf('config.json.') === 0) .sort() .reverse() .map(x => { const ext = x.split('.'); if (ext.length === 3 && !isNaN(ext[2])) { return { id: ext[2], timestamp: new Date(parseInt(ext[2], 10)), file: x, }; } else { return null; } }) .filter((x => x && !isNaN(x.timestamp.getTime()))); return backups; }); } getConfigBackup(backupId) { return __awaiter(this, void 0, void 0, function* () { if (!(yield fs.pathExists(this.configService.configPath + '.' + parseInt(backupId, 10)))) { throw new Error(`Backup ${backupId} Not Found`); } return yield fs.readFile(this.configService.configPath + '.' + parseInt(backupId, 10)); }); } deleteAllConfigBackups() { return __awaiter(this, void 0, void 0, function* () { const backups = yield this.listConfigBackups(); yield backups.forEach((backupFile) => __awaiter(this, void 0, void 0, function* () { yield fs.unlink(path.resolve(this.configService.storagePath, backupFile.file)); })); }); } generatePin() { let code = Math.floor(10000000 + Math.random() * 90000000) + ''; code = code.split(''); code.splice(3, 0, '-'); code.splice(6, 0, '-'); code = code.join(''); return code; } generateUsername() { const hexDigits = '0123456789ABCDEF'; let username = '0E:'; for (let i = 0; i < 5; i++) { username += hexDigits.charAt(Math.round(Math.random() * 15)); username += hexDigits.charAt(Math.round(Math.random() * 15)); if (i !== 4) { username += ':'; } } return username; } }; ConfigEditorService = __decorate([ common_1.Injectable(), __metadata("design:paramtypes", [config_service_1.ConfigService, logger_service_1.Logger]) ], ConfigEditorService); exports.ConfigEditorService = ConfigEditorService; //# sourceMappingURL=config-editor.service.js.map