"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); require("dotenv/config"); const express_1 = __importDefault(require("express")); const promises_1 = __importDefault(require("fs/promises")); const body_parser_1 = __importDefault(require("body-parser")); const uuid_1 = require("uuid"); const Handlebars = __importStar(require("handlebars")); const ws_1 = require("ws"); const log_1 = require("./log"); const files_1 = require("./files"); const fd_1 = require("./fd"); const display_1 = require("./display"); const ffmpeg_1 = require("./ffmpeg"); const ffprobe_1 = require("./ffprobe"); const camera_1 = require("./camera"); const sequence_1 = require("./sequence"); const image_1 = require("./image"); let mock = false; const log = (0, log_1.createLog)('fm'); const app = (0, express_1.default)(); let wss; let fd; let display; let ffmpeg; let ffprobe; let image; let camera; let sequence; let index; let port; let wsPort; let sequences; let videos; let width; let height; log.info('Starting filmout_manager...'); app.use(body_parser_1.default.json()); app.use(body_parser_1.default.urlencoded({ extended: true })); app.use('/static', express_1.default.static('./static')); async function createTemplate(filePath) { let tmpl; try { tmpl = await promises_1.default.readFile(filePath, 'utf8'); } catch (err) { log.error(err); return null; } return Handlebars.compile(tmpl); } async function settings() { let sequencesExists = false; let videosExists = false; if (typeof process.env['FD'] === 'undefined') { log.error('Please include an FD value containing the path to your fd binary in .env'); process.exit(1); } else { log.info(`FD=${process.env['FD']}`); } if (typeof process.env['FFMPEG'] === 'undefined') { log.error('Please include an FFMPEG value containing the path to your ffmpeg binary in .env'); process.exit(1); } else { log.info(`FFMPEG=${process.env['FFMPEG']}`); } if (typeof process.env['WIDTH'] === 'undefined') { log.error('Please include a WIDTH value containing the width of the screen you are using in .env'); process.exit(2); } else { width = parseInt(process.env['WIDTH']); log.info(`WIDTH=${width}`); } if (typeof process.env['HEIGHT'] === 'undefined') { log.error('Please include a HEIGHT value containing the height of the screen you are using in .env'); process.exit(3); } else { height = parseInt(process.env['HEIGHT']); log.info(`HEIGHT=${height}`); } if (typeof process.env['FD_HOST'] === 'undefined') { log.error('Please include a FD_HOST value with the host that the fd socket server is hosted on in .env'); process.exit(4); } else { log.info(`FD_HOST=${process.env['FD_HOST']}`); } if (typeof process.env['FD_PORT'] === 'undefined') { log.error('Please include a FD_PORT value with the port that the fd socket server is hosted on in .env'); process.exit(5); } else { log.info(`FD_PORT=${process.env['FD_PORT']}`); } if (typeof process.env['PORT'] === 'undefined') { log.error('Please include a PORT value with the port that the HTTP web process is hosted on in .env'); process.exit(6); } else { port = parseInt(process.env['PORT']); log.info(`PORT=${port}`); } if (typeof process.env['WS_PORT'] === 'undefined') { log.error('Please include a WSPORT value with the port that the WebSocket web process is hosted on in .env'); process.exit(6); } else { wsPort = parseInt(process.env['WS_PORT']); log.info(`WS_PORT=${wsPort}`); } if (wsPort === port) { log.error(`Websocket port (${wsPort}) should not be the same as HTTP port (${port})`); process.exit(7); } if (typeof process.env['SEQUENCES'] === 'undefined') { log.error('Please include a SEQUENCES directory where the image sequences will be located in .env'); process.exit(7); } else { sequences = process.env['SEQUENCES']; sequencesExists = await files_1.Files.exists(sequences); if (!sequencesExists) { log.error(`The SEQUENCES directory in .env, ${sequences}, does not exist`); process.exit(8); } log.info(`SEQUENCES=${sequences}`); } if (typeof process.env['VIDEOS'] === 'undefined') { log.error('Please include a VIDEOS directory where the videos will be located in .env'); process.exit(7); } else { videos = process.env['VIDEOS']; videosExists = await files_1.Files.exists(videos); if (!sequencesExists) { log.error(`The VIDEOS directory in .env, ${videos}, does not exist`); process.exit(8); } log.info(`VIDEOS=${videos}`); } if (typeof process.env['MOCK'] !== 'undefined') { if (process.env['MOCK'].trim().toLowerCase() === "true" || process.env['MOCK'].trim() === '1') { mock = true; log.info(`MOCK=true`); } else { mock = false; } } } function onWssConnection(ws, req) { let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; if (ip.substr(0, 7) === "::ffff:") ip = ip.substr(7); log.info(`Client ${ip} connected to WebSocket server`); ws.ip = ip; ws.session = (0, uuid_1.v4)(); ws.on('message', function (data) { onClientMessage(data, ws); }); sequence.updateClientsOnLoad(); } async function onClientMessage(data, ws) { let msg = null; try { msg = JSON.parse(data); } catch (err) { log.error('Error parsing message', err); } if (msg !== null && typeof msg.cmd !== 'undefined') { await cmd(msg); } } async function cmd(msg) { let success = false; switch (msg.cmd) { case 'pong': //received keepalive break; case 'open': await cameraOpen(); break; case 'close': await cameraClose(); break; case 'select': await select(msg.state.sequence.hash); break; case 'start': start(); break; case 'stop': stop(); break; case 'advance': frameAdvance(); break; case 'rewind': frameRewind(); break; default: log.warn(`No matching command: ${msg.cmd}`); } } async function cameraOpen() { await camera.open(); send({ cmd: 'open' }); } async function cameraClose() { await camera.close(); send({ cmd: 'close' }); } function frameAdvance() { sequence.frameAdvance(); } function frameRewind() { sequence.frameRewind(); } async function select(id) { const sequencesArr = await files_1.Files.enumerateSequences(sequences); const seq = sequencesArr.find(el => el.hash === id); if (typeof seq == 'undefined' || seq == null) { log.error('Sequence not found, maybe deleted?', new Error(`Cannot find sequence ${id}`)); return false; } await sequence.load(seq); return true; } function start() { sequence.start(); } function stop() { sequence.stop(); } async function send(msg) { const msgStr = JSON.stringify(msg); wss.clients.forEach((client) => { client.send(msgStr); }); } async function keepAlive() { await send({ cmd: 'ping' }); } app.get('/', async (req, res, next) => { const sequencesArr = await files_1.Files.enumerateSequences(sequences); //const videosArr : VideoObject[] = await Files.enumerateVideos(videos); const html = index({ sequences: sequencesArr, width, height, wsPort }); res.send(html); }); app.get('/:width/:height/image.jpg', async (req, res, next) => { let data; let current = sequence.getCurrent(); let width = parseInt(req.params.width); let height = parseInt(req.params.height); if (current !== null) { try { data = await image.thumbnail(current.path, width, height); log.info(`Image: ${current.path} - ${width},${height}`); } catch (err) { log.error('Error getting thumbnail of ${current}', err); return next(new Error('Error getting thumbnail')); } } else { try { data = await image.blank(width, height); log.info(`Blank - ${width},${height}`); } catch (err) { log.error('Error generating blank image', err); return next(new Error('Error generating blank image')); } } res.contentType('image/jpeg'); res.send(data); }); async function main() { await settings(); index = await createTemplate('./views/index.hbs'); ffmpeg = new ffmpeg_1.FFMPEG(process.env['FFMPEG']); ffprobe = new ffprobe_1.FFPROBE(); image = new image_1.Image(); camera = new camera_1.Camera(mock); display = new display_1.Display(width, height); fd = new fd_1.FD(process.env['FD'], width, height, process.env['FD_HOST'], parseInt(process.env['FD_PORT']), mock); app.listen(port, async () => { log.info(`filmout_manager HTTP server running on port ${port}`); }); wss = new ws_1.Server({ port: wsPort, clientTracking: true }); wss.on('connection', onWssConnection); log.info(`filmout_manager WebSocket server running on port ${wsPort}`); //ffmpeg.listFormats(); //log.info(await TestImage.Focus(640, 480)); sequence = new sequence_1.Sequence(camera, fd, display, ffprobe, send); setInterval(keepAlive, 30000); } main(); //# sourceMappingURL=index.js.map