<commit_before>/* eslint-disable no-unused-vars */


// Load global variables and functions
var globals = require('../globals');


// --------------------------------------------------------
// Set up UDP server handlers for acting on Sense failed task events
// --------------------------------------------------------
module.exports.udpInitTaskErrorServer = function () {

    // Handler for UDP server startup event
    globals.udpServerTaskFailureSocket.on('listening', function(message, remote) {
        var address = globals.udpServerTaskFailureSocket.address();

        var oldLogLevel = globals.logger.transports.console.level;
        globals.logger.transports.console.level = 'info';

        globals.logger.log('info', 'UDP server listening on %s:%s', address.address, address.port);

        globals.logger.transports.console.level = oldLogLevel;
        
        // console.info('UDP server listening on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has started
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'start');
    });

    // Handler for UDP error event
    globals.udpServerTaskFailureSocket.on('error', function(message, remote) {
        var address = globals.udpServerTaskFailureSocket.address();
        globals.logger.log('error', 'UDP server error on %s:%s', address.address, address.port);
        // console.error('UDP server error on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has reported an error
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
    });

    // Main handler for UDP messages relating to failed tasks
    globals.udpServerTaskFailureSocket.on('message', function(message, remote) {
        var msg = message.toString().split(';');
        globals.logger.log('warning', '%s: Task "%s" failed, associated with app "%s', msg[0], msg[1], msg[2], msg[3]);
        // console.info('%s: Task "%s" failed, associated with app "%s', msg[0], msg[1], msg[2], msg[3]);

        // Post to Slack when a task has failed
        globals.slack.send({
            text: 'Failed task: "' + msg[1] + '", linked to app "' + msg[2] + '".',
            channel: globals.config.get('Butler.slackConfig.taskFailureChannel'),
            username: msg[0],
            icon_emoji: ':ghost:'
        });

        // Publish MQTT message when a task has failed
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureTopic'), msg[1]);
    });

};


// --------------------------------------------------------
// Set up UDP server for acting on Sense session and connection events
// --------------------------------------------------------
module.exports.udpInitSessionConnectionServer = function () {

    // Handler for UDP server startup event
    //  globals.udpServerSessionConnectionSocket.on('listening', () => {
    globals.udpServerSessionConnectionSocket.on('listening', function(message, remote) {
        var address = globals.udpServerSessionConnectionSocket.address();

        var oldLogLevel = globals.logger.transports.console.level;
        globals.logger.transports.console.level = 'info';

        globals.logger.log('info', 'UDP server listening on %s:%s', address.address, address.port);

        globals.logger.transports.console.level = oldLogLevel;

        // console.info('UDP server listening on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has started
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionServerStatusTopic'), 'start');
    });

    // Handler for UDP error event
    //  globals.udpServerSessionConnectionSocket.on('error', () => {
    globals.udpServerSessionConnectionSocket.on('error', function(message, remote) {
        var address = globals.udpServerSessionConnectionSocket.address();
        globals.logger.log('error', 'UDP server error on %s:%s', address.address, address.port);
        // console.error('UDP server error on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has reported an error
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionServerStatusTopic'), 'error');
    });

    // Main handler for UDP messages relating to session and connection events
    globals.udpServerSessionConnectionSocket.on('message', function(message, remote) {
        var msg = message.toString().split(';');
        globals.logger.log('info', '%s: %s for user %s/%s', msg[0], msg[1], msg[2], msg[3]);
        // console.info('%s: %s for user %s/%s', msg[0], msg[1], msg[2], msg[3]);

        // Send Slack message when session starts/stops, or a connection open/close
        globals.slack.send({
            text: msg[1] + ' for user ' + msg[2] + '/' + msg[3],
            channel: globals.config.get('Butler.slackConfig.loginNotificationChannel'),
            username: msg[0],
            icon_emoji: ''
        });

        // Handle session events
        if (msg[1] == 'Start session') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionStartTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

        if (msg[1] == 'Stop session') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionStopTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

        // Handle connection events
        if (msg[1] == 'Open connection') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.connectionOpenTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

        if (msg[1] == 'Close connection') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.connectionCloseTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

    });<commit_msg>Changed name of Slack object<commit_after>/* eslint-disable no-unused-vars */


// Load global variables and functions
var globals = require('../globals');


// --------------------------------------------------------
// Set up UDP server handlers for acting on Sense failed task events
// --------------------------------------------------------
module.exports.udpInitTaskErrorServer = function () {

    // Handler for UDP server startup event
    globals.udpServerTaskFailureSocket.on('listening', function(message, remote) {
        var address = globals.udpServerTaskFailureSocket.address();

        var oldLogLevel = globals.logger.transports.console.level;
        globals.logger.transports.console.level = 'info';

        globals.logger.log('info', 'UDP server listening on %s:%s', address.address, address.port);

        globals.logger.transports.console.level = oldLogLevel;
        
        // console.info('UDP server listening on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has started
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'start');
    });

    // Handler for UDP error event
    globals.udpServerTaskFailureSocket.on('error', function(message, remote) {
        var address = globals.udpServerTaskFailureSocket.address();
        globals.logger.log('error', 'UDP server error on %s:%s', address.address, address.port);
        // console.error('UDP server error on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has reported an error
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
    });

    // Main handler for UDP messages relating to failed tasks
    globals.udpServerTaskFailureSocket.on('message', function(message, remote) {
        var msg = message.toString().split(';');
        globals.logger.log('warning', '%s: Task "%s" failed, associated with app "%s', msg[0], msg[1], msg[2], msg[3]);
        // console.info('%s: Task "%s" failed, associated with app "%s', msg[0], msg[1], msg[2], msg[3]);

        // Post to Slack when a task has failed
        globals.slackObj.send({
            text: 'Failed task: "' + msg[1] + '", linked to app "' + msg[2] + '".',
            channel: globals.config.get('Butler.slackConfig.taskFailureChannel'),
            username: msg[0],
            icon_emoji: ':ghost:'
        });

        // Publish MQTT message when a task has failed
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureTopic'), msg[1]);
    });

};


// --------------------------------------------------------
// Set up UDP server for acting on Sense session and connection events
// --------------------------------------------------------
module.exports.udpInitSessionConnectionServer = function () {

    // Handler for UDP server startup event
    //  globals.udpServerSessionConnectionSocket.on('listening', () => {
    globals.udpServerSessionConnectionSocket.on('listening', function(message, remote) {
        var address = globals.udpServerSessionConnectionSocket.address();

        var oldLogLevel = globals.logger.transports.console.level;
        globals.logger.transports.console.level = 'info';

        globals.logger.log('info', 'UDP server listening on %s:%s', address.address, address.port);

        globals.logger.transports.console.level = oldLogLevel;

        // console.info('UDP server listening on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has started
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionServerStatusTopic'), 'start');
    });

    // Handler for UDP error event
    //  globals.udpServerSessionConnectionSocket.on('error', () => {
    globals.udpServerSessionConnectionSocket.on('error', function(message, remote) {
        var address = globals.udpServerSessionConnectionSocket.address();
        globals.logger.log('error', 'UDP server error on %s:%s', address.address, address.port);
        // console.error('UDP server error on %s:%s', address.address, address.port);

        // Publish MQTT message that UDP server has reported an error
        globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionServerStatusTopic'), 'error');
    });

    // Main handler for UDP messages relating to session and connection events
    globals.udpServerSessionConnectionSocket.on('message', function(message, remote) {
        var msg = message.toString().split(';');
        globals.logger.log('info', '%s: %s for user %s/%s', msg[0], msg[1], msg[2], msg[3]);
        // console.info('%s: %s for user %s/%s', msg[0], msg[1], msg[2], msg[3]);

        // Send Slack message when session starts/stops, or a connection open/close
        globals.slackObj.send({
            text: msg[1] + ' for user ' + msg[2] + '/' + msg[3],
            channel: globals.config.get('Butler.slackConfig.loginNotificationChannel'),
            username: msg[0],
            icon_emoji: ''
        });

        // Handle session events
        if (msg[1] == 'Start session') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionStartTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

        if (msg[1] == 'Stop session') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.sessionStopTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

        // Handle connection events
        if (msg[1] == 'Open connection') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.connectionOpenTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

        if (msg[1] == 'Close connection') {
            globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.connectionCloseTopic'), msg[0] + ': ' + msg[2] + '/' + msg[3]);
        }

    });
{'fraction_non_alphanumeric': 0.179109502567124, 'fraction_numerical': 0.0067334399461324804, 'mean_word_length': 2.641434262948207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5181943', 'n_tokens_mistral': 3426, 'n_tokens_neox': 3333, 'n_words': 752}