"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterWebsocketRestoreTask", {
    enumerable: true,
    get: function() {
        return PrinterWebsocketRestoreTask;
    }
});
const _errorutils = require("../utils/error.utils");
const _node = require("@sentry/node");
const _apistatetype = require("../shared/dtos/api-state.type");
const _printerapiinterface = require("../services/printer-api.interface");
class PrinterWebsocketRestoreTask {
    settingsStore;
    printerSocketStore;
    octoprintClient;
    logger;
    constructor({ printerSocketStore, octoprintClient, settingsStore, loggerFactory }){
        this.printerSocketStore = printerSocketStore;
        this.octoprintClient = octoprintClient;
        this.settingsStore = settingsStore;
        this.logger = loggerFactory(PrinterWebsocketRestoreTask.name);
    }
    async run() {
        const startTime = Date.now();
        const existingSockets = this.printerSocketStore.listPrinterSockets();
        const resetAdapterIds = [];
        const silentSocketIds = [];
        for (const socket of existingSockets){
            if (socket.printerType !== _printerapiinterface.OctoprintType) {
                continue;
            }
            try {
                if (socket.isClosedOrAborted()) {
                    socket.close();
                    socket.resetSocketState();
                    this.logger.warn("Socket was closed or aborted, manually removing it");
                    resetAdapterIds.push(socket.printerId);
                    continue;
                }
            } catch (e) {
                (0, _node.captureException)(e);
                this.logger.error(`Resetting WebSocket socket failed for printer with error '${(0, _errorutils.errorSummary)(e)}'`, e.stack);
                continue;
            }
            if (socket.apiState !== _apistatetype.API_STATE.unset && !socket.lastMessageReceivedTimestamp || Date.now() - socket.lastMessageReceivedTimestamp > 10 * 1000) {
                const result = await this.octoprintClient.getConnection(socket.login);
                silentSocketIds.push(socket.printerId);
                try {
                    if (result?.current?.state !== "Closed") {
                        this.logger.warn(`Silence was detected, but the OctoPrint current connection was not closed. Connection state ${result?.current?.state}`);
                    }
                } catch (e) {
                    this.logger.error(`Silence was detected, but Websocket was not closed/aborted and API could not be called`);
                }
            }
        }
        const duration = Date.now() - startTime;
        if (this.settingsStore.getSettingsSensitive()?.server?.debugSettings.debugSocketRetries) {
            this.logger.log(`Reset ${resetAdapterIds.length} closed/aborted sockets and detected ${silentSocketIds.length} silent sockets (duration ${duration}ms)`);
        }
    }
}

//# sourceMappingURL=printer-websocket-restore.task.js.map