@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.
109 lines (108 loc) • 4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "PrinterEventsCache", {
enumerable: true,
get: function() {
return PrinterEventsCache;
}
});
const _keydiffcache = require("../utils/cache/key-diff.cache");
const _eventconstants = require("../constants/event.constants");
const _octoprintwebsocketadapter = require("../services/octoprint/octoprint-websocket.adapter");
const _moonrakerconstants = require("../services/moonraker/constants/moonraker.constants");
const _prusalinkconstants = require("../services/prusa-link/constants/prusalink.constants");
class PrinterEventsCache extends _keydiffcache.KeyDiffCache {
eventEmitter2;
logger;
constructor(eventEmitter2, loggerFactory){
super(), this.eventEmitter2 = eventEmitter2;
this.logger = loggerFactory(PrinterEventsCache.name);
this.subscribeToEvents();
}
async deletePrinterSocketEvents(id) {
await this.deleteKeyValue(id, true);
}
async getPrinterSocketEvents(id) {
return this.keyValueStore[id];
}
async getOrCreateEvents(printerId) {
let ref = await this.getValue(printerId);
if (!ref) {
ref = {
connected: null,
reauthRequired: null,
notify_status_update: null,
current: null,
history: null,
API_STATE_UPDATED: null,
WS_CLOSED: null,
WS_ERROR: null,
WS_OPENED: null,
WS_STATE_UPDATED: null
};
await this.setKeyValue(printerId, ref);
}
return ref;
}
async setEvent(printerId, label, payload) {
const ref = await this.getOrCreateEvents(printerId);
ref[label] = {
payload,
receivedAt: Date.now()
};
await this.setKeyValue(printerId, ref);
}
async setSubState(printerId, label, substateName, payload) {
const ref = await this.getOrCreateEvents(printerId);
if (!ref[label]) {
ref[label] = {};
}
ref[label][substateName] = {
payload,
receivedAt: Date.now()
};
await this.setKeyValue(printerId, ref);
}
async handlePrintersDeleted(event) {
await this.deleteKeysBatch(event.printerIds);
}
subscribeToEvents() {
this.eventEmitter2.on((0, _octoprintwebsocketadapter.octoPrintEvent)("*"), (e)=>this.onOctoPrintSocketMessage(e));
this.eventEmitter2.on((0, _moonrakerconstants.moonrakerEvent)("*"), (e)=>this.onMoonrakerSocketMessage(e));
this.eventEmitter2.on((0, _prusalinkconstants.prusaLinkEvent)("*"), (e)=>this.onPrusaLinkPollMessage(e));
this.eventEmitter2.on(_eventconstants.printerEvents.printersDeleted, this.handlePrintersDeleted.bind(this));
}
async onOctoPrintSocketMessage(e) {
const printerId = e.printerId;
if (![
"plugin",
"event"
].includes(e.event)) {
await this.setEvent(printerId, e.event, e.event === "history" ? this.pruneHistoryPayload(e.payload) : e.payload);
}
}
async onMoonrakerSocketMessage(e) {
const printerId = e.printerId;
const eventType = e.event;
if ([
"notify_status_update",
"current"
].includes(eventType)) {
await this.setEvent(printerId, eventType, e.payload);
}
}
async onPrusaLinkPollMessage(e) {
const printerId = e.printerId;
this.logger.debug(`Received prusaLink event ${e.event}, printerId ${e.printerId}`, e);
if (e.event === "current") {
await this.setEvent(printerId, e.event, e.payload);
}
}
pruneHistoryPayload(payload) {
const { logs, temps, messages, plugins, ...prunedPayload } = payload;
return prunedPayload;
}
}
//# sourceMappingURL=printer-events.cache.js.map