UNPKG

homebridge-config-ui-x

Version:

A web based management, configuration and control platform for Homebridge

126 lines 6.56 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 bufferShim = require("buffer-shims"); const qr = require("qr-image"); const child_process = require("child_process"); const common_1 = require("@nestjs/common"); const config_service_1 = require("../../core/config/config.service"); const logger_service_1 = require("../../core/logger/logger.service"); const config_editor_service_1 = require("../config-editor/config-editor.service"); let ServerService = class ServerService { constructor(configService, configEditorService, logger) { this.configService = configService; this.configEditorService = configEditorService; this.logger = logger; this.accessoryId = this.configService.homebridgeConfig.bridge.username.split(':').join(''); this.accessoryInfoPath = path.join(this.configService.storagePath, 'persist', `AccessoryInfo.${this.accessoryId}.json`); } restartServer() { return __awaiter(this, void 0, void 0, function* () { this.logger.log('Homebridge restart request received'); setTimeout(() => { if (this.configService.ui.restart) { this.logger.log(`Executing restart command: ${this.configService.ui.restart}`); child_process.exec(this.configService.ui.restart, (err) => { if (err) { this.logger.log('Restart command exited with an error. Failed to restart Homebridge.'); } }); } else { this.logger.log(`No restart command defined, killing process...`); process.kill(process.pid, 'SIGTERM'); } }, 500); return { ok: true, command: this.configService.ui.restart }; }); } resetHomebridgeAccessory() { return __awaiter(this, void 0, void 0, function* () { const configFile = yield this.configEditorService.getConfigFile(); configFile.bridge.pin = this.configEditorService.generatePin(); configFile.bridge.username = this.configEditorService.generateUsername(); this.logger.warn(`Homebridge Reset: New Username: ${configFile.bridge.username}`); this.logger.warn(`Homebridge Reset: New Pin: ${configFile.bridge.pin}`); yield this.configEditorService.updateConfigFile(configFile); yield fs.remove(path.resolve(this.configService.storagePath, 'accessories')); yield fs.remove(path.resolve(this.configService.storagePath, 'persist')); this.logger.log(`Homebridge Reset: "persist" directory removed.`); this.logger.log(`Homebridge Reset: "accessories" directory removed.`); }); } generateQrCode() { return __awaiter(this, void 0, void 0, function* () { const setupCode = yield this.getSetupCode(); if (!setupCode) { throw new common_1.NotFoundException(); } const qrImg = qr.svgObject(setupCode, { type: 'svg' }); return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 23 23"><path d="${qrImg.path}"/></svg>`; }); } getSetupCode() { return __awaiter(this, void 0, void 0, function* () { if (this.setupCode) { return this.setupCode; } else { this.setupCode = yield this.generateSetupCode(); return this.setupCode; } }); } generateSetupCode() { return __awaiter(this, void 0, void 0, function* () { if (!(yield fs.pathExists(this.accessoryInfoPath))) { return null; } const accessoryInfo = yield fs.readJson(this.accessoryInfoPath); const buffer = bufferShim.alloc(8); const setupCode = parseInt(accessoryInfo.pincode.replace(/-/g, ''), 10); let valueLow = setupCode; const valueHigh = accessoryInfo.category >> 1; valueLow |= 1 << 28; buffer.writeUInt32BE(valueLow, 4); if (accessoryInfo.category & 1) { buffer[4] = buffer[4] | 1 << 7; } buffer.writeUInt32BE(valueHigh, 0); let encodedPayload = (buffer.readUInt32BE(4) + (buffer.readUInt32BE(0) * Math.pow(2, 32))).toString(36).toUpperCase(); if (encodedPayload.length !== 9) { for (let i = 0; i <= 9 - encodedPayload.length; i++) { encodedPayload = '0' + encodedPayload; } } return 'X-HM://' + encodedPayload + accessoryInfo.setupID; }); } }; ServerService = __decorate([ common_1.Injectable(), __metadata("design:paramtypes", [config_service_1.ConfigService, config_editor_service_1.ConfigEditorService, logger_service_1.Logger]) ], ServerService); exports.ServerService = ServerService; //# sourceMappingURL=server.service.js.map