UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.

92 lines (91 loc) 4.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "PrintCompletionSocketIoTask", { enumerable: true, get: function() { return PrintCompletionSocketIoTask; } }); const _eventconstants = require("../constants/event.constants"); const _octoprintwebsocketconstants = require("../services/octoprint/constants/octoprint-websocket.constants"); const _correlationtokenutil = require("../utils/correlation-token.util"); const _octoprintwebsocketadapter = require("../services/octoprint/octoprint-websocket.adapter"); class PrintCompletionSocketIoTask { eventEmitter2; printCompletionService; printerEventsCache; contextCache; logger; constructor(loggerFactory, eventEmitter2, printCompletionService, printerEventsCache){ this.eventEmitter2 = eventEmitter2; this.printCompletionService = printCompletionService; this.printerEventsCache = printerEventsCache; this.contextCache = {}; this.logger = loggerFactory(PrintCompletionSocketIoTask.name); this.eventEmitter2.on((0, _octoprintwebsocketadapter.octoPrintWebsocketEvent)("*"), async (octoPrintEvent)=>{ await this.handleMessage(octoPrintEvent); }); } get contexts() { return this.contextCache; } async handleMessage(event) { const printerId = event.printerId; if (!printerId) { this.logger.log(`Skipping print completion log as PrinterId is unset`); return; } if (event.event !== "event") { return; } const completion = { status: event.payload.type, fileName: event.payload?.payload?.name, createdAt: Date.now(), completionLog: event.payload?.error, printerId, context: { correlationId: null } }; if (event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.EStop || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintCancelling || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintCancelled || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Home || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.TransferStarted || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.TransferDone || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnecting || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnected || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.MetadataAnalysisStarted || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.MetadataAnalysisFinished || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Error) { this.contextCache[printerId] = { ...this.contextCache[printerId], [event.payload.type]: completion }; if (event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnecting || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnected) { await this.printerEventsCache.setSubState(printerId, "current", "state", { text: event.payload.type, flags: { operational: false, printing: false, ready: false, closedOrError: true, error: false } }); } const corrId = this.contextCache[printerId].correlationId; await this.printCompletionService.updateContext(corrId, this.contextCache[printerId]); return; } if (event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintStarted) { const token = (0, _correlationtokenutil.generateCorrelationToken)(); this.contextCache[printerId] = { correlationId: token }; completion.context = this.contextCache[printerId]; await this.printCompletionService.create(completion); } else if (event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintFailed || event.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintDone) { completion.context = this.contextCache[printerId]; await this.printCompletionService.create(completion); this.eventEmitter2.emit((0, _eventconstants.fdmMonsterPrinterStoppedEvent)(printerId)); this.contextCache[printerId] = { correlationId: undefined }; } } } //# sourceMappingURL=print-completion.socketio.task.js.map