"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerController = void 0;
const common_1 = require("@nestjs/common");
const passport_1 = require("@nestjs/passport");
const swagger_1 = require("@nestjs/swagger");
const admin_guard_1 = require("../../core/auth/guards/admin.guard");
const logger_service_1 = require("../../core/logger/logger.service");
const spa_html_service_1 = require("../../core/spa/spa-html.service");
const child_bridges_service_1 = require("../child-bridges/child-bridges.service");
const server_dto_1 = require("./server.dto");
const server_service_1 = require("./server.service");
let ServerController = class ServerController {
    constructor(serverService, childBridgesService, logger) {
        this.serverService = serverService;
        this.childBridgesService = childBridgesService;
        this.logger = logger;
    }
    restartServer() {
        return this.serverService.restartServer();
    }
    restartChildBridge(deviceId) {
        return this.childBridgesService.restartChildBridge(deviceId);
    }
    stopChildBridge(deviceId) {
        return this.childBridgesService.stopChildBridge(deviceId);
    }
    startChildBridge(deviceId) {
        return this.childBridgesService.startChildBridge(deviceId);
    }
    getBridgePairingInformation() {
        return this.serverService.getBridgePairingInformation();
    }
    resetHomebridgeAccessory() {
        return this.serverService.resetHomebridgeAccessory();
    }
    deleteAllCachedAccessories() {
        return this.serverService.deleteAllCachedAccessories();
    }
    getCachedAccessories() {
        return this.serverService.getCachedAccessories();
    }
    deleteCachedAccessory(uuid, cacheFile) {
        return this.serverService.deleteCachedAccessory(uuid, cacheFile);
    }
    deleteCachedAccessories(accessories) {
        return this.serverService.deleteCachedAccessories(accessories);
    }
    getDevicePairings() {
        return this.serverService.getDevicePairings();
    }
    getDevicePairingById(deviceId) {
        return this.serverService.getDevicePairingById(deviceId);
    }
    deleteDevicePairing(deviceId, resetPairingInfo) {
        const resetPairingInfoBool = resetPairingInfo === 'true';
        return this.serverService.deleteDevicePairing(deviceId, resetPairingInfoBool);
    }
    deleteDevicesPairings(bridges) {
        return this.serverService.deleteDevicesPairing(bridges);
    }
    deleteDeviceAccessories(deviceId) {
        return this.serverService.deleteDeviceAccessories(deviceId);
    }
    deleteDevicesAccessories(bridges) {
        return this.serverService.deleteDevicesAccessories(bridges);
    }
    lookupUnusedPort() {
        return this.serverService.lookupUnusedPort();
    }
    getSystemNetworkInterfaces() {
        return this.serverService.getSystemNetworkInterfaces();
    }
    getHomebridgeNetworkInterfaces() {
        return this.serverService.getHomebridgeNetworkInterfaces();
    }
    setHomebridgeNetworkInterfaces(body) {
        return this.serverService.setHomebridgeNetworkInterfaces(body.adapters);
    }
    getHomebridgeMdnsSetting() {
        return this.serverService.getHomebridgeMdnsSetting();
    }
    setHomebridgeMdnsSetting(body) {
        return this.serverService.setHomebridgeMdnsSetting(body);
    }
    setHomebridgeName(body) {
        return this.serverService.setHomebridgeName(body.name);
    }
    getHomebridgePort() {
        return this.serverService.getHomebridgePort();
    }
    setHomebridgePort(body) {
        return this.serverService.setHomebridgePort(body.port);
    }
    getUsablePort() {
        return this.serverService.getUsablePorts();
    }
    setUsablePorts(body) {
        return this.serverService.setUsablePorts(body);
    }
    async uploadWallpaper(req) {
        try {
            const data = await req.file();
            if (data.file.truncated) {
                throw new common_1.InternalServerErrorException(`Wallpaper exceeds maximum size ${globalThis.backup.maxBackupSizeText}.`);
            }
            await this.serverService.uploadWallpaper(data);
        }
        catch (err) {
            this.logger.error(`Wallpaper upload failed as ${err.message}`);
            throw new common_1.InternalServerErrorException(err.message);
        }
    }
    async deleteWallpaper() {
        await this.serverService.deleteWallpaper();
    }
    async updateWebroot(body) {
        await spa_html_service_1.SpaHtmlService.updateIndexHtml(body.webroot);
    }
};
exports.ServerController = ServerController;
__decorate([
    (0, common_1.Put)('/restart'),
    (0, swagger_1.ApiOperation)({ summary: 'Restart the Homebridge instance.' }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "restartServer", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, common_1.Put)('/restart/:deviceId'),
    (0, swagger_1.ApiOperation)({
        summary: 'Restart a child bridge instance.',
    }),
    __param(0, (0, common_1.Param)('deviceId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "restartChildBridge", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, common_1.Put)('/stop/:deviceId'),
    (0, swagger_1.ApiOperation)({
        summary: 'Stop a child bridge instance.',
    }),
    __param(0, (0, common_1.Param)('deviceId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "stopChildBridge", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, common_1.Put)('/start/:deviceId'),
    (0, swagger_1.ApiOperation)({
        summary: 'Start a child bridge instance.',
    }),
    __param(0, (0, common_1.Param)('deviceId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "startChildBridge", null);
__decorate([
    (0, common_1.Get)('/pairing'),
    (0, swagger_1.ApiOperation)({ summary: 'Get the Homebridge <> HomeKit pairing information and status.' }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getBridgePairingInformation", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Reset the main Homebridge bridge, and change its username and pin. Also remove cached bridges and accessories.' }),
    (0, common_1.Put)('/reset-homebridge-accessory'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "resetHomebridgeAccessory", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove Homebridge cached accessories.',
    }),
    (0, common_1.Put)('/reset-cached-accessories'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteAllCachedAccessories", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'List cached Homebridge accessories.' }),
    (0, common_1.Get)('/cached-accessories'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getCachedAccessories", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove a single Homebridge cached accessory.',
    }),
    (0, swagger_1.ApiParam)({ name: 'uuid' }),
    (0, swagger_1.ApiQuery)({ name: 'cacheFile' }),
    (0, common_1.Delete)('/cached-accessories/:uuid'),
    (0, common_1.HttpCode)(204),
    __param(0, (0, common_1.Param)('uuid')),
    __param(1, (0, common_1.Query)('cacheFile')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteCachedAccessory", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove multiple Homebridge cached accessories.',
    }),
    (0, swagger_1.ApiBody)({ description: 'Array of accessories (uuid and cacheFile) to remove from the cache', type: 'json', isArray: true }),
    (0, common_1.Delete)('/cached-accessories'),
    (0, common_1.HttpCode)(204),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteCachedAccessories", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'List all paired accessories (main bridge, external cameras, TVs etc).' }),
    (0, common_1.Get)('/pairings'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getDevicePairings", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Get a single device pairing.' }),
    (0, common_1.Get)('/pairings/:deviceId'),
    __param(0, (0, common_1.Param)('deviceId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getDevicePairingById", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove a single paired bridge.',
    }),
    (0, swagger_1.ApiParam)({ name: 'deviceId' }),
    (0, swagger_1.ApiQuery)({ name: 'resetPairingInfo', type: Boolean }),
    (0, common_1.Delete)('/pairings/:deviceId'),
    (0, common_1.HttpCode)(204),
    __param(0, (0, common_1.Param)('deviceId')),
    __param(1, (0, common_1.Query)('resetPairingInfo')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteDevicePairing", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove multiple paired bridges.',
    }),
    (0, swagger_1.ApiBody)({ description: 'Array of paired bridges (id and resetPairingInfo) to remove from the cache', type: 'json', isArray: true }),
    (0, common_1.Delete)('/pairings'),
    (0, common_1.HttpCode)(204),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteDevicesPairings", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove a paired bridge\'s cached accessories.',
    }),
    (0, swagger_1.ApiParam)({ name: 'deviceId' }),
    (0, common_1.Delete)('/pairings/:deviceId/accessories'),
    (0, common_1.HttpCode)(204),
    __param(0, (0, common_1.Param)('deviceId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteDeviceAccessories", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({
        summary: 'Remove multiple paired bridges\'s cached accessories.',
    }),
    (0, swagger_1.ApiBody)({ description: 'Array of bridges (id) for which to remove accessories.', type: 'json', isArray: true }),
    (0, common_1.Delete)('/pairings/accessories'),
    (0, common_1.HttpCode)(204),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "deleteDevicesAccessories", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Return a random, unused port.' }),
    (0, common_1.Get)('/port/new'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "lookupUnusedPort", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Return a list of available network interfaces on the server.' }),
    (0, common_1.Get)('/network-interfaces/system'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getSystemNetworkInterfaces", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Return a list of the network interface names assigned to Homebridge.' }),
    (0, common_1.Get)('/network-interfaces/bridge'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getHomebridgeNetworkInterfaces", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Set a list of the network interface names assigned to Homebridge.' }),
    (0, common_1.Put)('/network-interfaces/bridge'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [server_dto_1.HomebridgeNetworkInterfacesDto]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "setHomebridgeNetworkInterfaces", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Return the current mDNS advertiser settings.' }),
    (0, common_1.Get)('/mdns-advertiser'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], ServerController.prototype, "getHomebridgeMdnsSetting", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Set the mDNS advertiser settings.' }),
    (0, common_1.Put)('/mdns-advertiser'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [server_dto_1.HomebridgeMdnsSettingDto]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "setHomebridgeMdnsSetting", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Set the Homebridge name.' }),
    (0, common_1.Put)('/name'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "setHomebridgeName", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Get the Homebridge port.' }),
    (0, common_1.Get)('/port'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getHomebridgePort", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Set the Homebridge port.' }),
    (0, common_1.Put)('/port'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "setHomebridgePort", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Get the usable ports as set in the config file.' }),
    (0, common_1.Get)('/ports'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "getUsablePort", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, common_1.Put)('/ports'),
    (0, swagger_1.ApiOperation)({ summary: 'Update the usable ports for Homebridge.' }),
    (0, swagger_1.ApiBody)({ description: 'Object with start and end properties.', type: 'json', isArray: false }),
    (0, common_1.Put)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], ServerController.prototype, "setUsablePorts", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, common_1.Post)('/wallpaper'),
    (0, swagger_1.ApiOperation)({ summary: 'Upload an image file to the Homebridge storage directory and reference this as a wallpaper in the config file.' }),
    (0, swagger_1.ApiConsumes)('multipart/form-data'),
    (0, swagger_1.ApiBody)({
        schema: {
            type: 'object',
            properties: {
                file: {
                    type: 'string',
                    format: 'binary',
                },
            },
        },
    }),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ServerController.prototype, "uploadWallpaper", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Delete the current wallpaper file and remove the reference from the config file.' }),
    (0, common_1.Delete)('/wallpaper'),
    (0, common_1.HttpCode)(204),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], ServerController.prototype, "deleteWallpaper", null);
__decorate([
    (0, common_1.UseGuards)(admin_guard_1.AdminGuard),
    (0, swagger_1.ApiOperation)({ summary: 'Update the index.html file to use the specified webroot.' }),
    (0, common_1.Put)('/webroot'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ServerController.prototype, "updateWebroot", null);
exports.ServerController = ServerController = __decorate([
    (0, swagger_1.ApiTags)('Homebridge'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.UseGuards)((0, passport_1.AuthGuard)()),
    (0, common_1.Controller)('server'),
    __metadata("design:paramtypes", [server_service_1.ServerService,
        child_bridges_service_1.ChildBridgesService,
        logger_service_1.Logger])
], ServerController);
//# sourceMappingURL=server.controller.js.map