<commit_before>/**
 * Routes for CasparCG
 *
 * Includes routes test
 *
 * Function parameters:
 *  - router. Express router object
 *  - connection. MySQL connection object for queries
 *
 * Required:
 *  - casparcg      CasparCG Node.js library (https://github.com/respectTheCode/node-caspar-cg) for communication
 *
 */

var CasparCG = require('caspar-cg');
var ccg = new CasparCG("192.168.1.100", 5250);
var _cg_connected = false;

function _connect(callback) {
    ccg.connect(function () {
        ccg.on('connected', function () {
            _cg_connected = true;
            callback();
            return true;
        });

        ccg.on('error', function(error) {
            callback(error);
            return false;
        });

        ccg.on('end', function() {
            ccg.connect();
            return false;
        });
    });
}

function _disconnect() {
    ccg.disconnect();
    _cg_connected = false;
    return true;
}

module.exports = function(router, connection) {
    router.post('/casparcg/connect', function(req, res) {
        _connect(function(error) {
            if(_cg_connected) {
                res.status(200).json({success: true, message: 'Connection successful'});
            } else {
                res.status(401).json({success: false, message: error});
            }
        });
    });

    router.post('/casparcg/disconnect', function(req, res) {
        _disconnect();
        return res.status(200).json({success: true, message: 'Disconnected from server'});
    });

    router.post('/casparcg/play', function(req, res) {
        var fields = req.body;

        if(!_cg_connected) {
            return res.status(300).json({success: false, message: 'Not connected to server'});
        }

        ccg.loadTemplate(fields.channel, 'JS/' + fields.template, 1, fields.inputs);
    });

    router.post('/casparcg/stop', function() {
        if(!_cg_connected) {
            return false;
        }

        ccg.stopTemplate('1-1');
    });

    router.put('/casparcg/update', function(req, res) {
        var fields = req.body;

        if(!_cg_connected) {
            return res.status(300).json({success: false, message: 'Not connected to server'});
        }

        ccg.updateTemplateData('1-1', fields.inputs);
    });
};<commit_msg>Commented routes and code. Changed error codes.<commit_after>/**
 * Routes for CasparCG
 *
 * Includes routes connect, disconnect, play, stop
 *
 * Function parameters:
 *  - router. Express router object
 *  - connection. MySQL connection object for queries
 *
 * Required:
 *  - caspar-cg      CasparCG Node.js library (https://github.com/respectTheCode/node-caspar-cg) for communication
 *
 */

var CasparCG = require('caspar-cg');
var ccg = new CasparCG("192.168.1.100", 5250);
var _cg_connected = false;

/**
 * Helper function for connection because it's used multiple times
 */
function _connect(callback) {
    ccg.connect(function () {
        ccg.on('connected', function () {
            _cg_connected = true;
            callback();
            return true;
        });

        ccg.on('error', function(error) {
            callback(error);
            return false;
        });

        ccg.on('end', function() {
            ccg.connect();
            return false;
        });
    });
}

/**
 * Helper function for disconnection
 */
function _disconnect() {
    ccg.disconnect();
    _cg_connected = false;
    return true;
}

module.exports = function(router, connection) {

    /**
     * Route: connect
     * Path: /api/casparcg/connect/
     *
     * Connects to CasparCG server
     *
     * Returns either a success code (200), or an error code 503 and message.
     */
    router.post('/casparcg/connect', function(req, res) {
        _connect(function(error) {
            if(_cg_connected) {
                res.status(200).json({success: true, message: 'Connection successful'});
            } else {
                res.status(503).json({success: false, message: error});
            }
        });
    });

    /**
     * Route: disconnect
     * Path: /api/casparcg/disconnect/
     *
     * Disconnects from CasparCG server
     *
     * Returns a success code (200) and message
     */
    router.post('/casparcg/disconnect', function(req, res) {
        _disconnect();
        return res.status(200).json({success: true, message: 'Disconnected from server'});
    });

    /**
     * Route: play
     * Path: /api/casparcg/play/
     *
     * Plays a given template in given channel passing on data from inputs as object
     * Request body example: { channel: '1-1', template: 'information', inputs: { f0: 'First', f1: 'Second', f2: 'Third } }
     *
     */
    router.post('/casparcg/play', function(req, res) {
        var fields = req.body;

        // Has to be changed to handle not connected -> reconnect. Experienced problems on first test
        if(!_cg_connected) {
            return res.status(503).json({success: false, message: 'Not connected to server'});
        }

        ccg.loadTemplate(fields.channel, 'JS/' + fields.template, 1, fields.inputs);
    });

    /**
     * Route: stop
     * Path: /api/casparcg/stop/
     *
     * Stops a template in given channel
     * Request body example: { channel: '1-1' }
     *
     */
    router.post('/casparcg/stop', function() {
        // Has to be changed to handle not connected -> reconnect. Experienced problems on first test
        if(!_cg_connected) {
            return res.status(503).json({success: false, message: 'Not connected to server'});
        }

        // Change this to get channel from request
        ccg.stopTemplate('1-1');
    });

    /**
     * Route: update
     * Path: /api/casparcg/update/
     *
     * Updates a template in given channel with given inputs
     * Request body example: { channel: '1-1', inputs: { f0: 'First', f1: 'Second', f2: 'Third } }
     *
     */
    router.put('/casparcg/update', function(req, res) {
        var fields = req.body;

        // Has to be changed to handle not connected -> reconnect. Experienced problems on first test
        if(!_cg_connected) {
            return res.status(503).json({success: false, message: 'Not connected to server'});
        }

        ccg.updateTemplateData('1-1', fields.inputs);
    });
};
{'fraction_non_alphanumeric': 0.13888005090677696, 'fraction_numerical': 0.014635698377346485, 'mean_word_length': 2.2043832823649336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7097068', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1828, 'n_words': 459}