all files / lib/routers/ mountArchiveInfo.js

87.04% Statements 47/54
83.33% Branches 10/12
100% Functions 2/2
86.27% Lines 44/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                                                                                      16×    
'use strict';
 
Object.defineProperty(exports, "__esModule", {
        value: true
});
exports.default = mountArchiveInfo;
 
var _express = require('express');
 
var _express2 = _interopRequireDefault(_express);
 
var _mongodb = require('mongodb');
 
var _mongodb2 = _interopRequireDefault(_mongodb);
 
var _checkObjectID = require('../middleware/checkObjectID');
 
var _checkObjectID2 = _interopRequireDefault(_checkObjectID);
 
var _checkStreamIndex = require('../middleware/checkStreamIndex');
 
var _checkStreamIndex2 = _interopRequireDefault(_checkStreamIndex);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
//endregion
 
//endregion
 
//region 2. Project Libraries
//region 1. Platform Libraries
const context = {
        router: _express2.default.Router({ mergeParams: true }),
        cameras: null,
        logger: null
};
 
context.router.get('/', _checkObjectID2.default);
context.router.get('/', _checkStreamIndex2.default);
context.router.get('/', (req, res) => {
        const log = (lvl, msg) => context.logger.log(lvl, msg, { tags: `GET ${ req.originalUrl }` });
        log('debug', JSON.stringify(req.params, null, 2));
        const _id = new _mongodb2.default.ObjectID(req.params.id);
        context.cameras.findOne({ _id }, (findOneCameraErr, camera) => {
                Iif (findOneCameraErr) {
                        log('error', findOneCameraErr.message);
                        res.status(500).send(findOneCameraErr.message);
                        return;
                }
                if (!camera) {
                        const message = 'Camera Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                /** @type Stream */
                const stream = camera.streams[req.params.index];
                if (!stream) {
                        const message = 'Stream Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                const rtspUrl = stream.rtsp_url;
                Iif (!rtspUrl) {
                        const message = 'RTSP URL Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                const reply = {
                        rtspUrl,
                        audio: 'OFF'
                };
                log('info', '200 OK');
                log('debug', JSON.stringify(reply, null, 2));
                res.status(200).send(reply);
        });
});
 
//noinspection JSUnusedGlobalSymbols
/**
 * Mount router for /c/:/s/:/archive-info
 * @param {object}         options          Options
 * @param {object}         options.app      Express App
 * @param {MongoClient.Db} options.database Associated Database
 * @param {winston.Logger} options.logger   Logger
 */
function mountArchiveInfo(options) {
        context.logger = options.logger;
        const log = (lvl, msg) => context.logger.log(lvl, msg, { tags: 'mountArchiveInfo' });
        log('debug', 'Mounting router for /c/:/s/:/archive-info....');
        options.app.use('/cameras/:id/streams/:index/archive-info', context.router);
        context.cameras = options.database.collection('cameras');
        log('info', 'Router for /c/:/s/:/archive-info Mounted');
}
//# sourceMappingURL=mountArchiveInfo.js.map