"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sequence = void 0; const files_1 = require("../files"); const log_1 = require("../log"); var SequenceStatus; (function (SequenceStatus) { SequenceStatus[SequenceStatus["IDLE"] = 0] = "IDLE"; SequenceStatus[SequenceStatus["RUNNING"] = 1] = "RUNNING"; SequenceStatus[SequenceStatus["PAUSED"] = 2] = "PAUSED"; })(SequenceStatus || (SequenceStatus = {})); class Sequence { constructor(camera, fd, display, ffprobe, send) { this.current = null; this.info = null; this.images = []; this.running = false; this.paused = false; this.progress = 0; this.frame = 0; this.frames = 0; this.exposure = 1000; this.log = (0, log_1.createLog)('seq'); this.camera = camera; this.fd = fd; this.display = display; this.ffprobe = ffprobe; this.send = send; } start() { if (this.current !== null) { this.running = true; this.log.info(`Started sequence: ${this.current.name}`); this.run(); } } stop() { if (this.running && this.current !== null) { this.log.info(`Stopped sequence: ${this.current.name}`); this.running = false; } } isRunning() { return this.running; } async run() { //update running for (let i = this.frame; i < this.images.length; i++) { if (!this.running) { break; } try { await this.frameRecord(); } catch (err) { this.log.error(`Error recording frame`, err); } this.frameAdvance(); } //complete running } async load(seq) { this.current = seq; this.frame = 0; this.progress = 0; await this.enumerate(); this.updateClientsOnLoad(); } updateClientsOnLoad() { if (this.current !== null) { this.send({ cmd: 'select', state: this.getState() }); } } updateClientsOnState() { if (this.current !== null) { this.send({ cmd: 'update', state: this.getState() }); } } async enumerate() { let screen; if (this.current === null) { this.log.error('Cannot enumerate sequence because it is not set'); return; } try { this.images = await files_1.Files.enumerateSequence(this.current.path); } catch (err) { this.log.error(`Error enumerating images in sequence: ${this.current.name}`, err); return; } this.frames = this.images.length; this.log.info(`Sequence ${this.current.name} contains ${this.images.length} image${this.images.length === 1 ? '' : 's'}`); if (this.frames > 0) { this.info = await this.ffprobe.info(this.images[0].path); } if (this.info !== null) { screen = this.display.getScreen(); this.display.setSource(this.info.width, this.info.height); this.log.info(`Screen : ${screen.width},${screen.height}`); this.log.info(`Sequence : ${this.info.width},${this.info.height}`); this.log.info(`Display : ${JSON.stringify(this.display.getDimensions())}`); } } unload() { this.current = null; this.info = null; this.images = []; } getState() { const dimensions = this.display.getDimensions(); const source = this.display.getSource(); const screen = this.display.getScreen(); return { display: { width: dimensions.w, height: dimensions.h }, offset: { x: dimensions.x, y: dimensions.y }, screen, source, sequence: { hash: this.current.hash, name: this.current.name, progress: this.progress, current: this.frame, frames: this.frames, status: this.getStatus() }, exposure: this.exposure }; } getUpdateState() { return { sequence: { hash: this.current.hash, name: this.current.name, progress: this.progress, current: this.frame, frames: this.frames, status: this.getStatus() }, exposure: this.exposure }; } getSequenceState() { if (this.current === null) { return null; } return { hash: this.current.hash, name: this.current.name, progress: this.progress }; } setExposure(ms) { this.exposure = ms; } getStatus() { if (this.running && this.paused) { return SequenceStatus.PAUSED; } else if (this.running && !this.paused) { return SequenceStatus.RUNNING; } return SequenceStatus.IDLE; } getCurrent() { if (this.current !== null && this.images.length > 0 && typeof this.images[this.frame] !== 'undefined') { return this.images[this.frame]; } return null; } frameAdvance(frames = 1) { if (this.frame + frames >= this.images.length) { this.frame = this.images.length - 1; } else { this.frame += frames; } this.progress = this.frame / (this.images.length - 1); this.updateClientsOnState(); } frameRewind(frames = 1) { if (this.frame + frames < 0) { this.frame = 0; } else { this.frame -= frames; } this.progress = this.frame > 0 ? this.frame / (this.images.length - 1) : 0; this.updateClientsOnState(); } frameSet(frame) { if (frame < 0) { frame = 0; } else if (frame > this.images.length - 1) { frame = this.images.length - 1; } this.progress = this.frame > 0 ? this.frame / (this.images.length - 1) : 0; this.updateClientsOnState(); } async frameRecord() { const img = this.images[this.frame]; const dimensions = this.display.getDimensions(); this.log.info(`Frame: ${this.frame} / ${this.images.length}`); await this.fd.load(img.path, dimensions.x, dimensions.y, dimensions.w, dimensions.h); await this.camera.open(); await this.fd.display(img.path, [this.exposure]); await this.camera.close(); } } exports.Sequence = Sequence; //# sourceMappingURL=index.js.map