@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.
114 lines (113 loc) • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "PrinterCache", {
enumerable: true,
get: function() {
return PrinterCache;
}
});
const _keydiffcache = require("../utils/cache/key-diff.cache");
const _eventconstants = require("../constants/event.constants");
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
class PrinterCache extends _keydiffcache.KeyDiffCache {
printerService;
eventEmitter2;
constructor(printerService, eventEmitter2){
super(), this.printerService = printerService, this.eventEmitter2 = eventEmitter2;
this.eventEmitter2.on(_eventconstants.printerEvents.batchPrinterCreated, this.handleBatchPrinterCreated.bind(this));
this.eventEmitter2.on(_eventconstants.printerEvents.printerCreated, this.handlePrinterCreatedOrUpdated.bind(this));
this.eventEmitter2.on(_eventconstants.printerEvents.printerUpdated, this.handlePrinterCreatedOrUpdated.bind(this));
this.eventEmitter2.on(_eventconstants.printerEvents.printersDeleted, this.handlePrintersDeleted.bind(this));
}
async loadCache() {
const printerDocs = await this.printerService.list();
const dtos = this.mapArray(printerDocs);
const keyValues = dtos.map((p)=>({
key: this.getId(p),
value: p
}));
await this.setKeyValuesBatch(keyValues, true);
return dtos;
}
async countDisabledPrinters() {
return (await this.getAllValues()).filter((p)=>!p.enabled).length;
}
async listCachedPrinters(includeDisabled = false) {
const printers = await this.getAllValues();
if (!includeDisabled) {
return printers.filter((p)=>p.enabled);
}
return printers;
}
async getCachedPrinterOrThrowAsync(id) {
const printer = await this.getValue(id);
if (!printer) {
throw new _runtimeexceptions.NotFoundException(`Printer with provided id not found`);
}
return printer;
}
getCachedPrinterOrThrow(id) {
const printer = this.keyValueStore[id];
if (!printer) {
throw new _runtimeexceptions.NotFoundException(`Printer with provided id not found`);
}
return printer;
}
async getNameAsync(id) {
const printer = await this.getCachedPrinterOrThrowAsync(id);
return printer.name;
}
getName(id) {
const printer = this.getCachedPrinterOrThrow(id);
return printer.name;
}
async getLoginDtoAsync(id) {
const printer = await this.getCachedPrinterOrThrowAsync(id);
return {
printerURL: printer.printerURL,
apiKey: printer.apiKey,
username: printer.username,
password: printer.password,
printerType: printer.printerType
};
}
getLoginDto(id) {
const printer = this.getCachedPrinterOrThrow(id);
return {
printerURL: printer.printerURL,
apiKey: printer.apiKey,
printerType: printer.printerType,
username: printer.username,
password: printer.password
};
}
async handleBatchPrinterCreated(event) {
const mappedPrinters = this.mapArray(event.printers);
const keyValues = mappedPrinters.map((p)=>({
key: this.getId(p),
value: p
}));
await this.setKeyValuesBatch(keyValues, true);
}
async handlePrinterCreatedOrUpdated(event) {
const printerDto = this.map(event.printer);
await this.setKeyValue(printerDto.id, printerDto, true);
}
async handlePrintersDeleted(event) {
await this.deleteKeysBatch(event.printerIds, true);
}
getId(value) {
return value.id.toString();
}
mapArray(entities) {
return entities.map((p)=>{
return this.map(p);
});
}
map(entity) {
return this.printerService.toDto(entity);
}
}
//# sourceMappingURL=printer.cache.js.map