@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
89 lines (88 loc) • 2.93 kB
JavaScript
import { NotFoundException } from "../exceptions/runtime.exceptions.js";
import { printerEvents } from "../constants/event.constants.js";
import { KeyDiffCache } from "../utils/cache/key-diff.cache.js";
//#region src/state/printer.cache.ts
var PrinterCache = class extends KeyDiffCache {
constructor(printerService, eventEmitter2) {
super();
this.printerService = printerService;
this.eventEmitter2 = eventEmitter2;
this.eventEmitter2.on(printerEvents.batchPrinterCreated, this.handleBatchPrinterCreated.bind(this));
this.eventEmitter2.on(printerEvents.printerCreated, this.handlePrinterCreatedOrUpdated.bind(this));
this.eventEmitter2.on(printerEvents.printerUpdated, this.handlePrinterCreatedOrUpdated.bind(this));
this.eventEmitter2.on(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: p.id,
value: p
}));
await this.setKeyValuesBatch(keyValues, true);
return dtos;
}
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 NotFoundException(`Printer with provided id not found`);
return printer;
}
getCachedPrinterOrThrow(id) {
const printer = this.keyValueStore.get(id);
if (!printer) throw new NotFoundException(`Printer with provided id not found`);
return printer;
}
async getNameAsync(id) {
return (await this.getCachedPrinterOrThrowAsync(id)).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 keyValues = this.mapArray(event.printers).map((p) => ({
key: p.id,
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);
}
mapArray(entities) {
return entities.map((p) => {
return this.map(p);
});
}
map(entity) {
return this.printerService.toDto(entity);
}
};
//#endregion
export { PrinterCache };
//# sourceMappingURL=printer.cache.js.map