all files / lib/routers/ mountEventInfo.js

73.33% Statements 55/75
71.43% Branches 20/28
100% Functions 2/2
72.22% Lines 52/72
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                                                                                                                            16×    
'use strict';
 
Object.defineProperty(exports, "__esModule", {
        value: true
});
exports.default = mountEventInfo;
 
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);
 
var _Utils = require('../Utils');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
//endregion
 
const context = {
        router: _express2.default.Router({ mergeParams: true }),
        cameras: null,
        logger: null
};
//endregion
 
//region 2. Project Libraries
//region 1. Platform Libraries
 
 
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 }, (findOneErr, /** @type Camera */camera) => {
                Iif (findOneErr) {
                        log('error', findOneErr.message);
                        res.status(500).send(findOneErr.message);
                        return;
                }
                Iif (!camera) {
                        const message = 'Camera Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                /** @type Stream */
                const stream = camera.streams[req.params.index];
                Iif (!stream) {
                        const message = 'Stream Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                //region Get RTSP URL
                const rtspUrl = stream.rtsp_url;
                Iif (!rtspUrl) {
                        const message = 'RTSP URL Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                log('debug', `rtspUrl ${ rtspUrl }`);
                //endregion
                //region Get HTTP URL
                const username = camera.username;
                const password = camera.password;
                const hostname = camera.public_address;
                const port = camera.web_port;
                Iif (!hostname || !port) {
                        const message = 'HTTP URL Not Found';
                        log('error', message);
                        res.status(500).send(message);
                        return;
                }
                let httpUrl;
                Eif (username) {
                        httpUrl = `http://${ username }:${ password }@${ hostname }:${ port }`;
                } else {
                        httpUrl = `http://${ hostname }:${ port }`;
                }
                log('debug', `httpUrl ${ httpUrl }`);
                //endregion
                const idleTime = stream.trigger_idle_duration || 60000;
                const preAlarmSec = stream.trigger_prealarm_duration || 5;
                const videoDurationSec = stream.trigger_video_duration || 10;
                const frameRateRef = (0, _Utils.valueOf)(stream, 'rate_control', 'frame_rate_limit') || 30;
                const gopSizeRef = (0, _Utils.valueOf)(stream, 'h264', 'gov_length') || 100;
                const reply = {
                        rtspUrl,
                        httpUrl,
                        idleTime,
                        preAlarmSec,
                        videoDurationSec,
                        frameRateRef,
                        gopSizeRef
                };
                log('info', '200 OK');
                log('debug', JSON.stringify(reply, null, 2));
                res.status(200).send(reply);
        });
});
 
//noinspection JSUnusedGlobalSymbols
/**
 * Mount router for /c/:/s/:/event-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 mountEventInfo(options) {
        context.logger = options.logger;
        const log = (lvl, msg) => context.logger.log(lvl, msg, { tags: 'mountEventInfo' });
        log('debug', 'Mounting router for /c/:/s/:/event-info....');
        options.app.use('/cameras/:id/streams/:index/event-info', context.router);
        context.cameras = options.database.collection('cameras');
        log('info', 'Router for /c/:/s/:/event-info Mounted');
}
//# sourceMappingURL=mountEventInfo.js.map